Skip to content

Commit

Permalink
Merge pull request #10 from layer5io/kumarabd/feature/proto
Browse files Browse the repository at this point in the history
added updated for apply manifest
  • Loading branch information
kumarabd authored Nov 23, 2020
2 parents cdcfda1 + 6b71968 commit 3ebe57c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 39 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 21 additions & 6 deletions utils/kubernetes/apply-manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package kubernetes

import (
"context"
"reflect"
"strings"

v1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -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{}
Expand All @@ -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 {
Expand Down Expand Up @@ -133,6 +149,5 @@ func createNamespaceIfNotExist(kubeClientset kubernetes.Interface, ctx context.C
if !errors.IsAlreadyExists(err) {
return err
}

return nil
}
File renamed without changes.
33 changes: 0 additions & 33 deletions utils/kubernetes/namespace.go

This file was deleted.

0 comments on commit 3ebe57c

Please sign in to comment.