Skip to content

Commit

Permalink
Use an unstructured object to avoid managing fields we do not care ab…
Browse files Browse the repository at this point in the history
…out.

Signed-off-by: cmoulliard <[email protected]>
  • Loading branch information
cmoulliard committed Nov 21, 2024
1 parent a8c3016 commit 80a785b
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions pkg/controllers/localbuild/argo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package localbuild
import (
"context"
"embed"
"encoding/json"
"fmt"
"golang.org/x/crypto/bcrypt"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/json"
"sigs.k8s.io/controller-runtime/pkg/client"
"time"

Expand Down Expand Up @@ -74,9 +75,22 @@ func (r *LocalbuildReconciler) ReconcileArgo(ctx context.Context, req ctrl.Reque
password := argocdDevModePassword
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), 0)
if err != nil {
return ctrl.Result{}, fmt.Errorf("Error hashing password: %w", err)
return ctrl.Result{}, fmt.Errorf("error hashing password: %w", err)
}

// Getting the argocd-secret
obj := v1.Secret{}
err = r.Client.Get(ctx, client.ObjectKey{Name: argocdAdminSecretName, Namespace: argocdNamespace}, &obj)
if err != nil {
return ctrl.Result{}, fmt.Errorf("getting argocd secret: %w", err)
}

// Using an unstructured object to avoid managing fields we do not care about.
u := unstructured.Unstructured{}
u.SetName(obj.GetName())
u.SetNamespace(obj.GetNamespace())
u.SetGroupVersionKind(obj.GetObjectKind().GroupVersionKind())

// Prepare the patch for the Secret's `stringData` field
patchData := map[string]interface{}{
"stringData": map[string]string{
Expand All @@ -85,23 +99,17 @@ func (r *LocalbuildReconciler) ReconcileArgo(ctx context.Context, req ctrl.Reque
},
}
// Convert patch data to JSON
patchBytes, err := json.Marshal(patchData)
patch, err := json.Marshal(patchData)
if err != nil {
return ctrl.Result{}, fmt.Errorf("Error marshalling patch data: %w", err)
}

// Getting the argocd-secret
s := v1.Secret{}
err = r.Client.Get(ctx, client.ObjectKey{Name: argocdAdminSecretName, Namespace: argocdNamespace}, &s)
if err != nil {
return ctrl.Result{}, fmt.Errorf("getting argocd secret: %w", err)
}

// Patching the argocd-secret with the user's hashed password
err = r.Client.Patch(ctx, &s, client.RawPatch(types.StrategicMergePatchType, patchBytes))
err = r.Client.Patch(ctx, &u, client.RawPatch(types.MergePatchType, patch))
if err != nil {
return ctrl.Result{}, fmt.Errorf("Error patching the Secret: %w", err)
return ctrl.Result{}, fmt.Errorf("error patching the Secret: %w", err)
}
return ctrl.Result{}, nil

/*
This is not needed as we will not generate a new admin password
Expand Down

0 comments on commit 80a785b

Please sign in to comment.