Skip to content

Commit

Permalink
fix: Do not copy all labels from ArgoCD CR to resources we create (#414)
Browse files Browse the repository at this point in the history
* fix: Remove application instance label from secrets we create

* fix: Do not copy all labels from ArgoCD CR to resources we create

* Include tests
  • Loading branch information
jannfis committed Aug 31, 2021
1 parent 2da7728 commit 516e937
Show file tree
Hide file tree
Showing 26 changed files with 123 additions and 99 deletions.
10 changes: 10 additions & 0 deletions api/v1alpha1/argocd_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -675,3 +675,13 @@ func (argocd *ArgoCD) IsDeletionFinalizerPresent() bool {
}
return false
}

// ApplicationInstanceLabelKey returns either the custom application instance
// label key if set, or the default value.
func (a *ArgoCD) ApplicationInstanceLabelKey() string {
if a.Spec.ApplicationInstanceLabelKey != "" {
return a.Spec.ApplicationInstanceLabelKey
} else {
return common.ArgoCDDefaultApplicationInstanceLabelKey
}
}
17 changes: 17 additions & 0 deletions api/v1alpha1/argocd_types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package v1alpha1

import (
"testing"

"gotest.tools/assert"

"github.com/argoproj-labs/argocd-operator/common"
)

func Test_ArgoCD_ApplicationInstanceLabelKey(t *testing.T) {
cr := &ArgoCD{}
cr.Spec.ApplicationInstanceLabelKey = "my.corp/instance"
assert.Equal(t, cr.ApplicationInstanceLabelKey(), "my.corp/instance")
cr = &ArgoCD{}
assert.Equal(t, cr.ApplicationInstanceLabelKey(), common.ArgoCDDefaultApplicationInstanceLabelKey)
}
19 changes: 18 additions & 1 deletion common/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const (
ArgoCDDefaultApplicationSetVersion = "v0.1.0"

// ArgoCDDefaultApplicationInstanceLabelKey is the default app name as a tracking label.
ArgoCDDefaultApplicationInstanceLabelKey = "mycompany.com/appname"
ArgoCDDefaultApplicationInstanceLabelKey = "app.kubernetes.io/instance"

// ArgoCDDefaultArgoImage is the ArgoCD container image to use when not specified.
ArgoCDDefaultArgoImage = "argoproj/argocd"
Expand Down Expand Up @@ -284,3 +284,20 @@ ssh.dev.azure.com ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC7Hr1oTWqNqOlzGJOfGJ4Nak
vs-ssh.visualstudio.com ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC7Hr1oTWqNqOlzGJOfGJ4NakVyIzf1rXYd4d7wo6jBlkLvCA4odBlL0mDUyZ0/QUfTTqeu+tm22gOsv+VrVTMk6vwRU75gY/y9ut5Mb3bR5BV58dKXyq9A9UeB5Cakehn5Zgm6x1mKoVyf+FFn26iYqXJRgzIZZcZ5V6hrE0Qg39kZm4az48o0AUbf6Sp4SLdvnuMa2sVNwHBboS7EJkm57XQPVU3/QpyNLHbWDdzwtrlS+ez30S3AdYhLKEOxAG8weOnyrtLJAUen9mTkol8oII1edf7mWWbWVf0nBmly21+nZcmCTISQBtdcyPaEno7fFQMDD26/s0lfKob4Kw8H
`
)

// DefaultLabels returns the default set of labels for controllers.
func DefaultLabels(name string) map[string]string {
return map[string]string{
ArgoCDKeyName: name,
ArgoCDKeyPartOf: ArgoCDAppName,
ArgoCDKeyManagedBy: name,
}
}

// DefaultAnnotations returns the default set of annotations for child resources of ArgoCD
func DefaultAnnotations(name string, namespace string) map[string]string {
return map[string]string{
AnnotationName: name,
AnnotationNamespace: namespace,
}
}
6 changes: 3 additions & 3 deletions controllers/argocd/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func newConfigMap(cr *argoprojv1a1.ArgoCD) *corev1.ConfigMap {
ObjectMeta: metav1.ObjectMeta{
Name: cr.Name,
Namespace: cr.Namespace,
Labels: labelsForCluster(cr),
Labels: argoutil.LabelsForCluster(cr),
},
}
}
Expand Down Expand Up @@ -287,7 +287,7 @@ func (r *ReconcileArgoCD) reconcileCAConfigMap(cr *argoprojv1a1.ArgoCD) error {
return nil // ConfigMap found, do nothing
}

caSecret := argoutil.NewSecretWithSuffix(cr.ObjectMeta, common.ArgoCDCASuffix)
caSecret := argoutil.NewSecretWithSuffix(cr, common.ArgoCDCASuffix)
if !argoutil.IsObjectFound(r.Client, cr.Namespace, caSecret.Name, caSecret) {
log.Info(fmt.Sprintf("ca secret [%s] not found, waiting to reconcile ca configmap [%s]", caSecret.Name, cm.Name))
return nil
Expand Down Expand Up @@ -506,7 +506,7 @@ func (r *ReconcileArgoCD) reconcileGrafanaConfiguration(cr *argoprojv1a1.ArgoCD)
return nil // ConfigMap found, do nothing
}

secret := argoutil.NewSecretWithSuffix(cr.ObjectMeta, "grafana")
secret := argoutil.NewSecretWithSuffix(cr, "grafana")
secret, err := argoutil.FetchSecret(r.Client, cr.ObjectMeta, secret.Name)
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions controllers/argocd/configmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func TestReconcileArgoCD_reconcileArgoConfigMap(t *testing.T) {
assert.NilError(t, err)

want := map[string]string{
"application.instanceLabelKey": "mycompany.com/appname",
"application.instanceLabelKey": common.ArgoCDDefaultApplicationInstanceLabelKey,
"admin.enabled": "true",
"configManagementPlugins": "",
"dex.config": "",
Expand Down Expand Up @@ -224,7 +224,7 @@ func TestReconcileArgoCD_reconcileArgoConfigMap_withDexConnector(t *testing.T) {
}},
}

secret := argoutil.NewSecretWithName(metav1.ObjectMeta{Name: "token", Namespace: "argocd"}, "token")
secret := argoutil.NewSecretWithName(a, "token")
r := makeTestReconciler(t, a, sa, secret)
err := r.reconcileArgoConfigMap(a)
assert.NilError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion controllers/argocd/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ func newDeployment(cr *argoprojv1a1.ArgoCD) *appsv1.Deployment {
ObjectMeta: metav1.ObjectMeta{
Name: cr.Name,
Namespace: cr.Namespace,
Labels: labelsForCluster(cr),
Labels: argoutil.LabelsForCluster(cr),
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion controllers/argocd/hpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func newHorizontalPodAutoscaler(cr *argoprojv1a1.ArgoCD) *autoscaling.Horizontal
ObjectMeta: metav1.ObjectMeta{
Name: cr.Name,
Namespace: cr.Namespace,
Labels: labelsForCluster(cr),
Labels: argoutil.LabelsForCluster(cr),
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion controllers/argocd/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func newIngress(cr *argoprojv1a1.ArgoCD) *extv1beta1.Ingress {
ObjectMeta: metav1.ObjectMeta{
Name: cr.Name,
Namespace: cr.Namespace,
Labels: labelsForCluster(cr),
Labels: argoutil.LabelsForCluster(cr),
},
}
}
Expand Down
4 changes: 2 additions & 2 deletions controllers/argocd/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func newPrometheus(cr *argoprojv1a1.ArgoCD) *monitoringv1.Prometheus {
ObjectMeta: metav1.ObjectMeta{
Name: cr.Name,
Namespace: cr.Namespace,
Labels: labelsForCluster(cr),
Labels: argoutil.LabelsForCluster(cr),
},
}
}
Expand All @@ -100,7 +100,7 @@ func newServiceMonitor(cr *argoprojv1a1.ArgoCD) *monitoringv1.ServiceMonitor {
ObjectMeta: metav1.ObjectMeta{
Name: cr.Name,
Namespace: cr.Namespace,
Labels: labelsForCluster(cr),
Labels: argoutil.LabelsForCluster(cr),
},
}
}
Expand Down
7 changes: 4 additions & 3 deletions controllers/argocd/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

argoprojv1a1 "github.com/argoproj-labs/argocd-operator/api/v1alpha1"
"github.com/argoproj-labs/argocd-operator/common"
"github.com/argoproj-labs/argocd-operator/controllers/argoutil"
)

const (
Expand All @@ -30,7 +31,7 @@ func newRole(name string, rules []v1.PolicyRule, cr *argoprojv1a1.ArgoCD) *v1.Ro
ObjectMeta: metav1.ObjectMeta{
Name: generateResourceName(name, cr),
Namespace: cr.Namespace,
Labels: labelsForCluster(cr),
Labels: argoutil.LabelsForCluster(cr),
},
Rules: rules,
}
Expand All @@ -49,8 +50,8 @@ func newClusterRole(name string, rules []v1.PolicyRule, cr *argoprojv1a1.ArgoCD)
return &v1.ClusterRole{
ObjectMeta: metav1.ObjectMeta{
Name: GenerateUniqueResourceName(name, cr),
Labels: labelsForCluster(cr),
Annotations: annotationsForCluster(cr),
Labels: argoutil.LabelsForCluster(cr),
Annotations: argoutil.AnnotationsForCluster(cr),
},
Rules: rules,
}
Expand Down
9 changes: 5 additions & 4 deletions controllers/argocd/rolebinding.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ import (

argoprojv1a1 "github.com/argoproj-labs/argocd-operator/api/v1alpha1"
"github.com/argoproj-labs/argocd-operator/common"
"github.com/argoproj-labs/argocd-operator/controllers/argoutil"
)

// newClusterRoleBinding returns a new ClusterRoleBinding instance.
func newClusterRoleBinding(name string, cr *argoprojv1a1.ArgoCD) *v1.ClusterRoleBinding {
return &v1.ClusterRoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: cr.Name,
Labels: labelsForCluster(cr),
Annotations: annotationsForCluster(cr),
Labels: argoutil.LabelsForCluster(cr),
Annotations: argoutil.AnnotationsForCluster(cr),
},
}
}
Expand All @@ -45,8 +46,8 @@ func newRoleBinding(cr *argoprojv1a1.ArgoCD) *v1.RoleBinding {
return &v1.RoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: cr.Name,
Labels: labelsForCluster(cr),
Annotations: annotationsForCluster(cr),
Labels: argoutil.LabelsForCluster(cr),
Annotations: argoutil.AnnotationsForCluster(cr),
Namespace: cr.Namespace,
},
}
Expand Down
2 changes: 1 addition & 1 deletion controllers/argocd/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func newRoute(cr *argoprojv1a1.ArgoCD) *routev1.Route {
ObjectMeta: metav1.ObjectMeta{
Name: cr.Name,
Namespace: cr.Namespace,
Labels: labelsForCluster(cr),
Labels: argoutil.LabelsForCluster(cr),
},
}
}
Expand Down
24 changes: 12 additions & 12 deletions controllers/argocd/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func nowNano() string {

// newCASecret creates a new CA secret with the given suffix for the given ArgoCD.
func newCASecret(cr *argoprojv1a1.ArgoCD) (*corev1.Secret, error) {
secret := argoutil.NewTLSSecret(cr.ObjectMeta, "ca")
secret := argoutil.NewTLSSecret(cr, "ca")

key, err := argoutil.NewPrivateKey()
if err != nil {
Expand All @@ -109,7 +109,7 @@ func newCASecret(cr *argoprojv1a1.ArgoCD) (*corev1.Secret, error) {

// newCertificateSecret creates a new secret using the given name suffix for the given TLS certificate.
func newCertificateSecret(suffix string, caCert *x509.Certificate, caKey *rsa.PrivateKey, cr *argoprojv1a1.ArgoCD) (*corev1.Secret, error) {
secret := argoutil.NewTLSSecret(cr.ObjectMeta, suffix)
secret := argoutil.NewTLSSecret(cr, suffix)

key, err := argoutil.NewPrivateKey()
if err != nil {
Expand Down Expand Up @@ -151,15 +151,15 @@ func newCertificateSecret(suffix string, caCert *x509.Certificate, caKey *rsa.Pr

// reconcileArgoSecret will ensure that the Argo CD Secret is present.
func (r *ReconcileArgoCD) reconcileArgoSecret(cr *argoprojv1a1.ArgoCD) error {
clusterSecret := argoutil.NewSecretWithSuffix(cr.ObjectMeta, "cluster")
secret := argoutil.NewSecretWithName(cr.ObjectMeta, common.ArgoCDSecretName)
clusterSecret := argoutil.NewSecretWithSuffix(cr, "cluster")
secret := argoutil.NewSecretWithName(cr, common.ArgoCDSecretName)

if !argoutil.IsObjectFound(r.Client, cr.Namespace, clusterSecret.Name, clusterSecret) {
log.Info(fmt.Sprintf("cluster secret [%s] not found, waiting to reconcile argo secret [%s]", clusterSecret.Name, secret.Name))
return nil
}

tlsSecret := argoutil.NewSecretWithSuffix(cr.ObjectMeta, "tls")
tlsSecret := argoutil.NewSecretWithSuffix(cr, "tls")
if !argoutil.IsObjectFound(r.Client, cr.Namespace, tlsSecret.Name, tlsSecret) {
log.Info(fmt.Sprintf("tls secret [%s] not found, waiting to reconcile argo secret [%s]", tlsSecret.Name, secret.Name))
return nil
Expand Down Expand Up @@ -196,7 +196,7 @@ func (r *ReconcileArgoCD) reconcileArgoSecret(cr *argoprojv1a1.ArgoCD) error {

// reconcileClusterMainSecret will ensure that the main Secret is present for the Argo CD cluster.
func (r *ReconcileArgoCD) reconcileClusterMainSecret(cr *argoprojv1a1.ArgoCD) error {
secret := argoutil.NewSecretWithSuffix(cr.ObjectMeta, "cluster")
secret := argoutil.NewSecretWithSuffix(cr, "cluster")
if argoutil.IsObjectFound(r.Client, cr.Namespace, secret.Name, secret) {
return nil // Secret found, do nothing
}
Expand All @@ -218,12 +218,12 @@ func (r *ReconcileArgoCD) reconcileClusterMainSecret(cr *argoprojv1a1.ArgoCD) er

// reconcileClusterTLSSecret ensures the TLS Secret is created for the ArgoCD cluster.
func (r *ReconcileArgoCD) reconcileClusterTLSSecret(cr *argoprojv1a1.ArgoCD) error {
secret := argoutil.NewTLSSecret(cr.ObjectMeta, "tls")
secret := argoutil.NewTLSSecret(cr, "tls")
if argoutil.IsObjectFound(r.Client, cr.Namespace, secret.Name, secret) {
return nil // Secret found, do nothing
}

caSecret := argoutil.NewSecretWithSuffix(cr.ObjectMeta, "ca")
caSecret := argoutil.NewSecretWithSuffix(cr, "ca")
caSecret, err := argoutil.FetchSecret(r.Client, cr.ObjectMeta, caSecret.Name)
if err != nil {
return err
Expand Down Expand Up @@ -253,7 +253,7 @@ func (r *ReconcileArgoCD) reconcileClusterTLSSecret(cr *argoprojv1a1.ArgoCD) err

// reconcileClusterCASecret ensures the CA Secret is created for the ArgoCD cluster.
func (r *ReconcileArgoCD) reconcileClusterCASecret(cr *argoprojv1a1.ArgoCD) error {
secret := argoutil.NewSecretWithSuffix(cr.ObjectMeta, "ca")
secret := argoutil.NewSecretWithSuffix(cr, "ca")
if argoutil.IsObjectFound(r.Client, cr.Namespace, secret.Name, secret) {
return nil // Secret found, do nothing
}
Expand Down Expand Up @@ -335,8 +335,8 @@ func (r *ReconcileArgoCD) reconcileGrafanaSecret(cr *argoprojv1a1.ArgoCD) error
return nil // Grafana not enabled, do nothing.
}

clusterSecret := argoutil.NewSecretWithSuffix(cr.ObjectMeta, "cluster")
secret := argoutil.NewSecretWithSuffix(cr.ObjectMeta, "grafana")
clusterSecret := argoutil.NewSecretWithSuffix(cr, "cluster")
secret := argoutil.NewSecretWithSuffix(cr, "grafana")

if !argoutil.IsObjectFound(r.Client, cr.Namespace, clusterSecret.Name, clusterSecret) {
log.Info(fmt.Sprintf("cluster secret [%s] not found, waiting to reconcile grafana secret [%s]", clusterSecret.Name, secret.Name))
Expand Down Expand Up @@ -394,7 +394,7 @@ func (r *ReconcileArgoCD) reconcileGrafanaSecret(cr *argoprojv1a1.ArgoCD) error
// reconcileClusterPermissionsSecret ensures ArgoCD instance is namespace-scoped
func (r *ReconcileArgoCD) reconcileClusterPermissionsSecret(cr *argoprojv1a1.ArgoCD) error {
var clusterConfigInstance bool
secret := argoutil.NewSecretWithSuffix(cr.ObjectMeta, "default-cluster-config")
secret := argoutil.NewSecretWithSuffix(cr, "default-cluster-config")
secret.Labels[common.ArgoCDSecretTypeLabel] = "cluster"
dataBytes, _ := json.Marshal(map[string]interface{}{
"tlsClientConfig": map[string]interface{}{
Expand Down
2 changes: 1 addition & 1 deletion controllers/argocd/secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func Test_ReconcileArgoCD_ClusterPermissionsSecret(t *testing.T) {
r := makeTestReconciler(t, a)
assert.NilError(t, createNamespace(r, a.Namespace, a.Namespace))

testSecret := argoutil.NewSecretWithSuffix(a.ObjectMeta, "default-cluster-config")
testSecret := argoutil.NewSecretWithSuffix(a, "default-cluster-config")
assert.ErrorContains(t, r.Client.Get(context.TODO(), types.NamespacedName{Name: testSecret.Name, Namespace: testSecret.Namespace}, testSecret), "not found")

assert.NilError(t, r.reconcileClusterPermissionsSecret(a))
Expand Down
2 changes: 1 addition & 1 deletion controllers/argocd/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func newService(cr *argoprojv1a1.ArgoCD) *corev1.Service {
ObjectMeta: metav1.ObjectMeta{
Name: cr.Name,
Namespace: cr.Namespace,
Labels: labelsForCluster(cr),
Labels: argoutil.LabelsForCluster(cr),
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion controllers/argocd/service_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func newServiceAccount(cr *argoprojv1a1.ArgoCD) *corev1.ServiceAccount {
ObjectMeta: metav1.ObjectMeta{
Name: cr.Name,
Namespace: cr.Namespace,
Labels: labelsForCluster(cr),
Labels: argoutil.LabelsForCluster(cr),
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion controllers/argocd/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func newStatefulSet(cr *argoprojv1a1.ArgoCD) *appsv1.StatefulSet {
ObjectMeta: metav1.ObjectMeta{
Name: cr.Name,
Namespace: cr.Namespace,
Labels: labelsForCluster(cr),
Labels: argoutil.LabelsForCluster(cr),
},
}
}
Expand Down
22 changes: 2 additions & 20 deletions controllers/argocd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ func (r *ReconcileArgoCD) getDexOAuthClientSecret(cr *argoprojv1a1.ArgoCD) (*str
}

// Fetch the secret to obtain the token
secret := argoutil.NewSecretWithName(cr.ObjectMeta, tokenSecret.Name)
secret := argoutil.NewSecretWithName(cr, tokenSecret.Name)
if err := argoutil.FetchObject(r.Client, cr.Namespace, secret.Name, secret); err != nil {
return nil, err
}
Expand Down Expand Up @@ -846,24 +846,6 @@ func removeString(slice []string, s string) []string {
return result
}

// labelsForCluster returns the labels for all cluster resources.
func labelsForCluster(cr *argoprojv1a1.ArgoCD) map[string]string {
labels := argoutil.DefaultLabels(cr.Name)
for key, val := range cr.ObjectMeta.Labels {
labels[key] = val
}
return labels
}

// annotationsForCluster returns the annotations for all cluster resources.
func annotationsForCluster(cr *argoprojv1a1.ArgoCD) map[string]string {
annotations := argoutil.DefaultAnnotations(cr)
for key, val := range cr.ObjectMeta.Annotations {
annotations[key] = val
}
return annotations
}

// setResourceWatches will register Watches for each of the supported Resources.
func setResourceWatches(bldr *builder.Builder, clusterResourceMapper, tlsSecretMapper, namespaceResourceMapper handler.MapFunc) *builder.Builder {

Expand Down Expand Up @@ -993,7 +975,7 @@ func setResourceWatches(bldr *builder.Builder, clusterResourceMapper, tlsSecretM

// withClusterLabels will add the given labels to the labels for the cluster and return the result.
func withClusterLabels(cr *argoprojv1a1.ArgoCD, addLabels map[string]string) map[string]string {
labels := labelsForCluster(cr)
labels := argoutil.LabelsForCluster(cr)
for key, val := range addLabels {
labels[key] = val
}
Expand Down
2 changes: 1 addition & 1 deletion controllers/argocd/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ func TestDeleteRBACsForNamespace(t *testing.T) {
_, err = testClient.RbacV1().RoleBindings(testNameSpace).Create(context.TODO(), roleBinding2, metav1.CreateOptions{})
assert.NilError(t, err)

secret := argoutil.NewSecretWithSuffix(a.ObjectMeta, "xyz")
secret := argoutil.NewSecretWithSuffix(a, "xyz")
secret.Labels = map[string]string{common.ArgoCDSecretTypeLabel: "cluster"}
secret.Data = map[string][]byte{
"server": []byte(common.ArgoCDDefaultServer),
Expand Down
Loading

0 comments on commit 516e937

Please sign in to comment.