diff options
author | Lukasz Rajewski <lukasz.rajewski@orange.com> | 2021-10-04 21:56:02 +0200 |
---|---|---|
committer | Ritu Sood <ritu.sood@intel.com> | 2021-10-04 22:37:57 +0000 |
commit | 3766e380c40dc1e4c839372dcdc0c71a972ffa70 (patch) | |
tree | 411e06da7f3bc9ed999b02007a91fc3e114d8b63 /src/k8splugin/internal/helm | |
parent | 2dde920d0f80d7c7fc3f2ed7ff41675ae6c28733 (diff) |
Fixed installation of CRD resources
Issue-ID: MULTICLOUD-1397
Signed-off-by: Lukasz Rajewski <lukasz.rajewski@orange.com>
Change-Id: Id8e653f1b5c61278ee2d64da409ac5b0685b36b8
Diffstat (limited to 'src/k8splugin/internal/helm')
-rw-r--r-- | src/k8splugin/internal/helm/helm.go | 63 | ||||
-rw-r--r-- | src/k8splugin/internal/helm/helm_test.go | 5 |
2 files changed, 48 insertions, 20 deletions
diff --git a/src/k8splugin/internal/helm/helm.go b/src/k8splugin/internal/helm/helm.go index 849674a9..6064b2c8 100644 --- a/src/k8splugin/internal/helm/helm.go +++ b/src/k8splugin/internal/helm/helm.go @@ -19,13 +19,14 @@ package helm import ( "fmt" - "github.com/onap/multicloud-k8s/src/k8splugin/internal/utils" "io/ioutil" "os" "path/filepath" "regexp" "strings" + "github.com/onap/multicloud-k8s/src/k8splugin/internal/utils" + pkgerrors "github.com/pkg/errors" "helm.sh/helm/v3/pkg/action" "helm.sh/helm/v3/pkg/chart/loader" @@ -47,7 +48,7 @@ type Template interface { GenerateKubernetesArtifacts( chartPath string, valueFiles []string, - values []string) ([]KubernetesResourceTemplate, []*Hook, error) + values []string) ([]KubernetesResourceTemplate, []KubernetesResourceTemplate, []*Hook, error) } // TemplateClient implements the Template interface @@ -90,10 +91,11 @@ func (h *TemplateClient) processValues(valueFiles []string, values []string) (ma // GenerateKubernetesArtifacts a mapping of type to fully evaluated helm template func (h *TemplateClient) GenerateKubernetesArtifacts(inputPath string, valueFiles []string, - values []string) ([]KubernetesResourceTemplate, []*Hook, error) { + values []string) ([]KubernetesResourceTemplate, []KubernetesResourceTemplate, []*Hook, error) { var outputDir, chartPath, namespace, releaseName string var retData []KubernetesResourceTemplate + var crdData []KubernetesResourceTemplate var hookList []*Hook releaseName = h.releaseName @@ -102,16 +104,16 @@ func (h *TemplateClient) GenerateKubernetesArtifacts(inputPath string, valueFile // verify chart path exists if _, err := os.Stat(inputPath); err == nil { if chartPath, err = filepath.Abs(inputPath); err != nil { - return retData, hookList, err + return retData, crdData, hookList, err } } else { - return retData, hookList, err + return retData, crdData, hookList, err } //Create a temp directory in the system temp folder outputDir, err := ioutil.TempDir("", "helm-tmpl-") if err != nil { - return retData, hookList, pkgerrors.Wrap(err, "Got error creating temp dir") + return retData, crdData, hookList, pkgerrors.Wrap(err, "Got error creating temp dir") } if namespace == "" { @@ -121,11 +123,11 @@ func (h *TemplateClient) GenerateKubernetesArtifacts(inputPath string, valueFile // get combined values and create config rawVals, err := h.processValues(valueFiles, values) if err != nil { - return retData, hookList, err + return retData, crdData, hookList, err } if msgs := validation.IsDNS1123Label(releaseName); releaseName != "" && len(msgs) > 0 { - return retData, hookList, fmt.Errorf("release name %s is not a valid DNS label: %s", releaseName, strings.Join(msgs, ";")) + return retData, crdData, hookList, fmt.Errorf("release name %s is not a valid DNS label: %s", releaseName, strings.Join(msgs, ";")) } // Initialize the install client @@ -133,27 +135,52 @@ func (h *TemplateClient) GenerateKubernetesArtifacts(inputPath string, valueFile client.DryRun = true client.ClientOnly = true client.ReleaseName = releaseName - client.IncludeCRDs = true + client.IncludeCRDs = false client.DisableHooks = true //to ensure no duplicates in case of defined pre/post install hooks // Check chart dependencies to make sure all are present in /charts chartRequested, err := loader.Load(chartPath) if err != nil { - return retData, hookList, err + return retData, crdData, hookList, err } if chartRequested.Metadata.Type != "" && chartRequested.Metadata.Type != "application" { - return retData, hookList, fmt.Errorf( + return retData, crdData, hookList, fmt.Errorf( "chart %q has an unsupported type and is not installable: %q", chartRequested.Metadata.Name, chartRequested.Metadata.Type, ) } + for _, crd := range chartRequested.CRDObjects() { + if strings.HasPrefix(crd.Name, "_") { + continue + } + filePath := filepath.Join(outputDir, crd.Name) + data := string(crd.File.Data) + // blank template after execution + if h.emptyRegex.MatchString(data) { + continue + } + utils.EnsureDirectory(filePath) + err = ioutil.WriteFile(filePath, []byte(crd.File.Data), 0600) + if err != nil { + return retData, crdData, hookList, err + } + gvk, err := getGroupVersionKind(data) + if err != nil { + return retData, crdData, hookList, err + } + kres := KubernetesResourceTemplate{ + GVK: gvk, + FilePath: filePath, + } + crdData = append(crdData, kres) + } client.Namespace = namespace release, err := client.Run(chartRequested, rawVals) if err != nil { - return retData, hookList, err + return retData, crdData, hookList, err } // SplitManifests returns integer-sortable so that manifests get output // in the same order as the input by `BySplitManifestsOrder`. @@ -161,7 +188,7 @@ func (h *TemplateClient) GenerateKubernetesArtifacts(inputPath string, valueFile // We won't get any meaningful hooks from here _, m, err := releaseutil.SortManifests(rmap, nil, releaseutil.InstallOrder) if err != nil { - return retData, hookList, err + return retData, crdData, hookList, err } for _, k := range m { data := k.Content @@ -180,11 +207,11 @@ func (h *TemplateClient) GenerateKubernetesArtifacts(inputPath string, valueFile utils.EnsureDirectory(mfilePath) err = ioutil.WriteFile(mfilePath, []byte(k.Content), 0600) if err != nil { - return retData, hookList, err + return retData, crdData, hookList, err } gvk, err := getGroupVersionKind(data) if err != nil { - return retData, hookList, err + return retData, crdData, hookList, err } kres := KubernetesResourceTemplate{ GVK: gvk, @@ -197,15 +224,15 @@ func (h *TemplateClient) GenerateKubernetesArtifacts(inputPath string, valueFile utils.EnsureDirectory(hFilePath) err = ioutil.WriteFile(hFilePath, []byte(h.Manifest), 0600) if err != nil { - return retData, hookList, err + return retData, crdData, hookList, err } gvk, err := getGroupVersionKind(h.Manifest) if err != nil { - return retData, hookList, err + return retData, crdData, hookList, err } hookList = append(hookList, &Hook{*h, KubernetesResourceTemplate{gvk, hFilePath}}) } - return retData, hookList, nil + return retData, crdData, hookList, nil } func getGroupVersionKind(data string) (schema.GroupVersionKind, error) { diff --git a/src/k8splugin/internal/helm/helm_test.go b/src/k8splugin/internal/helm/helm_test.go index 29d446fa..b805b59b 100644 --- a/src/k8splugin/internal/helm/helm_test.go +++ b/src/k8splugin/internal/helm/helm_test.go @@ -20,11 +20,12 @@ package helm import ( "crypto/sha256" "fmt" - "gopkg.in/yaml.v2" "io/ioutil" "path/filepath" "strings" "testing" + + "gopkg.in/yaml.v2" ) func TestProcessValues(t *testing.T) { @@ -202,7 +203,7 @@ func TestGenerateKubernetesArtifacts(t *testing.T) { for _, testCase := range testCases { t.Run(testCase.label, func(t *testing.T) { tc := NewTemplateClient("1.12.3", "testnamespace", "testreleasename") - out, hooks, err := tc.GenerateKubernetesArtifacts(testCase.chartPath, testCase.valueFiles, + out, _, hooks, err := tc.GenerateKubernetesArtifacts(testCase.chartPath, testCase.valueFiles, testCase.values) if err != nil { if testCase.expectedError == "" { |