Skip to content

Commit

Permalink
refactor namespace/name logic (#6871)
Browse files Browse the repository at this point in the history
  • Loading branch information
pdabelf5 authored Nov 27, 2024
1 parent ce95536 commit 2767500
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
10 changes: 7 additions & 3 deletions internal/k8s/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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)
Expand All @@ -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":
Expand Down
29 changes: 29 additions & 0 deletions internal/k8s/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3505,3 +3505,32 @@ 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",
},
}

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)
}
})
}
}

0 comments on commit 2767500

Please sign in to comment.