Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not try to update type of Secret in selfSignedCertProvider #6205

Merged
merged 1 commit into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pkg/apiserver/certificate/selfsignedcert_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,11 @@ func (p *selfSignedCertProvider) saveCertKeyToSecret(secret *corev1.Secret, cert
if bytes.Equal(cert, secret.Data[corev1.TLSCertKey]) && bytes.Equal(key, secret.Data[corev1.TLSPrivateKeyKey]) {
return nil
}
secret.Type = corev1.SecretTypeTLS
// Do not update the existing Secret's type. Otherwise, the update would fail if it's not of type
luolanzone marked this conversation as resolved.
Show resolved Hide resolved
// "kubernetes.io/tls" as the type field is immutable.
secret.Data[corev1.TLSCertKey] = cert
secret.Data[corev1.TLSPrivateKeyKey] = key
klog.InfoS("Updating Secret to persist self-signed cert", "secret", klog.KObj(secret))
_, err := p.client.CoreV1().Secrets(p.secretNamespace).Update(context.TODO(), secret, metav1.UpdateOptions{})
hangyan marked this conversation as resolved.
Show resolved Hide resolved
return err
}
Expand All @@ -331,6 +333,7 @@ func (p *selfSignedCertProvider) saveCertKeyToSecret(secret *corev1.Secret, cert
corev1.TLSPrivateKeyKey: key,
},
}
klog.InfoS("Creating Secret to persist self-signed cert", "secret", klog.KObj(secret))
_, err := p.client.CoreV1().Secrets(p.secretNamespace).Create(context.TODO(), caSecret, metav1.CreateOptions{})
return err
}
19 changes: 19 additions & 0 deletions pkg/apiserver/certificate/selfsignedcert_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,12 @@ func TestSelfSignedCertProviderRotate(t *testing.T) {
}, 5*time.Second, 100*time.Millisecond)
}

func copyAndMutateSecret(secret *corev1.Secret, mutator func(_ *corev1.Secret)) *corev1.Secret {
s := secret.DeepCopy()
mutator(s)
return s
}

func TestSelfSignedCertProviderRun(t *testing.T) {
t.Setenv(env.PodNamespaceEnvKey, testSecretNamespace)
testSecret := &corev1.Secret{
Expand Down Expand Up @@ -224,6 +230,19 @@ func TestSelfSignedCertProviderRun(t *testing.T) {
expectedCert: testOneYearCert2,
expectedKey: testOneYearKey2,
},
{
name: "should not update secret type when secret is opaque",
tlsSecretName: testSecretName,
existingSecret: copyAndMutateSecret(testSecret, func(s *corev1.Secret) {
s.Type = corev1.SecretTypeOpaque
}),
expectedSecret: copyAndMutateSecret(testSecret2, func(s *corev1.Secret) {
s.Type = corev1.SecretTypeOpaque
}),
minValidDuration: time.Hour * 24 * 370,
expectedCert: testOneYearCert2,
expectedKey: testOneYearKey2,
},
{
name: "should generate TLS and update secret when secret is empty",
tlsSecretName: testSecretName,
Expand Down
Loading