Skip to content

Commit

Permalink
feat: Add option to customize local cluster name
Browse files Browse the repository at this point in the history
Signed-off-by: Kevin Huber <[email protected]>
  • Loading branch information
ItsKev authored Oct 4, 2024
1 parent 6e304b2 commit ecfc3df
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 1 deletion.
18 changes: 18 additions & 0 deletions api/v1beta1/argocd_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,20 @@ type ArgoCDNodePlacementSpec struct {
Tolerations []corev1.Toleration `json:"tolerations,omitempty"`
}

// ArgoCDLocalClusterSpec defines the configuration for the local cluster in Argo CD
type ArgoCDLocalClusterSpec struct {

// Name represents the name of the local cluster
Name string `json:"name,omitempty"`
}

// ArgoCDClustersSpec defines options related to cluster management in Argo CD
type ArgoCDClustersSpec struct {

// Local provides the configuration for the local cluster
Local ArgoCDLocalClusterSpec `json:"local,omitempty"`
}

// ArgoCDSpec defines the desired state of ArgoCD
// +k8s:openapi-gen=true
type ArgoCDSpec struct {
Expand All @@ -754,6 +768,10 @@ type ArgoCDSpec struct {
//+operator-sdk:csv:customresourcedefinitions:type=spec,displayName="Application Instance Label Key'",xDescriptors={"urn:alm:descriptor:com.tectonic.ui:text","urn:alm:descriptor:com.tectonic.ui:advanced"}
ApplicationInstanceLabelKey string `json:"applicationInstanceLabelKey,omitempty"`

// Clusters defines specific cluster options for managing Argo CD clusters.
// +operator-sdk:csv:customresourcedefinitions:type=spec,displayName="Clusters",xDescriptors={"urn:alm:descriptor:com.tectonic.ui:text","urn:alm:descriptor:com.tectonic.ui:advanced"}
Clusters ArgoCDClustersSpec `json:"clusters,omitempty"`

// ConfigManagementPlugins is used to specify additional config management plugins.
//+operator-sdk:csv:customresourcedefinitions:type=spec,displayName="Config Management Plugins'",xDescriptors={"urn:alm:descriptor:com.tectonic.ui:text","urn:alm:descriptor:com.tectonic.ui:advanced"}
ConfigManagementPlugins string `json:"configManagementPlugins,omitempty"`
Expand Down
32 changes: 32 additions & 0 deletions api/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions config/crd/bases/argoproj.io_argocds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7658,6 +7658,18 @@ spec:
required:
- content
type: object
clusters:
description: Clusters defines specific cluster options for managing
Argo CD clusters.
properties:
local:
description: Local provides the configuration for the local cluster
properties:
name:
description: Name represents the name of the local cluster
type: string
type: object
type: object
configManagementPlugins:
description: ConfigManagementPlugins is used to specify additional
config management plugins.
Expand Down
10 changes: 9 additions & 1 deletion controllers/argocd/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,9 +406,14 @@ func (r *ReconcileArgoCD) reconcileClusterPermissionsSecret(cr *argoproj.ArgoCD)
}
sort.Strings(namespaces)

localClusteName := "in-cluster"
if cr.Spec.Clusters.Local.Name != "" {
localClusteName = cr.Spec.Clusters.Local.Name
}

secret.Data = map[string][]byte{
"config": dataBytes,
"name": []byte("in-cluster"),
"name": []byte(localClusteName),
"server": []byte(common.ArgoCDDefaultServer),
"namespaces": []byte(strings.Join(namespaces, ",")),
}
Expand Down Expand Up @@ -440,6 +445,9 @@ func (r *ReconcileArgoCD) reconcileClusterPermissionsSecret(cr *argoproj.ArgoCD)
sort.Strings(ns)
s.Data["namespaces"] = []byte(strings.Join(ns, ","))
}
// update Argo CD cluster name
s.Data["name"] = []byte(localClusteName)

