Skip to content

Commit

Permalink
Watch namespaces based on CR value
Browse files Browse the repository at this point in the history
  • Loading branch information
sedroche committed Feb 4, 2019
1 parent afe260d commit 513ad1b
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 6 deletions.
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ code/compile:
.PHONY: code/gen
code/gen:
operator-sdk generate k8s
@go generate ./...s

.PHONY: code/check
code/check:
Expand Down
47 changes: 47 additions & 0 deletions deploy/crds/Grafana.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,50 @@ spec:
singular: grafana
scope: Namespaced
version: v1alpha1
validation:
openAPIV3Schema:
properties:
spec:
properties:
monitoringNamespaceSelector:
description: A label selector is a label query over a set of resources.
The result of matchLabels and matchExpressions are ANDed. An empty
label selector matches all objects. A null label selector matches
no objects.
properties:
matchLabels:
description: matchLabels is a map of {key,value} pairs. A single
{key,value} in the matchLabels map is equivalent to an element
of matchExpressions, whose key field is "key", the operator is
"In", and the values array contains only "value". The requirements
are ANDed.
type: object
matchExpressions:
description: matchExpressions is a list of label selector requirements.
The requirements are ANDed.
items:
description: A label selector requirement is a selector that contains
values, a key, and an operator that relates the key and values.
properties:
key:
description: key is the label key that the selector applies
to.
type: string
operator:
description: operator represents a key's relationship to a
set of values. Valid operators are In, NotIn, Exists and
DoesNotExist.
type: string
values:
description: values is an array of string values. If the operator
is In or NotIn, the values array must be non-empty. If the
operator is Exists or DoesNotExist, the values array must
be empty. This array is replaced during a strategic merge
patch.
items:
type: string
type: array
required:
- key
- operator
type: array
3 changes: 3 additions & 0 deletions deploy/examples/Grafana.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ metadata:
name: example-grafana
spec:
prometheusUrl: "http://prometheus-application-monitoring:9090"
monitoringNamespaceSelector:
matchLabels:
monitoring-key: middleware
3 changes: 2 additions & 1 deletion pkg/apis/integreatly/v1alpha1/grafana_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import (
type GrafanaSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "operator-sdk generate k8s" to regenerate code after modifying this file
PrometheusUrl string `json:"prometheusUrl"`
PrometheusUrl string `json:"prometheusUrl"`
MonitoringNamespaceSelector *metav1.LabelSelector `json:"monitoringNamespaceSelector,omitempty"`
}

// GrafanaStatus defines the observed state of Grafana
Expand Down
8 changes: 7 additions & 1 deletion pkg/apis/integreatly/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/controller/grafana/grafana_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (r *ReconcileGrafana) Reconcile(request reconcile.Request) (reconcile.Resul
}

func (r *ReconcileGrafana) ReconcileNamespaces(cr *integreatly.Grafana) (reconcile.Result, error) {
namespaces, err := r.helper.getMonitoringNamespaces()
namespaces, err := r.helper.getMonitoringNamespaces(cr.Spec.MonitoringNamespaceSelector)
if err != nil {
log.Error(err, "Error listing namespaces")
return reconcile.Result{}, err
Expand Down
11 changes: 9 additions & 2 deletions pkg/controller/grafana/kubeHelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,16 @@ func newKubeHelper() *KubeHelperImpl {
return helper
}

func (h KubeHelperImpl) getMonitoringNamespaces() ([]v1.Namespace, error) {
func (h KubeHelperImpl) getMonitoringNamespaces(ls *metav1.LabelSelector) ([]v1.Namespace, error) {
s, err := metav1.LabelSelectorAsSelector(ls)
if err != nil {
return nil, err
}

var ss string
metav1.Convert_labels_Selector_To_string(&s, &ss, nil)
selector := metav1.ListOptions{
LabelSelector: "monitoring=enabled",
LabelSelector: ss,
}

namespaces, err := h.k8client.CoreV1().Namespaces().List(selector)
Expand Down

0 comments on commit 513ad1b

Please sign in to comment.