Skip to content

Commit

Permalink
clean up some logging, direct access object fields
Browse files Browse the repository at this point in the history
Co-authored-by: sindrerh2 <[email protected]>
Co-authored-by: Morten Lied Johansen <[email protected]>
  • Loading branch information
3 people committed Apr 10, 2024
1 parent 508aca3 commit 4f5211d
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions internal/controller/sqlsslcert_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,10 @@ func (r *SQLSSLCertReconciler) reconcileSQLSSLCert(ctx context.Context, req ctrl
sqlSslCert := &v1beta1.SQLSSLCert{}
if err := r.Client.Get(ctx, req.NamespacedName, sqlSslCert); err != nil {
if apierrors.IsNotFound(err) {
logger.Info("SQLSSLCert not found, aborting reconcile", "sqlsslcert", req.NamespacedName)
logger.Info("SQLSSLCert not found, aborting reconcile")
return nil
}
logger.Error(err, "Failed to get SQLSSLCert")
return temporaryFailureError(err)
return temporaryFailureError(fmt.Errorf("failed to get SQLSSLCert: %w", err))
}

if sqlSslCert.Status.Cert == nil || sqlSslCert.Status.PrivateKey == nil || sqlSslCert.Status.ServerCaCert == nil {
Expand All @@ -93,16 +92,13 @@ func (r *SQLSSLCertReconciler) reconcileSQLSSLCert(ctx context.Context, req ctrl

var secretName string
var ok bool
if secretName, ok = sqlSslCert.GetAnnotations()["sqeletor.nais.io/secret-name"]; !ok {
if secretName, ok = sqlSslCert.Annotations["sqeletor.nais.io/secret-name"]; !ok {
return fmt.Errorf("secret name not found")
}
logger = logger.WithValues("secret", secretName)
logger = logger.WithValues("secret", "secretName", secretName)

secret := &core_v1.Secret{ObjectMeta: meta_v1.ObjectMeta{Namespace: req.Namespace, Name: secretName}}
op, err := controllerutil.CreateOrUpdate(ctx, r.Client, secret, func() error {
annotations := secret.GetAnnotations()
labels := secret.GetLabels()

// if new resource, add owner reference and managed-by label
if secret.CreationTimestamp.IsZero() {
secret.OwnerReferences = []meta_v1.OwnerReference{{
Expand All @@ -111,19 +107,19 @@ func (r *SQLSSLCertReconciler) reconcileSQLSSLCert(ctx context.Context, req ctrl
Name: sqlSslCert.GetName(),
UID: sqlSslCert.GetUID(),
}}
labels[managedByKey] = sqeletorFqdnId
secret.Labels[managedByKey] = sqeletorFqdnId
}

// if we don't manage this resource, error out
if labels[managedByKey] != sqeletorFqdnId {
if secret.Labels[managedByKey] != sqeletorFqdnId {
return fmt.Errorf("secret %s in namesapce %s is not managed by us: %w", secret.Name, secret.Namespace, errNotManaged)
}

labels[typeKey] = sqeletorFqdnId
labels[appKey] = sqlSslCert.GetLabels()[appKey]
labels[teamKey] = sqlSslCert.GetLabels()[teamKey]
secret.Labels[typeKey] = sqeletorFqdnId
secret.Labels[appKey] = sqlSslCert.Labels[appKey]
secret.Labels[teamKey] = sqlSslCert.Labels[teamKey]

annotations[deploymentCorrelationIdKey] = sqlSslCert.GetAnnotations()[deploymentCorrelationIdKey]
secret.Annotations[deploymentCorrelationIdKey] = sqlSslCert.Annotations[deploymentCorrelationIdKey]

secret.StringData = map[string]string{
certKey: *sqlSslCert.Status.Cert,
Expand Down

0 comments on commit 4f5211d

Please sign in to comment.