Skip to content

Commit

Permalink
fix(controller): empty service password stanza
Browse files Browse the repository at this point in the history
  • Loading branch information
dmolik authored Feb 18, 2025
1 parent 7fa6c34 commit b53476a
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions internal/controller/valkey_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -1405,22 +1405,36 @@ func (r *ValkeyReconciler) upsertCertificate(ctx context.Context, valkey *hyperv
return nil
}

func getServicePasswordKey(valkey *hyperv1.Valkey) (string) {
if valkey.Spec.ServicePassword == nil {
return "password"
}
return valkey.Spec.ServicePassword.Key
}

func getServicePasswordName(valkey *hyperv1.Valkey) (string) {
if valkey.Spec.ServicePassword == nil {
return valkey.Name
}
return valkey.Spec.ServicePassword.
}

Check failure on line 1420 in internal/controller/valkey_controller.go

View workflow job for this annotation

GitHub Actions / lint

syntax error: unexpected }, expected name or (

Check failure on line 1420 in internal/controller/valkey_controller.go

View workflow job for this annotation

GitHub Actions / lint

syntax error: unexpected }, expected name or (

Check failure on line 1420 in internal/controller/valkey_controller.go

View workflow job for this annotation

GitHub Actions / lint

expected selector or type assertion, found '}' (typecheck)

Check failure on line 1420 in internal/controller/valkey_controller.go

View workflow job for this annotation

GitHub Actions / build

expected selector or type assertion, found '}'

Check failure on line 1420 in internal/controller/valkey_controller.go

View workflow job for this annotation

GitHub Actions / build

expected selector or type assertion, found '}'

Check failure on line 1420 in internal/controller/valkey_controller.go

View workflow job for this annotation

GitHub Actions / build

expected selector or type assertion, found '}'

Check failure on line 1420 in internal/controller/valkey_controller.go

View workflow job for this annotation

GitHub Actions / build

expected selector or type assertion, found '}'

Check failure on line 1420 in internal/controller/valkey_controller.go

View workflow job for this annotation

GitHub Actions / build

expected selector or type assertion, found '}'

Check failure on line 1420 in internal/controller/valkey_controller.go

View workflow job for this annotation

GitHub Actions / build-operator-container

expected selector or type assertion, found '}'

Check failure on line 1420 in internal/controller/valkey_controller.go

View workflow job for this annotation

GitHub Actions / build-operator-container

expected selector or type assertion, found '}'

Check failure on line 1420 in internal/controller/valkey_controller.go

View workflow job for this annotation

GitHub Actions / build-operator-container

expected selector or type assertion, found '}'

Check failure on line 1420 in internal/controller/valkey_controller.go

View workflow job for this annotation

GitHub Actions / build-operator-container

expected selector or type assertion, found '}'

Check failure on line 1420 in internal/controller/valkey_controller.go

View workflow job for this annotation

GitHub Actions / build-operator-container

expected selector or type assertion, found '}'

Check failure on line 1420 in internal/controller/valkey_controller.go

View workflow job for this annotation

GitHub Actions / test

expected selector or type assertion, found '}'

Check failure on line 1420 in internal/controller/valkey_controller.go

View workflow job for this annotation

GitHub Actions / test

expected selector or type assertion, found '}'

Check failure on line 1420 in internal/controller/valkey_controller.go

View workflow job for this annotation

GitHub Actions / test

expected selector or type assertion, found '}'

Check failure on line 1420 in internal/controller/valkey_controller.go

View workflow job for this annotation

GitHub Actions / test

expected selector or type assertion, found '}'

Check failure on line 1420 in internal/controller/valkey_controller.go

View workflow job for this annotation

GitHub Actions / test

expected selector or type assertion, found '}'

func (r *ValkeyReconciler) getServicePassword(ctx context.Context, valkey *hyperv1.Valkey) (string, error) {

Check failure on line 1422 in internal/controller/valkey_controller.go

View workflow job for this annotation

GitHub Actions / lint

syntax error: unexpected name context in argument list; possibly missing comma or )) (typecheck)

