diff --git a/cmd/manager/main.go b/cmd/manager/main.go index 72f25f6..b9b09be 100644 --- a/cmd/manager/main.go +++ b/cmd/manager/main.go @@ -8,6 +8,8 @@ import ( "os" "runtime" + "github.com/ouzi-dev/credstash-operator/pkg/env" + "github.com/ouzi-dev/credstash-operator/pkg/flags" "sigs.k8s.io/controller-runtime/pkg/healthz" @@ -96,9 +98,17 @@ func main() { os.Exit(1) } + operatorName, err := k8sutil.GetOperatorName() + if err != nil { + log.Info("No operator name specified. Defaulting to: credstash-operator") + operatorName = "credstash-operator" + } + + lockName := fmt.Sprintf("%s-%s", operatorName, "lock") + ctx := context.TODO() // Become the leader before proceeding - err = leader.Become(ctx, "credstash-operator-lock") + err = leader.Become(ctx, lockName) if err != nil { log.Error(err, "") os.Exit(1) @@ -148,7 +158,12 @@ func main() { } // Add the Metrics Service - addMetrics(ctx, cfg, namespace) + serviceMonitorNamespace, err := env.GetServiceMonitorNamespace() + if err != nil { + log.Error(err, "Error getting service Monitor namespace") + } else { + addMetrics(ctx, cfg, serviceMonitorNamespace) + } log.Info("Starting the Cmd.") diff --git a/deploy/crds/credstash.ouzi.tech_v1alpha1_credstashsecret_cr.yaml b/deploy/crds/credstash.ouzi.tech_v1alpha1_credstashsecret_cr.yaml index ee6a7c2..8299a11 100644 --- a/deploy/crds/credstash.ouzi.tech_v1alpha1_credstashsecret_cr.yaml +++ b/deploy/crds/credstash.ouzi.tech_v1alpha1_credstashsecret_cr.yaml @@ -2,7 +2,7 @@ apiVersion: credstash.ouzi.tech/v1alpha1 kind: CredstashSecret metadata: name: example-credstashsecret - namespace: georgi + namespace: credstash-operator spec: secrets: - key: key1 diff --git a/deploy/helm/credstash-operator/Chart.yaml b/deploy/helm/credstash-operator/Chart.yaml index b31c6cb..7db32cc 100644 --- a/deploy/helm/credstash-operator/Chart.yaml +++ b/deploy/helm/credstash-operator/Chart.yaml @@ -14,8 +14,8 @@ type: application # This is the chart version. This version number should be incremented each time you make changes # to the chart and its templates, including the app version. -version: 0.0.0 +version: v0.0.0 # This is the version number of the application being deployed. This version number should be # incremented each time you make changes to the application. -appVersion: 0.0.0 +appVersion: v0.0.0 diff --git a/deploy/helm/credstash-operator/templates/deployment.yaml b/deploy/helm/credstash-operator/templates/deployment.yaml index e53b629..03263cf 100644 --- a/deploy/helm/credstash-operator/templates/deployment.yaml +++ b/deploy/helm/credstash-operator/templates/deployment.yaml @@ -12,6 +12,7 @@ spec: template: metadata: labels: + name: {{ include "credstash-operator.fullname" . }} {{- include "credstash-operator.selectorLabels" . | nindent 8 }} spec: serviceAccountName: {{ include "credstash-operator.serviceAccountName" . }} @@ -27,6 +28,12 @@ spec: - name: http containerPort: 8080 protocol: TCP + - name: http-metrics + containerPort: 8383 + protocol: TCP + - name: cr-metrics + containerPort: 8686 + protocol: TCP livenessProbe: httpGet: path: /healthz @@ -43,12 +50,16 @@ spec: - name: WATCH_NAMESPACE value: {{ .Values.namespaceToWatch }} {{- end }} + - name: SERVICE_MONITOR_NAMESPACE + valueFrom: + fieldRef: + fieldPath: metadata.namespace - name: POD_NAME valueFrom: fieldRef: fieldPath: metadata.name - name: OPERATOR_NAME - value: "credstash-operator" + value: {{ include "credstash-operator.fullname" . }} resources: {{- toYaml .Values.resources | nindent 12 }} {{- with .Values.nodeSelector }} diff --git a/deploy/helm/credstash-operator/templates/role.yaml b/deploy/helm/credstash-operator/templates/role.yaml index fc94998..175fbfc 100644 --- a/deploy/helm/credstash-operator/templates/role.yaml +++ b/deploy/helm/credstash-operator/templates/role.yaml @@ -23,6 +23,13 @@ rules: - secrets verbs: - '*' +- apiGroups: + - apps + resources: + - deployments + - replicasets + verbs: + - '*' - apiGroups: - monitoring.coreos.com resources: diff --git a/deploy/role.yaml b/deploy/role.yaml index 765bf4a..71b9fb1 100644 --- a/deploy/role.yaml +++ b/deploy/role.yaml @@ -17,6 +17,13 @@ rules: - secrets verbs: - '*' +- apiGroups: + - apps + resources: + - deployments + - replicasets + verbs: + - '*' - apiGroups: - monitoring.coreos.com resources: diff --git a/pkg/env/getters.go b/pkg/env/getters.go new file mode 100644 index 0000000..e802697 --- /dev/null +++ b/pkg/env/getters.go @@ -0,0 +1,17 @@ +package env + +import ( + "fmt" + "os" +) + +const ServiceMonitorNamespaceEnvVar = "SERVICE_MONITOR_NAMESPACE" + +func GetServiceMonitorNamespace() (string, error) { + ns, found := os.LookupEnv(ServiceMonitorNamespaceEnvVar) + if !found { + return "", fmt.Errorf("%s must be set", ServiceMonitorNamespaceEnvVar) + } + + return ns, nil +} diff --git a/pkg/env/getters_test.go b/pkg/env/getters_test.go new file mode 100644 index 0000000..3ef3960 --- /dev/null +++ b/pkg/env/getters_test.go @@ -0,0 +1,24 @@ +package env + +import ( + "os" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestGetServiceMonitorNamespace(t *testing.T) { + err := os.Unsetenv(ServiceMonitorNamespaceEnvVar) + assert.NoError(t, err) + + ns, err := GetServiceMonitorNamespace() + assert.Error(t, err) + assert.Equal(t, "", ns) + + err = os.Setenv(ServiceMonitorNamespaceEnvVar, "namespace") + assert.NoError(t, err) + + ns, err = GetServiceMonitorNamespace() + assert.NoError(t, err) + assert.Equal(t, "namespace", ns) +}