From 6b71968c4adc32a8cf6e7bd9b9b7e3b971913419 Mon Sep 17 00:00:00 2001 From: kumarabd Date: Mon, 23 Nov 2020 10:57:34 +0530 Subject: [PATCH] added updated for apply manifest Signed-off-by: kumarabd --- go.mod | 2 ++ utils/kubernetes/apply-manifest.go | 27 +++++++++++---- .../{detect-kubeconfig.go => client.go} | 0 utils/kubernetes/namespace.go | 33 ------------------- 4 files changed, 23 insertions(+), 39 deletions(-) rename utils/kubernetes/{detect-kubeconfig.go => client.go} (100%) delete mode 100644 utils/kubernetes/namespace.go diff --git a/go.mod b/go.mod index 1c26e5af..908de769 100644 --- a/go.mod +++ b/go.mod @@ -9,10 +9,12 @@ replace ( require ( github.com/go-kit/kit v0.10.0 + github.com/golang/protobuf v1.4.2 github.com/google/uuid v1.1.1 github.com/kr/pretty v0.2.1 // indirect github.com/layer5io/learn-layer5/smi-conformance v0.0.0-20201022191033-40468652a54f golang.org/x/sys v0.0.0-20200803210538-64077c9b5642 // indirect + google.golang.org/protobuf v1.23.0 helm.sh/helm/v3 v3.3.1 k8s.io/api v0.18.12 k8s.io/apimachinery v0.18.12 diff --git a/utils/kubernetes/apply-manifest.go b/utils/kubernetes/apply-manifest.go index 2551aced..94c38d26 100644 --- a/utils/kubernetes/apply-manifest.go +++ b/utils/kubernetes/apply-manifest.go @@ -2,6 +2,7 @@ package kubernetes import ( "context" + "reflect" "strings" v1 "k8s.io/api/core/v1" @@ -30,14 +31,20 @@ type ApplyOptions struct { // The namespace has to exist. func (client *Client) ApplyManifest(contents []byte, options ApplyOptions) error { - if options == (ApplyOptions{}) { + if reflect.DeepEqual(options, (ApplyOptions{})) { options = ApplyOptions{ Namespace: "default", Update: true, Delete: false, } } - manifests := strings.Split(string(contents), "---")[1:] + + manifests := strings.Split(string(contents), "---") + manifests = manifests[1:] + if len(manifests) > 0 && manifests[len(manifests)-1] == "\n" { + manifests = manifests[:len(manifests)-1] + } + for _, manifest := range manifests { // decode YAML into unstructured.Unstructured obj := &unstructured.Unstructured{} @@ -52,18 +59,27 @@ func (client *Client) ApplyManifest(contents []byte, options ApplyOptions) error return ErrApplyManifest(err) } + // Default to namespace from the UI. If no namespace is passed, use the namespace used in the manifest. val, err := meta.NewAccessor().Namespace(object) - if err == nil && len(val) > 0 { - options.Namespace = val + if err == nil && len(val) > 1 { + if len(options.Namespace) > 1 { + er := meta.NewAccessor().SetNamespace(object, options.Namespace) + if er != nil { + return ErrApplyManifest(er) + } + } else { + options.Namespace = val + } } + // Create namespace if it doesnt already exist if err = createNamespaceIfNotExist(client.Clientset, context.TODO(), options.Namespace); err != nil { return ErrApplyManifest(err) } if options.Delete { _, err = deleteObject(helper, options.Namespace, object) - if err != nil { + if err != nil && !kubeerror.IsNotFound(err) { return ErrApplyManifest(err) } } else { @@ -133,6 +149,5 @@ func createNamespaceIfNotExist(kubeClientset kubernetes.Interface, ctx context.C if !errors.IsAlreadyExists(err) { return err } - return nil } diff --git a/utils/kubernetes/detect-kubeconfig.go b/utils/kubernetes/client.go similarity index 100% rename from utils/kubernetes/detect-kubeconfig.go rename to utils/kubernetes/client.go diff --git a/utils/kubernetes/namespace.go b/utils/kubernetes/namespace.go deleted file mode 100644 index cf1aabb7..00000000 --- a/utils/kubernetes/namespace.go +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright 2020 Layer5, Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package kubernetes - -import ( - "context" - - v1 "k8s.io/api/core/v1" - apierrors "k8s.io/apimachinery/pkg/api/errors" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -func (client *Client) CreateNamespace(ctx context.Context, namespace string) error { - _, errGetNs := client.Clientset.CoreV1().Namespaces().Get(ctx, namespace, metav1.GetOptions{}) - if apierrors.IsNotFound(errGetNs) { - nsSpec := &v1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: namespace}} - _, err := client.Clientset.CoreV1().Namespaces().Create(ctx, nsSpec, metav1.CreateOptions{}) - return err - } - return errGetNs -}