-
Notifications
You must be signed in to change notification settings - Fork 104
MON-4500: Migrate Prometheus targets discovering from Endpoints to EndpointSlices #460
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -91,6 +91,7 @@ rules: | |
| - rbac.authorization.k8s.io | ||
| resources: | ||
| - clusterroles | ||
| - roles | ||
| verbs: | ||
| - update | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,3 +18,11 @@ rules: | |
| - get | ||
| - list | ||
| - watch | ||
| - apiGroups: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you need to add these same permissions to the ClusterRole, otherwise the user running openshift-dns-operator won´t be able to give this permission. See: |
||
| - discovery.k8s.io | ||
| resources: | ||
| - endpointslices | ||
| verbs: | ||
| - get | ||
| - list | ||
| - watch | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,3 +20,4 @@ spec: | |
| selector: | ||
| matchLabels: | ||
| name: dns-operator | ||
| serviceDiscoveryRole: EndpointSlice | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| package controller | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/google/go-cmp/cmp" | ||
| "github.com/google/go-cmp/cmp/cmpopts" | ||
| "github.com/openshift/cluster-dns-operator/pkg/manifests" | ||
|
|
||
| "github.com/sirupsen/logrus" | ||
|
|
||
| rbacv1 "k8s.io/api/rbac/v1" | ||
| "k8s.io/apimachinery/pkg/api/errors" | ||
| "k8s.io/apimachinery/pkg/types" | ||
| ) | ||
|
|
||
| func (r *reconciler) ensureDNSMetricsRole() (bool, *rbacv1.Role, error) { | ||
| desired := manifests.MetricsRole() | ||
|
|
||
| have, current, err := r.currentDNSMetricsRole() | ||
| if err != nil { | ||
| return false, nil, err | ||
| } | ||
|
|
||
| switch { | ||
| case !have: | ||
| if err := r.client.Create(context.TODO(), desired); err != nil { | ||
| return false, nil, fmt.Errorf("failed to create dns metrics role %s/%s: %v", desired.GetNamespace(), desired.GetName(), err) | ||
| } | ||
| logrus.Infof("created dns metrics role %s/%s", desired.GetNamespace(), desired.GetName()) | ||
| return r.currentDNSMetricsRole() | ||
| case have: | ||
| if updated, err := r.updateDNSMetricsRole(current, desired); err != nil { | ||
| return true, current, err | ||
| } else if updated { | ||
| return r.currentDNSMetricsRole() | ||
| } | ||
| } | ||
| return true, current, nil | ||
| } | ||
|
|
||
| func (r *reconciler) currentDNSMetricsRole() (bool, *rbacv1.Role, error) { | ||
| desired := manifests.MetricsRole() | ||
| current := &rbacv1.Role{} | ||
| if err := r.client.Get(context.TODO(), types.NamespacedName{Namespace: desired.GetNamespace(), Name: desired.GetName()}, current); err != nil { | ||
| if errors.IsNotFound(err) { | ||
| return false, nil, nil | ||
| } | ||
| return false, nil, err | ||
| } | ||
| return true, current, nil | ||
| } | ||
|
|
||
| func (r *reconciler) updateDNSMetricsRole(current, desired *rbacv1.Role) (bool, error) { | ||
| changed, updated := dnsMetricsRoleChanged(current, desired) | ||
| if !changed { | ||
| return false, nil | ||
| } | ||
|
|
||
| // Diff before updating because the client may mutate the object. | ||
| diff := cmp.Diff(current, updated, cmpopts.EquateEmpty()) | ||
| if err := r.client.Update(context.TODO(), updated); err != nil { | ||
| return false, fmt.Errorf("failed to update dns metrics role %s/%s: %v", updated.GetNamespace(), updated.GetName(), err) | ||
| } | ||
| logrus.Infof("updated dns metrics role %s/%s: %v", updated.GetNamespace(), updated.GetName(), diff) | ||
| return true, nil | ||
| } | ||
|
|
||
| func dnsMetricsRoleChanged(current, desired *rbacv1.Role) (bool, *rbacv1.Role) { | ||
| if cmp.Equal(current.Rules, desired.Rules, cmpopts.EquateEmpty()) { | ||
| return false, nil | ||
| } | ||
|
|
||
| updated := current.DeepCopy() | ||
| updated.Rules = desired.Rules | ||
|
|
||
| return true, updated | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package controller | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please follow the same approach of unit tests on https://github.com/openshift/cluster-dns-operator/blob/master/pkg/operator/controller/controller_cluster_role_test.go |
||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/openshift/cluster-dns-operator/pkg/manifests" | ||
| rbacv1 "k8s.io/api/rbac/v1" | ||
| ) | ||
|
|
||
| func TestDNSggMetricsRoleChanged(t *testing.T) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: |
||
| role1 := manifests.MetricsRole() | ||
| role2 := manifests.MetricsRole() | ||
| if changed, _ := dnsMetricsRoleChanged(role1, role2); changed { | ||
| t.Fatal("expected changed to be false for two roles with identical rules") | ||
| } | ||
| role2.Rules = append(role2.Rules, rbacv1.PolicyRule{ | ||
| APIGroups: []string{"example.io"}, | ||
| Resources: []string{"foos"}, | ||
| Verbs: []string{"get"}, | ||
| }) | ||
| if changed, updated := dnsMetricsRoleChanged(role1, role2); !changed { | ||
| t.Fatal("expected changed to be true after adding a rule") | ||
| } else if changedAgain, _ := dnsMetricsRoleChanged(role2, updated); changedAgain { | ||
| t.Fatal("dnsMetricsRoleChanged does not behave as a fixed-point function") | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
allows the operator to update
pkg/manifests/assets/dns/metrics/role.yamlon the cluster