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

fix: update cert secret if exists #401

Merged
merged 6 commits into from
Dec 14, 2022
Merged
Changes from 1 commit
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
36 changes: 21 additions & 15 deletions helper/cert/source_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,25 +236,31 @@ func (s *GenSource) retryUpdateSecret(ctx context.Context, leaderCh chan bool, b
}

func (s *GenSource) updateSecret(ctx context.Context, bundle Bundle) error {
secret := &v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: certSecretName,
},
Data: map[string][]byte{
"cert": bundle.Cert,
"key": bundle.Key,
},
}
// Attempt updating the Secret first, and if it doesn't exist, fallback to
// create
_, err := s.K8sClient.CoreV1().Secrets(s.Namespace).Update(ctx, secret, metav1.UpdateOptions{})
secret, err := s.K8sClient.CoreV1().Secrets(s.Namespace).Get(ctx, certSecretName, metav1.GetOptions{})
tvoran marked this conversation as resolved.
Show resolved Hide resolved
if err != nil && !errors.IsNotFound(err) {
return err
}
if errors.IsNotFound(err) {
// The Secret does not exist, so create it.
secret = &v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: certSecretName,
},
Data: map[string][]byte{
"cert": bundle.Cert,
"key": bundle.Key,
},
}
_, err = s.K8sClient.CoreV1().Secrets(s.Namespace).Create(ctx, secret, metav1.CreateOptions{})
}
if err != nil {
return err
}
return nil
// The Secret exists, so update it.
secret.Data = map[string][]byte{
"cert": bundle.Cert,
"key": bundle.Key,
}
_, err = s.K8sClient.CoreV1().Secrets(s.Namespace).Update(ctx, secret, metav1.UpdateOptions{})
return err
}

func (s *GenSource) getBundleFromSecret() (Bundle, error) {
Expand Down