Skip to content

Commit

Permalink
feat(deploy): support single namespace
Browse files Browse the repository at this point in the history
Signed-off-by: Rory Z <[email protected]>
  • Loading branch information
Rory-Z committed Dec 23, 2024
1 parent 01b870d commit 87c6a14
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ jobs:

- [EMQX, emqx, "config/samples/emqx/v2beta1/emqx-slim.yaml"]
- [EMQX, emqx, "config/samples/emqx/v2beta1/emqx-full.yaml"]
single_namespace:
- false
include:
- install: helm
single_namespace: true

steps:
- run: minikube start
Expand Down Expand Up @@ -63,6 +68,7 @@ jobs:
helm install emqx-operator deploy/charts/emqx-operator \
--set image.tag=${{ github.sha }} \
--set development=true \
--set singleNamespace=${{ matrix.single_namespace }} \
--namespace emqx-operator-system \
--create-namespace
- name: Check operator
Expand Down
1 change: 1 addition & 0 deletions deploy/charts/emqx-operator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ The following table lists the configurable parameters of the cert-manager chart
| Parameter | Description | Default |
| --------- | ----------- | ------- |
| `skipCRDs` | If `true`, skips installing CRDs | `false` |
| `singleNamespace` | If true, the operator will watch only the namespace where it is deployed. If false, the operator will watch all namespaces. | `false` |
| `development` | Development configures the logger to use a Zap development config (stacktraces on warnings, no sampling), otherwise a Zap production config will be used (stacktraces on errors, sampling). | `false` |
| `image.repository` | Image repository | `emqx/emqx-operator-controller` |
| `image.tag` | Image tag | `{{RELEASE_VERSION}}` |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,16 @@ imagePullSecrets:
{{- end }}
---
apiVersion: rbac.authorization.k8s.io/v1
{{ if .Values.singleNamespace }}
kind: RoleBinding
metadata:
name: {{ include "emqx-operator.fullname" . }}-manager-rolebinding
namespace: {{ .Release.Namespace }}
{{- else }}
kind: ClusterRoleBinding
metadata:
name: {{ include "emqx-operator.fullname" . }}-manager-rolebinding
{{- end }}
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
Expand All @@ -29,10 +36,16 @@ subjects:
namespace: {{ .Release.Namespace }}
---
apiVersion: rbac.authorization.k8s.io/v1
{{ if .Values.singleNamespace }}
kind: Role
metadata:
name: {{ include "emqx-operator.fullname" . }}-manager-role
namespace: {{ .Release.Namespace }}
{{- else }}
kind: ClusterRole
metadata:
creationTimestamp: null
name: {{ include "emqx-operator.fullname" . }}-manager-role
{{- end }}
rules:
- apiGroups:
- ""
Expand Down
7 changes: 7 additions & 0 deletions deploy/charts/emqx-operator/templates/controller-manager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ spec:
- containerPort: 9443
name: webhook-server
protocol: TCP
{{- if .Values.singleNamespace }}
env:
- name: WATCH_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
{{- end }}
readinessProbe:
httpGet:
path: /readyz
Expand Down
3 changes: 3 additions & 0 deletions deploy/charts/emqx-operator/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

skipCRDs: false

## If true, the operator will watch only the namespace where it is deployed. If false, the operator will watch all namespaces.
singleNamespace: false

# Development configures the logger to use a Zap development config
# (stacktraces on warnings, no sampling), otherwise a Zap production
# config will be used (stacktraces on errors, sampling).
Expand Down
20 changes: 20 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ import (
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/utils/ptr"

"sigs.k8s.io/controller-runtime/pkg/cache"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
Expand Down Expand Up @@ -102,6 +105,9 @@ func main() {
LeaderElectionID: "19fd6fcc.emqx.io",
LeaseDuration: ptr.To(time.Second * 30),
RenewDeadline: ptr.To(time.Second * 20),
Cache: cache.Options{
DefaultNamespaces: map[string]cache.Config{getWatchNamespace(): {}},
},
})
if err != nil {
setupLog.Error(err, "unable to start manager")
Expand Down Expand Up @@ -173,3 +179,17 @@ func main() {
os.Exit(1)
}
}

// getWatchNamespace returns the Namespace the operator should be watching for changes
func getWatchNamespace() string {
// WatchNamespaceEnvVar is the constant for env variable WATCH_NAMESPACE
// which specifies the Namespace to watch.
// An empty value means the operator is running with cluster scope.
var watchNamespaceEnvVar = "WATCH_NAMESPACE"

ns, found := os.LookupEnv(watchNamespaceEnvVar)
if !found {
return metav1.NamespaceAll
}
return ns
}

0 comments on commit 87c6a14

Please sign in to comment.