Check failure on line 1422 in internal/controller/valkey_controller.go

View workflow job for this annotation

GitHub Actions / lint

syntax error: unexpected name context in argument list; possibly missing comma or ) (typecheck)
logger := log.FromContext(ctx)

secret := &corev1.Secret{}
err := r.Get(ctx, types.NamespacedName{Namespace: valkey.Namespace, Name: valkey.Spec.ServicePassword.Name}, secret)
err := r.Get(ctx, types.NamespacedName{Namespace: valkey.Namespace, Name: getServicePasswordName(valkey)}, secret)
if err != nil {
logger.Error(err, "failed to fetch secret", "name", valkey.Spec.ServicePassword.Name)
logger.Error(err, "failed to fetch secret", "name", getServicePasswordName(valkey))
return "", err
}
if secret.Data == nil {
return "", fmt.Errorf("secret %s/%s is empty", valkey.Namespace, valkey.Spec.ServicePassword.Name)
return "", fmt.Errorf("secret %s/%s is empty", valkey.Namespace, getServicePasswordName(valkey))
}
if secret.Data[valkey.Spec.ServicePassword.Key] == nil {
return "", fmt.Errorf("key %s is empty in secret %s/%s", valkey.Spec.ServicePassword.Key, valkey.Namespace, valkey.Spec.ServicePassword.Name)
if secret.Data[getServicePasswordKey(valkey)] == nil {
return "", fmt.Errorf("key %s is empty in secret %s/%s", getServicePasswordKey(valkey), valkey.Namespace, getServicePasswordName(valkey))
}
return string(secret.Data[valkey.Spec.ServicePassword.Key]), nil
return string(secret.Data[getServicePasswordKey(valkey)]), nil
}

func (r *ValkeyReconciler) upsertSecret(ctx context.Context, valkey *hyperv1.Valkey, once bool) (string, error) {
Expand Down Expand Up @@ -1902,9 +1916,9 @@ func (r *ValkeyReconciler) exporter(valkey *hyperv1.Valkey) corev1.Container {
Name: "VALKEY_PASSWORD",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
Key: valkey.Spec.ServicePassword.Key,
Key: getServicePasswordKey(valkey),
LocalObjectReference: corev1.LocalObjectReference{
Name: valkey.Spec.ServicePassword.Name,
Name: getServicePasswordName(valkey),
},
},
},
Expand All @@ -1913,9 +1927,9 @@ func (r *ValkeyReconciler) exporter(valkey *hyperv1.Valkey) corev1.Container {
Name: "REDIS_PASSWORD",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
Key: valkey.Spec.ServicePassword.Key,
Key: getServicePasswordKey(valkey),
LocalObjectReference: corev1.LocalObjectReference{
Name: valkey.Spec.ServicePassword.Name,
Name: getServicePasswordName(valkey),
},
},
},
Expand Down Expand Up @@ -2346,9 +2360,9 @@ func (r *ValkeyReconciler) upsertStatefulSet(ctx context.Context, valkey *hyperv
Name: "REDISCLI_AUTH",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
Key: valkey.Spec.ServicePassword.Key,
Key: getServicePasswordKey(valkey),
LocalObjectReference: corev1.LocalObjectReference{
Name: valkey.Spec.ServicePassword.Name,
Name: getServicePasswordName(valkey),
},
},
},
Expand All @@ -2357,9 +2371,9 @@ func (r *ValkeyReconciler) upsertStatefulSet(ctx context.Context, valkey *hyperv
Name: "VALKEY_PASSWORD",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
Key: valkey.Spec.ServicePassword.Key,
Key: getServicePasswordKey(valkey),
LocalObjectReference: corev1.LocalObjectReference{
Name: valkey.Spec.ServicePassword.Name,
Name: getServicePasswordName(valkey),
},
},
},
Expand Down

0 comments on commit b53476a

Please sign in to comment.