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

feat(controllers): add support for instanceMatchSelector #1497

Merged
merged 21 commits into from May 16, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
506fb70
Add - matchExpression
Ronan-WeScale Apr 16, 2024
976c6ea
Add - MatchExpression kind resources
Ronan-WeScale Apr 16, 2024
abc474a
Fix - trailing whitespace
Ronan-WeScale Apr 16, 2024
1884ab4
Remove - unused kind resources
Ronan-WeScale Apr 29, 2024
337b660
Update - controller matchInstance func
Ronan-WeScale Apr 29, 2024
ac4450b
Add - instanceSelector e2e
Ronan-WeScale Apr 29, 2024
d6106d5
Fix - file-checks newline
Ronan-WeScale Apr 29, 2024
e612089
Fix - e2e test by deleting ingress
Ronan-WeScale Apr 30, 2024
3a55005
Add - unit test for labelMatchedExpression func
Ronan-WeScale Apr 30, 2024
20c80ce
Fix - e2e test
Ronan-WeScale Apr 30, 2024
92e4070
chore(controllers): move TestLabelMatchedExpression to another file
weisdd May 5, 2024
57c8cf4
chore: rewrite labelsMatchExpressions and TestLabelsMatchExpressions
weisdd May 5, 2024
5997012
chore: remove e2e for matchExpressions
weisdd May 5, 2024
4fa7dc5
chore: rename labelsMatchExpressions to labelsSatisfyMatchExpressions
weisdd May 5, 2024
3827260
Merge branch 'master' into fix-1401
weisdd May 5, 2024
77c0217
Merge branch 'master' into fix-1401
NissesSenap May 5, 2024
cafe456
fix(labelsMatchExpressions): cover case with different labels
weisdd May 13, 2024
cbabed9
chore(TestLabelsSatisfyMatchExpressions): improve naming for labels
weisdd May 13, 2024
f44b828
Merge branch 'master' into fix-1401
weisdd May 13, 2024
ea373ea
fix(labelsMatchExpressions): reintroduce support for instanceSelector…
weisdd May 16, 2024
c018681
Merge branch 'master' into fix-1401
weisdd May 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 41 additions & 2 deletions controllers/controller_shared.go
Expand Up @@ -33,9 +33,48 @@ func GetMatchingInstances(ctx context.Context, k8sClient client.Client, labelSel
opts := []client.ListOption{
client.MatchingLabels(labelSelector.MatchLabels),
}

err := k8sClient.List(ctx, &list, opts...)
return list, err

var selectedList v1beta1.GrafanaList

for _, instance := range list.Items {
selected := true
for _, matchExpression := range labelSelector.MatchExpressions {
if val, ok := instance.Labels[matchExpression.Key]; ok {
if matchExpression.Operator == "DoesNotExist" {
selected = false
break
} else if matchExpression.Operator == "Exists" {
break
}
valueMatched := labelMatchedExpression(val, matchExpression)
if !valueMatched {
selected = false
break
}
} else if matchExpression.Operator == "In" || matchExpression.Operator == "Exists" {
selected = false
break
}
}

if selected {
selectedList.Items = append(selectedList.Items, instance)
}
}

return selectedList, err
}

func labelMatchedExpression(val string, matchExpression v1.LabelSelectorRequirement) bool {
valueMatched := matchExpression.Operator == "NotIn"

for _, value := range matchExpression.Values {
if val == value {
return matchExpression.Operator == "In"
}
}
return valueMatched
}

