Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Stop reconciling if K8sGPT CR is not in the operator namespace #480

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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" \
Expand Down
4 changes: 4 additions & 0 deletions chart/operator/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 5 additions & 0 deletions config/manager/manager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,10 @@ spec:
requests:
cpu: 10m
memory: 64Mi
env:
- name: OPERATOR_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
serviceAccountName: controller-manager
terminationGracePeriodSeconds: 10
13 changes: 12 additions & 1 deletion controllers/k8sgpt_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package controllers
import (
"context"
"fmt"
"os"
"strings"
"time"

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down