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: replace image registry dynamically #8018

Open
wants to merge 25 commits into
base: main
Choose a base branch
from

Conversation

cjc7373
Copy link
Contributor

@cjc7373 cjc7373 commented Aug 23, 2024

When does image replcaement happen

Bascially, replacement will happen when workload resources (like pod, job, deployment, statefulset) are created.

Specifically, the replacement will inject to:

  • instanceset controller, so that it affects:

    • new cluster creation
    • cluster upgrade

    But it won't affect:

    • cluster scaling
    • instance template
  • addon controller, so that it affects helm install jobs

  • backup/restore controller

Replace Configs

sample config:

registries:
  defaultRegistry: foo.bar
  defaultNamespace: default
  registriyConfig:
  - from: docker.io
    to: docker-mirror.io
  - from: k8s.io
    to: company-registry.io
    registryDefaultNamespace: k8sio
    namespaces:
    - from: special-ns
      to: my-special-ns

The replacement rules are as follows:

  • If there's no specific registry config (registriyConfig field), or registry does not match any specific registry config, defaultRegistry and defaultNamespace will be used.
    • If defaultNamespace is not defined, namespace remains unchanged.
    • If defaultRegistry is not defined, registry remains unchanged.
  • If image's registry matches a specific registry config:
    • from and to should not be empty. Registry to will be used.
    • If there's no namespace config, or namespace does not match any specific namespace config, registryDefaultNamespace will be used. If registryDefaultNamespace is not defined, namespace remains unchanged.
    • If namespace matches a specific namespace config, namespace to will be used.
      • from and to of the namespace may be empty (empty namespace is legal)

Other things to note

One side effect of this change is that omitted image name (e.g. busybox) will be expanded to full format (e.g. docker.io/library/busybox). IMO this will not have any user facing change.

@github-actions github-actions bot added the size/L Denotes a PR that changes 100-499 lines. label Aug 23, 2024
Copy link

codecov bot commented Aug 23, 2024

Codecov Report

Attention: Patch coverage is 77.04918% with 28 lines in your changes missing coverage. Please review.

Project coverage is 60.38%. Comparing base (a5bcd50) to head (c74f60d).
Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
pkg/controllerutil/image_util.go 74.74% 24 Missing and 1 partial ⚠️
pkg/dataprotection/backup/deleter.go 0.00% 2 Missing ⚠️
pkg/dataprotection/utils/backuprepo.go 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8018      +/-   ##
==========================================
+ Coverage   60.28%   60.38%   +0.10%     
==========================================
  Files         376      377       +1     
  Lines       45858    45965     +107     
==========================================
+ Hits        27645    27756     +111     
+ Misses      15614    15612       -2     
+ Partials     2599     2597       -2     
Flag Coverage Δ
unittests 60.38% <77.04%> (+0.10%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@cjc7373 cjc7373 marked this pull request as ready for review August 26, 2024 03:00
}

if dstNamespace == "" {
dstNamespace = namespace
Copy link
Contributor

Choose a reason for hiding this comment

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

so if namespace is not found, the original namespace rather than the defaultNamespace is used.

Copy link
Contributor

@zjx20 zjx20 Sep 6, 2024

Choose a reason for hiding this comment

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

Maybe add a DefaultNamespace field to the RegistryConfig struct:

type RegistryConfig struct {
	From       string
	To         string
	DefaultNamespace string
	Namespaces []RegistryNamespaceConfig
}

When the namespace is not found in Namespaces, use the DefaultNamespace if it's not empty, otherwise use the original namespace.

Copy link
Contributor Author

@cjc7373 cjc7373 Sep 9, 2024

Choose a reason for hiding this comment

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

I think simply use the global defaultNamespace is enough.

It does seem better.

@cjc7373 cjc7373 requested review from leon-inf and a team as code owners September 9, 2024 09:19
@github-actions github-actions bot added size/XL Denotes a PR that changes 500-999 lines. and removed size/L Denotes a PR that changes 100-499 lines. labels Sep 9, 2024
pkg/controllerutil/image_util.go Show resolved Hide resolved
pkg/controllerutil/image_util.go Outdated Show resolved Hide resolved
pkg/controllerutil/image_util.go Outdated Show resolved Hide resolved
pkg/controllerutil/image_util.go Outdated Show resolved Hide resolved
@github-actions github-actions bot added size/L Denotes a PR that changes 100-499 lines. and removed size/XL Denotes a PR that changes 500-999 lines. labels Sep 10, 2024
@github-actions github-actions bot added size/XL Denotes a PR that changes 500-999 lines. and removed size/L Denotes a PR that changes 100-499 lines. labels Sep 29, 2024
cmd/manager/main.go Outdated Show resolved Hide resolved
@apecloud-bot apecloud-bot added the approved PR Approved Test label Oct 11, 2024
@zjx20
Copy link
Contributor

zjx20 commented Nov 29, 2024

@cjc7373 Please resolve conflicts and cover the case at #8539 .

// or they won't be matched. Note empty namespace is legal too.
//
// key is the orignal namespace, value is the new namespace
NamespaceMapping map[string]string `mapstructure:"namespaceMapping"`
Copy link
Contributor

Choose a reason for hiding this comment

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

NamespaceMapping -> Namespaces

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This name was suggested by @zjx20. I think it's more accurate.


var imageLogger = log.Log.WithName("ImageUtil")

type RegistryConfig struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

RegistryConfig -> registryConfig?

NamespaceMapping map[string]string `mapstructure:"namespaceMapping"`
}

type RegistriesConfig struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

RegistriesConfig -> registriesConfig?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The config will be used in a test outside this package, so it needs to be exported.

@apecloud-bot apecloud-bot removed the approved PR Approved Test label Dec 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size/XL Denotes a PR that changes 500-999 lines.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants