Merge "Add ovn-networkobj for Multus"
[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         "log"
18
19         pkgerrors "github.com/pkg/errors"
20         coreV1 "k8s.io/api/core/v1"
21         metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
22         "k8s.io/apimachinery/pkg/runtime/schema"
23
24         utils "k8splugin/internal"
25         "k8splugin/internal/helm"
26         "k8splugin/internal/plugin"
27 )
28
29 // ExportedVariable is what we will look for when calling the plugin
30 var ExportedVariable servicePlugin
31
32 type servicePlugin struct {
33 }
34
35 // Create a service object in a specific Kubernetes cluster
36 func (p servicePlugin) Create(yamlFilePath string, namespace string, client plugin.KubernetesConnector) (string, error) {
37         if namespace == "" {
38                 namespace = "default"
39         }
40
41         obj, err := utils.DecodeYAML(yamlFilePath, nil)
42         if err != nil {
43                 return "", pkgerrors.Wrap(err, "Decode service object error")
44         }
45
46         service, ok := obj.(*coreV1.Service)
47         if !ok {
48                 return "", pkgerrors.New("Decoded object contains another resource different than Service")
49         }
50         service.Namespace = namespace
51
52         result, err := client.GetStandardClient().CoreV1().Services(namespace).Create(service)
53         if err != nil {
54                 return "", pkgerrors.Wrap(err, "Create Service error")
55         }
56
57         return result.GetObjectMeta().GetName(), nil
58 }
59
60 // List of existing services hosted in a specific Kubernetes cluster
61 // gvk parameter is not used as this plugin is specific to services only
62 func (p servicePlugin) List(gvk schema.GroupVersionKind, namespace string, client plugin.KubernetesConnector) ([]helm.KubernetesResource, error) {
63         if namespace == "" {
64                 namespace = "default"
65         }
66
67         opts := metaV1.ListOptions{
68                 Limit: utils.ResourcesListLimit,
69         }
70
71         list, err := client.GetStandardClient().CoreV1().Services(namespace).List(opts)
72         if err != nil {
73                 return nil, pkgerrors.Wrap(err, "Get Service list error")
74         }
75
76         result := make([]helm.KubernetesResource, 0, utils.ResourcesListLimit)
77         if list != nil {
78                 for _, service := range list.Items {
79                         log.Printf("%v", service.Name)
80                         result = append(result,
81                                 helm.KubernetesResource{
82                                         GVK: schema.GroupVersionKind{
83                                                 Group:   "",
84                                                 Version: "v1",
85                                                 Kind:    "Service",
86                                         },
87                                         Name: service.GetName(),
88                                 })
89                 }
90         }
91
92         return result, nil
93 }
94
95 // Delete an existing service hosted in a specific Kubernetes cluster
96 func (p servicePlugin) Delete(resource helm.KubernetesResource, namespace string, client plugin.KubernetesConnector) error {
97         if namespace == "" {
98                 namespace = "default"
99         }
100
101         deletePolicy := metaV1.DeletePropagationForeground
102         opts := &metaV1.DeleteOptions{
103                 PropagationPolicy: &deletePolicy,
104         }
105
106         log.Println("Deleting service: " + resource.Name)
107         if err := client.GetStandardClient().CoreV1().Services(namespace).Delete(resource.Name, opts); err != nil {
108                 return pkgerrors.Wrap(err, "Delete service error")
109         }
110
111         return nil
112 }
113
114 // Get an existing service hosted in a specific Kubernetes cluster
115 func (p servicePlugin) Get(resource helm.KubernetesResource, namespace string, client plugin.KubernetesConnector) (string, error) {
116         if namespace == "" {
117                 namespace = "default"
118         }
119
120         opts := metaV1.GetOptions{}
121         service, err := client.GetStandardClient().CoreV1().Services(namespace).Get(resource.Name, opts)
122         if err != nil {
123                 return "", pkgerrors.Wrap(err, "Get Service error")
124         }
125
126         return service.Name, nil
127 }