diff --git a/Makefile b/Makefile index 091cd4a38..a09ce1ac9 100644 --- a/Makefile +++ b/Makefile @@ -31,7 +31,6 @@ code/compile: .PHONY: code/gen code/gen: operator-sdk generate k8s - @go generate ./...s .PHONY: code/check code/check: diff --git a/deploy/crds/Grafana.yaml b/deploy/crds/Grafana.yaml index d83df1d1e..6075d56af 100644 --- a/deploy/crds/Grafana.yaml +++ b/deploy/crds/Grafana.yaml @@ -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 \ No newline at end of file diff --git a/deploy/examples/Grafana.yaml b/deploy/examples/Grafana.yaml index 19583b964..c298337e0 100644 --- a/deploy/examples/Grafana.yaml +++ b/deploy/examples/Grafana.yaml @@ -4,3 +4,6 @@ metadata: name: example-grafana spec: prometheusUrl: "http://prometheus-application-monitoring:9090" + monitoringNamespaceSelector: + matchLabels: + monitoring-key: middleware diff --git a/pkg/apis/integreatly/v1alpha1/grafana_types.go b/pkg/apis/integreatly/v1alpha1/grafana_types.go index 57203a0c5..77a8258a8 100644 --- a/pkg/apis/integreatly/v1alpha1/grafana_types.go +++ b/pkg/apis/integreatly/v1alpha1/grafana_types.go @@ -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 diff --git a/pkg/apis/integreatly/v1alpha1/zz_generated.deepcopy.go b/pkg/apis/integreatly/v1alpha1/zz_generated.deepcopy.go index 84b9ae397..036caed52 100644 --- a/pkg/apis/integreatly/v1alpha1/zz_generated.deepcopy.go +++ b/pkg/apis/integreatly/v1alpha1/zz_generated.deepcopy.go @@ -21,6 +21,7 @@ limitations under the License. package v1alpha1 import ( + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" runtime "k8s.io/apimachinery/pkg/runtime" ) @@ -29,7 +30,7 @@ func (in *Grafana) DeepCopyInto(out *Grafana) { *out = *in out.TypeMeta = in.TypeMeta in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) - out.Spec = in.Spec + in.Spec.DeepCopyInto(&out.Spec) out.Status = in.Status return } @@ -181,6 +182,11 @@ func (in *GrafanaList) DeepCopyObject() runtime.Object { // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *GrafanaSpec) DeepCopyInto(out *GrafanaSpec) { *out = *in + if in.MonitoringNamespaceSelector != nil { + in, out := &in.MonitoringNamespaceSelector, &out.MonitoringNamespaceSelector + *out = new(v1.LabelSelector) + (*in).DeepCopyInto(*out) + } return } diff --git a/pkg/controller/grafana/grafana_controller.go b/pkg/controller/grafana/grafana_controller.go index c19c9e2a2..3a862ab8d 100644 --- a/pkg/controller/grafana/grafana_controller.go +++ b/pkg/controller/grafana/grafana_controller.go @@ -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 diff --git a/pkg/controller/grafana/kubeHelper.go b/pkg/controller/grafana/kubeHelper.go index 41f5ae9a1..d81aeacaf 100644 --- a/pkg/controller/grafana/kubeHelper.go +++ b/pkg/controller/grafana/kubeHelper.go @@ -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 }