From a2195caa10de473415a41e2631b4e29adb216dd8 Mon Sep 17 00:00:00 2001 From: Prabhat Khera <91852476+prabhat-org@users.noreply.github.com> Date: Mon, 5 Feb 2024 16:30:36 +1300 Subject: [PATCH] keep labels on edit ingress, configmaps and secrets (#11050) --- api/kubernetes/cli/ingress.go | 7 +++++-- app/kubernetes/converters/configMap.js | 2 ++ app/kubernetes/converters/configuration.js | 2 ++ app/kubernetes/converters/secret.js | 2 ++ app/kubernetes/models/config-map/models.js | 1 + app/kubernetes/models/configuration/formvalues.js | 1 + app/kubernetes/models/secret/models.js | 1 + app/kubernetes/services/configurationService.js | 2 ++ .../configurations/configmap/edit/configMapController.js | 1 + .../views/configurations/secret/edit/secretController.js | 1 + .../ingresses/CreateIngressView/CreateIngressView.tsx | 1 + app/react/kubernetes/ingresses/CreateIngressView/types.ts | 1 + app/react/kubernetes/ingresses/CreateIngressView/utils.ts | 1 + 13 files changed, 21 insertions(+), 2 deletions(-) diff --git a/api/kubernetes/cli/ingress.go b/api/kubernetes/cli/ingress.go index 895ebd893..54486781c 100644 --- a/api/kubernetes/cli/ingress.go +++ b/api/kubernetes/cli/ingress.go @@ -241,7 +241,10 @@ func (kcl *KubeClient) DeleteIngresses(reqs models.K8sIngressDeleteRequests) err // UpdateIngress updates an existing ingress in a given namespace in a k8s endpoint. func (kcl *KubeClient) UpdateIngress(namespace string, info models.K8sIngressInfo) error { ingressClient := kcl.cli.NetworkingV1().Ingresses(namespace) - var ingress netv1.Ingress + ingress, err := ingressClient.Get(context.Background(), info.Name, metav1.GetOptions{}) + if err != nil { + return err + } ingress.Name = info.Name ingress.Namespace = info.Namespace @@ -299,6 +302,6 @@ func (kcl *KubeClient) UpdateIngress(namespace string, info models.K8sIngressInf } } - _, err := ingressClient.Update(context.Background(), &ingress, metav1.UpdateOptions{}) + _, err = ingressClient.Update(context.Background(), ingress, metav1.UpdateOptions{}) return err } diff --git a/app/kubernetes/converters/configMap.js b/app/kubernetes/converters/configMap.js index 262fb65cf..ec3d1087f 100644 --- a/app/kubernetes/converters/configMap.js +++ b/app/kubernetes/converters/configMap.js @@ -38,6 +38,7 @@ class KubernetesConfigMapConverter { res.ConfigurationOwner = data.metadata.labels ? data.metadata.labels[KubernetesPortainerConfigurationOwnerLabel] : ''; res.CreationDate = data.metadata.creationTimestamp; res.Yaml = yaml ? yaml.data : ''; + res.Labels = data.metadata.labels; res.Data = _.concat( _.map(data.data, (value, key) => { @@ -98,6 +99,7 @@ class KubernetesConfigMapConverter { res.metadata.uid = data.Id; res.metadata.name = data.Name; res.metadata.namespace = data.Namespace; + res.metadata.labels = data.Labels || {}; res.metadata.labels[KubernetesPortainerConfigurationOwnerLabel] = data.ConfigurationOwner; _.forEach(data.Data, (entry) => { if (entry.IsBinary) { diff --git a/app/kubernetes/converters/configuration.js b/app/kubernetes/converters/configuration.js index c31c8d006..6c69a9581 100644 --- a/app/kubernetes/converters/configuration.js +++ b/app/kubernetes/converters/configuration.js @@ -21,6 +21,7 @@ class KubernetesConfigurationConverter { if (secret.Annotations) { res.ServiceAccountName = secret.Annotations['kubernetes.io/service-account.name']; } + res.Labels = secret.Labels; return res; } @@ -37,6 +38,7 @@ class KubernetesConfigurationConverter { }); res.data = res.Data; res.ConfigurationOwner = configMap.ConfigurationOwner; + res.Labels = configMap.Labels; return res; } } diff --git a/app/kubernetes/converters/secret.js b/app/kubernetes/converters/secret.js index 1ab9702b1..725398c2b 100644 --- a/app/kubernetes/converters/secret.js +++ b/app/kubernetes/converters/secret.js @@ -39,6 +39,7 @@ class KubernetesSecretConverter { res.metadata.name = secret.Name; res.metadata.namespace = secret.Namespace; res.type = secret.Type; + res.metadata.labels = secret.Labels || {}; res.metadata.labels[KubernetesPortainerConfigurationOwnerLabel] = secret.ConfigurationOwner; let annotation = ''; @@ -67,6 +68,7 @@ class KubernetesSecretConverter { res.Name = payload.metadata.name; res.Namespace = payload.metadata.namespace; res.Type = payload.type; + res.Labels = payload.metadata.labels || {}; res.ConfigurationOwner = payload.metadata.labels ? payload.metadata.labels[KubernetesPortainerConfigurationOwnerLabel] : ''; res.CreationDate = payload.metadata.creationTimestamp; res.Annotations = payload.metadata.annotations; diff --git a/app/kubernetes/models/config-map/models.js b/app/kubernetes/models/config-map/models.js index 0ff339385..bde4b859b 100644 --- a/app/kubernetes/models/config-map/models.js +++ b/app/kubernetes/models/config-map/models.js @@ -21,6 +21,7 @@ const _KubernetesConfigMap = Object.freeze({ Yaml: '', ConfigurationOwner: '', Data: [], + Labels: {}, }); export class KubernetesConfigMap { diff --git a/app/kubernetes/models/configuration/formvalues.js b/app/kubernetes/models/configuration/formvalues.js index 700c43485..4dd455874 100644 --- a/app/kubernetes/models/configuration/formvalues.js +++ b/app/kubernetes/models/configuration/formvalues.js @@ -14,6 +14,7 @@ const _KubernetesConfigurationFormValues = Object.freeze({ IsSimple: true, ServiceAccountName: '', Type: KubernetesSecretTypeOptions.OPAQUE.value, + Labels: {}, }); export class KubernetesConfigurationFormValues { diff --git a/app/kubernetes/models/secret/models.js b/app/kubernetes/models/secret/models.js index a72479108..7cababcd3 100644 --- a/app/kubernetes/models/secret/models.js +++ b/app/kubernetes/models/secret/models.js @@ -12,6 +12,7 @@ const _KubernetesApplicationSecret = Object.freeze({ Data: [], SecretType: '', Annotations: [], + Labels: {}, }); export class KubernetesApplicationSecret { diff --git a/app/kubernetes/services/configurationService.js b/app/kubernetes/services/configurationService.js index a2b01eb55..5bdcd639f 100644 --- a/app/kubernetes/services/configurationService.js +++ b/app/kubernetes/services/configurationService.js @@ -82,10 +82,12 @@ class KubernetesConfigurationService { if (formValues.Kind === KubernetesConfigurationKinds.CONFIGMAP) { const configMap = KubernetesConfigMapConverter.configurationFormValuesToConfigMap(formValues); configMap.ConfigurationOwner = configuration.ConfigurationOwner; + configMap.Labels = configuration.Labels; await this.KubernetesConfigMapService.update(configMap); } else { const secret = KubernetesSecretConverter.configurationFormValuesToSecret(formValues); secret.ConfigurationOwner = configuration.ConfigurationOwner; + secret.Labels = configuration.Labels; await this.KubernetesSecretService.update(secret); } } diff --git a/app/kubernetes/views/configurations/configmap/edit/configMapController.js b/app/kubernetes/views/configurations/configmap/edit/configMapController.js index 916576d83..fcb558955 100644 --- a/app/kubernetes/views/configurations/configmap/edit/configMapController.js +++ b/app/kubernetes/views/configurations/configmap/edit/configMapController.js @@ -159,6 +159,7 @@ class KubernetesConfigMapController { this.formValues.Type = this.configuration.Type; this.formValues.Kind = this.configuration.Kind; this.oldDataYaml = this.formValues.DataYaml; + this.formValues.Labels = this.configuration.Labels; return this.configuration; } catch (err) { diff --git a/app/kubernetes/views/configurations/secret/edit/secretController.js b/app/kubernetes/views/configurations/secret/edit/secretController.js index 10133da4d..a71fb70de 100644 --- a/app/kubernetes/views/configurations/secret/edit/secretController.js +++ b/app/kubernetes/views/configurations/secret/edit/secretController.js @@ -155,6 +155,7 @@ class KubernetesSecretController { this.formValues.Type = this.configuration.Type; this.formValues.Kind = this.configuration.Kind; this.oldDataYaml = this.formValues.DataYaml; + this.formValues.Labels = this.configuration.Labels; return this.configuration; } catch (err) { diff --git a/app/react/kubernetes/ingresses/CreateIngressView/CreateIngressView.tsx b/app/react/kubernetes/ingresses/CreateIngressView/CreateIngressView.tsx index c85243271..dc8067a7f 100644 --- a/app/react/kubernetes/ingresses/CreateIngressView/CreateIngressView.tsx +++ b/app/react/kubernetes/ingresses/CreateIngressView/CreateIngressView.tsx @@ -828,6 +828,7 @@ export function CreateIngressView() { Paths: preparePaths(rule.IngressName, rule.Hosts), TLS: prepareTLS(rule.Hosts), Annotations: prepareAnnotations(rule.Annotations || []), + Labels: rule.Labels, }; if (isEdit) { diff --git a/app/react/kubernetes/ingresses/CreateIngressView/types.ts b/app/react/kubernetes/ingresses/CreateIngressView/types.ts index 79f29f964..6a723678e 100644 --- a/app/react/kubernetes/ingresses/CreateIngressView/types.ts +++ b/app/react/kubernetes/ingresses/CreateIngressView/types.ts @@ -28,6 +28,7 @@ export interface Rule { Hosts: Host[]; Annotations?: Annotation[]; IngressType?: string; + Labels?: Record; } export interface ServicePorts { diff --git a/app/react/kubernetes/ingresses/CreateIngressView/utils.ts b/app/react/kubernetes/ingresses/CreateIngressView/utils.ts index 9c70d4c34..29f99774f 100644 --- a/app/react/kubernetes/ingresses/CreateIngressView/utils.ts +++ b/app/react/kubernetes/ingresses/CreateIngressView/utils.ts @@ -104,6 +104,7 @@ export function prepareRuleFromIngress( Hosts: prepareRuleHostsFromIngress(ing) || [], Annotations: ing.Annotations ? getAnnotationsForEdit(ing.Annotations) : [], IngressType: ing.Type, + Labels: ing.Labels, }; }