func ReconcilePlugins(ctx context.Context, k8sClient client.Client, scheme *runtime.Scheme, grafana *v1beta1.Grafana, plugins v1beta1.PluginList, resource string) error {
Expand Down
32 changes: 32 additions & 0 deletions hack/kind/resources/default/grafana-2.yaml
@@ -0,0 +1,32 @@
apiVersion: grafana.integreatly.org/v1beta1
kind: Grafana
metadata:
name: grafana-2
labels:
dashboards: "grafana-2"
exists: ""
spec:
client:
preferIngress: true
config:
log:
mode: "console"
auth:
disable_login_form: "false"
security:
admin_user: root
admin_password: secret
ingress:
spec:
ingressClassName: nginx
rules:
- host: grafana2.127.0.0.1.nip.io
http:
paths:
- backend:
service:
name: grafana-2-service
port:
number: 3000
path: /
pathType: Prefix
42 changes: 42 additions & 0 deletions hack/kind/resources/default/grafana-dashboard-exist.yaml
@@ -0,0 +1,42 @@
apiVersion: grafana.integreatly.org/v1beta1
kind: GrafanaDashboard
metadata:
name: grafana-exist
labels:
dashboards: "grafana"
spec:
folder: my-folder-name
instanceSelector:
matchExpressions:
- key: exists
operator: Exists
json: |
{
"id": null,
"title": "Simple Dashboard Exist",
"tags": [],
"style": "dark",
"timezone": "browser",
"editable": true,
"hideControls": false,
"graphTooltip": 1,
"panels": [],
"time": {
"from": "now-6h",
"to": "now"
},
"timepicker": {
"time_options": [],
"refresh_intervals": []
},
"templating": {
"list": []
},
"annotations": {
"list": []
},
"refresh": "5s",
"schemaVersion": 17,
"version": 0,
"links": []
}
44 changes: 44 additions & 0 deletions hack/kind/resources/default/grafana-dashboard-in.yaml
@@ -0,0 +1,44 @@
apiVersion: grafana.integreatly.org/v1beta1
kind: GrafanaDashboard
metadata:
name: grafana-in
labels:
dashboards: "grafana"
spec:
folder: my-folder-name
instanceSelector:
matchExpressions:
- key: dashboards
operator: In
values:
- grafana-2
json: |
{
"id": null,
"title": "Simple Dashboard In Grafana-2",
"tags": [],
"style": "dark",
"timezone": "browser",
"editable": true,
"hideControls": false,
"graphTooltip": 1,
"panels": [],
"time": {
"from": "now-6h",
"to": "now"
},
"timepicker": {
"time_options": [],
"refresh_intervals": []
},
"templating": {
"list": []
},
"annotations": {
"list": []
},
"refresh": "5s",
"schemaVersion": 17,
"version": 0,
"links": []
}
42 changes: 42 additions & 0 deletions hack/kind/resources/default/grafana-dashboard-notexist.yaml
@@ -0,0 +1,42 @@
apiVersion: grafana.integreatly.org/v1beta1
kind: GrafanaDashboard
metadata:
name: grafana-notexist
labels:
dashboards: "grafana"
spec:
folder: my-folder-name
instanceSelector:
matchExpressions:
- key: exists
operator: DoesNotExist
json: |
{
"id": null,
"title": "Simple Dashboard NotExist",
"tags": [],
"style": "dark",
"timezone": "browser",
"editable": true,
"hideControls": false,
"graphTooltip": 1,
"panels": [],
"time": {
"from": "now-6h",
"to": "now"
},
"timepicker": {
"time_options": [],
"refresh_intervals": []
},
"templating": {
"list": []
},
"annotations": {
"list": []
},
"refresh": "5s",
"schemaVersion": 17,
"version": 0,
"links": []
}
44 changes: 44 additions & 0 deletions hack/kind/resources/default/grafana-dashboard-notin.yaml
@@ -0,0 +1,44 @@
apiVersion: grafana.integreatly.org/v1beta1
kind: GrafanaDashboard
metadata:
name: grafana-notin-grafana-2
labels:
dashboards: "grafana"
spec:
folder: my-folder-name
instanceSelector:
matchExpressions:
- key: dashboards
operator: NotIn
values:
- grafana-2
json: |
{
"id": null,
"title": "Simple Dashboard notIn grafana-2",
"tags": [],
"style": "dark",
"timezone": "browser",
"editable": true,
"hideControls": false,
"graphTooltip": 1,
"panels": [],
"time": {
"from": "now-6h",
"to": "now"
},
"timepicker": {
"time_options": [],
"refresh_intervals": []
},
"templating": {
"list": []
},
"annotations": {
"list": []
},
"refresh": "5s",
"schemaVersion": 17,
"version": 0,
"links": []
}
4 changes: 2 additions & 2 deletions hack/kind/resources/default/grafana-dashboard.yaml
Expand Up @@ -8,11 +8,11 @@ spec:
folder: my-folder-name
instanceSelector:
matchLabels:
dashboards: "grafana"
dashboards: grafana
json: |
{
"id": null,
"title": "Simple Dashboard",
"title": "Simple Dashboard MatchLabels",
"tags": [],
"style": "dark",
"timezone": "browser",
Expand Down
5 changes: 5 additions & 0 deletions hack/kind/resources/default/kustomization.yaml
@@ -1,5 +1,10 @@
resources:
- grafana.yaml
- grafana-2.yaml
- grafana-dashboard.yaml
- grafana-dashboard-in.yaml
- grafana-dashboard-notin.yaml
- grafana-dashboard-exist.yaml
- grafana-dashboard-notexist.yaml
- grafana-datasource.yaml
- grafana-contactpoint.yaml