Skip to content

Commit

Permalink
Only update secret when cluster secret contents differ
Browse files Browse the repository at this point in the history
Also change the label name
  • Loading branch information
sibucan committed Dec 4, 2024
1 parent 8aca681 commit fd175ed
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 23 deletions.
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ var rootCmd = &cobra.Command{
if _, ok := s.Labels[common.ControllerNameLabel]; ok {
var name string
var namespace string
if name, ok = s.Annotations[common.SecretNameAnnotation]; !ok {
if name, ok = s.Labels[common.ClusterNameLabel]; !ok {
return nil
}
if namespace, ok = s.Annotations[common.SecretNamespaceAnnotation]; !ok {
if namespace, ok = s.Labels[common.ClusterNamespaceLabel]; !ok {
return nil
}
return []reconcile.Request{
Expand Down
37 changes: 21 additions & 16 deletions internal/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"reflect"
"time"

"github.com/superorbital/capargo/pkg/common"
Expand Down Expand Up @@ -127,34 +128,38 @@ func (c *ClusterKubeconfigReconciler) createOrUpdateArgoCluster(ctx context.Cont
Labels: map[string]string{
argocdcommon.LabelKeySecretType: argocdcommon.LabelValueSecretTypeCluster,
common.ControllerNameLabel: common.ControllerName,
},
Annotations: map[string]string{
common.SecretNameAnnotation: cluster.Name,
common.SecretNamespaceAnnotation: cluster.Namespace,
common.ClusterNameLabel: cluster.Name,
common.ClusterNamespaceLabel: cluster.Namespace,
},
},
StringData: map[string]string{
"name": cluster.Name,
"server": config.Host,
"config": string(ccJson),
Data: map[string][]byte{
"name": []byte(cluster.Name),
"server": []byte(config.Host),
"config": ccJson,
},
}

action := "Created ArgoCD cluster secret"
err = c.Create(ctx, &argoClusterSecret, &client.CreateOptions{})
if err != nil && !errors.IsAlreadyExists(err) {
existingArgoClusterSecret := corev1.Secret{}
err = c.Get(ctx, client.ObjectKeyFromObject(&argoClusterSecret), &existingArgoClusterSecret, &client.GetOptions{})
if err != nil && !errors.IsNotFound(err) {
return err
}

if err != nil && errors.IsAlreadyExists(err) {
action = "Updated ArgoCD cluster secret"
err = c.Update(ctx, &argoClusterSecret, &client.UpdateOptions{})
if err != nil {
if errors.IsNotFound(err) {
if err := c.Create(ctx, &argoClusterSecret, &client.CreateOptions{}); err != nil {
return err
}
logger.V(4).Info("Created ArgoCD cluster", "cluster", cluster)
} else {
if !reflect.DeepEqual(existingArgoClusterSecret.Labels, argoClusterSecret.Labels) ||
!reflect.DeepEqual(existingArgoClusterSecret.Data, argoClusterSecret.Data) {
if err := c.Update(ctx, &argoClusterSecret, &client.UpdateOptions{}); err != nil {
return err
}
logger.V(4).Info("Updated ArgoCD cluster", "cluster", cluster)
}
}

logger.V(4).Info(action, "cluster", cluster)
return nil
}

Expand Down
10 changes: 5 additions & 5 deletions pkg/common/common.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package common

const (
slug = "superorbital.io"
ControllerName = "capargo"
ControllerNameLabel = slug + "/controller-name"
SecretNameAnnotation = slug + "/cluster-name"
SecretNamespaceAnnotation = slug + "/cluster-namespace"
slug = "superorbital.io"
ControllerName = "capargo"
ControllerNameLabel = ControllerName + "." + slug + "/controller-name"
ClusterNameLabel = ControllerName + "." + slug + "/cluster-name"
ClusterNamespaceLabel = ControllerName + "." + slug + "/cluster-namespace"
)

0 comments on commit fd175ed

Please sign in to comment.