From bf49d552b003072c6bc64ae838a4699c1f4028bd Mon Sep 17 00:00:00 2001 From: Kiran Kamineni Date: Tue, 16 Apr 2019 18:09:13 -0700 Subject: Replace Kind with GroupVersionKind Kind is not unique to track resources in Kubernetes GroupVersionKind is unique. We are just using that to track our data. It is abstracted behind a couple of new types for templates and resources. This change makes a lot of the old kind based operations redundant and simplified. Issue-ID: MULTICLOUD-573 Change-Id: I8f4ded2ba6a0821a8fbd679dc99ce3a44d805524 Signed-off-by: Kiran Kamineni --- src/k8splugin/internal/helm/helm.go | 38 ++++++++++++++++++++++------- src/k8splugin/internal/helm/helm_test.go | 35 +++++++++++++-------------- src/k8splugin/internal/helm/types.go | 41 ++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 26 deletions(-) create mode 100644 src/k8splugin/internal/helm/types.go (limited to 'src/k8splugin/internal/helm') diff --git a/src/k8splugin/internal/helm/helm.go b/src/k8splugin/internal/helm/helm.go index 65a36d6b..1ab701ae 100644 --- a/src/k8splugin/internal/helm/helm.go +++ b/src/k8splugin/internal/helm/helm.go @@ -28,8 +28,10 @@ import ( "github.com/ghodss/yaml" pkgerrors "github.com/pkg/errors" - + "k8s.io/apimachinery/pkg/runtime/schema" + "k8s.io/apimachinery/pkg/runtime/serializer/json" "k8s.io/apimachinery/pkg/util/validation" + k8syaml "k8s.io/apimachinery/pkg/util/yaml" "k8s.io/helm/pkg/chartutil" "k8s.io/helm/pkg/manifest" "k8s.io/helm/pkg/proto/hapi/chart" @@ -143,10 +145,11 @@ func (h *TemplateClient) ensureDirectory(f string) error { } // GenerateKubernetesArtifacts a mapping of type to fully evaluated helm template -func (h *TemplateClient) GenerateKubernetesArtifacts(inputPath string, valueFiles []string, values []string) (map[string][]string, error) { +func (h *TemplateClient) GenerateKubernetesArtifacts(inputPath string, valueFiles []string, + values []string) ([]KubernetesResourceTemplate, error) { var outputDir, chartPath, namespace, releaseName string - var retData map[string][]string + var retData []KubernetesResourceTemplate releaseName = h.releaseName namespace = h.kubeNameSpace @@ -226,7 +229,6 @@ func (h *TemplateClient) GenerateKubernetesArtifacts(inputPath string, valueFile var manifestsToRender []manifest.Manifest //render all manifests in the chart manifestsToRender = listManifests - retData = make(map[string][]string) for _, m := range tiller.SortByKind(manifestsToRender) { data := m.Content b := filepath.Base(m.Name) @@ -249,11 +251,31 @@ func (h *TemplateClient) GenerateKubernetesArtifacts(inputPath string, valueFile return retData, err } - if val, ok := retData[m.Head.Kind]; ok { - retData[m.Head.Kind] = append(val, mfilePath) - } else { - retData[m.Head.Kind] = []string{mfilePath} + gvk, err := getGroupVersionKind(data) + if err != nil { + return retData, err } + + kres := KubernetesResourceTemplate{ + GVK: gvk, + FilePath: mfilePath, + } + retData = append(retData, kres) } return retData, nil } + +func getGroupVersionKind(data string) (schema.GroupVersionKind, error) { + out, err := k8syaml.ToJSON([]byte(data)) + if err != nil { + return schema.GroupVersionKind{}, pkgerrors.Wrap(err, "Converting yaml to json") + } + + simpleMeta := json.SimpleMetaFactory{} + gvk, err := simpleMeta.Interpret(out) + if err != nil { + return schema.GroupVersionKind{}, pkgerrors.Wrap(err, "Parsing apiversion and kind") + } + + return *gvk, nil +} diff --git a/src/k8splugin/internal/helm/helm_test.go b/src/k8splugin/internal/helm/helm_test.go index 27bb9d79..a13c67ba 100644 --- a/src/k8splugin/internal/helm/helm_test.go +++ b/src/k8splugin/internal/helm/helm_test.go @@ -165,27 +165,26 @@ func TestGenerateKubernetesArtifacts(t *testing.T) { } else { //Compute the hash of returned data and compare for _, v := range out { - for _, f := range v { - data, err := ioutil.ReadFile(f) - if err != nil { - t.Errorf("Unable to read file %s", v) - } - h.Write(data) - gotHash := fmt.Sprintf("%x", h.Sum(nil)) - h.Reset() + f := v.FilePath + data, err := ioutil.ReadFile(f) + if err != nil { + t.Errorf("Unable to read file %s", v) + } + h.Write(data) + gotHash := fmt.Sprintf("%x", h.Sum(nil)) + h.Reset() - //Find the right hash from expectedHashMap - expectedHash := "" - for k1, v1 := range testCase.expectedHashMap { - if strings.Contains(f, k1) == true { - expectedHash = v1 - break - } - } - if gotHash != expectedHash { - t.Fatalf("Got unexpected hash for %s", f) + //Find the right hash from expectedHashMap + expectedHash := "" + for k1, v1 := range testCase.expectedHashMap { + if strings.Contains(f, k1) == true { + expectedHash = v1 + break } } + if gotHash != expectedHash { + t.Fatalf("Got unexpected hash for %s", f) + } } } }) diff --git a/src/k8splugin/internal/helm/types.go b/src/k8splugin/internal/helm/types.go new file mode 100644 index 00000000..2c8badb8 --- /dev/null +++ b/src/k8splugin/internal/helm/types.go @@ -0,0 +1,41 @@ +/* + * Copyright 2018 Intel Corporation, 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 helm + +import ( + "k8s.io/apimachinery/pkg/runtime/schema" +) + +// Represents the template that is used to create a particular +// resource in Kubernetes +type KubernetesResourceTemplate struct { + // Tracks the apiVersion and Kind of the resource + GVK schema.GroupVersionKind + // Path to the file that contains the resource info + FilePath string +} + +// KubernetesResource is the resource that is created in Kubernetes +// It is the type that will be used for tracking a resource. +// Any future information such as status, time can be added here +// for tracking. +type KubernetesResource struct { + // Tracks the apiVersion and Kind of the resource + GVK schema.GroupVersionKind + // Name of resource in Kubernetes + Name string +} -- cgit 1.2.3-korg