-
Notifications
You must be signed in to change notification settings - Fork 218
OCPBUGS-65629: ensure canary daemon set uses its own service account. #1310
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
Open
ehearne-redhat
wants to merge
4
commits into
openshift:master
Choose a base branch
from
ehearne-redhat:add-ingress-service-account-to-canary-daemon-set
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
37bcbc5
add ingress-operator service account to canary daemon set
ehearne-redhat 7e64ffe
[canary daemonset] add service account to daemonset + add SA and CRB
ehearne-redhat 5e98ff4
ensure cluster role binding and service account in canary reconciler
ehearne-redhat e999f8a
remove unnecessary code
ehearne-redhat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| # Binds the operator cluster role to its Service Account. | ||
| kind: ClusterRoleBinding | ||
| apiVersion: rbac.authorization.k8s.io/v1 | ||
| metadata: | ||
| name: openshift-ingress-canary | ||
| annotations: | ||
| capability.openshift.io/name: Ingress | ||
| include.release.openshift.io/ibm-cloud-managed: "true" | ||
| include.release.openshift.io/self-managed-high-availability: "true" | ||
| include.release.openshift.io/single-node-developer: "true" | ||
| subjects: | ||
| - kind: ServiceAccount | ||
| name: ingress-canary | ||
| namespace: openshift-ingress-canary | ||
| roleRef: | ||
| kind: ClusterRole | ||
| apiGroup: rbac.authorization.k8s.io | ||
| name: openshift-ingress-operator |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| # Account for the operator itself. It should require namespace scoped | ||
| # permissions. | ||
| kind: ServiceAccount | ||
| apiVersion: v1 | ||
| metadata: | ||
| name: ingress-canary | ||
| namespace: openshift-ingress-canary | ||
| annotations: | ||
| capability.openshift.io/name: Ingress | ||
| include.release.openshift.io/ibm-cloud-managed: "true" | ||
| include.release.openshift.io/self-managed-high-availability: "true" | ||
| include.release.openshift.io/single-node-developer: "true" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| package canary | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/google/go-cmp/cmp" | ||
| "github.com/google/go-cmp/cmp/cmpopts" | ||
| "github.com/openshift/cluster-ingress-operator/pkg/manifests" | ||
| "github.com/openshift/cluster-ingress-operator/pkg/operator/controller" | ||
|
|
||
| rbacv1 "k8s.io/api/rbac/v1" | ||
| "k8s.io/apimachinery/pkg/api/errors" | ||
| ) | ||
|
|
||
| // ensureCanaryClusterRoleBinding ensures the canary cluster role binding exists. | ||
| func (r *reconciler) ensureCanaryClusterRoleBinding() (bool, *rbacv1.ClusterRoleBinding, error) { | ||
| desired := desiredCanaryClusterRoleBinding() | ||
| haveCrb, current, err := r.currentCanaryClusterRoleBinding() | ||
|
|
||
| if err != nil { | ||
| return false, nil, err | ||
| } | ||
|
|
||
| switch { | ||
| case !haveCrb: | ||
| if err := r.createCanaryClusterRoleBinding(desired); err != nil { | ||
| return false, nil, err | ||
| } | ||
| return r.currentCanaryClusterRoleBinding() | ||
| case haveCrb: | ||
| if updated, err := r.updateCanaryClusterRoleBinding(current, desired); err != nil { | ||
| return true, current, err | ||
| } else if updated { | ||
| return r.currentCanaryClusterRoleBinding() | ||
| } | ||
| } | ||
|
|
||
| return true, current, nil | ||
| } | ||
|
|
||
| // currentCanaryClusterRoleBinding returns the current cluster role binding. | ||
| func (r *reconciler) currentCanaryClusterRoleBinding() (bool, *rbacv1.ClusterRoleBinding, error) { | ||
| clusterRoleBinding := &rbacv1.ClusterRoleBinding{} | ||
| if err := r.client.Get(context.TODO(), controller.CanaryClusterRoleBindingName(), clusterRoleBinding); err != nil { | ||
| if errors.IsNotFound(err) { | ||
| return false, nil, nil | ||
| } | ||
| return false, nil, err | ||
| } | ||
| return true, clusterRoleBinding, nil | ||
| } | ||
|
|
||
| // createCanaryClusterRoleBinding creates the given cluster role binding resource. | ||
| func (r *reconciler) createCanaryClusterRoleBinding(clusterRoleBinding *rbacv1.ClusterRoleBinding) error { | ||
| if err := r.client.Create(context.TODO(), clusterRoleBinding); err != nil { | ||
| return fmt.Errorf("failed to create canary cluster role binding %s/%s: %v", clusterRoleBinding.Namespace, clusterRoleBinding.Name, err) | ||
| } | ||
| log.Info("created canary cluster role binding", "namespace", clusterRoleBinding.Namespace, "name", clusterRoleBinding.Name) | ||
| return nil | ||
| } | ||
|
|
||
| // updateCanaryClusterBinding updates the canary clusterRoleBinding if an appropriate | ||
| // change has been detected. | ||
| func (r *reconciler) updateCanaryClusterRoleBinding(current, desired *rbacv1.ClusterRoleBinding) (bool, error) { | ||
| changed, updated := canaryClusterRoleBindingChanged(current, desired) | ||
| if !changed { | ||
| return false, nil | ||
| } | ||
|
|
||
| diff := cmp.Diff(current, updated, cmpopts.EquateEmpty()) | ||
| if err := r.client.Update(context.TODO(), updated); err != nil { | ||
| return false, fmt.Errorf("failed to update canary cluster role binding %s/%s: %v", updated.Namespace, updated.Name, err) | ||
| } | ||
| log.Info("updated canary cluster role binding", "namespace", updated.Namespace, "name", updated.Name, "diff", diff) | ||
| return true, nil | ||
| } | ||
|
|
||
| // desiredCanaryClusterRoleBinding returns the desired canary clusterRoleBinding | ||
| // read in from manifests. | ||
| func desiredCanaryClusterRoleBinding() *rbacv1.ClusterRoleBinding { | ||
| clusterRoleBinding := manifests.CanaryClusterRoleBinding() | ||
| name := controller.CanaryClusterRoleBindingName() | ||
| clusterRoleBinding.Name = name.Name | ||
| clusterRoleBinding.Namespace = name.Namespace | ||
|
|
||
| clusterRoleBinding.Labels = map[string]string{ | ||
| // associate the cluster role binding with the ingress canary controller | ||
| manifests.OwningIngressCanaryCheckLabel: canaryControllerName, | ||
| } | ||
|
|
||
| return clusterRoleBinding | ||
| } | ||
|
|
||
| // canaryClusterRoleBindingChanged returns true if current and expected differ by the | ||
| // binding's subjects. | ||
| func canaryClusterRoleBindingChanged(current, expected *rbacv1.ClusterRoleBinding) (bool, *rbacv1.ClusterRoleBinding) { | ||
| if len(current.Subjects) == 0 && len(expected.Subjects) == 0 { | ||
| return false, nil | ||
| } | ||
|
|
||
| if len(current.Subjects) != len(expected.Subjects) { | ||
| return true, expected.DeepCopy() | ||
| } | ||
| currentSubjectsMap := make(map[string]rbacv1.Subject, len(current.Subjects)) | ||
|
|
||
| for _, subject := range current.Subjects { | ||
| currentSubjectsMap[getSubjectKey(subject)] = subject | ||
| } | ||
|
|
||
| for _, subject := range expected.Subjects { | ||
| if _, exists := currentSubjectsMap[getSubjectKey(subject)]; !exists { | ||
| return true, expected.DeepCopy() | ||
| } | ||
| } | ||
|
|
||
| return false, nil | ||
| } | ||
|
|
||
| // getSubjectKey returns a unique, complete key for a subject object. | ||
| // It is a helper function for canaryClusterRoleBindingChanged. | ||
| func getSubjectKey(s rbacv1.Subject) string { | ||
| return fmt.Sprintf("%s/%s/%s/%s", s.Kind, s.APIGroup, s.Namespace, s.Name) | ||
| } |
79 changes: 79 additions & 0 deletions
79
pkg/operator/controller/canary/cluster_role_binding_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| package canary | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/google/go-cmp/cmp" | ||
| "github.com/openshift/cluster-ingress-operator/pkg/manifests" | ||
| "github.com/openshift/cluster-ingress-operator/pkg/operator/controller" | ||
|
|
||
| rbacv1 "k8s.io/api/rbac/v1" | ||
| ) | ||
|
|
||
| func Test_desiredClusterRoleBinding(t *testing.T) { | ||
| clusterRoleBinding := desiredCanaryClusterRoleBinding() | ||
|
|
||
| expectedClusterRoleBindingName := controller.CanaryClusterRoleBindingName() | ||
|
|
||
| if !cmp.Equal(clusterRoleBinding.Name, expectedClusterRoleBindingName.Name) { | ||
| t.Errorf("expected clusterrolebinding name to be %s, but got %s", expectedClusterRoleBindingName.Name, clusterRoleBinding.Name) | ||
| } | ||
|
|
||
| if !cmp.Equal(clusterRoleBinding.Namespace, expectedClusterRoleBindingName.Namespace) { | ||
| t.Errorf("expected clusterrolebinding namespace to be %s, but got %s", expectedClusterRoleBindingName.Namespace, clusterRoleBinding.Namespace) | ||
| } | ||
|
|
||
| expectedLabels := map[string]string{ | ||
| manifests.OwningIngressCanaryCheckLabel: canaryControllerName, | ||
| } | ||
|
|
||
| if !cmp.Equal(clusterRoleBinding.Labels, expectedLabels) { | ||
| t.Errorf("expected clusterrolebinding labels to be %q, but got %q", expectedLabels, clusterRoleBinding.Labels) | ||
| } | ||
| } | ||
|
|
||
| func Test_canaryClusterRoleBindingChanged(t *testing.T) { | ||
| testCases := []struct { | ||
| description string | ||
| mutate func(*rbacv1.ClusterRoleBinding) | ||
| expect bool | ||
| }{ | ||
| { | ||
| description: "if nothing changes", | ||
| mutate: func(_ *rbacv1.ClusterRoleBinding) {}, | ||
| expect: false, | ||
| }, | ||
| { | ||
| description: "if subjects change", | ||
| mutate: func(crb *rbacv1.ClusterRoleBinding) { | ||
| crb.Subjects = []rbacv1.Subject{ | ||
| { | ||
| Kind: "test", | ||
| APIGroup: "foo", | ||
| Name: "bar", | ||
| Namespace: "foobar", | ||
| }, | ||
| } | ||
| }, | ||
| expect: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.description, func(t *testing.T) { | ||
| original := desiredCanaryClusterRoleBinding() | ||
| mutated := original.DeepCopy() | ||
| tc.mutate(mutated) | ||
| if changed, updated := canaryClusterRoleBindingChanged(original, mutated); changed != tc.expect { | ||
| t.Errorf("expected canaryClusterRoleBindingChanged to be %t, got %t", tc.expect, changed) | ||
| } else if changed { | ||
| if updatedChanged, _ := canaryClusterRoleBindingChanged(original, updated); !updatedChanged { | ||
| t.Error("canaryClusterRoleBindingChanged reported changes but did not make any update") | ||
| } | ||
| if changedAgain, _ := canaryClusterRoleBindingChanged(mutated, updated); changedAgain { | ||
| t.Error("canaryClusterRoleBindingChanged does not behave as a fixed point function") | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.