-
Notifications
You must be signed in to change notification settings - Fork 51
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
pkg/components: Refactor to reduce duplicate code #1845
base: main
Are you sure you want to change the base?
Conversation
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here.
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
var rules []rbacv1.PolicyRule | ||
|
||
rules = append(rules, | ||
newPolicyRule([]string{""}, []string{"events"}, []string{"update"}), |
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.
wdyt about this ?
easier to read imo please
newPolicyRule(
[]string{""},
[]string{"events"},
[]string{"update"}
),
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.
ouch sonar is not happy from that
i guess we dont have a choice
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.
tried: duplicity went up 18%
perhaps we could use new line only on lines that are too long?
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.
either that, or just revert to the initial please (better imo)
whatever you prefer
thanks
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.
I think we can continue with this change as is.
beside cosmetics there are no changes right ? |
It's only a refactor. No logic changed. |
pkg/components/components.go
Outdated
var rules []rbacv1.PolicyRule | ||
|
||
rules = append(rules, | ||
newPolicyRule([]string{"apps"}, []string{"daemonsets"}, []string{"get", "create", "update", "delete"}), | ||
newPolicyRule([]string{""}, []string{"configmaps"}, []string{"get", "create", "update"}), | ||
newPolicyRule([]string{"apps"}, []string{"deployments"}, []string{"delete"}), | ||
newPolicyRule([]string{""}, []string{"namespaces"}, []string{"update", "get", "patch"}), | ||
newPolicyRule([]string{""}, []string{"serviceaccounts"}, []string{"get", "create", "update", "delete"}), | ||
newPolicyRule([]string{"monitoring.coreos.com"}, []string{"prometheusrules", "servicemonitors"}, []string{"get", "create", "update", "delete"}), | ||
newPolicyRule([]string{"rbac.authorization.k8s.io"}, []string{"roles", "rolebindings"}, []string{"get", "create", "update", "delete"}), | ||
newPolicyRule([]string{"policy"}, []string{"poddisruptionbudgets"}, []string{"get", "delete"}), | ||
newPolicyRule([]string{""}, []string{"configmaps"}, []string{"patch"}), | ||
newPolicyRule([]string{"coordination.k8s.io"}, []string{"leases"}, []string{"get", "list", "watch", "create", "update", "patch", "delete"}), | ||
newPolicyRule([]string{"cert-manager.io"}, []string{"certificates", "issuers"}, []string{"get", "create", "update", "delete"}), | ||
) |
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.
why using append
? append
is pricey. I think you can initialize the slice with the required data; e.g.
var rules []rbacv1.PolicyRule | |
rules = append(rules, | |
newPolicyRule([]string{"apps"}, []string{"daemonsets"}, []string{"get", "create", "update", "delete"}), | |
newPolicyRule([]string{""}, []string{"configmaps"}, []string{"get", "create", "update"}), | |
newPolicyRule([]string{"apps"}, []string{"deployments"}, []string{"delete"}), | |
newPolicyRule([]string{""}, []string{"namespaces"}, []string{"update", "get", "patch"}), | |
newPolicyRule([]string{""}, []string{"serviceaccounts"}, []string{"get", "create", "update", "delete"}), | |
newPolicyRule([]string{"monitoring.coreos.com"}, []string{"prometheusrules", "servicemonitors"}, []string{"get", "create", "update", "delete"}), | |
newPolicyRule([]string{"rbac.authorization.k8s.io"}, []string{"roles", "rolebindings"}, []string{"get", "create", "update", "delete"}), | |
newPolicyRule([]string{"policy"}, []string{"poddisruptionbudgets"}, []string{"get", "delete"}), | |
newPolicyRule([]string{""}, []string{"configmaps"}, []string{"patch"}), | |
newPolicyRule([]string{"coordination.k8s.io"}, []string{"leases"}, []string{"get", "list", "watch", "create", "update", "patch", "delete"}), | |
newPolicyRule([]string{"cert-manager.io"}, []string{"certificates", "issuers"}, []string{"get", "create", "update", "delete"}), | |
) | |
rules := []rbacv1.PolicyRule{ | |
newPolicyRule([]string{"apps"}, []string{"daemonsets"}, []string{"get", "create", "update", "delete"}), | |
newPolicyRule([]string{""}, []string{"configmaps"}, []string{"get", "create", "update"}), | |
newPolicyRule([]string{"apps"}, []string{"deployments"}, []string{"delete"}), | |
newPolicyRule([]string{""}, []string{"namespaces"}, []string{"update", "get", "patch"}), | |
newPolicyRule([]string{""}, []string{"serviceaccounts"}, []string{"get", "create", "update", "delete"}), | |
newPolicyRule([]string{"monitoring.coreos.com"}, []string{"prometheusrules", "servicemonitors"}, []string{"get", "create", "update", "delete"}), | |
newPolicyRule([]string{"rbac.authorization.k8s.io"}, []string{"roles", "rolebindings"}, []string{"get", "create", "update", "delete"}), | |
newPolicyRule([]string{"policy"}, []string{"poddisruptionbudgets"}, []string{"get", "delete"}), | |
newPolicyRule([]string{""}, []string{"configmaps"}, []string{"patch"}), | |
newPolicyRule([]string{"coordination.k8s.io"}, []string{"leases"}, []string{"get", "list", "watch", "create", "update", "patch", "delete"}), | |
newPolicyRule([]string{"cert-manager.io"}, []string{"certificates", "issuers"}, []string{"get", "create", "update", "delete"}), | |
} |
pkg/components/components.go
Outdated
} | ||
return role | ||
} | ||
|
||
func GetClusterRole(allowMultus bool) *rbacv1.ClusterRole { | ||
var rules []rbacv1.PolicyRule |
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.
same
pkg/components/components.go
Outdated
var rules []rbacv1.PolicyRule | ||
|
||
rules = append(rules, |
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.
same
5d31b26
to
6896c42
Compare
6896c42
to
968a49b
Compare
/hold |
if any are left out, tests should find it, or we are missing tests, or those arent needed anymore |
you didnt rebase correctly, please check it Those need to be included please: |
thanks! |
968a49b
to
cc6fe21
Compare
|
Issues go stale after 90d of inactivity. If this issue is safe to close now please do so with /lifecycle stale |
Stale issues rot after 30d of inactivity. If this issue is safe to close now please do so with /lifecycle rotten |
cc6fe21
to
519dec3
Compare
|
Change: Rebase |
nice catch! - fixed. PTAL |
/hold cancel |
/remove-lifecycle rotten |
This is done in order to eliminate sonarCloud issue. Signed-off-by: Ram Lavi <[email protected]>
519dec3
to
c70c726
Compare
Change: Rebase |
|
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.
I think that if you can use the group names consts from the group themselves, it would be clearer and safer.
Added several examples inline.
newPolicyRule([]string{"apps"}, []string{"daemonsets"}, []string{"get", "create", "update", "delete"}), | ||
newPolicyRule([]string{""}, []string{"configmaps"}, []string{"get", "create", "update"}), | ||
newPolicyRule([]string{"apps"}, []string{"deployments"}, []string{"delete"}), | ||
newPolicyRule([]string{""}, []string{"namespaces"}, []string{"update", "get", "patch"}), | ||
newPolicyRule([]string{""}, []string{"serviceaccounts"}, []string{"get", "create", "update", "delete"}), | ||
newPolicyRule([]string{"monitoring.coreos.com"}, []string{"prometheusrules", "servicemonitors"}, []string{"get", "create", "update", "delete"}), | ||
newPolicyRule([]string{"rbac.authorization.k8s.io"}, []string{"roles", "rolebindings"}, []string{"get", "create", "update", "delete"}), | ||
newPolicyRule([]string{"policy"}, []string{"poddisruptionbudgets"}, []string{"get", "delete"}), | ||
newPolicyRule([]string{""}, []string{"configmaps"}, []string{"patch"}), | ||
newPolicyRule([]string{"coordination.k8s.io"}, []string{"leases"}, []string{"get", "list", "watch", "create", "update", "patch", "delete"}), | ||
newPolicyRule([]string{"cert-manager.io"}, []string{"certificates", "issuers"}, []string{"get", "create", "update", "delete"}), |
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.
What do you think about defining a core group variable, e.g. var coreGroup = []string{""}
and then use it instead of the empty string group? i.e.
newPolicyRule([]string{"apps"}, []string{"daemonsets"}, []string{"get", "create", "update", "delete"}), | |
newPolicyRule([]string{""}, []string{"configmaps"}, []string{"get", "create", "update"}), | |
newPolicyRule([]string{"apps"}, []string{"deployments"}, []string{"delete"}), | |
newPolicyRule([]string{""}, []string{"namespaces"}, []string{"update", "get", "patch"}), | |
newPolicyRule([]string{""}, []string{"serviceaccounts"}, []string{"get", "create", "update", "delete"}), | |
newPolicyRule([]string{"monitoring.coreos.com"}, []string{"prometheusrules", "servicemonitors"}, []string{"get", "create", "update", "delete"}), | |
newPolicyRule([]string{"rbac.authorization.k8s.io"}, []string{"roles", "rolebindings"}, []string{"get", "create", "update", "delete"}), | |
newPolicyRule([]string{"policy"}, []string{"poddisruptionbudgets"}, []string{"get", "delete"}), | |
newPolicyRule([]string{""}, []string{"configmaps"}, []string{"patch"}), | |
newPolicyRule([]string{"coordination.k8s.io"}, []string{"leases"}, []string{"get", "list", "watch", "create", "update", "patch", "delete"}), | |
newPolicyRule([]string{"cert-manager.io"}, []string{"certificates", "issuers"}, []string{"get", "create", "update", "delete"}), | |
newPolicyRule([]string{"apps"}, []string{"daemonsets"}, []string{"get", "create", "update", "delete"}), | |
newPolicyRule(coreGroup, []string{"configmaps"}, []string{"get", "create", "update"}), | |
newPolicyRule([]string{"apps"}, []string{"deployments"}, []string{"delete"}), | |
newPolicyRule(coreGroup, []string{"namespaces"}, []string{"update", "get", "patch"}), | |
newPolicyRule(coreGroup, []string{"serviceaccounts"}, []string{"get", "create", "update", "delete"}), | |
newPolicyRule([]string{"monitoring.coreos.com"}, []string{"prometheusrules", "servicemonitors"}, []string{"get", "create", "update", "delete"}), | |
newPolicyRule([]string{"rbac.authorization.k8s.io"}, []string{"roles", "rolebindings"}, []string{"get", "create", "update", "delete"}), | |
newPolicyRule([]string{"policy"}, []string{"poddisruptionbudgets"}, []string{"get", "delete"}), | |
newPolicyRule(coreGroup, []string{"configmaps"}, []string{"patch"}), | |
newPolicyRule([]string{"coordination.k8s.io"}, []string{"leases"}, []string{"get", "list", "watch", "create", "update", "patch", "delete"}), | |
newPolicyRule([]string{"cert-manager.io"}, []string{"certificates", "issuers"}, []string{"get", "create", "update", "delete"}), |
rules := []rbacv1.PolicyRule{ | ||
newPolicyRule([]string{""}, []string{"events"}, []string{"update"}), | ||
newPolicyRule([]string{""}, []string{"pods", "pods/status"}, []string{"get", "update", "list", "watch"}), | ||
newPolicyRule([]string{"events.k8s.io"}, []string{"events"}, []string{"create", "patch", "update"}), |
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.
if you'll import eventsv1 "k8s.io/api/events/v1"
,
you could use:
newPolicyRule([]string{"events.k8s.io"}, []string{"events"}, []string{"create", "patch", "update"}), | |
newPolicyRule([]string{eventsv1.GroupName}, []string{"events"}, []string{"create", "patch", "update"}), |
newPolicyRule([]string{""}, []string{"nodes", "nodes/status"}, []string{"get", "update", "patch"}), | ||
newPolicyRule([]string{""}, []string{"configmaps"}, []string{"get", "delete"}), | ||
newPolicyRule([]string{""}, []string{"secrets"}, []string{"list", "watch", "create", "update"}), | ||
newPolicyRule([]string{"admissionregistration.k8s.io"}, []string{"validatingwebhookconfigurations", "mutatingwebhookconfigurations"}, []string{"list", "watch"}), |
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.
Same, with admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
newPolicyRule([]string{""}, []string{"secrets"}, []string{"list", "watch", "create", "update"}), | ||
newPolicyRule([]string{"admissionregistration.k8s.io"}, []string{"validatingwebhookconfigurations", "mutatingwebhookconfigurations"}, []string{"list", "watch"}), | ||
newPolicyRule([]string{""}, []string{"services"}, []string{"get", "create", "update", "list", "watch"}), | ||
newPolicyRule([]string{"kubevirt.io"}, []string{"virtualmachines"}, []string{"get", "list", "watch", "update"}), |
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.
same with kvcore "kubevirt.io/api/core"
newPolicyRule([]string{"authentication.k8s.io"}, []string{"tokenreviews"}, []string{"create"}), | ||
newPolicyRule([]string{"authorization.k8s.io"}, []string{"subjectaccessreviews"}, []string{"create"}), | ||
newPolicyRule([]string{"apps"}, []string{"deployments"}, []string{"get", "create", "update"}), | ||
newPolicyRule([]string{"kubevirt.io"}, []string{"virtualmachineinstances"}, []string{"get", "list", "watch"}), |
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.
same with kvcore "kubevirt.io/api/core"
newPolicyRule([]string{"authentication.k8s.io"}, []string{"tokenreviews"}, []string{"create"}), | ||
newPolicyRule([]string{"authorization.k8s.io"}, []string{"subjectaccessreviews"}, []string{"create"}), |
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.
Same with authenticationv1 "k8s.io/api/authentication/v1"
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.
fix: the 2nd one should be withauthorizationv1 "k8s.io/api/authorization/v1"
newPolicyRule([]string{"kubevirt.io"}, []string{"virtualmachines"}, []string{"get", "list", "watch", "update"}), | ||
newPolicyRule([]string{"authentication.k8s.io"}, []string{"tokenreviews"}, []string{"create"}), | ||
newPolicyRule([]string{"authorization.k8s.io"}, []string{"subjectaccessreviews"}, []string{"create"}), | ||
newPolicyRule([]string{"apps"}, []string{"deployments"}, []string{"get", "create", "update"}), |
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.
same with appsv1 "k8s.io/api/apps/v1"
newPolicyRule([]string{"k8s.cni.cncf.io"}, []string{"ipamclaims"}, []string{"get", "list", "watch", "create", "update"}), | ||
newPolicyRule([]string{"k8s.cni.cncf.io"}, []string{"network-attachment-definitions"}, []string{"get", "list", "watch", "create", "delete", "update"}), |
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.
same with cnicncf "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io"
What this PR does / why we need it:
currently sonarCloud reports a lot of code is duplicate on pkg/components (37.6%).
Refactoring the code to reduce duplicate to 0% and make thing more clear (all policies are visible in one screen, no endless scrolling).
Special notes for your reviewer:
Release note: