Update status check endpoint 
[multicloud/k8s.git] / src / k8splugin / plugins / service / plugin.go
1 /*
2 Copyright 2018 Intel Corporation.
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6     http://www.apache.org/licenses/LICENSE-2.0
7 Unless required by applicable law or agreed to in writing, software
8 distributed under the License is distributed on an "AS IS" BASIS,
9 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 See the License for the specific language governing permissions and
11 limitations under the License.
12 */
13
14 package main
15
16 import (
17         "context"
18         "github.com/onap/multicloud-k8s/src/k8splugin/internal/utils"
19         "log"
20
21         pkgerrors "github.com/pkg/errors"
22         coreV1 "k8s.io/api/core/v1"
23         metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24         "k8s.io/apimachinery/pkg/runtime/schema"
25
26         "github.com/onap/multicloud-k8s/src/k8splugin/internal/config"
27         "github.com/onap/multicloud-k8s/src/k8splugin/internal/helm"
28         "github.com/onap/multicloud-k8s/src/k8splugin/internal/plugin"
29 )
30
31 // Compile time check to see if servicePlugin implements the correct interface
32 var _ plugin.Reference = servicePlugin{}
33
34 // ExportedVariable is what we will look for when calling the plugin
35 var ExportedVariable servicePlugin
36
37 type servicePlugin struct {
38 }
39
40 // Create a service object in a specific Kubernetes cluster
41 func (p servicePlugin) Create(yamlFilePath string, namespace string, client plugin.KubernetesConnector) (string, error) {
42         if namespace == "" {
43                 namespace = "default"
44         }
45
46         obj, err := utils.DecodeYAML(yamlFilePath, nil)
47         if err != nil {
48                 return "", pkgerrors.Wrap(err, "Decode service object error")
49         }
50
51         service, ok := obj.(*coreV1.Service)
52         if !ok {
53                 return "", pkgerrors.New("Decoded object contains another resource different than Service")
54         }
55         service.Namespace = namespace
56
57         labels := service.GetLabels()
58         //Check if labels exist for this object
59         if labels == nil {
60                 labels = map[string]string{}
61         }
62         labels[config.GetConfiguration().KubernetesLabelName] = client.GetInstanceID()
63         service.SetLabels(labels)
64
65         result, err := client.GetStandardClient().CoreV1().Services(namespace).Create(context.TODO(), service, metaV1.CreateOptions{})
66         if err != nil {
67                 return "", pkgerrors.Wrap(err, "Create Service error")
68         }
69
70         return result.GetObjectMeta().GetName(), nil
71 }
72
73 // List of existing services hosted in a specific Kubernetes cluster
74 // gvk parameter is not used as this plugin is specific to services only
75 func (p servicePlugin) List(gvk schema.GroupVersionKind, namespace string, client plugin.KubernetesConnector) ([]helm.KubernetesResource, error) {
76         if namespace == "" {
77                 namespace = "default"
78         }
79
80         opts := metaV1.ListOptions{
81                 Limit: utils.ResourcesListLimit,
82         }
83
84         list, err := client.GetStandardClient().CoreV1().Services(namespace).List(context.TODO(), opts)
85         if err != nil {
86                 return nil, pkgerrors.Wrap(err, "Get Service list error")
87         }
88
89         result := make([]helm.KubernetesResource, 0, utils.ResourcesListLimit)
90         if list != nil {
91                 for _, service := range list.Items {
92                         log.Printf("%v", service.Name)
93                         result = append(result,
94                                 helm.KubernetesResource{
95                                         GVK: schema.GroupVersionKind{
96                                                 Group:   "",
97                                                 Version: "v1",
98                                                 Kind:    "Service",
99                                         },
100                                         Name: service.GetName(),
101                                 })
102                 }
103         }
104
105         return result, nil
106 }
107
108 // Delete an existing service hosted in a specific Kubernetes cluster
109 func (p servicePlugin) Delete(resource helm.KubernetesResource, namespace string, client plugin.KubernetesConnector) error {
110         if namespace == "" {
111                 namespace = "default"
112         }
113
114         deletePolicy := metaV1.DeletePropagationBackground
115         opts := metaV1.DeleteOptions{
116                 PropagationPolicy: &deletePolicy,
117         }
118
119         log.Println("Deleting service: " + resource.Name)
120         if err := client.GetStandardClient().CoreV1().Services(namespace).Delete(context.TODO(), resource.Name, opts); err != nil {
121                 return pkgerrors.Wrap(err, "Delete service error")
122         }
123
124         return nil
125 }
126
127 // Get an existing service hosted in a specific Kubernetes cluster
128 func (p servicePlugin) Get(resource helm.KubernetesResource, namespace string, client plugin.KubernetesConnector) (string, error) {
129         if namespace == "" {
130                 namespace = "default"
131         }
132
133         opts := metaV1.GetOptions{}
134         service, err := client.GetStandardClient().CoreV1().Services(namespace).Get(context.TODO(), resource.Name, opts)
135         if err != nil {
136                 return "", pkgerrors.Wrap(err, "Get Service error")
137         }
138
139         return service.Name, nil
140 }
141
142 func (p servicePlugin) Update(yamlFilePath string, namespace string, client plugin.KubernetesConnector) (string, error) {
143
144         return "", nil
145
146 }