return r.Client.Update(context.TODO(), &s)
}
}
Expand Down
14 changes: 14 additions & 0 deletions controllers/argocd/secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -550,4 +550,18 @@ func Test_ReconcileArgoCD_ClusterPermissionsSecret(t *testing.T) {
//TODO: https://github.com/stretchr/testify/pull/1022 introduced ErrorContains, but is not yet available in a tagged release. Revert to ErrorContains once this becomes available
assert.NoError(t, r.Client.Get(context.TODO(), types.NamespacedName{Name: testSecret.Name, Namespace: testSecret.Namespace}, testSecret))
assert.Nil(t, r.Client.Get(context.TODO(), types.NamespacedName{Name: testSecret.Name, Namespace: testSecret.Namespace}, testSecret))

customClusterName := "custom-cluster-name"
a.Spec.Clusters.Local.Name = customClusterName
assert.NoError(t, r.reconcileClusterPermissionsSecret(a))
assert.NoError(t, r.Client.Get(context.TODO(), types.NamespacedName{
Name: testSecret.Name, Namespace: testSecret.Namespace}, testSecret))
assert.Equal(t, customClusterName, string(testSecret.Data["name"]))

// Reset InClusterName and test for default value
a.Spec.Clusters.Local.Name = ""
assert.NoError(t, r.reconcileClusterPermissionsSecret(a))
assert.NoError(t, r.Client.Get(context.TODO(), types.NamespacedName{
Name: testSecret.Name, Namespace: testSecret.Namespace}, testSecret))
assert.Equal(t, "in-cluster", string(testSecret.Data["name"]))
}
20 changes: 20 additions & 0 deletions docs/reference/argocd.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Name | Default | Description
--- | --- | ---
[**ApplicationInstanceLabelKey**](#application-instance-label-key) | `mycompany.com/appname` | The metadata.label key name where Argo CD injects the app name as a tracking label.
[**ApplicationSet**](#applicationset-controller-options) | [Object] | ApplicationSet controller configuration options.
[**Clusters**](#clusters) | `clusters` | Defines specific cluster options for managing Argo CD clusters.
[**ConfigManagementPlugins**](#config-management-plugins) | [Empty] | Configuration to add a config management plugin.
[**Controller**](#controller-options) | [Object] | Argo CD Application Controller options.
[**DisableAdmin**](#disable-admin) | `false` | Disable the admin user.
Expand Down Expand Up @@ -156,6 +157,25 @@ data:
-----END CERTIFICATE-----
```

## Clusters

Defines options related to cluster management in Argo CD. Currently, it only allows customization of the local cluster's name.

### Local Cluster Name Example

The following example sets the cluster name to `example-cluster` in the `<argocd-name>-default-cluster-config` secret.

``` yaml
apiVersion: argoproj.io/v1beta1
kind: ArgoCD
metadata:
name: argocd
spec:
clusters:
local:
name: example-cluster
```

## Config Management Plugins

Configuration to add a config management plugin. This property maps directly to the `configManagementPlugins` field in the `argocd-cm` ConfigMap.
Expand Down
17 changes: 17 additions & 0 deletions tests/k8s/1-044_validate_cluster_name/01-assert.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
apiVersion: kuttl.dev/v1beta1
kind: TestAssert
timeout: 120
---
apiVersion: argoproj.io/v1beta1
kind: ArgoCD
metadata:
name: argocd
status:
phase: Available
---
apiVersion: v1
kind: Secret
metadata:
name: argocd-default-cluster-config
data:
name: dGVzdC1jbHVzdGVy # test-cluster
8 changes: 8 additions & 0 deletions tests/k8s/1-044_validate_cluster_name/01-install.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: argoproj.io/v1beta1
kind: ArgoCD
metadata:
name: argocd
spec:
clusters:
local:
name: test-cluster
17 changes: 17 additions & 0 deletions tests/k8s/1-044_validate_cluster_name/02-assert.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
apiVersion: kuttl.dev/v1beta1
kind: TestAssert
timeout: 120
---
apiVersion: argoproj.io/v1beta1
kind: ArgoCD
metadata:
name: argocd
status:
phase: Available
---
apiVersion: v1
kind: Secret
metadata:
name: argocd-default-cluster-config
data:
name: YW5vdGhlci10ZXN0LWNsdXN0ZXI= # another-test-cluster
8 changes: 8 additions & 0 deletions tests/k8s/1-044_validate_cluster_name/02-install.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: argoproj.io/v1beta1
kind: ArgoCD
metadata:
name: argocd
spec:
clusters:
local:
name: another-test-cluster

0 comments on commit ecfc3df

Please sign in to comment.