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

fix: unable to pull images from private ECR #67

Merged
merged 1 commit into from
Jul 10, 2023
Merged

fix: unable to pull images from private ECR #67

merged 1 commit into from
Jul 10, 2023

Conversation

vadasambar
Copy link
Contributor

@vadasambar vadasambar commented Jul 3, 2023

  • this started happening since v0.6.0 release of warm metal driver

Fixes #64

- this started happening since v0.6.0 release of warm metal driver
@@ -127,7 +127,7 @@ func (s keyringStore) GetDockerKeyring(ctx context.Context, secretData map[strin
return credentialprovider.UnionDockerKeyring{preferredKeyring, daemonKeyring}, nil
}

return daemonKeyring, err
return credentialprovider.UnionDockerKeyring{daemonKeyring, credentialprovider.NewDockerKeyring()}, err
Copy link
Contributor Author

Choose a reason for hiding this comment

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

NewDockerKeyring enables authentication with cloudproviders like AWS, Azure and GCP. This was present in v0.5.1 but was removed in v0.6.0 (maybe we missed adding NewDockerKeyring in v0.6.0?).

You can check the diff between v0.5.1 and v0.6.0 here: v0.5.1...v0.6.0#diff-ded1b59e8d55d0b1494a34a4d36b7d3711e552252fcca2a223177373ef8f0f5eL48 (search for NewDockerKeyring)

Copy link
Contributor Author

@vadasambar vadasambar Jul 3, 2023

Choose a reason for hiding this comment

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

NewDockerKeyring is important because it creates providersDockerKeyring

// NewDockerKeyring creates a DockerKeyring to use for resolving credentials,
// which draws from the set of registered credential providers.
func NewDockerKeyring() DockerKeyring {
	keyring := &providersDockerKeyring{
		Providers: make([]DockerConfigProvider, 0),
	}

https://github.com/kubernetes/kubernetes/blob/5835544ca568b757a8ecae5c153f317e5736700e/pkg/credentialprovider/plugins.go#L56-L61

providersDockerKeyring supports RegisterCredentialProvider (note that providers is a package/global variable)

// RegisterCredentialProvider is called by provider implementations on
// initialization to register themselves, like so:
//
//	func init() {
//	 	RegisterCredentialProvider("name", &myProvider{...})
//	}
func RegisterCredentialProvider(name string, provider DockerConfigProvider) {
	providersMutex.Lock()
	defer providersMutex.Unlock()
	_, found := providers[name]
	if found {
		klog.Fatalf("Credential provider %q was registered twice", name)
	}
	klog.V(4).Infof("Registered credential provider %q", name)
	providers[name] = provider
}

https://github.com/kubernetes/kubernetes/blob/5835544ca568b757a8ecae5c153f317e5736700e/pkg/credentialprovider/plugins.go#L33C1-L48C2

RegisterCredentialProvider is called by imports in secrets/cache.go

File: pkg/secret/cache.go
17: 	// register credential providers
18: 	_ "k8s.io/kubernetes/pkg/credentialprovider/aws"
19: 	_ "k8s.io/kubernetes/pkg/credentialprovider/azure"
20: 	_ "k8s.io/kubernetes/pkg/credentialprovider/gcp"

https://github.com/warm-metal/csi-driver-image/blob/6f689a83e23a51a790ffac26fcc0b15a559e6e00/pkg/secret/cache.go#L17-L20

If you check the code for initialisation of "k8s.io/kubernetes/pkg/credentialprovider/aws", it calls RegisterCredentialProvider in init()

// init registers a credential provider for each registryURLTemplate and creates
// an ECR token getter factory with a new cache to store token getters
func init() {
	credentialprovider.RegisterCredentialProvider("amazon-ecr",
		newECRProvider(&ecrTokenGetterFactory{cache: make(map[string]tokenGetter)},
			ec2ValidationImpl,
		))
}

https://github.com/kubernetes/kubernetes/blob/5835544ca568b757a8ecae5c153f317e5736700e/pkg/credentialprovider/aws/aws_credentials.go#L49C1-L56C2

During lookup, providersDockerKeyring's Lookup function is called (line 33)
https://github.com/warm-metal/csi-driver-image/blob/28f7e80fb73dc617b707741b719731febf02d13c/pkg/remoteimage/pull.go#L30-L34

providersDockerKeyring's Lookup function calls Provide function to get credentials from the AWS provider

File: kubernetes@v1.25.2/pkg/credentialprovider/keyring.go
263: // Lookup implements the DockerKeyring method for fetching credentials
264: // based on image name.
265: func (dk *providersDockerKeyring) Lookup(image string) ([]AuthConfig, bool) {
266: 	keyring := &BasicDockerKeyring{}
267: 
268: 	for _, p := range dk.Providers {
269: 		keyring.Add(p.Provide(image))
270: 	}
271: 
272: 	return keyring.Lookup(image)
273: }

https://github.com/kubernetes/kubernetes/blob/229dd79efde26caba43feb376d0e81e705d38220/pkg/credentialprovider/keyring.go#L265-L273

Basically, warm metal CSI driver is able to fetch images from private ECR registry because:
NewDockerRegistry -> uses providersDockerKeyring -> imports like k8s.io/kubernetes/pkg/credentialprovider/aws call providersDockerKeyring's RegisterCredentialProvider function to register -> warm metal calls providersDockerKeyring to get docker config -> providersDockerKeyring calls k8s.io/kubernetes/pkg/credentialprovider/aws package's Provide function which gets private ECR credentials from AWS

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, my explanation is complicated.

Copy link
Contributor Author

@vadasambar vadasambar Jul 4, 2023

Choose a reason for hiding this comment

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

@vadasambar vadasambar marked this pull request as ready for review July 3, 2023 18:11
@vadasambar vadasambar changed the title fix: (wip) unable to pull images from private ECR fix: unable to pull images from private ECR Jul 3, 2023
@vadasambar vadasambar changed the title fix: unable to pull images from private ECR fix: (wip) unable to pull images from private ECR Jul 3, 2023
@vadasambar
Copy link
Contributor Author

vadasambar commented Jul 3, 2023

I tried testing the code in this PR but the warm metal CSI driver pod goes in CrashLoopBackoff

$ kubectl logs -f csi-image-warm-metal-6vr4d -nkube-system  plugin
I0703 16:36:24.976470       1 driver.go:94] Enabling volume access mode: MULTI_NODE_READER_ONLY
I0703 16:36:24.976489       1 driver.go:82] Enabling controller service capability: CREATE_DELETE_VOLUME
F0703 16:36:24.976497       1 main.go:78] The mode of the driver is required. 

