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

Watch namespaces based on CR value #4

Merged
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
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
15 changes: 11 additions & 4 deletions pkg/controller/grafana/kubeHelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,19 @@ func newKubeHelper() *KubeHelperImpl {
return helper
}

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

var selectorString string
metav1.Convert_labels_Selector_To_string(&selector, &selectorString, nil)
opts := metav1.ListOptions{
LabelSelector: selectorString,
}

namespaces, err := h.k8client.CoreV1().Namespaces().List(selector)
namespaces, err := h.k8client.CoreV1().Namespaces().List(opts)
if err != nil {
return nil, err
}
Expand Down