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 requested a review from phoracek August 6, 2024 11:45
@kubevirt-bot kubevirt-bot added the dco-signoff: yes Indicates the PR's author has DCO signed all their commits. label Aug 6, 2024
@kubevirt-bot kubevirt-bot requested a review from qinqon August 6, 2024 11:45
@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

@RamLavi RamLavi force-pushed the refactor_components branch from 5d31b26 to 6896c42 Compare August 13, 2024 09:35
@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 RamLavi force-pushed the refactor_components branch from 6896c42 to 968a49b Compare August 13, 2024 09:36
@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!

@RamLavi RamLavi force-pushed the refactor_components branch from 968a49b to cc6fe21 Compare August 13, 2024 10:40
Copy link

@kubevirt-bot
Copy link
Collaborator

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

/lifecycle stale

@kubevirt-bot kubevirt-bot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Nov 11, 2024
@kubevirt-bot
Copy link
Collaborator

Stale issues rot after 30d of inactivity.
Mark the issue as fresh with /remove-lifecycle rotten.
Rotten issues close after an additional 30d of inactivity.

If this issue is safe to close now please do so with /close.

/lifecycle rotten

@kubevirt-bot kubevirt-bot added lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed. and removed lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. labels Dec 11, 2024
This is done in order to eliminate sonarCloud issue.

Signed-off-by: Ram Lavi <[email protected]>
@RamLavi RamLavi force-pushed the refactor_components branch from cc6fe21 to 519dec3 Compare December 15, 2024 07:47
@RamLavi
Copy link
Collaborator Author

RamLavi commented Dec 15, 2024

Change: Rebase

@RamLavi
Copy link
Collaborator Author

RamLavi commented Dec 15, 2024

@RamLavi
Copy link
Collaborator Author

RamLavi commented Dec 15, 2024

/hold cancel

@kubevirt-bot kubevirt-bot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Dec 15, 2024
@RamLavi
Copy link
Collaborator Author

RamLavi commented Dec 15, 2024

/remove-lifecycle rotten

@kubevirt-bot kubevirt-bot removed the lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed. label Dec 15, 2024
@kubevirt-bot
Copy link
Collaborator

kubevirt-bot commented Dec 15, 2024

@RamLavi: The following test 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 519dec3 link true /test pull-e2e-cnao-multus-dynamic-networks-functests

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. 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