Skip to content

Commit b52523e

Browse files
Merge pull request #607 from raghavi101/label-selector-cli
feat: Add labelSelector option to filter the ArgoCD instances for reconciliation
2 parents 8152452 + 0a2046b commit b52523e

File tree

17 files changed

+188
-5
lines changed

17 files changed

+188
-5
lines changed

common/common.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ limitations under the License.
1616

1717
package common
1818

19+
import "os"
20+
1921
const (
2022
// ArgoCDInstanceName is the default Argo CD instance name
2123
ArgoCDInstanceName = "openshift-gitops"
@@ -39,3 +41,10 @@ func InfraNodeSelector() map[string]string {
3941
"node-role.kubernetes.io/infra": "",
4042
}
4143
}
44+
45+
func StringFromEnv(env string, defaultValue string) string {
46+
if str := os.Getenv(env); str != "" {
47+
return str
48+
}
49+
return defaultValue
50+
}

config/manager/manager.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ spec:
3232
value: openshift-gitops
3333
- name: OPERATOR_NAME
3434
value: gitops-operator
35+
- name : LABEL_SELECTOR
36+
value: ""
3537
image: controller:latest
3638
livenessProbe:
3739
httpGet:

hack/non-olm-install/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ The following environment variables can be set to configure various options for
8686
| **SERVER_CLUSTER_ROLE** |This environment variable enables administrators to configure a common cluster role to use across all of the managed namespaces in the role bindings the operator creates for the Argo CD server. | None |
8787
| **WATCH_NAMESPACE** | namespaces in which Argo applications can be created | None |
8888
| **ENABLE_CONVERSION_WEBHOOK** | This environment variable enables conversion webhook to convert v1alpha1 ArgoCD resources to v1beta1 | true |
89+
| **ARGOCD_LABEL_SELECTOR** | This environment variable will enable the operator to reconcile only the corresponding ArgoCD instances having the matching label selector. The label selector can be set on argocd-opertor by exporting `ARGOCD_LABEL_SELECTOR` (eg: `export ARGOCD_LABEL_SELECTOR=foo=bar`). The labels can be added to the argocd instances using the command `kubectl label argocd test1 foo=bar -n test-argocd`. | None |
8990
### Running the script
9091

9192
#### Usage

hack/non-olm-install/install-gitops-operator.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ CONTROLLER_CLUSTER_ROLE=${CONTROLLER_CLUSTER_ROLE:-""}
4141
DISABLE_DEFAULT_ARGOCD_INSTANCE=${DISABLE_DEFAULT_ARGOCD_INSTANCE:-"false"}
4242
SERVER_CLUSTER_ROLE=${SERVER_CLUSTER_ROLE:-""}
4343
WATCH_NAMESPACE=${WATCH_NAMESPACE:-""}
44+
ARGOCD_LABEL_SELECTOR=${ARGOCD_LABEL_SELECTOR:-""}
4445
ENABLE_CONVERSION_WEBHOOK=${ENABLE_CONVERSION_WEBHOOK:-"true"}
4546

4647
# Print help message
@@ -232,6 +233,8 @@ spec:
232233
value: \"${SERVER_CLUSTER_ROLE}\"
233234
- name: WATCH_NAMESPACE
234235
value: \"${WATCH_NAMESPACE}\"
236+
- name: ARGOCD_LABEL_SELECTOR
237+
value: \"${ARGOCD_LABEL_SELECTOR}\"
235238
- name: ENABLE_CONVERSION_WEBHOOK
236239
value: \"${ENABLE_CONVERSION_WEBHOOK}\"" > ${WORK_DIR}/env-overrides.yaml
237240
}
@@ -449,6 +452,9 @@ function print_info() {
449452
if [ ! -z "${WATCH_NAMESPACE}" ]; then
450453
echo "WATCH_NAMESPACE: ${WATCH_NAMESPACE}"
451454
fi
455+
if [ ! -z "${ARGOCD_LABEL_SELECTOR}" ]; then
456+
echo "ARGOCD_LABEL_SELECTOR: ${ARGOCD_LABEL_SELECTOR}"
457+
fi
452458
if [ ! -z "${ENABLE_CONVERSION_WEBHOOK}" ]; then
453459
echo "ENABLE_CONVERSION_WEBHOOK: ${ENABLE_CONVERSION_WEBHOOK}"
454460
fi

main.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,11 @@ import (
2929
"go.uber.org/zap/zapcore"
3030
_ "k8s.io/client-go/plugin/pkg/client/auth"
3131

32-
"k8s.io/apimachinery/pkg/runtime"
33-
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
34-
3532
rolloutManagerApi "github.com/argoproj-labs/argo-rollouts-manager/api/v1alpha1"
3633
rolloutManagerProvisioner "github.com/argoproj-labs/argo-rollouts-manager/controllers"
3734
argov1alpha1api "github.com/argoproj-labs/argocd-operator/api/v1alpha1"
3835
argov1beta1api "github.com/argoproj-labs/argocd-operator/api/v1beta1"
36+
argocdcommon "github.com/argoproj-labs/argocd-operator/common"
3937
argocdprovisioner "github.com/argoproj-labs/argocd-operator/controllers/argocd"
4038
monitoringv1 "github.com/coreos/prometheus-operator/pkg/apis/monitoring/v1"
4139
appsv1 "github.com/openshift/api/apps/v1"
@@ -46,6 +44,9 @@ import (
4644
templatev1 "github.com/openshift/api/template/v1"
4745
operatorsv1 "github.com/operator-framework/api/pkg/operators/v1"
4846
operatorsv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
47+
"k8s.io/apimachinery/pkg/labels"
48+
"k8s.io/apimachinery/pkg/runtime"
49+
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
4950
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
5051
ctrl "sigs.k8s.io/controller-runtime"
5152
"sigs.k8s.io/controller-runtime/pkg/healthz"
@@ -84,8 +85,10 @@ func main() {
8485

8586
var enableHTTP2 = false
8687

88+
var labelSelectorFlag string
8789
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
8890
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
91+
flag.StringVar(&labelSelectorFlag, "label-selector", common.StringFromEnv(argocdcommon.ArgoCDLabelSelectorKey, argocdcommon.ArgoCDDefaultLabelSelector), "The label selector is used to map to a subset of ArgoCD instances to reconcile")
8992
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
9093
"Enable leader election for controller manager. "+
9194
"Enabling this will ensure there is only one active controller manager.")
@@ -179,10 +182,17 @@ func main() {
179182
setupLog.Error(err, "unable to create controller", "controller", "Argo CD metrics")
180183
os.Exit(1)
181184
}
185+
// Check the label selector format eg. "foo=bar"
186+
if _, err := labels.Parse(labelSelectorFlag); err != nil {
187+
setupLog.Error(err, "error parsing the labelSelector '%s'.", labelSelectorFlag)
188+
os.Exit(1)
189+
}
190+
setupLog.Info(fmt.Sprintf("Watching label-selector \"%s\"", labelSelectorFlag))
182191

183192
if err = (&argocdprovisioner.ReconcileArgoCD{
184-
Client: mgr.GetClient(),
185-
Scheme: mgr.GetScheme(),
193+
Client: mgr.GetClient(),
194+
Scheme: mgr.GetScheme(),
195+
LabelSelector: labelSelectorFlag,
186196
}).SetupWithManager(mgr); err != nil {
187197
setupLog.Error(err, "unable to create controller", "controller", "Argo CD")
188198
os.Exit(1)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
apiVersion: kuttl.dev/v1beta1
2+
kind: TestStep
3+
commands:
4+
# patches the subscription to add label selector in argocd-operator
5+
6+
# - script: |
7+
# oc set env deployment openshift-gitops-operator-controller-manager -n openshift-gitops-operator ARGOCD_LABEL_SELECTOR=foo=bar -c manager
8+
# - script: sleep 30
9+
- script: |
10+
if ! [ -z $NON_OLM ]; then
11+
oc set env deployment openshift-gitops-operator-controller-manager -n openshift-gitops-operator ARGOCD_LABEL_SELECTOR=foo=bar -c manager
12+
13+
elif [ -z $CI ]; then
14+
15+
oc patch -n openshift-gitops-operator subscription openshift-gitops-operator \
16+
--type merge --patch '{"spec": {"config": {"env": [{"name": "ARGOCD_LABEL_SELECTOR", "value": "foo=bar"}]}}}'
17+
18+
else
19+
20+
oc patch -n openshift-gitops-operator subscription `subscription=gitops-operator- && oc get subscription --all-namespaces | grep $subscription | head -1 | awk '{print $2}'` \
21+
--type merge --patch '{"spec": {"config": {"env": [{"name": "ARGOCD_LABEL_SELECTOR", "value": "foo=bar"}]}}}'
22+
fi
23+
- script: sleep 120
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
apiVersion: argoproj.io/v1beta1
2+
kind: ArgoCD
3+
metadata:
4+
name: test1
5+
namespace: test-argocd
6+
status:
7+
phase: Available
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
apiVersion: argoproj.io/v1beta1
2+
kind: ArgoCD
3+
metadata:
4+
name: test1
5+
namespace: test-argocd
6+
labels:
7+
example: basic
8+
spec: {}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
apiVersion: kuttl.dev/v1beta1
2+
kind: TestStep
3+
commands:
4+
# patches the argocd instance with the label
5+
- script: |
6+
kubectl label argocd test1 foo=bar -n test-argocd
7+
8+
- script: sleep 30
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
apiVersion: argoproj.io/v1beta1
3+
kind: ArgoCD
4+
metadata:
5+
name: test1
6+
namespace: test-argocd
7+
status:
8+
phase: Available
9+
---

0 commit comments

Comments
 (0)