diff options
-rw-r--r-- | deployments/helm/servicemesh/istio/README.md | 20 | ||||
-rw-r--r-- | deployments/helm/servicemesh/istio/istio-instance/.helmignore | 22 | ||||
-rw-r--r-- | deployments/helm/servicemesh/istio/istio-instance/Chart.yaml | 22 | ||||
-rw-r--r-- | deployments/helm/servicemesh/istio/istio-instance/templates/_helpers.tpl | 63 | ||||
-rw-r--r-- | deployments/helm/servicemesh/istio/istio-instance/templates/istio-sds.yaml | 50 | ||||
-rw-r--r-- | deployments/helm/servicemesh/istio/istio-instance/values.yaml | 39 | ||||
-rw-r--r-- | src/k8splugin/api/api.go | 6 | ||||
-rw-r--r-- | src/k8splugin/api/brokerhandler_test.go | 18 | ||||
-rw-r--r-- | src/k8splugin/api/instancehandler.go | 10 | ||||
-rw-r--r-- | src/k8splugin/api/instancehandler_test.go | 49 | ||||
-rw-r--r-- | src/k8splugin/internal/app/instance.go | 92 | ||||
-rw-r--r-- | src/k8splugin/internal/app/instance_test.go | 148 | ||||
-rw-r--r-- | src/k8splugin/internal/plugin/helpers.go | 35 |
13 files changed, 373 insertions, 201 deletions
diff --git a/deployments/helm/servicemesh/istio/README.md b/deployments/helm/servicemesh/istio/README.md new file mode 100644 index 00000000..8fcba4f8 --- /dev/null +++ b/deployments/helm/servicemesh/istio/README.md @@ -0,0 +1,20 @@ +#/* +# * Copyright 2019 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. +# */ + +# Steps for Instaling Istio with Istio- Operator + +# Step 1 - Add the helm chart to install Istio in sds configuration +helm install istio-instance --name istio --namespace istio-system diff --git a/deployments/helm/servicemesh/istio/istio-instance/.helmignore b/deployments/helm/servicemesh/istio/istio-instance/.helmignore new file mode 100644 index 00000000..50af0317 --- /dev/null +++ b/deployments/helm/servicemesh/istio/istio-instance/.helmignore @@ -0,0 +1,22 @@ +# Patterns to ignore when building packages. +# This supports shell glob matching, relative path matching, and +# negation (prefixed with !). Only one pattern per line. +.DS_Store +# Common VCS dirs +.git/ +.gitignore +.bzr/ +.bzrignore +.hg/ +.hgignore +.svn/ +# Common backup files +*.swp +*.bak +*.tmp +*~ +# Various IDEs +.project +.idea/ +*.tmproj +.vscode/ diff --git a/deployments/helm/servicemesh/istio/istio-instance/Chart.yaml b/deployments/helm/servicemesh/istio/istio-instance/Chart.yaml new file mode 100644 index 00000000..ca2ff626 --- /dev/null +++ b/deployments/helm/servicemesh/istio/istio-instance/Chart.yaml @@ -0,0 +1,22 @@ + +#/* +# * Copyright 2019 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. +# */ + +apiVersion: v1 +appVersion: "1.0" +description: A Helm chart for Istio +name: istio-instance +version: 0.1.0 diff --git a/deployments/helm/servicemesh/istio/istio-instance/templates/_helpers.tpl b/deployments/helm/servicemesh/istio/istio-instance/templates/_helpers.tpl new file mode 100644 index 00000000..c2e7c701 --- /dev/null +++ b/deployments/helm/servicemesh/istio/istio-instance/templates/_helpers.tpl @@ -0,0 +1,63 @@ +#/* +# * Copyright 2019 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. +# */ + + +{{/* +Expand the name of the chart. +*/}} +{{- define "Chart-name.name" -}} +{{- default .Chart.name .Values.nameOverride | trunc 63 | trimSuffix "-" -}} +{{- end -}} + +{{/* +Create a default fully qualified app name. +We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec). +If release name contains chart name it will be used as a full name. +*/}} +{{- define "istio.fullname" -}} +{{- if .Values.fullnameOverride -}} +{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" -}} +{{- else -}} +{{- $name := default .Chart.Name .Values.nameOverride -}} +{{- if contains $name .Release.Name -}} +{{- .Release.Name | trunc 63 | trimSuffix "-" -}} +{{- else -}} +{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}} +{{- end -}} +{{- end -}} +{{- end -}} + +{{/* +Create chart name and version as used by the chart label. +*/}} +{{- define "istio.chart" -}} +{{- .Chart.Name | trunc 63 | trimSuffix "-" -}} +{{- end -}} + +{{/* +Create a fully qualified configmap name. +*/}} +{{- define "istio.configmap.fullname" -}} +{{- printf "%s-%s" .Release.Name "istio-mesh-config" | trunc 63 | trimSuffix "-" -}} +{{- end -}} + +{{/* +Configmap checksum. +*/}} +{{- define "istio.configmap.checksum" -}} +{{- print $.Template.BasePath "/configmap.yaml" | sha256sum -}} +{{- end -}} + diff --git a/deployments/helm/servicemesh/istio/istio-instance/templates/istio-sds.yaml b/deployments/helm/servicemesh/istio/istio-instance/templates/istio-sds.yaml new file mode 100644 index 00000000..8c440a4e --- /dev/null +++ b/deployments/helm/servicemesh/istio/istio-instance/templates/istio-sds.yaml @@ -0,0 +1,50 @@ + + +#/*Copyright 2019 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. +# */ +apiVersion: istio.banzaicloud.io/v1beta1 +kind: Istio +metadata: + labels: + controller-tools.k8s.io: "1.0" + name: {{ .Values.metadata.name }} +spec: + version: {{ .Values.spec.version | quote }} + mtls: {{ .Values.spec.mtls }} + autoInjectionNamespaces: {{- range .Values.spec.autoInjectionNamespaces }} + - {{ . | quote }} + {{- end }} + sds: + enabled: {{ .Values.spec.sds.enabled }} + udsPath: {{ .Values.spec.sds.udsPath | quote }} + useTrustworthyJwt: {{ .Values.spec.sds.useTrustworthyJwt }} + useNormalJwt: {{ .Values.spec.sds.useNormalJwt }} + gateways: + enabled: {{ .Values.spec.gateways.enabled }} + ingress: + enabled: {{ .Values.spec.gateways.ingress.enabled }} + sds: + enabled: {{ .Values.spec.gateways.ingress.sds.enabled }} + image: {{ .Values.spec.gateways.ingress.sds.image | quote }} + resources: {} + # requests: + # cpu: 100m + # memory: 128Mi + # limits: + # cpu: 2000m + # memory: 1024Mi + nodeAgent: + enabled: {{ .Values.spec.nodeAgent.enabled }} + image: {{ .Values.spec.nodeAgent.image | quote }} diff --git a/deployments/helm/servicemesh/istio/istio-instance/values.yaml b/deployments/helm/servicemesh/istio/istio-instance/values.yaml new file mode 100644 index 00000000..091999ac --- /dev/null +++ b/deployments/helm/servicemesh/istio/istio-instance/values.yaml @@ -0,0 +1,39 @@ + +#/* +# * Copyright 2019 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. +# */ +#Declare variables to be passed into Istio SDS template file. +metadata: + name: "istio-sample" +spec: + version: "1.2.2" + mtls: true + autoInjectionNamespaces: + - + sds: + enabled: true + udsPath: "unix:/var/run/sds/uds_path" + useTrustworthyJwt: false + useNormalJwt: true + gateways: + enabled: true + ingress: + enabled: true + sds: + enabled: true + image: "docker.io/istio/node-agent-k8s:1.2.2" + nodeAgent: + enabled: true + image : "docker.io/istio/node-agent-k8s:1.2.2" diff --git a/src/k8splugin/api/api.go b/src/k8splugin/api/api.go index 4308db4f..726bd116 100644 --- a/src/k8splugin/api/api.go +++ b/src/k8splugin/api/api.go @@ -39,6 +39,12 @@ func NewRouter(defClient rb.DefinitionManager, instRouter := router.PathPrefix("/v1").Subrouter() instRouter.HandleFunc("/instance", instHandler.createHandler).Methods("POST") instRouter.HandleFunc("/instance", instHandler.listHandler).Methods("GET") + // Match rb-names, versions or profiles + instRouter.HandleFunc("/instance", instHandler.listHandler). + Queries("rb-name", "{rb-name}", + "rb-version", "{rb-version}", + "profile-name", "{profile-name}").Methods("GET") + instRouter.HandleFunc("/instance/{instID}", instHandler.getHandler).Methods("GET") instRouter.HandleFunc("/instance/{instID}", instHandler.deleteHandler).Methods("DELETE") // (TODO): Fix update method diff --git a/src/k8splugin/api/brokerhandler_test.go b/src/k8splugin/api/brokerhandler_test.go index 319c64e7..8ef5e184 100644 --- a/src/k8splugin/api/brokerhandler_test.go +++ b/src/k8splugin/api/brokerhandler_test.go @@ -285,7 +285,7 @@ func TestBrokerFindHandler(t *testing.T) { }, }, instClient: &mockInstanceClient{ - items: []app.InstanceResponse{ + miniitems: []app.InstanceMiniResponse{ { ID: "HaKpys8e", Request: app.InstanceRequest{ @@ -295,22 +295,6 @@ func TestBrokerFindHandler(t *testing.T) { CloudRegion: "region1", }, Namespace: "testnamespace", - Resources: []helm.KubernetesResource{ - { - GVK: schema.GroupVersionKind{ - Group: "apps", - Version: "v1", - Kind: "Deployment"}, - Name: "test-deployment", - }, - { - GVK: schema.GroupVersionKind{ - Group: "", - Version: "v1", - Kind: "Service"}, - Name: "test-service", - }, - }, }, }, }, diff --git a/src/k8splugin/api/instancehandler.go b/src/k8splugin/api/instancehandler.go index 3ec055bc..be8e64fa 100644 --- a/src/k8splugin/api/instancehandler.go +++ b/src/k8splugin/api/instancehandler.go @@ -106,10 +106,16 @@ func (i instanceHandler) getHandler(w http.ResponseWriter, r *http.Request) { } } -// getHandler retrieves information about an instance via the ID +// listHandler retrieves information about an instance via the ID func (i instanceHandler) listHandler(w http.ResponseWriter, r *http.Request) { - resp, err := i.client.List() + //If parameters are not provided, they are sent as empty strings + //Which will list all instances + rbName := r.FormValue("rb-name") + rbVersion := r.FormValue("rb-version") + ProfileName := r.FormValue("profile-name") + + resp, err := i.client.List(rbName, rbVersion, ProfileName) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return diff --git a/src/k8splugin/api/instancehandler_test.go b/src/k8splugin/api/instancehandler_test.go index 83fa3d2b..418054ec 100644 --- a/src/k8splugin/api/instancehandler_test.go +++ b/src/k8splugin/api/instancehandler_test.go @@ -60,7 +60,7 @@ func (m *mockInstanceClient) Get(id string) (app.InstanceResponse, error) { return m.items[0], nil } -func (m *mockInstanceClient) List() ([]app.InstanceMiniResponse, error) { +func (m *mockInstanceClient) List(rbname, rbversion, profilename string) ([]app.InstanceMiniResponse, error) { if m.err != nil { return []app.InstanceMiniResponse{}, m.err } @@ -68,12 +68,12 @@ func (m *mockInstanceClient) List() ([]app.InstanceMiniResponse, error) { return m.miniitems, nil } -func (m *mockInstanceClient) Find(rbName string, ver string, profile string, labelKeys map[string]string) ([]app.InstanceResponse, error) { +func (m *mockInstanceClient) Find(rbName string, ver string, profile string, labelKeys map[string]string) ([]app.InstanceMiniResponse, error) { if m.err != nil { return nil, m.err } - return m.items, nil + return m.miniitems, nil } func (m *mockInstanceClient) Delete(id string) error { @@ -312,6 +312,8 @@ func TestInstanceListHandler(t *testing.T) { label string input string expectedCode int + queryParams bool + queryParamsMap map[string]string expectedResponse []app.InstanceMiniResponse instClient *mockInstanceClient }{ @@ -373,11 +375,52 @@ func TestInstanceListHandler(t *testing.T) { }, }, }, + { + label: "List Instances Based on Query Parameters", + queryParams: true, + queryParamsMap: map[string]string{ + "rb-name": "test-rbdef1", + }, + expectedCode: http.StatusOK, + expectedResponse: []app.InstanceMiniResponse{ + { + ID: "HaKpys8e", + Request: app.InstanceRequest{ + RBName: "test-rbdef", + RBVersion: "v1", + ProfileName: "profile1", + CloudRegion: "region1", + }, + Namespace: "testnamespace", + }, + }, + instClient: &mockInstanceClient{ + miniitems: []app.InstanceMiniResponse{ + { + ID: "HaKpys8e", + Request: app.InstanceRequest{ + RBName: "test-rbdef", + RBVersion: "v1", + ProfileName: "profile1", + CloudRegion: "region1", + }, + Namespace: "testnamespace", + }, + }, + }, + }, } for _, testCase := range testCases { t.Run(testCase.label, func(t *testing.T) { request := httptest.NewRequest("GET", "/v1/instance", nil) + if testCase.queryParams { + q := request.URL.Query() + for k, v := range testCase.queryParamsMap { + q.Add(k, v) + } + request.URL.RawQuery = q.Encode() + } resp := executeRequest(request, NewRouter(nil, nil, testCase.instClient, nil, nil, nil)) if testCase.expectedCode != resp.StatusCode { diff --git a/src/k8splugin/internal/app/instance.go b/src/k8splugin/internal/app/instance.go index 5cfdaea1..cf96d50c 100644 --- a/src/k8splugin/internal/app/instance.go +++ b/src/k8splugin/internal/app/instance.go @@ -60,8 +60,8 @@ type InstanceMiniResponse struct { type InstanceManager interface { Create(i InstanceRequest) (InstanceResponse, error) Get(id string) (InstanceResponse, error) - List() ([]InstanceMiniResponse, error) - Find(rbName string, ver string, profile string, labelKeys map[string]string) ([]InstanceResponse, error) + List(rbname, rbversion, profilename string) ([]InstanceMiniResponse, error) + Find(rbName string, ver string, profile string, labelKeys map[string]string) ([]InstanceMiniResponse, error) Delete(id string) error } @@ -184,7 +184,7 @@ func (v *InstanceClient) Get(id string) (InstanceResponse, error) { // List returns the instance for corresponding ID // Empty string returns all -func (v *InstanceClient) List() ([]InstanceMiniResponse, error) { +func (v *InstanceClient) List(rbname, rbversion, profilename string) ([]InstanceMiniResponse, error) { dbres, err := db.DBconn.ReadAll(v.storeName, v.tagInst) if err != nil || len(dbres) == 0 { @@ -192,6 +192,7 @@ func (v *InstanceClient) List() ([]InstanceMiniResponse, error) { } var results []InstanceMiniResponse + for key, value := range dbres { //value is a byte array if value != nil { @@ -206,6 +207,21 @@ func (v *InstanceClient) List() ([]InstanceMiniResponse, error) { Request: resp.Request, Namespace: resp.Namespace, } + + //Filter based on the accepted keys + if len(rbname) != 0 && + miniresp.Request.RBName != rbname { + continue + } + if len(rbversion) != 0 && + miniresp.Request.RBVersion != rbversion { + continue + } + if len(profilename) != 0 && + miniresp.Request.ProfileName != profilename { + continue + } + results = append(results, miniresp) } } @@ -218,70 +234,36 @@ func (v *InstanceClient) List() ([]InstanceMiniResponse, error) { // If profile is empty, it will return all instances for a given rbName+version // If labelKeys are provided, the results are filtered based on that. // It is an AND operation for labelkeys. -func (v *InstanceClient) Find(rbName string, version string, profile string, labelKeys map[string]string) ([]InstanceResponse, error) { +func (v *InstanceClient) Find(rbName string, version string, profile string, labelKeys map[string]string) ([]InstanceMiniResponse, error) { if rbName == "" && len(labelKeys) == 0 { - return []InstanceResponse{}, pkgerrors.New("rbName or labelkeys is required and cannot be empty") + return []InstanceMiniResponse{}, pkgerrors.New("rbName or labelkeys is required and cannot be empty") } - values, err := db.DBconn.ReadAll(v.storeName, v.tagInst) - if err != nil || len(values) == 0 { - return []InstanceResponse{}, pkgerrors.Wrap(err, "Find Instance") + responses, err := v.List(rbName, version, profile) + if err != nil { + return []InstanceMiniResponse{}, pkgerrors.Wrap(err, "Listing Instances") } - response := []InstanceResponse{} - //values is a map[string][]byte -InstanceResponseLoop: - for _, value := range values { - resp := InstanceResponse{} - db.DBconn.Unmarshal(value, &resp) - if err != nil { - return []InstanceResponse{}, pkgerrors.Wrap(err, "Unmarshaling Instance Value") - } + ret := []InstanceMiniResponse{} - // Filter by labels provided - if len(labelKeys) != 0 { - for lkey, lvalue := range labelKeys { - //Check if label key exists and get its value - if val, ok := resp.Request.Labels[lkey]; ok { - if lvalue != val { - continue InstanceResponseLoop - } - } else { - continue InstanceResponseLoop - } - } - } + //filter the list by labelKeys now + for _, resp := range responses { - if rbName != "" { - if resp.Request.RBName == rbName { - - //Check if a version is provided and if it matches - if version != "" { - if resp.Request.RBVersion == version { - //Check if a profilename matches or if it is not provided - if profile == "" || resp.Request.ProfileName == profile { - response = append(response, resp) - } - } - } else { - //Append all versions as version is not provided - response = append(response, resp) - } + add := true + for k, v := range labelKeys { + if resp.Request.Labels[k] != v { + add = false + break } - } else { - response = append(response, resp) } - } - - //filter the list by labelKeys now - for _, value := range response { - for _, label := range labelKeys { - if _, ok := value.Request.Labels[label]; ok { - } + // If label was not found in the response, don't add it + if add { + ret = append(ret, resp) } + } - return response, nil + return ret, nil } // Delete the Instance from database diff --git a/src/k8splugin/internal/app/instance_test.go b/src/k8splugin/internal/app/instance_test.go index 3cb62ee1..b79cf388 100644 --- a/src/k8splugin/internal/app/instance_test.go +++ b/src/k8splugin/internal/app/instance_test.go @@ -434,7 +434,7 @@ func TestInstanceFind(t *testing.T) { Items: items, } - expected := []InstanceResponse{ + expected := []InstanceMiniResponse{ { ID: "HaKpys8e", Request: InstanceRequest{ @@ -447,22 +447,6 @@ func TestInstanceFind(t *testing.T) { }, }, Namespace: "testnamespace", - Resources: []helm.KubernetesResource{ - { - GVK: schema.GroupVersionKind{ - Group: "apps", - Version: "v1", - Kind: "Deployment"}, - Name: "deployment-1", - }, - { - GVK: schema.GroupVersionKind{ - Group: "", - Version: "v1", - Kind: "Service"}, - Name: "service-1", - }, - }, }, { ID: "HaKpys8f", @@ -473,22 +457,6 @@ func TestInstanceFind(t *testing.T) { CloudRegion: "region1", }, Namespace: "testnamespace", - Resources: []helm.KubernetesResource{ - { - GVK: schema.GroupVersionKind{ - Group: "apps", - Version: "v1", - Kind: "Deployment"}, - Name: "deployment-1", - }, - { - GVK: schema.GroupVersionKind{ - Group: "", - Version: "v1", - Kind: "Service"}, - Name: "service-1", - }, - }, }, { ID: "HaKpys8g", @@ -499,22 +467,6 @@ func TestInstanceFind(t *testing.T) { CloudRegion: "region1", }, Namespace: "testnamespace", - Resources: []helm.KubernetesResource{ - { - GVK: schema.GroupVersionKind{ - Group: "apps", - Version: "v1", - Kind: "Deployment"}, - Name: "deployment-1", - }, - { - GVK: schema.GroupVersionKind{ - Group: "", - Version: "v1", - Kind: "Service"}, - Name: "service-1", - }, - }, }, } ic := NewInstanceClient() @@ -541,12 +493,12 @@ func TestInstanceFind(t *testing.T) { } }) - t.Run("Successfully Find Instance By Name Version", func(t *testing.T) { + t.Run("Successfully Find Instance By Name and Label", func(t *testing.T) { db.DBconn = &db.MockDB{ Items: items, } - expected := []InstanceResponse{ + expected := []InstanceMiniResponse{ { ID: "HaKpys8e", Request: InstanceRequest{ @@ -559,23 +511,53 @@ func TestInstanceFind(t *testing.T) { }, }, Namespace: "testnamespace", + }, + } + ic := NewInstanceClient() + name := "test-rbdef" + labels := map[string]string{ + "vf_module_id": "test-vf-module-id", + } + data, err := ic.Find(name, "", "", labels) + if err != nil { + t.Fatalf("TestInstanceFind returned an error (%s)", err) + } - Resources: []helm.KubernetesResource{ - { - GVK: schema.GroupVersionKind{ - Group: "apps", - Version: "v1", - Kind: "Deployment"}, - Name: "deployment-1", - }, - { - GVK: schema.GroupVersionKind{ - Group: "", - Version: "v1", - Kind: "Service"}, - Name: "service-1", + // Since the order of returned slice is not guaranteed + // Check both and return error if both don't match + sort.Slice(data, func(i, j int) bool { + return data[i].ID < data[j].ID + }) + // Sort both as it is not expected that testCase.expected + // is sorted + sort.Slice(expected, func(i, j int) bool { + return expected[i].ID < expected[j].ID + }) + + if !reflect.DeepEqual(expected, data) { + t.Fatalf("TestInstanceFind returned:\n result=%v\n expected=%v", + data, expected) + } + }) + + t.Run("Successfully Find Instance By Name Version", func(t *testing.T) { + db.DBconn = &db.MockDB{ + Items: items, + } + + expected := []InstanceMiniResponse{ + { + ID: "HaKpys8e", + Request: InstanceRequest{ + RBName: "test-rbdef", + RBVersion: "v1", + ProfileName: "profile1", + CloudRegion: "region1", + Labels: map[string]string{ + "vf_module_id": "test-vf-module-id", }, }, + Namespace: "testnamespace", }, { ID: "HaKpys8f", @@ -586,23 +568,6 @@ func TestInstanceFind(t *testing.T) { CloudRegion: "region1", }, Namespace: "testnamespace", - - Resources: []helm.KubernetesResource{ - { - GVK: schema.GroupVersionKind{ - Group: "apps", - Version: "v1", - Kind: "Deployment"}, - Name: "deployment-1", - }, - { - GVK: schema.GroupVersionKind{ - Group: "", - Version: "v1", - Kind: "Service"}, - Name: "service-1", - }, - }, }, } ic := NewInstanceClient() @@ -634,7 +599,7 @@ func TestInstanceFind(t *testing.T) { Items: items, } - expected := []InstanceResponse{ + expected := []InstanceMiniResponse{ { ID: "HaKpys8e", Request: InstanceRequest{ @@ -647,23 +612,6 @@ func TestInstanceFind(t *testing.T) { }, }, Namespace: "testnamespace", - - Resources: []helm.KubernetesResource{ - { - GVK: schema.GroupVersionKind{ - Group: "apps", - Version: "v1", - Kind: "Deployment"}, - Name: "deployment-1", - }, - { - GVK: schema.GroupVersionKind{ - Group: "", - Version: "v1", - Kind: "Service"}, - Name: "service-1", - }, - }, }, } ic := NewInstanceClient() diff --git a/src/k8splugin/internal/plugin/helpers.go b/src/k8splugin/internal/plugin/helpers.go index b5c9109c..ad785ab7 100644 --- a/src/k8splugin/internal/plugin/helpers.go +++ b/src/k8splugin/internal/plugin/helpers.go @@ -17,7 +17,6 @@ package plugin import ( - "encoding/json" "log" "strings" @@ -29,6 +28,7 @@ import ( corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/meta" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/client-go/dynamic" "k8s.io/client-go/kubernetes" @@ -107,44 +107,31 @@ func TagPodsIfPresent(unstruct *unstructured.Unstructured, tag string) { log.Println("Error converting spec to map") return } + template, ok := spec["template"].(map[string]interface{}) if !ok { log.Println("Error converting template to map") return } - data, err := json.Marshal(template) - if err != nil { - log.Println("Error Marshaling Podspec") - return - } - //Attempt to convert the template to a podtemplatespec. //This is to check if we have any pods being created. podTemplateSpec := &corev1.PodTemplateSpec{} - _, err = podTemplateSpec.MarshalTo(data) + err := runtime.DefaultUnstructuredConverter.FromUnstructured(template, podTemplateSpec) if err != nil { - log.Println("Did not find a podTemplateSpec" + err.Error()) + log.Println("Did not find a podTemplateSpec: " + err.Error()) return } - //At this point, we know that the data contains a PodTemplateSpec - metadata, ok := template["metadata"].(map[string]interface{}) - if !ok { - log.Println("Error converting metadata to map") - return - } - - //Get the labels map - labels, ok := metadata["labels"].(map[string]string) - if !ok { - log.Println("Error converting labels to map") - return - } - - //Check if labels exist for this object + labels := podTemplateSpec.GetLabels() if labels == nil { labels = map[string]string{} } labels[config.GetConfiguration().KubernetesLabelName] = tag + podTemplateSpec.SetLabels(labels) + + updatedTemplate, err := runtime.DefaultUnstructuredConverter.ToUnstructured(podTemplateSpec) + + //Set the label + spec["template"] = updatedTemplate } |