Add UTs to plugins
[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         "k8s.io/client-go/kubernetes"
20
21         pkgerrors "github.com/pkg/errors"
22
23         coreV1 "k8s.io/api/core/v1"
24         metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25
26         "k8splugin/krd"
27 )
28
29 // Create a service object in a specific Kubernetes cluster
30 func Create(data *krd.ResourceData, client kubernetes.Interface) (string, error) {
31         namespace := data.Namespace
32         if namespace == "" {
33                 namespace = "default"
34         }
35         obj, err := krd.DecodeYAML(data.YamlFilePath)
36         if err != nil {
37                 return "", pkgerrors.Wrap(err, "Decode service object error")
38         }
39
40         service, ok := obj.(*coreV1.Service)
41         if !ok {
42                 return "", pkgerrors.New("Decoded object contains another resource different than Service")
43         }
44         service.Namespace = namespace
45         service.Name = data.VnfId + "-" + service.Name
46
47         result, err := client.CoreV1().Services(namespace).Create(service)
48         if err != nil {
49                 return "", pkgerrors.Wrap(err, "Create Service error")
50         }
51
52         return result.GetObjectMeta().GetName(), nil
53 }
54
55 // List of existing services hosted in a specific Kubernetes cluster
56 func List(namespace string, kubeclient kubernetes.Interface) ([]string, error) {
57         if namespace == "" {
58                 namespace = "default"
59         }
60
61         opts := metaV1.ListOptions{
62                 Limit: krd.ResourcesListLimit,
63         }
64         opts.APIVersion = "apps/v1"
65         opts.Kind = "Service"
66
67         list, err := kubeclient.CoreV1().Services(namespace).List(opts)
68         if err != nil {
69                 return nil, pkgerrors.Wrap(err, "Get Service list error")
70         }
71
72         result := make([]string, 0, krd.ResourcesListLimit)
73         if list != nil {
74                 for _, deployment := range list.Items {
75                         log.Printf("%v", deployment.Name)
76                         result = append(result, deployment.Name)
77                 }
78         }
79
80         return result, nil
81 }
82
83 // Delete an existing service hosted in a specific Kubernetes cluster
84 func Delete(name string, namespace string, kubeclient kubernetes.Interface) error {
85         if namespace == "" {
86                 namespace = "default"
87         }
88
89         deletePolicy := metaV1.DeletePropagationForeground
90         opts := &metaV1.DeleteOptions{
91                 PropagationPolicy: &deletePolicy,
92         }
93
94         log.Println("Deleting service: " + name)
95         if err := kubeclient.CoreV1().Services(namespace).Delete(name, opts); err != nil {
96                 return pkgerrors.Wrap(err, "Delete service error")
97         }
98
99         return nil
100 }
101
102 // Get an existing service hosted in a specific Kubernetes cluster
103 func Get(name string, namespace string, kubeclient kubernetes.Interface) (string, error) {
104         if namespace == "" {
105                 namespace = "default"
106         }
107
108         opts := metaV1.GetOptions{}
109         opts.APIVersion = "apps/v1"
110         opts.Kind = "Service"
111
112         service, err := kubeclient.CoreV1().Services(namespace).Get(name, opts)
113         if err != nil {
114                 return "", pkgerrors.Wrap(err, "Get Deployment error")
115         }
116
117         return service.Name, nil
118 }