image

I think this is because of #57

I created a new branch (on my local machine) by cherry-picking the commit (from this PR) to v0.6.1 and tested it. It works.

@vadasambar vadasambar marked this pull request as draft July 3, 2023 18:16
@vadasambar
Copy link
Contributor Author

vadasambar commented Jul 4, 2023

master branch has a lot of changes since v0.6.0 and v0.6.1

@kitt1987 would it be possible to backport this change to v0.6.0 and v0.6.1 so that these releases have the bug fix as well. 🙏

cc: @mugdha-adhav

I have basically tested the PR with v0.6.1 and it works. We need to test it once with master.

@kitt1987
Copy link
Contributor

kitt1987 commented Jul 4, 2023

would it be possible to backport this change to v0.6.0 and v0.6.1 so that these releases have the bug fix as well.

I can cherry-pick this fix to v0.6.1, then file a new release v0.6.2. Does it work for you?

@kitt1987
Copy link
Contributor

kitt1987 commented Jul 4, 2023

I tried testing the code in this PR but the warm metal CSI driver pod goes in CrashLoopBackoff

$ kubectl logs -f csi-image-warm-metal-6vr4d -nkube-system  plugin
I0703 16:36:24.976470       1 driver.go:94] Enabling volume access mode: MULTI_NODE_READER_ONLY
I0703 16:36:24.976489       1 driver.go:82] Enabling controller service capability: CREATE_DELETE_VOLUME
F0703 16:36:24.976497       1 main.go:78] The mode of the driver is required. 

image

I think this is because of #57

The latest version introduced a new Deployment to support dynamic provisioning. It can only be installed through Helm currently. The install utility can be removed from the next release.

@mugdha-adhav
Copy link
Collaborator

would it be possible to backport this change to v0.6.0 and v0.6.1 so that these releases have the bug fix as well.

I can cherry-pick this fix to v0.6.1, then file a new release v0.6.2. Does it work for you?

Yep, that would be great.

Note: We are currently working on testing the changes on master branch. Once we are done, we'll send the PR out for review.

@vadasambar
Copy link
Contributor Author

would it be possible to backport this change to v0.6.0 and v0.6.1 so that these releases have the bug fix as well.

I can cherry-pick this fix to v0.6.1, then file a new release v0.6.2. Does it work for you?

Like @mugdha-adhav said, that works great! and thank you 🙏 ❤️ !

@kitt1987 kitt1987 marked this pull request as ready for review July 10, 2023 04:12
@kitt1987 kitt1987 changed the title fix: (wip) unable to pull images from private ECR fix: unable to pull images from private ECR Jul 10, 2023
@kitt1987 kitt1987 merged commit c0b13fc into warm-metal:master Jul 10, 2023
@mbtamuli mbtamuli mentioned this pull request Jan 15, 2024
5 tasks
mbtamuli added a commit to mbtamuli/container-image-csi-driver that referenced this pull request Jan 21, 2024
…ndys shit

Signed-off-by: Mriyam Tamuli <mbtamuli@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Pulling image failed with status code [manifests simple-fs]: 401 Unauthorized
3 participants