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

[credential provider] Add a flag mirrorMapping #6846

Conversation

lzhecheng
Copy link
Contributor

What type of PR is this?

/kind feature

What this PR does / why we need it:

[credential provider] Add a flag mirrorMapping. This flag is to mirror registry A to B when fetching credential.

Which issue(s) this PR fixes:

Fixes #6845

Special notes for your reviewer:

Does this PR introduce a user-facing change?

[credential provider] Add a flag mirrorMapping. This flag is to mirror registry A to B when fetching credential.

Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:


@k8s-ci-robot k8s-ci-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. kind/feature Categorizes issue or PR as related to a new feature. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Aug 19, 2024
@k8s-ci-robot k8s-ci-robot added approved Indicates a PR has been approved by an approver from all required OWNERS files. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Aug 19, 2024
return image, ""
}

// processMirrorMapping input format: "--mirror-mapping=aaa:bbb,ccc:ddd"

Choose a reason for hiding this comment

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

how to handle "aaa:bbb,aaa:ccc"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Current logic takes aaa:ccc

Copy link
Member

Choose a reason for hiding this comment

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

so this PR only supports one mirror mapping?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, it supports multiple.
Bin's question is how to handle two mappings with the same source registry. The logic is that the latter one takes effect.

@lzhecheng lzhecheng force-pushed the credential-provider-mirror-mapping branch from 8c0a9e2 to 3d563f5 Compare August 28, 2024 08:50
Copy link
Member

@andyzhangx andyzhangx left a comment

Choose a reason for hiding this comment

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

pls also add detailed description in the PR for mirror-mapping=mcr.microsoft.com:xxx.azurecr.io mapping

@@ -60,6 +63,9 @@ func main() {
logs.InitLogs()
defer logs.FlushLogs()

// Flags
command.Flags().StringVarP(&mirrorMappingStr, "mirror-mapping", "m", "", "mirror mapping to use")
Copy link
Member

Choose a reason for hiding this comment

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

this flag value is defined after it's used in L50, then how could external user use defined flag to override this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think as long as it is before command.Execute() then it is ok.

Please check the CCM code. You can see cmd is defined first, then flags.

cmd := &cobra.Command{
Use: "cloud-controller-manager",
Long: `The Cloud controller manager is a daemon that embeds the cloud specific control loops shipped with Kubernetes.`,
Run: func(cmd *cobra.Command, _ []string) {
verflag.PrintAndExitIfRequested("Cloud Provider Azure")
cliflag.PrintFlags(cmd.Flags())
c, err := s.Config(KnownControllers(), ControllersDisabledByDefault.List(), controllerAliases)
if err != nil {
klog.Errorf("Run: failed to configure cloud controller manager: %v", err)
os.Exit(1)
}
healthHandler, err := StartHTTPServer(c.Complete(), wait.NeverStop)
if err != nil {
klog.Errorf("Run: railed to start HTTP server: %v", err)
os.Exit(1)
}
if c.ComponentConfig.Generic.LeaderElection.LeaderElect {
// Identity used to distinguish between multiple cloud controller manager instances
id, err := os.Hostname()
if err != nil {
klog.Errorf("Run: failed to get host name: %v", err)
os.Exit(1)
}
// add a uniquifier so that two processes on the same host don't accidentally both become active
id = id + "_" + string(uuid.NewUUID())
// Lock required for leader election
rl, err := resourcelock.NewFromKubeconfig(c.ComponentConfig.Generic.LeaderElection.ResourceLock,
c.ComponentConfig.Generic.LeaderElection.ResourceNamespace,
c.ComponentConfig.Generic.LeaderElection.ResourceName,
resourcelock.ResourceLockConfig{
Identity: id,
EventRecorder: c.EventRecorder,
},
c.Kubeconfig,
c.ComponentConfig.Generic.LeaderElection.RenewDeadline.Duration)
if err != nil {
klog.Fatalf("error creating lock: %v", err)
}
// Try and become the leader and start cloud controller manager loops
var electionChecker *leaderelection.HealthzAdaptor
if c.ComponentConfig.Generic.LeaderElection.LeaderElect {
electionChecker = leaderelection.NewLeaderHealthzAdaptor(time.Second * 20)
}
leaderelection.RunOrDie(context.TODO(), leaderelection.LeaderElectionConfig{
Lock: rl,
LeaseDuration: c.ComponentConfig.Generic.LeaderElection.LeaseDuration.Duration,
RenewDeadline: c.ComponentConfig.Generic.LeaderElection.RenewDeadline.Duration,
RetryPeriod: c.ComponentConfig.Generic.LeaderElection.RetryPeriod.Duration,
Callbacks: leaderelection.LeaderCallbacks{
OnStartedLeading: RunWrapper(s, c, healthHandler),
OnStoppedLeading: func() {
klog.ErrorS(nil, "leaderelection lost")
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
},
},
WatchDog: electionChecker,
Name: "cloud-controller-manager",
})
panic("unreachable")
}
RunWrapper(s, c, healthHandler)(context.TODO())
panic("unreachable")
},
}
fs := cmd.Flags()
namedFlagSets := s.Flags(KnownControllers(), ControllersDisabledByDefault.List())
verflag.AddFlags(namedFlagSets.FlagSet("global"))
globalflag.AddGlobalFlags(namedFlagSets.FlagSet("global"), cmd.Name())
for _, f := range namedFlagSets.FlagSets {
fs.AddFlagSet(f)
}

return image, ""
}

// processMirrorMapping input format: "--mirror-mapping=aaa:bbb,ccc:ddd"
Copy link
Member

Choose a reason for hiding this comment

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

so this PR only supports one mirror mapping?

Username: username,
Password: password,
}
if sourceloginServer != "" {
response.Auth[sourceloginServer] = v1.AuthConfig{
Copy link
Member

Choose a reason for hiding this comment

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

you will add auth for both sourceloginServer and targetloginServer, is that necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. I think it does no harm, right.

Copy link
Member

Choose a reason for hiding this comment

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

then you will use password pull the mcr image, not sure whether that has side effect, in this case, only targetloginServer needs password?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When mirror mapping is used, in this case, sourceLoginServer is like MCR and targetloginServer is like ACR. So, it is the sourceloginServer needs password.

Copy link
Contributor

Choose a reason for hiding this comment

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

mcr + acr auth is a MUST, additional auth should bring no hard.
kubelet will send mcr image + acr auth to containerd to pull mcr image, and containerd will mirror the mcr image to acr through registry host, and finally pull mcr image from acr server.

@lzhecheng lzhecheng force-pushed the credential-provider-mirror-mapping branch from 3d563f5 to 72952fe Compare August 30, 2024 10:43
Username: username,
Password: password,
}
if sourceloginServer != "" {
response.Auth[sourceloginServer] = v1.AuthConfig{
Copy link
Contributor

Choose a reason for hiding this comment

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

mcr + acr auth is a MUST, additional auth should bring no hard.
kubelet will send mcr image + acr auth to containerd to pull mcr image, and containerd will mirror the mcr image to acr through registry host, and finally pull mcr image from acr server.


// processMirrorMapping input format: "--mirror-mapping=aaa:bbb,ccc:ddd"
// output format: map[string]string{"aaa": "bbb", "ccc": "ddd"}
func processMirrorMapping(mirrorMappingStr string) map[string]string {
Copy link
Contributor

@mainred mainred Sep 3, 2024

Choose a reason for hiding this comment

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

nit, Maybe parseMirrorMapping to be specific, and to not conflict with other processXXX func.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@@ -60,6 +63,9 @@ func main() {
logs.InitLogs()
defer logs.FlushLogs()

// Flags
command.Flags().StringVarP(&mirrorMappingStr, "mirror-mapping", "m", "", "mirror mapping to use")
Copy link
Contributor

Choose a reason for hiding this comment

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

Please let's elaborate the flags. I did not see the detailed description of this flag anywhere else.

Suggested change
command.Flags().StringVarP(&mirrorMappingStr, "mirror-mapping", "m", "", "mirror mapping to use")
command.Flags().StringVarP(&mirrorMappingStr, "mirror-mapping", "m", "", "mirror mapping to mirror a source registry host to a target registry host, and image pull credentail will be requested to the target registry host when the image is from source registry host")

Copy link
Contributor

Choose a reason for hiding this comment

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

The name may be registry-mirror, since mirror is also the term used in containerd host namespace feature https://github.com/containerd/containerd/blob/main/docs/hosts.md#registry-host-namespace

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@lzhecheng lzhecheng force-pushed the credential-provider-mirror-mapping branch from 72952fe to ec05532 Compare September 9, 2024 01:51
With registry mirror, e.g. --registry-mirror=mcr.microsoft.com:xxx.azurecr.io
credential provider will return credentials of xxx.azurecr.io for
mcr.microsoft.com. In this way, an image with URL prefix mcr.microsoft.com,
but actually in xxx.azurecr.io, can be successfully pulled.

Signed-off-by: Zhecheng Li <zhechengli@microsoft.com>
@lzhecheng lzhecheng force-pushed the credential-provider-mirror-mapping branch from ec05532 to b12bb67 Compare September 9, 2024 01:54
@coveralls
Copy link

coveralls commented Sep 9, 2024

Coverage Status

coverage: 77.247% (+0.01%) from 77.233%
when pulling b12bb67 on lzhecheng:credential-provider-mirror-mapping
into 6d2a767 on kubernetes-sigs:master.

@lzhecheng
Copy link
Contributor Author

Verified locally. SP and MSI work.

Copy link
Member

@andyzhangx andyzhangx left a comment

Choose a reason for hiding this comment

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

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Sep 10, 2024
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: andyzhangx, lzhecheng

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:
  • OWNERS [andyzhangx,lzhecheng]

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot merged commit 220c71c into kubernetes-sigs:master Sep 10, 2024
18 checks passed
@lzhecheng lzhecheng deleted the credential-provider-mirror-mapping branch September 10, 2024 13:18
@lzhecheng
Copy link
Contributor Author

/cherrypick release-1.31

@lzhecheng
Copy link
Contributor Author

/cherrypick release-1.30

@k8s-infra-cherrypick-robot

@lzhecheng: new pull request created: #7335

In response to this:

/cherrypick release-1.31

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.

@k8s-infra-cherrypick-robot

@lzhecheng: new pull request created: #7336

In response to this:

/cherrypick release-1.30

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/feature Categorizes issue or PR as related to a new feature. lgtm "Looks good to me", indicates that a PR is ready to be merged. release-note Denotes a PR that will be considered when it comes time to generate release notes. 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.

[Feat][Credential Provider] Registry mirror mapping
7 participants