From ce52efd65381d1b36a02ee3e232be4536ca5dd88 Mon Sep 17 00:00:00 2001 From: Paul Abel Date: Wed, 27 Nov 2024 11:26:58 +0000 Subject: [PATCH 1/3] refactor namespace/name logic --- internal/k8s/controller.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/internal/k8s/controller.go b/internal/k8s/controller.go index 864a46e6e4..830abce4ed 100644 --- a/internal/k8s/controller.go +++ b/internal/k8s/controller.go @@ -1747,7 +1747,7 @@ func (lbc *LoadBalancerController) handleRegularSecretDeletion(resources []Resou } func (lbc *LoadBalancerController) handleSecretUpdate(secret *api_v1.Secret, resources []Resource) { - secretNsName := secret.Namespace + "/" + secret.Name + secretNsName := generateSecretNSName(secret) var warnings configs.Warnings var addOrUpdateErr error @@ -1764,7 +1764,7 @@ func (lbc *LoadBalancerController) handleSecretUpdate(secret *api_v1.Secret, res } func (lbc *LoadBalancerController) validationTLSSpecialSecret(secret *api_v1.Secret, secretName string, secretList *[]string) { - secretNsName := secret.Namespace + "/" + secret.Name + secretNsName := generateSecretNSName(secret) err := secrets.ValidateTLSSecret(secret) if err != nil { @@ -1777,7 +1777,7 @@ func (lbc *LoadBalancerController) validationTLSSpecialSecret(secret *api_v1.Sec func (lbc *LoadBalancerController) handleSpecialSecretUpdate(secret *api_v1.Secret) { var specialTLSSecretsToUpdate []string - secretNsName := secret.Namespace + "/" + secret.Name + secretNsName := generateSecretNSName(secret) if secretNsName == lbc.specialSecrets.defaultServerSecret { lbc.validationTLSSpecialSecret(secret, configs.DefaultServerSecretFileName, &specialTLSSecretsToUpdate) @@ -1795,6 +1795,10 @@ func (lbc *LoadBalancerController) handleSpecialSecretUpdate(secret *api_v1.Secr lbc.recorder.Eventf(secret, api_v1.EventTypeNormal, "Updated", "the special Secret %v was updated", secretNsName) } +func generateSecretNSName(secret *api_v1.Secret) string { + return secret.Namespace + "/" + secret.Name +} + func getStatusFromEventTitle(eventTitle string) string { switch eventTitle { case "AddedOrUpdatedWithError", "Rejected", "NoVirtualServersFound", "Missing Secret", "UpdatedWithError": From c61a11686f6f9648efeec7f0e9e0d8528e77de22 Mon Sep 17 00:00:00 2001 From: Paul Abel Date: Wed, 27 Nov 2024 12:04:08 +0000 Subject: [PATCH 2/3] add tests --- internal/k8s/controller_test.go | 59 +++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/internal/k8s/controller_test.go b/internal/k8s/controller_test.go index c7fb4bb7ab..7abf53dada 100644 --- a/internal/k8s/controller_test.go +++ b/internal/k8s/controller_test.go @@ -3505,3 +3505,62 @@ func TestNewTelemetryCollector(t *testing.T) { } } } + +func TestGenerateSecretNSName(t *testing.T) { + t.Parallel() + testCases := []struct { + name string + secret *api_v1.Secret + expected string + }{ + { + name: "Valid secret", + secret: &api_v1.Secret{ + ObjectMeta: meta_v1.ObjectMeta{ + Namespace: "testns", + Name: "test-secret", + }, + }, + expected: "testns/test-secret", + }, + { + name: "invalid #1", + secret: &api_v1.Secret{ + ObjectMeta: meta_v1.ObjectMeta{ + Namespace: "", + Name: "", + }, + }, + expected: "/", + }, + { + name: "invalid #2", + secret: &api_v1.Secret{ + ObjectMeta: meta_v1.ObjectMeta{ + Namespace: "testns", + Name: "", + }, + }, + expected: "testns/", + }, + { + name: "invalid #3", + secret: &api_v1.Secret{ + ObjectMeta: meta_v1.ObjectMeta{ + Namespace: "", + Name: "test-secret", + }, + }, + expected: "/test-secret", + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + result := generateSecretNSName(tc.secret) + if result != tc.expected { + t.Fatalf("Expected %v, but got %v", tc.expected, result) + } + }) + } +} From 8324d0f0b3be365fb084bb15f98751b08d766795 Mon Sep 17 00:00:00 2001 From: Paul Abel Date: Wed, 27 Nov 2024 12:15:20 +0000 Subject: [PATCH 3/3] remove invalid tests --- internal/k8s/controller_test.go | 30 ------------------------------ 1 file changed, 30 deletions(-) diff --git a/internal/k8s/controller_test.go b/internal/k8s/controller_test.go index 7abf53dada..777a195b41 100644 --- a/internal/k8s/controller_test.go +++ b/internal/k8s/controller_test.go @@ -3523,36 +3523,6 @@ func TestGenerateSecretNSName(t *testing.T) { }, expected: "testns/test-secret", }, - { - name: "invalid #1", - secret: &api_v1.Secret{ - ObjectMeta: meta_v1.ObjectMeta{ - Namespace: "", - Name: "", - }, - }, - expected: "/", - }, - { - name: "invalid #2", - secret: &api_v1.Secret{ - ObjectMeta: meta_v1.ObjectMeta{ - Namespace: "testns", - Name: "", - }, - }, - expected: "testns/", - }, - { - name: "invalid #3", - secret: &api_v1.Secret{ - ObjectMeta: meta_v1.ObjectMeta{ - Namespace: "", - Name: "test-secret", - }, - }, - expected: "/test-secret", - }, } for _, tc := range testCases {