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

feat: add registry namespace rewrite policy #2494

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

jojotong
Copy link

@jojotong jojotong commented Feb 13, 2025

What type of PR is this?

/kind feature

What this PR does / why we need it:

Feat: add registry namespace rewrite policy, eg:

  registry:
    # define a policy to modify image namespace, the policy below will be like:
    # namespace1 -> library
    # kubesphere -> library/kubesphere
    namespaceRewrite:
      policy: changePrefix
      src: 
        - namespace1
      dest: library

Which issue(s) this PR fixes:

Fixes #

Special notes for reviewers:

Does this PR introduced a user-facing change?


Additional documentation, usage docs, etc.:


Copy link

Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it.

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.

@kubesphere-prow kubesphere-prow bot added kind/feature Categorizes issue or PR as related to a new feature. do-not-merge/release-note-label-needed labels Feb 13, 2025
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: jojotong
Once this PR has been reviewed and has the lgtm label, please assign 24sama for approval. 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

@kubesphere-prow kubesphere-prow bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Feb 13, 2025
@jojotong jojotong force-pushed the feat_add_registry_namespace_rewrite_policy branch from f9cd188 to 05edfca Compare February 13, 2025 11:10
Copy link

This PR has multiple commits, and the default merge method is: squash.
You can request commits to be merged using the label: tide/merge-method-merge

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.

@jojotong jojotong force-pushed the feat_add_registry_namespace_rewrite_policy branch from 0d76dbb to 5691686 Compare February 17, 2025 09:04
@zheng1
Copy link
Member

zheng1 commented Feb 19, 2025

@CodiumAI-Agent /review

@CodiumAI-Agent
Copy link

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 4 🔵🔵🔵🔵⚪
🧪 No relevant tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Possible Issue

The NamespaceRewrite struct and its associated logic introduce a new feature. Ensure that all edge cases, such as empty src or invalid policy values, are handled gracefully to avoid runtime errors.

	NamespaceRewrite   *NamespaceRewrite    `yaml:"namespaceRewrite" json:"namespaceRewrite"`
}

// NamespaceRewritePolicy define namespaceRewrite policy
type NamespaceRewritePolicy string

const (
	// ChangePrefix change image namespace prefix
	ChangePrefix NamespaceRewritePolicy = "changePrefix"
)

// NamespaceRewrite define a policy to modify image namespace
type NamespaceRewrite struct {
	Policy NamespaceRewritePolicy `yaml:"policy" json:"policy"`
	Src    []string               `yaml:"src" json:"src"`
	Dest   string                 `yaml:"dest" json:"dest"`
Logic Validation

The logic for modifying namespaces in the ImageRepo function should be reviewed for correctness, especially the handling of matchSrc and the replacement logic. Ensure it behaves as expected for all possible inputs.

if image.NamespaceRewrite != nil {
	switch image.NamespaceRewrite.Policy {
	case v1alpha2.ChangePrefix:
		matchSrc := ""
		for _, src := range image.NamespaceRewrite.Src {
			if strings.Contains(image.Namespace, src) {
				matchSrc = src
			}
		}
		modifiedNamespace := ""
		if matchSrc == "" {
			// if not match, add dest prefix
			modifiedNamespace = fmt.Sprintf("%s/%s", image.NamespaceRewrite.Dest, image.Namespace)
		} else {
			// if match, change it
			modifiedNamespace = strings.ReplaceAll(image.Namespace, matchSrc, image.NamespaceRewrite.Dest)
		}
		logger.Log.Debugf("changed image namespace: %s -> %s", image.Namespace, modifiedNamespace)
		image.Namespace = modifiedNamespace
	default:
		logger.Log.Warn("namespace rewrite action not specified")
	}
Documentation Accuracy

The example configuration for namespaceRewrite should be validated to ensure it aligns with the implemented functionality and provides clear guidance to users.

# define a policy to modify image namespace, the policy below will be like:
# namespace1 -> library
# kubesphere -> library/kubesphere
namespaceRewrite:
  policy: changePrefix
  src: 
    - namespace1
  dest: library

@zheng1
Copy link
Member

zheng1 commented Feb 19, 2025

@CodiumAI-Agent /improve

@CodiumAI-Agent
Copy link

PR Code Suggestions ✨

CategorySuggestion                                                                                                                                    Impact
Possible issue
Validate Dest field in namespace rewrite

Add a check to ensure that image.NamespaceRewrite.Dest is not empty before using it
to construct modifiedNamespace, to avoid potential runtime errors or unintended
behavior.

cmd/kk/pkg/images/images.go [112]

-modifiedNamespace = fmt.Sprintf("%s/%s", image.NamespaceRewrite.Dest, image.Namespace)
+if image.NamespaceRewrite.Dest == "" {
+    logger.Log.Warn("namespace rewrite destination is empty")
+} else {
+    modifiedNamespace = fmt.Sprintf("%s/%s", image.NamespaceRewrite.Dest, image.Namespace)
+}
Suggestion importance[1-10]: 8

__

Why: Adding a check for an empty Dest field in NamespaceRewrite prevents potential runtime errors or unintended behavior when constructing modifiedNamespace. This is a critical safeguard for ensuring robust functionality.

Medium
General
Handle multiple matches in Src

Ensure that the matchSrc variable is properly handled when multiple matches are
found in image.NamespaceRewrite.Src, as the current implementation only considers
the last match.

cmd/kk/pkg/images/images.go [104-107]

 for _, src := range image.NamespaceRewrite.Src {
     if strings.Contains(image.Namespace, src) {
         matchSrc = src
+        break // Stop at the first match
     }
 }
Suggestion importance[1-10]: 7

__

Why: Breaking the loop upon finding the first match in Src ensures that the logic is deterministic and avoids unintended behavior when multiple matches occur. This improves the clarity and reliability of the code.

Medium
Handle unknown rewrite policies

Log a warning or error when image.NamespaceRewrite.Policy is not recognized, instead
of silently proceeding with no action.

cmd/kk/pkg/images/images.go [120]

-logger.Log.Warn("namespace rewrite action not specified")
+logger.Log.Errorf("unknown namespace rewrite policy: %s", image.NamespaceRewrite.Policy)
Suggestion importance[1-10]: 6

__

Why: Logging an error for unknown namespace rewrite policies provides better feedback for debugging and ensures that unexpected configurations are flagged. This enhances the maintainability and usability of the code.

Low

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
do-not-merge/release-note-label-needed kind/feature Categorizes issue or PR as related to a new feature. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants