diff --git a/Dockerfile b/Dockerfile index 0103c2b9..f40951f1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -27,7 +27,7 @@ RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -a -o manager m # Use distroless as minimal base image to package the manager binary # Refer to https://github.com/GoogleContainerTools/distroless for more details -FROM gcr.io/distroless/static:nonroot as production +FROM gcr.io/distroless/static:nonroot AS production LABEL org.opencontainers.image.source="https://github.com/k8sgpt-ai/k8sgpt-operator" \ org.opencontainers.image.url="https://k8sgpt.ai" \ diff --git a/chart/operator/templates/deployment.yaml b/chart/operator/templates/deployment.yaml index adf957c5..0642e684 100644 --- a/chart/operator/templates/deployment.yaml +++ b/chart/operator/templates/deployment.yaml @@ -79,6 +79,10 @@ spec: value: {{ quote .Values.kubernetesClusterDomain }} - name: OPERATOR_SINK_WEBHOOK_TIMEOUT_SECONDS value: {{ quote .Values.controllerManager.manager.sinkWebhookTimeout }} + - name: OPERATOR_NAMESPACE + valueFrom: + fieldRef: + fieldPath: metadata.namespace image: {{ .Values.controllerManager.manager.image.repository }}:{{ .Values.controllerManager.manager.image.tag | default .Chart.AppVersion }} livenessProbe: diff --git a/config/manager/manager.yaml b/config/manager/manager.yaml index dceaf31f..4de9438a 100644 --- a/config/manager/manager.yaml +++ b/config/manager/manager.yaml @@ -98,5 +98,10 @@ spec: requests: cpu: 10m memory: 64Mi + env: + - name: OPERATOR_NAMESPACE + valueFrom: + fieldRef: + fieldPath: metadata.namespace serviceAccountName: controller-manager terminationGracePeriodSeconds: 10 diff --git a/controllers/k8sgpt_controller.go b/controllers/k8sgpt_controller.go index ae1eef2e..7158c9f3 100644 --- a/controllers/k8sgpt_controller.go +++ b/controllers/k8sgpt_controller.go @@ -17,6 +17,7 @@ package controllers import ( "context" "fmt" + "os" "strings" "time" @@ -108,7 +109,7 @@ func (r *K8sGPTReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr return ctrl.Result{}, client.IgnoreNotFound(err) } - // Add a finaliser if there isn't one + // Add a finalizer if there isn't one if k8sgptConfig.ObjectMeta.DeletionTimestamp.IsZero() { // The object is not being deleted, so if it does not have our finalizer, // then lets add the finalizer and update the object. This is equivalent @@ -146,6 +147,16 @@ func (r *K8sGPTReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr return r.finishReconcile(nil, false) } + operatorNamespace := os.Getenv("OPERATOR_NAMESPACE") + if k8sgptConfig.Namespace != operatorNamespace { + fmt.Printf("K8sGPT CR (%s) is not in the operator namespace (%s)\n", + k8sgptConfig.Name, operatorNamespace) + k8sgptReconcileErrorCount.With(prometheus.Labels{ + "k8sgpt": k8sgptConfig.Name, + }).Inc() + return ctrl.Result{}, nil + } + if k8sgptConfig.Spec.AI.BackOff == nil { k8sgptConfig.Spec.AI.BackOff = &corev1alpha1.BackOff{ Enabled: true,