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.
20 pkgerrors "github.com/pkg/errors"
21 coreV1 "k8s.io/api/core/v1"
22 metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23 "k8s.io/apimachinery/pkg/runtime/schema"
25 utils "github.com/onap/multicloud-k8s/src/k8splugin/internal"
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"
31 // Compile time check to see if servicePlugin implements the correct interface
32 var _ plugin.Reference = servicePlugin{}
34 // ExportedVariable is what we will look for when calling the plugin
35 var ExportedVariable servicePlugin
37 type servicePlugin struct {
40 // Create a service object in a specific Kubernetes cluster
41 func (p servicePlugin) Create(yamlFilePath string, namespace string, client plugin.KubernetesConnector) (string, error) {
46 obj, err := utils.DecodeYAML(yamlFilePath, nil)
48 return "", pkgerrors.Wrap(err, "Decode service object error")
51 service, ok := obj.(*coreV1.Service)
53 return "", pkgerrors.New("Decoded object contains another resource different than Service")
55 service.Namespace = namespace
57 labels := service.GetLabels()
58 //Check if labels exist for this object
60 labels = map[string]string{}
62 labels[config.GetConfiguration().KubernetesLabelName] = client.GetInstanceID()
63 service.SetLabels(labels)
65 result, err := client.GetStandardClient().CoreV1().Services(namespace).Create(context.TODO(), service, metaV1.CreateOptions{})
67 return "", pkgerrors.Wrap(err, "Create Service error")
70 return result.GetObjectMeta().GetName(), nil
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) {
80 opts := metaV1.ListOptions{
81 Limit: utils.ResourcesListLimit,
84 list, err := client.GetStandardClient().CoreV1().Services(namespace).List(context.TODO(), opts)
86 return nil, pkgerrors.Wrap(err, "Get Service list error")
89 result := make([]helm.KubernetesResource, 0, utils.ResourcesListLimit)
91 for _, service := range list.Items {
92 log.Printf("%v", service.Name)
93 result = append(result,
94 helm.KubernetesResource{
95 GVK: schema.GroupVersionKind{
100 Name: service.GetName(),
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 {
111 namespace = "default"
114 deletePolicy := metaV1.DeletePropagationBackground
115 opts := metaV1.DeleteOptions{
116 PropagationPolicy: &deletePolicy,
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")
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) {
130 namespace = "default"
133 opts := metaV1.GetOptions{}
134 service, err := client.GetStandardClient().CoreV1().Services(namespace).Get(context.TODO(), resource.Name, opts)
136 return "", pkgerrors.Wrap(err, "Get Service error")
139 return service.Name, nil
142 func (p servicePlugin) Update(yamlFilePath string, namespace string, client plugin.KubernetesConnector) (string, error) {