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.
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"
24 utils "k8splugin/internal"
25 "k8splugin/internal/helm"
26 "k8splugin/internal/plugin"
29 // ExportedVariable is what we will look for when calling the plugin
30 var ExportedVariable servicePlugin
32 type servicePlugin struct {
35 // Create a service object in a specific Kubernetes cluster
36 func (p servicePlugin) Create(yamlFilePath string, namespace string, client plugin.KubernetesConnector) (string, error) {
41 obj, err := utils.DecodeYAML(yamlFilePath, nil)
43 return "", pkgerrors.Wrap(err, "Decode service object error")
46 service, ok := obj.(*coreV1.Service)
48 return "", pkgerrors.New("Decoded object contains another resource different than Service")
50 service.Namespace = namespace
52 result, err := client.GetStandardClient().CoreV1().Services(namespace).Create(service)
54 return "", pkgerrors.Wrap(err, "Create Service error")
57 return result.GetObjectMeta().GetName(), nil
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) {
67 opts := metaV1.ListOptions{
68 Limit: utils.ResourcesListLimit,
71 list, err := client.GetStandardClient().CoreV1().Services(namespace).List(opts)
73 return nil, pkgerrors.Wrap(err, "Get Service list error")
76 result := make([]helm.KubernetesResource, 0, utils.ResourcesListLimit)
78 for _, service := range list.Items {
79 log.Printf("%v", service.Name)
80 result = append(result,
81 helm.KubernetesResource{
82 GVK: schema.GroupVersionKind{
87 Name: service.GetName(),
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 {
101 deletePolicy := metaV1.DeletePropagationForeground
102 opts := &metaV1.DeleteOptions{
103 PropagationPolicy: &deletePolicy,
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")
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) {
117 namespace = "default"
120 opts := metaV1.GetOptions{}
121 service, err := client.GetStandardClient().CoreV1().Services(namespace).Get(resource.Name, opts)
123 return "", pkgerrors.Wrap(err, "Get Service error")
126 return service.Name, nil