From c8ba8f21b68b64b4068f188614dd7c891edf035f Mon Sep 17 00:00:00 2001 From: Rajamohan Raj Date: Fri, 3 Apr 2020 07:43:11 +0000 Subject: Test cases for resolving the helm templates Added test cases for overriding values and resolving the helm charts. Also addressed the merge conflicts and review comments Issue-ID: MULTICLOUD-1041 Signed-off-by: Rajamohan Raj Change-Id: I511e8e2e71c60e878df434370fc053f09cda1f66 --- src/k8splugin/go.mod | 2 - src/k8splugin/internal/rb/profile.go | 1 + src/orchestrator/api/instantiation_handler.go | 17 -- .../mock_files/mock_charts/testchart1/Chart.yaml | 4 + .../testchart1/charts/subcharta/Chart.yaml | 4 + .../charts/subcharta/templates/service.yaml | 15 ++ .../testchart1/charts/subcharta/values.yaml | 17 ++ .../testchart1/charts/subchartb/Chart.yaml | 4 + .../charts/subchartb/templates/service.yaml | 15 ++ .../testchart1/charts/subchartb/values.yaml | 35 ++++ .../mock_charts/testchart1/templates/NOTES.txt | 1 + .../mock_charts/testchart1/templates/service.yaml | 22 +++ .../mock_files/mock_charts/testchart1/values.yaml | 55 ++++++ .../mock_files/mock_charts/testchart2/Chart.yaml | 4 + .../testchart2/charts/subcharta/Chart.yaml | 4 + .../charts/subcharta/templates/service.yaml | 15 ++ .../testchart2/charts/subcharta/values.yaml | 17 ++ .../testchart2/charts/subchartb/Chart.yaml | 4 + .../charts/subchartb/templates/service.yaml | 15 ++ .../testchart2/charts/subchartb/values.yaml | 35 ++++ .../mock_files/mock_charts/testchart2/delete.yaml | 22 +++ .../mock_charts/testchart2/templates/NOTES.txt | 1 + .../mock_charts/testchart2/templates/service.yaml | 22 +++ .../mock_files/mock_charts/testchart2/values.yaml | 55 ++++++ .../profile1/faulty-dest-manifest.yaml | 7 + .../mock_profiles/profile1/faulty-manifest.yaml | 8 + .../profile1/faulty-src-manifest.yaml | 7 + .../mock_profiles/profile1/manifest.yaml | 7 + .../mock_profiles/profile1/override_values.yaml | 9 + .../mock_profiles/profile1/subdir/p1.yaml | 22 +++ src/orchestrator/pkg/module/instantiation.go | 7 +- src/orchestrator/pkg/rtcontext/rtcontext_test.go | 4 +- src/orchestrator/utils/helm/helm.go | 35 ++-- src/orchestrator/utils/helm/helm_test.go | 203 +++++++++++++++++++++ src/orchestrator/utils/helm/profile_yaml.go | 109 +++++++++++ src/orchestrator/utils/profile_yaml.go | 109 ----------- src/orchestrator/utils/types/types.go | 41 ----- src/orchestrator/utils/util-functions.go | 116 ------------ src/orchestrator/utils/utils.go | 116 ++++++++++++ 39 files changed, 883 insertions(+), 303 deletions(-) create mode 100644 src/orchestrator/mock_files/mock_charts/testchart1/Chart.yaml create mode 100644 src/orchestrator/mock_files/mock_charts/testchart1/charts/subcharta/Chart.yaml create mode 100644 src/orchestrator/mock_files/mock_charts/testchart1/charts/subcharta/templates/service.yaml create mode 100644 src/orchestrator/mock_files/mock_charts/testchart1/charts/subcharta/values.yaml create mode 100644 src/orchestrator/mock_files/mock_charts/testchart1/charts/subchartb/Chart.yaml create mode 100644 src/orchestrator/mock_files/mock_charts/testchart1/charts/subchartb/templates/service.yaml create mode 100644 src/orchestrator/mock_files/mock_charts/testchart1/charts/subchartb/values.yaml create mode 100644 src/orchestrator/mock_files/mock_charts/testchart1/templates/NOTES.txt create mode 100644 src/orchestrator/mock_files/mock_charts/testchart1/templates/service.yaml create mode 100644 src/orchestrator/mock_files/mock_charts/testchart1/values.yaml create mode 100644 src/orchestrator/mock_files/mock_charts/testchart2/Chart.yaml create mode 100644 src/orchestrator/mock_files/mock_charts/testchart2/charts/subcharta/Chart.yaml create mode 100644 src/orchestrator/mock_files/mock_charts/testchart2/charts/subcharta/templates/service.yaml create mode 100644 src/orchestrator/mock_files/mock_charts/testchart2/charts/subcharta/values.yaml create mode 100644 src/orchestrator/mock_files/mock_charts/testchart2/charts/subchartb/Chart.yaml create mode 100644 src/orchestrator/mock_files/mock_charts/testchart2/charts/subchartb/templates/service.yaml create mode 100644 src/orchestrator/mock_files/mock_charts/testchart2/charts/subchartb/values.yaml create mode 100644 src/orchestrator/mock_files/mock_charts/testchart2/delete.yaml create mode 100644 src/orchestrator/mock_files/mock_charts/testchart2/templates/NOTES.txt create mode 100644 src/orchestrator/mock_files/mock_charts/testchart2/templates/service.yaml create mode 100644 src/orchestrator/mock_files/mock_charts/testchart2/values.yaml create mode 100644 src/orchestrator/mock_files/mock_profiles/profile1/faulty-dest-manifest.yaml create mode 100644 src/orchestrator/mock_files/mock_profiles/profile1/faulty-manifest.yaml create mode 100644 src/orchestrator/mock_files/mock_profiles/profile1/faulty-src-manifest.yaml create mode 100644 src/orchestrator/mock_files/mock_profiles/profile1/manifest.yaml create mode 100644 src/orchestrator/mock_files/mock_profiles/profile1/override_values.yaml create mode 100644 src/orchestrator/mock_files/mock_profiles/profile1/subdir/p1.yaml create mode 100644 src/orchestrator/utils/helm/helm_test.go create mode 100644 src/orchestrator/utils/helm/profile_yaml.go delete mode 100644 src/orchestrator/utils/profile_yaml.go delete mode 100644 src/orchestrator/utils/types/types.go delete mode 100644 src/orchestrator/utils/util-functions.go create mode 100644 src/orchestrator/utils/utils.go (limited to 'src') diff --git a/src/k8splugin/go.mod b/src/k8splugin/go.mod index d26de6ef..f924828d 100644 --- a/src/k8splugin/go.mod +++ b/src/k8splugin/go.mod @@ -108,5 +108,3 @@ replace ( k8s.io/client-go => k8s.io/client-go v11.0.1-0.20190409021438-1a26190bd76a+incompatible k8s.io/cloud-provider => k8s.io/cloud-provider v0.0.0-20190409023720-1bc0c81fa51d ) - -go 1.13 diff --git a/src/k8splugin/internal/rb/profile.go b/src/k8splugin/internal/rb/profile.go index d81e857e..6efa23b8 100644 --- a/src/k8splugin/internal/rb/profile.go +++ b/src/k8splugin/internal/rb/profile.go @@ -25,6 +25,7 @@ import ( "github.com/onap/multicloud-k8s/src/k8splugin/internal/db" "github.com/onap/multicloud-k8s/src/k8splugin/internal/helm" + pkgerrors "github.com/pkg/errors" ) diff --git a/src/orchestrator/api/instantiation_handler.go b/src/orchestrator/api/instantiation_handler.go index d9da2183..c95785f2 100644 --- a/src/orchestrator/api/instantiation_handler.go +++ b/src/orchestrator/api/instantiation_handler.go @@ -29,23 +29,6 @@ type instantiationHandler struct { client moduleLib.InstantiationManager } -// func (h instantiationHandler) approveInstantiationHandler(w http.ResponseWriter, r *http.Request) { - -// vars := mux.Vars(r) -// p := vars["project-name"] -// ca := vars["composite-app-name"] -// v := vars["composite-app-version"] -// di := vars["deployment-intent-group-name"] - -// instantiateErr := h.client.ApproveInstantiation(p, ca, v, di) -// if instantiateErr != nil { -// http.Error(w, instantiateErr.Error(), http.StatusInternalServerError) -// return -// } - -// w.WriteHeader(http.StatusCreated) -// } - func (h instantiationHandler) instantiateHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) diff --git a/src/orchestrator/mock_files/mock_charts/testchart1/Chart.yaml b/src/orchestrator/mock_files/mock_charts/testchart1/Chart.yaml new file mode 100644 index 00000000..91a641bd --- /dev/null +++ b/src/orchestrator/mock_files/mock_charts/testchart1/Chart.yaml @@ -0,0 +1,4 @@ +apiVersion: v1 +description: A Helm chart for Kubernetes +name: testchart1 +version: 0.1.0 diff --git a/src/orchestrator/mock_files/mock_charts/testchart1/charts/subcharta/Chart.yaml b/src/orchestrator/mock_files/mock_charts/testchart1/charts/subcharta/Chart.yaml new file mode 100644 index 00000000..be3edcef --- /dev/null +++ b/src/orchestrator/mock_files/mock_charts/testchart1/charts/subcharta/Chart.yaml @@ -0,0 +1,4 @@ +apiVersion: v1 +description: A Helm chart for Kubernetes +name: subcharta +version: 0.1.0 diff --git a/src/orchestrator/mock_files/mock_charts/testchart1/charts/subcharta/templates/service.yaml b/src/orchestrator/mock_files/mock_charts/testchart1/charts/subcharta/templates/service.yaml new file mode 100644 index 00000000..fdf75aa9 --- /dev/null +++ b/src/orchestrator/mock_files/mock_charts/testchart1/charts/subcharta/templates/service.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ .Chart.Name }} + labels: + chart: "{{ .Chart.Name }}-{{ .Chart.Version }}" +spec: + type: {{ .Values.service.type }} + ports: + - port: {{ .Values.service.externalPort }} + targetPort: {{ .Values.service.internalPort }} + protocol: TCP + name: {{ .Values.service.name }} + selector: + app: {{ .Chart.Name }} diff --git a/src/orchestrator/mock_files/mock_charts/testchart1/charts/subcharta/values.yaml b/src/orchestrator/mock_files/mock_charts/testchart1/charts/subcharta/values.yaml new file mode 100644 index 00000000..f0381ae6 --- /dev/null +++ b/src/orchestrator/mock_files/mock_charts/testchart1/charts/subcharta/values.yaml @@ -0,0 +1,17 @@ +# Default values for subchart. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. +# subchartA +service: + name: apache + type: ClusterIP + externalPort: 80 + internalPort: 80 +SCAdata: + SCAbool: false + SCAfloat: 3.1 + SCAint: 55 + SCAstring: "jabba" + SCAnested1: + SCAnested2: true + diff --git a/src/orchestrator/mock_files/mock_charts/testchart1/charts/subchartb/Chart.yaml b/src/orchestrator/mock_files/mock_charts/testchart1/charts/subchartb/Chart.yaml new file mode 100644 index 00000000..c3c6bbaf --- /dev/null +++ b/src/orchestrator/mock_files/mock_charts/testchart1/charts/subchartb/Chart.yaml @@ -0,0 +1,4 @@ +apiVersion: v1 +description: A Helm chart for Kubernetes +name: subchartb +version: 0.1.0 diff --git a/src/orchestrator/mock_files/mock_charts/testchart1/charts/subchartb/templates/service.yaml b/src/orchestrator/mock_files/mock_charts/testchart1/charts/subchartb/templates/service.yaml new file mode 100644 index 00000000..fdf75aa9 --- /dev/null +++ b/src/orchestrator/mock_files/mock_charts/testchart1/charts/subchartb/templates/service.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ .Chart.Name }} + labels: + chart: "{{ .Chart.Name }}-{{ .Chart.Version }}" +spec: + type: {{ .Values.service.type }} + ports: + - port: {{ .Values.service.externalPort }} + targetPort: {{ .Values.service.internalPort }} + protocol: TCP + name: {{ .Values.service.name }} + selector: + app: {{ .Chart.Name }} diff --git a/src/orchestrator/mock_files/mock_charts/testchart1/charts/subchartb/values.yaml b/src/orchestrator/mock_files/mock_charts/testchart1/charts/subchartb/values.yaml new file mode 100644 index 00000000..774fdd75 --- /dev/null +++ b/src/orchestrator/mock_files/mock_charts/testchart1/charts/subchartb/values.yaml @@ -0,0 +1,35 @@ +# Default values for subchart. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. +service: + name: nginx + type: ClusterIP + externalPort: 80 + internalPort: 80 + +SCBdata: + SCBbool: true + SCBfloat: 7.77 + SCBint: 33 + SCBstring: "boba" + +exports: + SCBexported1: + SCBexported1A: + SCBexported1B: 1965 + + SCBexported2: + SCBexported2A: "blaster" + +global: + kolla: + nova: + api: + all: + port: 8774 + metadata: + all: + port: 8775 + + + diff --git a/src/orchestrator/mock_files/mock_charts/testchart1/templates/NOTES.txt b/src/orchestrator/mock_files/mock_charts/testchart1/templates/NOTES.txt new file mode 100644 index 00000000..4bdf443f --- /dev/null +++ b/src/orchestrator/mock_files/mock_charts/testchart1/templates/NOTES.txt @@ -0,0 +1 @@ +Sample notes for {{ .Chart.Name }} \ No newline at end of file diff --git a/src/orchestrator/mock_files/mock_charts/testchart1/templates/service.yaml b/src/orchestrator/mock_files/mock_charts/testchart1/templates/service.yaml new file mode 100644 index 00000000..e06d19b9 --- /dev/null +++ b/src/orchestrator/mock_files/mock_charts/testchart1/templates/service.yaml @@ -0,0 +1,22 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ .Chart.Name }} + labels: + chart: "{{ .Chart.Name }}-{{ .Chart.Version }}" + namespace: "{{ .Release.Namespace }}" + release-name: "{{ .Release.Name }}" + release-is-upgrade: "{{ .Release.IsUpgrade }}" + release-is-install: "{{ .Release.IsInstall }}" + kube-version/major: "{{ .Capabilities.KubeVersion.Major }}" + kube-version/minor: "{{ .Capabilities.KubeVersion.Minor }}" + kube-version/gitversion: "v{{ .Capabilities.KubeVersion.Major }}.{{ .Capabilities.KubeVersion.Minor }}.0" +spec: + type: {{ .Values.service.type }} + ports: + - port: {{ .Values.service.externalPort }} + targetPort: {{ .Values.service.internalPort }} + protocol: TCP + name: {{ .Values.service.name }} + selector: + app: {{ .Chart.Name }} diff --git a/src/orchestrator/mock_files/mock_charts/testchart1/values.yaml b/src/orchestrator/mock_files/mock_charts/testchart1/values.yaml new file mode 100644 index 00000000..72d3fa5c --- /dev/null +++ b/src/orchestrator/mock_files/mock_charts/testchart1/values.yaml @@ -0,0 +1,55 @@ +# Default values for subchart. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. +# subchart1 +service: + name: nginx + type: ClusterIP + externalPort: 80 + internalPort: 80 + + +SC1data: + SC1bool: true + SC1float: 3.14 + SC1int: 100 + SC1string: "dollywood" + SC1extra1: 11 + +imported-chartA: + SC1extra2: 1.337 + +overridden-chartA: + SCAbool: true + SCAfloat: 3.14 + SCAint: 100 + SCAstring: "jabathehut" + SC1extra3: true + +imported-chartA-B: + SC1extra5: "tiller" + +overridden-chartA-B: + SCAbool: true + SCAfloat: 3.33 + SCAint: 555 + SCAstring: "wormwood" + SCAextra1: 23 + + SCBbool: true + SCBfloat: 0.25 + SCBint: 98 + SCBstring: "murkwood" + SCBextra1: 13 + + SC1extra6: 77 + +SCBexported1A: + SC1extra7: true + +exports: + SC1exported1: + global: + SC1exported2: + all: + SC1exported3: "SC1expstr" \ No newline at end of file diff --git a/src/orchestrator/mock_files/mock_charts/testchart2/Chart.yaml b/src/orchestrator/mock_files/mock_charts/testchart2/Chart.yaml new file mode 100644 index 00000000..f2818e52 --- /dev/null +++ b/src/orchestrator/mock_files/mock_charts/testchart2/Chart.yaml @@ -0,0 +1,4 @@ +apiVersion: v1 +description: A Helm chart for Kubernetes +name: testchart2 +version: 0.1.0 diff --git a/src/orchestrator/mock_files/mock_charts/testchart2/charts/subcharta/Chart.yaml b/src/orchestrator/mock_files/mock_charts/testchart2/charts/subcharta/Chart.yaml new file mode 100644 index 00000000..be3edcef --- /dev/null +++ b/src/orchestrator/mock_files/mock_charts/testchart2/charts/subcharta/Chart.yaml @@ -0,0 +1,4 @@ +apiVersion: v1 +description: A Helm chart for Kubernetes +name: subcharta +version: 0.1.0 diff --git a/src/orchestrator/mock_files/mock_charts/testchart2/charts/subcharta/templates/service.yaml b/src/orchestrator/mock_files/mock_charts/testchart2/charts/subcharta/templates/service.yaml new file mode 100644 index 00000000..fdf75aa9 --- /dev/null +++ b/src/orchestrator/mock_files/mock_charts/testchart2/charts/subcharta/templates/service.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ .Chart.Name }} + labels: + chart: "{{ .Chart.Name }}-{{ .Chart.Version }}" +spec: + type: {{ .Values.service.type }} + ports: + - port: {{ .Values.service.externalPort }} + targetPort: {{ .Values.service.internalPort }} + protocol: TCP + name: {{ .Values.service.name }} + selector: + app: {{ .Chart.Name }} diff --git a/src/orchestrator/mock_files/mock_charts/testchart2/charts/subcharta/values.yaml b/src/orchestrator/mock_files/mock_charts/testchart2/charts/subcharta/values.yaml new file mode 100644 index 00000000..f0381ae6 --- /dev/null +++ b/src/orchestrator/mock_files/mock_charts/testchart2/charts/subcharta/values.yaml @@ -0,0 +1,17 @@ +# Default values for subchart. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. +# subchartA +service: + name: apache + type: ClusterIP + externalPort: 80 + internalPort: 80 +SCAdata: + SCAbool: false + SCAfloat: 3.1 + SCAint: 55 + SCAstring: "jabba" + SCAnested1: + SCAnested2: true + diff --git a/src/orchestrator/mock_files/mock_charts/testchart2/charts/subchartb/Chart.yaml b/src/orchestrator/mock_files/mock_charts/testchart2/charts/subchartb/Chart.yaml new file mode 100644 index 00000000..c3c6bbaf --- /dev/null +++ b/src/orchestrator/mock_files/mock_charts/testchart2/charts/subchartb/Chart.yaml @@ -0,0 +1,4 @@ +apiVersion: v1 +description: A Helm chart for Kubernetes +name: subchartb +version: 0.1.0 diff --git a/src/orchestrator/mock_files/mock_charts/testchart2/charts/subchartb/templates/service.yaml b/src/orchestrator/mock_files/mock_charts/testchart2/charts/subchartb/templates/service.yaml new file mode 100644 index 00000000..fdf75aa9 --- /dev/null +++ b/src/orchestrator/mock_files/mock_charts/testchart2/charts/subchartb/templates/service.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ .Chart.Name }} + labels: + chart: "{{ .Chart.Name }}-{{ .Chart.Version }}" +spec: + type: {{ .Values.service.type }} + ports: + - port: {{ .Values.service.externalPort }} + targetPort: {{ .Values.service.internalPort }} + protocol: TCP + name: {{ .Values.service.name }} + selector: + app: {{ .Chart.Name }} diff --git a/src/orchestrator/mock_files/mock_charts/testchart2/charts/subchartb/values.yaml b/src/orchestrator/mock_files/mock_charts/testchart2/charts/subchartb/values.yaml new file mode 100644 index 00000000..774fdd75 --- /dev/null +++ b/src/orchestrator/mock_files/mock_charts/testchart2/charts/subchartb/values.yaml @@ -0,0 +1,35 @@ +# Default values for subchart. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. +service: + name: nginx + type: ClusterIP + externalPort: 80 + internalPort: 80 + +SCBdata: + SCBbool: true + SCBfloat: 7.77 + SCBint: 33 + SCBstring: "boba" + +exports: + SCBexported1: + SCBexported1A: + SCBexported1B: 1965 + + SCBexported2: + SCBexported2A: "blaster" + +global: + kolla: + nova: + api: + all: + port: 8774 + metadata: + all: + port: 8775 + + + diff --git a/src/orchestrator/mock_files/mock_charts/testchart2/delete.yaml b/src/orchestrator/mock_files/mock_charts/testchart2/delete.yaml new file mode 100644 index 00000000..70efc671 --- /dev/null +++ b/src/orchestrator/mock_files/mock_charts/testchart2/delete.yaml @@ -0,0 +1,22 @@ +apiVersion: v1 +kind: Service +metadata: + name: testchart2 + labels: + chart: "testchart2-0.1.0" + namespace: "testnamespace" + release-name: "testreleasename" + release-is-upgrade: "false" + release-is-install: "true" + kube-version/major: "1" + kube-version/minor: "14" + kube-version/gitversion: "v1.14.0" +spec: + type: ClusterIP + ports: + - port: 80 + targetPort: 80 + protocol: TCP + name: nginx + selector: + app: testchart2 \ No newline at end of file diff --git a/src/orchestrator/mock_files/mock_charts/testchart2/templates/NOTES.txt b/src/orchestrator/mock_files/mock_charts/testchart2/templates/NOTES.txt new file mode 100644 index 00000000..4bdf443f --- /dev/null +++ b/src/orchestrator/mock_files/mock_charts/testchart2/templates/NOTES.txt @@ -0,0 +1 @@ +Sample notes for {{ .Chart.Name }} \ No newline at end of file diff --git a/src/orchestrator/mock_files/mock_charts/testchart2/templates/service.yaml b/src/orchestrator/mock_files/mock_charts/testchart2/templates/service.yaml new file mode 100644 index 00000000..e06d19b9 --- /dev/null +++ b/src/orchestrator/mock_files/mock_charts/testchart2/templates/service.yaml @@ -0,0 +1,22 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ .Chart.Name }} + labels: + chart: "{{ .Chart.Name }}-{{ .Chart.Version }}" + namespace: "{{ .Release.Namespace }}" + release-name: "{{ .Release.Name }}" + release-is-upgrade: "{{ .Release.IsUpgrade }}" + release-is-install: "{{ .Release.IsInstall }}" + kube-version/major: "{{ .Capabilities.KubeVersion.Major }}" + kube-version/minor: "{{ .Capabilities.KubeVersion.Minor }}" + kube-version/gitversion: "v{{ .Capabilities.KubeVersion.Major }}.{{ .Capabilities.KubeVersion.Minor }}.0" +spec: + type: {{ .Values.service.type }} + ports: + - port: {{ .Values.service.externalPort }} + targetPort: {{ .Values.service.internalPort }} + protocol: TCP + name: {{ .Values.service.name }} + selector: + app: {{ .Chart.Name }} diff --git a/src/orchestrator/mock_files/mock_charts/testchart2/values.yaml b/src/orchestrator/mock_files/mock_charts/testchart2/values.yaml new file mode 100644 index 00000000..72d3fa5c --- /dev/null +++ b/src/orchestrator/mock_files/mock_charts/testchart2/values.yaml @@ -0,0 +1,55 @@ +# Default values for subchart. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. +# subchart1 +service: + name: nginx + type: ClusterIP + externalPort: 80 + internalPort: 80 + + +SC1data: + SC1bool: true + SC1float: 3.14 + SC1int: 100 + SC1string: "dollywood" + SC1extra1: 11 + +imported-chartA: + SC1extra2: 1.337 + +overridden-chartA: + SCAbool: true + SCAfloat: 3.14 + SCAint: 100 + SCAstring: "jabathehut" + SC1extra3: true + +imported-chartA-B: + SC1extra5: "tiller" + +overridden-chartA-B: + SCAbool: true + SCAfloat: 3.33 + SCAint: 555 + SCAstring: "wormwood" + SCAextra1: 23 + + SCBbool: true + SCBfloat: 0.25 + SCBint: 98 + SCBstring: "murkwood" + SCBextra1: 13 + + SC1extra6: 77 + +SCBexported1A: + SC1extra7: true + +exports: + SC1exported1: + global: + SC1exported2: + all: + SC1exported3: "SC1expstr" \ No newline at end of file diff --git a/src/orchestrator/mock_files/mock_profiles/profile1/faulty-dest-manifest.yaml b/src/orchestrator/mock_files/mock_profiles/profile1/faulty-dest-manifest.yaml new file mode 100644 index 00000000..8c61a4e4 --- /dev/null +++ b/src/orchestrator/mock_files/mock_profiles/profile1/faulty-dest-manifest.yaml @@ -0,0 +1,7 @@ +--- +version: v1 +type: + values: "override_values.yaml" + configresource: + - filepath: subdir/p1.yaml + chartpath: testchart1/folderdoesNOTexist/p1.yaml diff --git a/src/orchestrator/mock_files/mock_profiles/profile1/faulty-manifest.yaml b/src/orchestrator/mock_files/mock_profiles/profile1/faulty-manifest.yaml new file mode 100644 index 00000000..a123111a --- /dev/null +++ b/src/orchestrator/mock_files/mock_profiles/profile1/faulty-manifest.yaml @@ -0,0 +1,8 @@ +--- +version: v1 +type: + values: + - override_values.yaml + configresource: + - filepath: subdir/p1.yaml + chartpath: testchart1/templates/p1.yaml diff --git a/src/orchestrator/mock_files/mock_profiles/profile1/faulty-src-manifest.yaml b/src/orchestrator/mock_files/mock_profiles/profile1/faulty-src-manifest.yaml new file mode 100644 index 00000000..eff534bb --- /dev/null +++ b/src/orchestrator/mock_files/mock_profiles/profile1/faulty-src-manifest.yaml @@ -0,0 +1,7 @@ +--- +version: v1 +type: + values: "override_values.yaml" + configresource: + - filepath: subdir/filedoesNOTexist.yaml + chartpath: testchart1/templates/p1.yaml diff --git a/src/orchestrator/mock_files/mock_profiles/profile1/manifest.yaml b/src/orchestrator/mock_files/mock_profiles/profile1/manifest.yaml new file mode 100644 index 00000000..e4beada0 --- /dev/null +++ b/src/orchestrator/mock_files/mock_profiles/profile1/manifest.yaml @@ -0,0 +1,7 @@ +--- +version: v1 +type: + values: "override_values.yaml" + configresource: + - filepath: subdir/p1.yaml + chartpath: testchart1/templates/p1.yaml diff --git a/src/orchestrator/mock_files/mock_profiles/profile1/override_values.yaml b/src/orchestrator/mock_files/mock_profiles/profile1/override_values.yaml new file mode 100644 index 00000000..0186c662 --- /dev/null +++ b/src/orchestrator/mock_files/mock_profiles/profile1/override_values.yaml @@ -0,0 +1,9 @@ +# Default values for subchart. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. +# subchart1 +service: + name: nginx + type: ClusterIP + externalPort: 8080 + internalPort: 8080 \ No newline at end of file diff --git a/src/orchestrator/mock_files/mock_profiles/profile1/subdir/p1.yaml b/src/orchestrator/mock_files/mock_profiles/profile1/subdir/p1.yaml new file mode 100644 index 00000000..2dad677a --- /dev/null +++ b/src/orchestrator/mock_files/mock_profiles/profile1/subdir/p1.yaml @@ -0,0 +1,22 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ .Chart.Name }}-override + labels: + chart: "{{ .Chart.Name }}-{{ .Chart.Version }}" + namespace: "{{ .Release.Namespace }}" + release-name: "{{ .Release.Name }}" + release-is-upgrade: "{{ .Release.IsUpgrade }}" + release-is-install: "{{ .Release.IsInstall }}" + kube-version/major: "{{ .Capabilities.KubeVersion.Major }}" + kube-version/minor: "{{ .Capabilities.KubeVersion.Minor }}" + kube-version/gitversion: "v{{ .Capabilities.KubeVersion.Major }}.{{ .Capabilities.KubeVersion.Minor }}.0" +spec: + type: {{ .Values.service.type }} + ports: + - port: {{ .Values.service.externalPort }} + targetPort: {{ .Values.service.internalPort }} + protocol: TCP + name: {{ .Values.service.name }} + selector: + app: {{ .Chart.Name }} diff --git a/src/orchestrator/pkg/module/instantiation.go b/src/orchestrator/pkg/module/instantiation.go index 3c704181..5fabe81e 100644 --- a/src/orchestrator/pkg/module/instantiation.go +++ b/src/orchestrator/pkg/module/instantiation.go @@ -20,7 +20,6 @@ import ( "fmt" "github.com/onap/multicloud-k8s/src/orchestrator/utils/helm" - "github.com/onap/multicloud-k8s/src/orchestrator/utils/types" pkgerrors "github.com/pkg/errors" "encoding/base64" @@ -67,11 +66,11 @@ func getOverrideValuesByAppName(ov []OverrideValues, a string) map[string]string // GetSortedTemplateForApp returns the sorted templates. //It takes in arguments - appName, project, compositeAppName, releaseName, compositeProfileName, array of override values -func GetSortedTemplateForApp(appName, p, ca, v, rName, cp string, overrideValues []OverrideValues) ([]types.KubernetesResourceTemplate, error) { +func GetSortedTemplateForApp(appName, p, ca, v, rName, cp string, overrideValues []OverrideValues) ([]helm.KubernetesResourceTemplate, error) { log.Println("Processing App.. ", appName) - var sortedTemplates []types.KubernetesResourceTemplate + var sortedTemplates []helm.KubernetesResourceTemplate aC, err := NewAppClient().GetAppContent(appName, p, ca, v) if err != nil { @@ -107,7 +106,7 @@ func GetSortedTemplateForApp(appName, p, ca, v, rName, cp string, overrideValues sortedTemplates, err = helm.NewTemplateClient("", "default", rName, ManifestFileName).Resolve(appContent, appProfileContent, overrideValuesOfAppStr, - rName, appName) + appName) log.Printf("The len of the sortedTemplates :: %d", len(sortedTemplates)) diff --git a/src/orchestrator/pkg/rtcontext/rtcontext_test.go b/src/orchestrator/pkg/rtcontext/rtcontext_test.go index e9610ef0..eedbeb82 100644 --- a/src/orchestrator/pkg/rtcontext/rtcontext_test.go +++ b/src/orchestrator/pkg/rtcontext/rtcontext_test.go @@ -59,7 +59,7 @@ func (c *MockContextDb) Delete(key string) error { // Delete all function func (c *MockContextDb) DeleteAll(key string) error { - for kvKey, _ := range c.Items { + for kvKey := range c.Items { delete(c.Items, kvKey) } return c.Err @@ -69,7 +69,7 @@ func (c *MockContextDb) DeleteAll(key string) error { func (c *MockContextDb) GetAllKeys(path string) ([]string, error) { var keys []string - for k, _ := range c.Items { + for k := range c.Items { keys = append(keys, string(k)) } return keys, c.Err diff --git a/src/orchestrator/utils/helm/helm.go b/src/orchestrator/utils/helm/helm.go index f0d15fbf..80cdfe5a 100644 --- a/src/orchestrator/utils/helm/helm.go +++ b/src/orchestrator/utils/helm/helm.go @@ -19,7 +19,6 @@ package helm import ( "bytes" utils "github.com/onap/multicloud-k8s/src/orchestrator/utils" - "github.com/onap/multicloud-k8s/src/orchestrator/utils/types" pkgerrors "github.com/pkg/errors" "log" @@ -46,6 +45,15 @@ import ( "k8s.io/helm/pkg/timeconv" ) +//KubernetesResourceTemplate - 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 +} + // Template is the interface for all helm templating commands // Any backend implementation will implement this interface and will // access the functionality via this. @@ -144,10 +152,10 @@ func (h *TemplateClient) mergeValues(dest map[string]interface{}, src map[string // GenerateKubernetesArtifacts a mapping of type to fully evaluated helm template func (h *TemplateClient) GenerateKubernetesArtifacts(inputPath string, valueFiles []string, - values []string) ([]types.KubernetesResourceTemplate, error) { + values []string) ([]KubernetesResourceTemplate, error) { var outputDir, chartPath, namespace, releaseName string - var retData []types.KubernetesResourceTemplate + var retData []KubernetesResourceTemplate releaseName = h.releaseName namespace = h.kubeNameSpace @@ -255,7 +263,7 @@ func (h *TemplateClient) GenerateKubernetesArtifacts(inputPath string, valueFile return retData, err } - kres := types.KubernetesResourceTemplate{ + kres := KubernetesResourceTemplate{ GVK: gvk, FilePath: mfilePath, } @@ -281,13 +289,20 @@ func getGroupVersionKind(data string) (schema.GroupVersionKind, error) { // Resolver is an interface exposes the helm related functionalities type Resolver interface { - Resolve(appContent, appProfileContent []byte, overrideValuesOfAppStr []string, rName string) ([]types.KubernetesResourceTemplate, error) + Resolve(appContent, appProfileContent []byte, overrideValuesOfAppStr []string, appName string) ([]KubernetesResourceTemplate, error) } // Resolve function -func (h *TemplateClient) Resolve(appContent []byte, appProfileContent []byte, overrideValuesOfAppStr []string, rName, appName string) ([]types.KubernetesResourceTemplate, error) { +func (h *TemplateClient) Resolve(appContent []byte, appProfileContent []byte, overrideValuesOfAppStr []string, appName string) ([]KubernetesResourceTemplate, error) { + + var sortedTemplates []KubernetesResourceTemplate - var sortedTemplates []types.KubernetesResourceTemplate + //chartBasePath is the tmp path where the appContent(rawHelmCharts) is extracted. + chartBasePath, err := utils.ExtractTarBall(bytes.NewBuffer(appContent)) + if err != nil { + return sortedTemplates, pkgerrors.Wrap(err, "Extracting appContent") + } + log.Printf("The chartBasePath :: %s", chartBasePath) //prPath is the tmp path where the appProfileContent is extracted. prPath, err := utils.ExtractTarBall(bytes.NewBuffer(appProfileContent)) @@ -296,16 +311,12 @@ func (h *TemplateClient) Resolve(appContent []byte, appProfileContent []byte, ov } log.Printf("The profile path:: %s", prPath) - prYamlClient, err := utils.ProcessProfileYaml(prPath, h.manifestName) + prYamlClient, err := ProcessProfileYaml(prPath, h.manifestName) if err != nil { return sortedTemplates, pkgerrors.Wrap(err, "Processing Profile Manifest") } log.Println("Got the profileYamlClient..") - //chartBasePath is the tmp path where the appContent(rawHelmCharts) is extracted. - chartBasePath, err := utils.ExtractTarBall(bytes.NewBuffer(appContent)) - log.Printf("The chartBasePath :: %s", chartBasePath) - err = prYamlClient.CopyConfigurationOverrides(chartBasePath) if err != nil { return sortedTemplates, pkgerrors.Wrap(err, "Copying configresources to chart") diff --git a/src/orchestrator/utils/helm/helm_test.go b/src/orchestrator/utils/helm/helm_test.go new file mode 100644 index 00000000..e9442e8a --- /dev/null +++ b/src/orchestrator/utils/helm/helm_test.go @@ -0,0 +1,203 @@ +/* + * Copyright 2020 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 ( + "crypto/sha256" + "fmt" + + "io/ioutil" + "path/filepath" + "strings" + "testing" +) + +func TestProcessValues(t *testing.T) { + + chartDir := "../../mock_files/mock_charts/testchart2" + profileDir := "../../mock_files/mock_profiles/profile1" + + testCases := []struct { + label string + valueFiles []string + values []string + expectedHash string + expectedError string + }{ + { + label: "Process Values with Value Files Override", + valueFiles: []string{ + filepath.Join(chartDir, "values.yaml"), + filepath.Join(profileDir, "override_values.yaml"), + }, + //Hash of a combined values.yaml file that is expected + expectedHash: "c18a70f426933de3c051c996dc34fd537d0131b2d13a2112a2ecff674db6c2f9", + expectedError: "", + }, + { + label: "Process Values with Values Pair Override", + valueFiles: []string{ + filepath.Join(chartDir, "values.yaml"), + }, + //Use the same convention as specified in helm template --set + values: []string{ + "service.externalPort=82", + }, + //Hash of a combined values.yaml file that is expected + expectedHash: "028a3521fc9f8777ea7e67a6de0c51f2c875b88ca91734999657f0ca924ddb7a", + expectedError: "", + }, + { + label: "Process Values with Both Overrides", + valueFiles: []string{ + filepath.Join(chartDir, "values.yaml"), + filepath.Join(profileDir, "override_values.yaml"), + }, + //Use the same convention as specified in helm template --set + //Key takes precedence over the value from override_values.yaml + values: []string{ + "service.externalPort=82", + }, + //Hash of a combined values.yaml file that is expected + expectedHash: "516fab4ab7b76ba2ff35a97c2a79b74302543f532857b945f2fe25e717e755be", + expectedError: "", + }, + { + label: "Process complex Pair Override", + values: []string{ + "name={a,b,c}", + "servers[0].port=80", + }, + expectedError: "", + expectedHash: "50d9401b003f65c1ccfd1c5155106fff88c8201ab8b7d66bd6ffa4fe2883bead", + }, + } + + h := sha256.New() + + for _, testCase := range testCases { + t.Run(testCase.label, func(t *testing.T) { + tc := NewTemplateClient("1.12.3", "testnamespace", "testreleasename", "manifest.yaml") + out, err := tc.processValues(testCase.valueFiles, testCase.values) + if err != nil { + if testCase.expectedError == "" { + t.Fatalf("Got an error %s", err) + } + if strings.Contains(err.Error(), testCase.expectedError) == false { + t.Fatalf("Got unexpected error message %s", err) + } + } else { + //Compute the hash of returned data and compare + h.Write(out) + gotHash := fmt.Sprintf("%x", h.Sum(nil)) + h.Reset() + if gotHash != testCase.expectedHash { + t.Fatalf("Got unexpected hash '%s' of values.yaml:\n%s", gotHash, out) + } + } + }) + } +} + +func TestGenerateKubernetesArtifacts(t *testing.T) { + + chartDir := "../../mock_files/mock_charts/testchart2" + profileDir := "../../mock_files/mock_profiles/profile1" + + testCases := []struct { + label string + chartPath string + valueFiles []string + values []string + expectedHashMap map[string]string + expectedError string + }{ + { + label: "Generate artifacts without any overrides", + chartPath: chartDir, + valueFiles: []string{}, + values: []string{}, + //sha256 hash of the evaluated templates in each chart + expectedHashMap: map[string]string{ + "testchart2/templates/service.yaml": "fdd6a2b6795486f0dd1d8c44379afb5ffe4072c09f9cf6594738e8ded4dd872d", + "subcharta/templates/service.yaml": "570389588fffdb7193ab265888d781f3d751f3a40362533344f9aa7bb93a8bb0", + "subchartb/templates/service.yaml": "5654e03d922e8ec49649b4bbda9dfc9e643b3b7c9c18b602cc7e26fd36a39c2a", + }, + expectedError: "", + }, + { + label: "Generate artifacts with overrides", + chartPath: chartDir, + valueFiles: []string{ + filepath.Join(profileDir, "override_values.yaml"), + }, + values: []string{ + "service.externalPort=82", + }, + //sha256 hash of the evaluated templates in each chart + expectedHashMap: map[string]string{ + "testchart2/templates/service.yaml": "2bb96e791ecb6a3404bc5de3f6c4182aed881630269e2aa6766df38b0f852724", + "subcharta/templates/service.yaml": "570389588fffdb7193ab265888d781f3d751f3a40362533344f9aa7bb93a8bb0", + "subchartb/templates/service.yaml": "5654e03d922e8ec49649b4bbda9dfc9e643b3b7c9c18b602cc7e26fd36a39c2a", + }, + expectedError: "", + }, + } + + h := sha256.New() + + for _, testCase := range testCases { + t.Run(testCase.label, func(t *testing.T) { + tc := NewTemplateClient("1.12.3", "testnamespace", "testreleasename", "manifest.yaml") + out, err := tc.GenerateKubernetesArtifacts(testCase.chartPath, testCase.valueFiles, + testCase.values) + if err != nil { + if testCase.expectedError == "" { + t.Fatalf("Got an error %s", err) + } + if strings.Contains(err.Error(), testCase.expectedError) == false { + t.Fatalf("Got unexpected error message %s", err) + } + } else { + //Compute the hash of returned data and compare + for _, v := range out { + 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) + } + } + } + }) + } + +} diff --git a/src/orchestrator/utils/helm/profile_yaml.go b/src/orchestrator/utils/helm/profile_yaml.go new file mode 100644 index 00000000..e72fdbdc --- /dev/null +++ b/src/orchestrator/utils/helm/profile_yaml.go @@ -0,0 +1,109 @@ +/* + * Copyright 2020 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 ( + "io/ioutil" + "log" + "path/filepath" + + "github.com/ghodss/yaml" + pkgerrors "github.com/pkg/errors" +) + +/* +#Sample Yaml format for profile manifest.yaml +--- +version: v1 +type: + values: "values_override.yaml" + configresource: + - filepath: config.yaml + chartpath: chart/config/resources/config.yaml + - filepath: config2.yaml + chartpath: chart/config/resources/config2.yaml +*/ + +type overrideFiles struct { + FilePath string `yaml:"filepath"` + ChartPath string `yaml:"chartpath"` +} + +type supportedOverrides struct { + ConfigResource []overrideFiles `yaml:"configresource"` + Values string `yaml:"values"` +} + +type profileOverride struct { + Version string `yaml:"version"` + Type supportedOverrides `yaml:"type"` +} + +type ProfileYamlClient struct { + path string + override profileOverride +} + +func (p ProfileYamlClient) Print() { + log.Println(p.override) +} + +//GetValues returns a path to the override values.yam +//that was part of the profile +func (p ProfileYamlClient) GetValues() string { + return filepath.Join(p.path, p.override.Type.Values) +} + +//CopyConfigurationOverrides copies the various files that are +//provided as overrides to their corresponding locations within +//the destination chart. +func (p ProfileYamlClient) CopyConfigurationOverrides(chartPath string) error { + + //Iterate over each configresource and copy that file into + //the respective path in the chart. + for _, v := range p.override.Type.ConfigResource { + data, err := ioutil.ReadFile(filepath.Join(p.path, v.FilePath)) + if err != nil { + return pkgerrors.Wrap(err, "Reading configuration file") + } + err = ioutil.WriteFile(filepath.Join(chartPath, v.ChartPath), data, 0644) + if err != nil { + return pkgerrors.Wrap(err, "Writing configuration file into chartpath") + } + } + + return nil +} + +//ProcessProfileYaml parses the manifest.yaml file that is part of the profile +//package and creates the appropriate structures out of it. +func ProcessProfileYaml(fpath string, manifestFileName string) (ProfileYamlClient, error) { + + p := filepath.Join(fpath, manifestFileName) + data, err := ioutil.ReadFile(p) + if err != nil { + return ProfileYamlClient{}, pkgerrors.Wrap(err, "Reading manifest file") + } + + out := profileOverride{} + err = yaml.Unmarshal(data, &out) + if err != nil { + return ProfileYamlClient{}, pkgerrors.Wrap(err, "Marshaling manifest yaml file") + } + + return ProfileYamlClient{path: fpath, override: out}, nil +} diff --git a/src/orchestrator/utils/profile_yaml.go b/src/orchestrator/utils/profile_yaml.go deleted file mode 100644 index 91687c0a..00000000 --- a/src/orchestrator/utils/profile_yaml.go +++ /dev/null @@ -1,109 +0,0 @@ -/* - * Copyright 2020 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 utils - -import ( - "io/ioutil" - "log" - "path/filepath" - - "github.com/ghodss/yaml" - pkgerrors "github.com/pkg/errors" -) - -/* -#Sample Yaml format for profile manifest.yaml ---- -version: v1 -type: - values: "values_override.yaml" - configresource: - - filepath: config.yaml - chartpath: chart/config/resources/config.yaml - - filepath: config2.yaml - chartpath: chart/config/resources/config2.yaml -*/ - -type overrideFiles struct { - FilePath string `yaml:"filepath"` - ChartPath string `yaml:"chartpath"` -} - -type supportedOverrides struct { - ConfigResource []overrideFiles `yaml:"configresource"` - Values string `yaml:"values"` -} - -type profileOverride struct { - Version string `yaml:"version"` - Type supportedOverrides `yaml:"type"` -} - -type ProfileYamlClient struct { - path string - override profileOverride -} - -func (p ProfileYamlClient) Print() { - log.Println(p.override) -} - -//GetValues returns a path to the override values.yam -//that was part of the profile -func (p ProfileYamlClient) GetValues() string { - return filepath.Join(p.path, p.override.Type.Values) -} - -//CopyConfigurationOverrides copies the various files that are -//provided as overrides to their corresponding locations within -//the destination chart. -func (p ProfileYamlClient) CopyConfigurationOverrides(chartPath string) error { - - //Iterate over each configresource and copy that file into - //the respective path in the chart. - for _, v := range p.override.Type.ConfigResource { - data, err := ioutil.ReadFile(filepath.Join(p.path, v.FilePath)) - if err != nil { - return pkgerrors.Wrap(err, "Reading configuration file") - } - err = ioutil.WriteFile(filepath.Join(chartPath, v.ChartPath), data, 0644) - if err != nil { - return pkgerrors.Wrap(err, "Writing configuration file into chartpath") - } - } - - return nil -} - -//ProcessProfileYaml parses the manifest.yaml file that is part of the profile -//package and creates the appropriate structures out of it. -func ProcessProfileYaml(fpath string, manifestFileName string) (ProfileYamlClient, error) { - - p := filepath.Join(fpath, manifestFileName) - data, err := ioutil.ReadFile(p) - if err != nil { - return ProfileYamlClient{}, pkgerrors.Wrap(err, "Reading manifest file") - } - - out := profileOverride{} - err = yaml.Unmarshal(data, &out) - if err != nil { - return ProfileYamlClient{}, pkgerrors.Wrap(err, "Marshaling manifest yaml file") - } - - return ProfileYamlClient{path: fpath, override: out}, nil -} diff --git a/src/orchestrator/utils/types/types.go b/src/orchestrator/utils/types/types.go deleted file mode 100644 index 4ffb4180..00000000 --- a/src/orchestrator/utils/types/types.go +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright 2020 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 types - -import ( - "k8s.io/apimachinery/pkg/runtime/schema" -) - -// KubernetesResourceTemplate - 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 -} diff --git a/src/orchestrator/utils/util-functions.go b/src/orchestrator/utils/util-functions.go deleted file mode 100644 index 13c78ba4..00000000 --- a/src/orchestrator/utils/util-functions.go +++ /dev/null @@ -1,116 +0,0 @@ -/* - * Copyright 2020 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 utils - -import ( - "archive/tar" - "compress/gzip" - "io" - "io/ioutil" - "os" - "path" - "path/filepath" - - pkgerrors "github.com/pkg/errors" -) - -//ExtractTarBall provides functionality to extract a tar.gz file -//into a temporary location for later use. -//It returns the path to the new location -func ExtractTarBall(r io.Reader) (string, error) { - //Check if it is a valid gz - gzf, err := gzip.NewReader(r) - if err != nil { - return "", pkgerrors.Wrap(err, "Invalid gzip format") - } - - //Check if it is a valid tar file - //Unfortunately this can only be done by inspecting all the tar contents - tarR := tar.NewReader(gzf) - first := true - - outDir, _ := ioutil.TempDir("", "k8s-ext-") - - for true { - header, err := tarR.Next() - - if err == io.EOF { - //Check if we have just a gzip file without a tar archive inside - if first { - return "", pkgerrors.New("Empty or non-existant Tar file found") - } - //End of archive - break - } - - if err != nil { - return "", pkgerrors.Wrap(err, "Error reading tar file") - } - - target := filepath.Join(outDir, header.Name) - - switch header.Typeflag { - case tar.TypeDir: - if _, err := os.Stat(target); err != nil { - // Using 755 read, write, execute for owner - // groups and others get read and execute permissions - // on the folder. - if err := os.MkdirAll(target, 0755); err != nil { - return "", pkgerrors.Wrap(err, "Creating directory") - } - } - case tar.TypeReg: - if target == outDir { // Handle '.' substituted to '' entry - continue - } - - err = EnsureDirectory(target) - if err != nil { - return "", pkgerrors.Wrap(err, "Creating Directory") - } - - f, err := os.OpenFile(target, os.O_CREATE|os.O_RDWR, os.FileMode(header.Mode)) - if err != nil { - return "", pkgerrors.Wrap(err, "Creating file") - } - - // copy over contents - if _, err := io.Copy(f, tarR); err != nil { - return "", pkgerrors.Wrap(err, "Copying file content") - } - - // close for each file instead of a defer for all - // at the end of the function - f.Close() - } - - first = false - } - - return outDir, nil -} - -//EnsureDirectory makes sure that the directories specified in the path exist -//If not, it will create them, if possible. -func EnsureDirectory(f string) error { - base := path.Dir(f) - _, err := os.Stat(base) - if err != nil && !os.IsNotExist(err) { - return err - } - return os.MkdirAll(base, 0755) -} diff --git a/src/orchestrator/utils/utils.go b/src/orchestrator/utils/utils.go new file mode 100644 index 00000000..13c78ba4 --- /dev/null +++ b/src/orchestrator/utils/utils.go @@ -0,0 +1,116 @@ +/* + * Copyright 2020 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 utils + +import ( + "archive/tar" + "compress/gzip" + "io" + "io/ioutil" + "os" + "path" + "path/filepath" + + pkgerrors "github.com/pkg/errors" +) + +//ExtractTarBall provides functionality to extract a tar.gz file +//into a temporary location for later use. +//It returns the path to the new location +func ExtractTarBall(r io.Reader) (string, error) { + //Check if it is a valid gz + gzf, err := gzip.NewReader(r) + if err != nil { + return "", pkgerrors.Wrap(err, "Invalid gzip format") + } + + //Check if it is a valid tar file + //Unfortunately this can only be done by inspecting all the tar contents + tarR := tar.NewReader(gzf) + first := true + + outDir, _ := ioutil.TempDir("", "k8s-ext-") + + for true { + header, err := tarR.Next() + + if err == io.EOF { + //Check if we have just a gzip file without a tar archive inside + if first { + return "", pkgerrors.New("Empty or non-existant Tar file found") + } + //End of archive + break + } + + if err != nil { + return "", pkgerrors.Wrap(err, "Error reading tar file") + } + + target := filepath.Join(outDir, header.Name) + + switch header.Typeflag { + case tar.TypeDir: + if _, err := os.Stat(target); err != nil { + // Using 755 read, write, execute for owner + // groups and others get read and execute permissions + // on the folder. + if err := os.MkdirAll(target, 0755); err != nil { + return "", pkgerrors.Wrap(err, "Creating directory") + } + } + case tar.TypeReg: + if target == outDir { // Handle '.' substituted to '' entry + continue + } + + err = EnsureDirectory(target) + if err != nil { + return "", pkgerrors.Wrap(err, "Creating Directory") + } + + f, err := os.OpenFile(target, os.O_CREATE|os.O_RDWR, os.FileMode(header.Mode)) + if err != nil { + return "", pkgerrors.Wrap(err, "Creating file") + } + + // copy over contents + if _, err := io.Copy(f, tarR); err != nil { + return "", pkgerrors.Wrap(err, "Copying file content") + } + + // close for each file instead of a defer for all + // at the end of the function + f.Close() + } + + first = false + } + + return outDir, nil +} + +//EnsureDirectory makes sure that the directories specified in the path exist +//If not, it will create them, if possible. +func EnsureDirectory(f string) error { + base := path.Dir(f) + _, err := os.Stat(base) + if err != nil && !os.IsNotExist(err) { + return err + } + return os.MkdirAll(base, 0755) +} -- cgit 1.2.3-korg