Skip to content

Commit

Permalink
feat: make sure that the integration with the prometheus operator wor…
Browse files Browse the repository at this point in the history
…ks (#30)

* feat: make sure that the integration with the prometheus operator works

* fix: pull image if not present
  • Loading branch information
givanov authored Feb 10, 2020
1 parent 4941073 commit 8e8544f
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 6 deletions.
19 changes: 17 additions & 2 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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.")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: credstash.ouzi.tech/v1alpha1
kind: CredstashSecret
metadata:
name: example-credstashsecret
namespace: georgi
namespace: credstash-operator
spec:
secrets:
- key: key1
Expand Down
4 changes: 2 additions & 2 deletions deploy/helm/credstash-operator/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 12 additions & 1 deletion deploy/helm/credstash-operator/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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" . }}
Expand All @@ -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
Expand All @@ -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 }}
Expand Down
7 changes: 7 additions & 0 deletions deploy/helm/credstash-operator/templates/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ rules:
- secrets
verbs:
- '*'
- apiGroups:
- apps
resources:
- deployments
- replicasets
verbs:
- '*'
- apiGroups:
- monitoring.coreos.com
resources:
Expand Down
7 changes: 7 additions & 0 deletions deploy/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ rules:
- secrets
verbs:
- '*'
- apiGroups:
- apps
resources:
- deployments
- replicasets
verbs:
- '*'
- apiGroups:
- monitoring.coreos.com
resources:
Expand Down
17 changes: 17 additions & 0 deletions pkg/env/getters.go
Original file line number Diff line number Diff line change
@@ -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
}
24 changes: 24 additions & 0 deletions pkg/env/getters_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 8e8544f

Please sign in to comment.