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

pkg/components: Refactor to reduce duplicate code #1845

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

RamLavi
Copy link
Collaborator

@RamLavi RamLavi commented Aug 6, 2024

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:

NONE

@kubevirt-bot kubevirt-bot added the release-note-none Denotes a PR that doesn't merit a release note. label Aug 6, 2024
@kubevirt-bot
Copy link
Collaborator

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please ask for approval from ramlavi. For more information see the Kubernetes Code Review Process.

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 /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@kubevirt-bot kubevirt-bot added the dco-signoff: yes Indicates the PR's author has DCO signed all their commits. label Aug 6, 2024
@RamLavi
Copy link
Collaborator Author

RamLavi commented Aug 6, 2024

@oshoval @phoracek PTAL

var rules []rbacv1.PolicyRule

rules = append(rules,
newPolicyRule([]string{""}, []string{"events"}, []string{"update"}),
Copy link
Collaborator

@oshoval oshoval Aug 6, 2024

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"}
),

Copy link
Collaborator

@oshoval oshoval Aug 6, 2024

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

Copy link
Collaborator Author

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?

Copy link
Collaborator

@oshoval oshoval Aug 6, 2024

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

@oshoval
Copy link
Collaborator

oshoval commented Aug 6, 2024

beside cosmetics there are no changes right ?

@RamLavi
Copy link
Collaborator Author

RamLavi commented Aug 6, 2024

beside cosmetics there are no changes right ?

It's only a refactor. No logic changed.

@kubevirt-bot kubevirt-bot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Aug 8, 2024
Comment on lines 361 to 375
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"}),
)
Copy link
Contributor

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.

Suggested change
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"}),
}

}
return role
}

func GetClusterRole(allowMultus bool) *rbacv1.ClusterRole {
var rules []rbacv1.PolicyRule
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

Comment on lines 479 to 439
var rules []rbacv1.PolicyRule

rules = append(rules,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

@kubevirt-bot kubevirt-bot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Aug 13, 2024
@RamLavi
Copy link
Collaborator Author

RamLavi commented Aug 13, 2024

Change: Rebase + removed @oshoval 's suggestion as it doesn't fix duplicate code issue

@RamLavi
Copy link
Collaborator Author

RamLavi commented Aug 13, 2024

Change: Fix for @nunnatsa review

@RamLavi
Copy link
Collaborator Author

RamLavi commented Aug 13, 2024

/hold
I want to manually check that no rbac was left out

@kubevirt-bot kubevirt-bot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Aug 13, 2024
@oshoval
Copy link
Collaborator

oshoval commented Aug 13, 2024

/hold I want to manually check that no rbac was left out

if any are left out, tests should find it, or we are missing tests, or those arent needed anymore
so if you find any, worth to check which of those reasons is the right one please

@oshoval
Copy link
Collaborator

oshoval commented Aug 13, 2024

you didnt rebase correctly, please check it
those seem to be missed, it will fail on OpenShift, but not on U/S (well it will also fail here as part of the deployment)
"virtualmachines/finalizers",
"virtualmachineinstances/finalizers",

Those need to be included please:
25b584e

@RamLavi
Copy link
Collaborator Author

RamLavi commented Aug 13, 2024

virtualmachines/finalizers

thanks!

This is done in order to eliminate sonarCloud issue.

Signed-off-by: Ram Lavi <[email protected]>
Copy link

sonarcloud bot commented Aug 13, 2024

@kubevirt-bot
Copy link
Collaborator

@RamLavi: The following tests failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
pull-e2e-cnao-multus-dynamic-networks-functests cc6fe21 link true /test pull-e2e-cnao-multus-dynamic-networks-functests
pull-e2e-cnao-kube-secondary-dns-functests cc6fe21 link true /test pull-e2e-cnao-kube-secondary-dns-functests
pull-e2e-cluster-network-addons-operator-multus-functests cc6fe21 link true /test pull-e2e-cluster-network-addons-operator-multus-functests
pull-e2e-cluster-network-addons-operator-ovs-cni-functests cc6fe21 link true /test pull-e2e-cluster-network-addons-operator-ovs-cni-functests
pull-e2e-cluster-network-addons-operator-br-marker-functests cc6fe21 link true /test pull-e2e-cluster-network-addons-operator-br-marker-functests
pull-e2e-cluster-network-addons-operator-kubemacpool-functests cc6fe21 link true /test pull-e2e-cluster-network-addons-operator-kubemacpool-functests
pull-e2e-cluster-network-addons-operator-macvtap-cni-functests cc6fe21 link true /test pull-e2e-cluster-network-addons-operator-macvtap-cni-functests
pull-e2e-cluster-network-addons-operator-monitoring-k8s cc6fe21 link true /test pull-e2e-cluster-network-addons-operator-monitoring-k8s
pull-e2e-cluster-network-addons-operator-workflow-k8s cc6fe21 link true /test pull-e2e-cluster-network-addons-operator-workflow-k8s
pull-e2e-cluster-network-addons-operator-lifecycle-k8s cc6fe21 link true /test pull-e2e-cluster-network-addons-operator-lifecycle-k8s

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dco-signoff: yes Indicates the PR's author has DCO signed all their commits. do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. release-note-none Denotes a PR that doesn't merit a release note. size/XL
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants