-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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(nodeadm): use ecr-credential-provider for public.ecr.aws in 1.27+ #2153
base: main
Are you sure you want to change the base?
Changes from all commits
eaaec85
fffb20e
d14a8f9
593aa16
344121d
aeb233c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,12 +7,18 @@ import ( | |
"os" | ||
"path" | ||
"path/filepath" | ||
"text/template" | ||
"time" | ||
|
||
"github.com/awslabs/amazon-eks-ami/nodeadm/internal/api" | ||
"github.com/awslabs/amazon-eks-ami/nodeadm/internal/util" | ||
"go.uber.org/zap" | ||
"golang.org/x/mod/semver" | ||
k8smetav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
k8sruntime "k8s.io/apimachinery/pkg/runtime" | ||
k8sjson "k8s.io/apimachinery/pkg/runtime/serializer/json" | ||
k8sconfigv1 "k8s.io/kubelet/config/v1" | ||
k8sconfigv1alpha1 "k8s.io/kubelet/config/v1alpha1" | ||
"sigs.k8s.io/controller-runtime/pkg/client/apiutil" | ||
) | ||
|
||
const ( | ||
|
@@ -25,12 +31,7 @@ const ( | |
ecrCredentialProviderBinPathEnvironmentName = "ECR_CREDENTIAL_PROVIDER_BIN_PATH" | ||
) | ||
|
||
var ( | ||
//go:embed image-credential-provider.template.json | ||
imageCredentialProviderTemplateData string | ||
imageCredentialProviderTemplate = template.Must(template.New("image-credential-provider").Parse(imageCredentialProviderTemplateData)) | ||
imageCredentialProviderConfigPath = path.Join(imageCredentialProviderRoot, imageCredentialProviderConfig) | ||
) | ||
var imageCredentialProviderConfigPath = path.Join(imageCredentialProviderRoot, imageCredentialProviderConfig) | ||
|
||
func (k *kubelet) writeImageCredentialProviderConfig(cfg *api.NodeConfig) error { | ||
// fallback default for image credential provider binary if not overridden | ||
|
@@ -43,7 +44,7 @@ func (k *kubelet) writeImageCredentialProviderConfig(cfg *api.NodeConfig) error | |
return err | ||
} | ||
|
||
config, err := generateImageCredentialProviderConfig(cfg, ecrCredentialProviderBinPath) | ||
config, err := generateImageCredentialProviderConfig(ecrCredentialProviderBinPath) | ||
if err != nil { | ||
return err | ||
} | ||
|
@@ -54,29 +55,68 @@ func (k *kubelet) writeImageCredentialProviderConfig(cfg *api.NodeConfig) error | |
return util.WriteFileWithDir(imageCredentialProviderConfigPath, config, imageCredentialProviderPerm) | ||
} | ||
|
||
type imageCredentialProviderTemplateVars struct { | ||
ConfigApiVersion string | ||
ProviderApiVersion string | ||
EcrProviderName string | ||
} | ||
|
||
func generateImageCredentialProviderConfig(cfg *api.NodeConfig, ecrCredentialProviderBinPath string) ([]byte, error) { | ||
templateVars := imageCredentialProviderTemplateVars{ | ||
EcrProviderName: filepath.Base(ecrCredentialProviderBinPath), | ||
func generateImageCredentialProviderConfig(ecrCredentialProviderBinPath string) ([]byte, error) { | ||
scheme := k8sruntime.NewScheme() | ||
providerName := filepath.Base(ecrCredentialProviderBinPath) | ||
defaultCacheDuration := &k8smetav1.Duration{Duration: 12 * time.Hour} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be a package-level constant, I think? |
||
matchImages := []string{ | ||
"*.dkr.ecr.*.amazonaws.com", | ||
"*.dkr-ecr.*.on.aws", | ||
"*.dkr.ecr.*.amazonaws.com.cn", | ||
"*.dkr-ecr.*.on.amazonwebservices.com.cn", | ||
"*.dkr.ecr-fips.*.amazonaws.com", | ||
"*.dkr-ecr-fips.*.on.aws", | ||
"*.dkr.ecr.*.c2s.ic.gov", | ||
"*.dkr.ecr.*.sc2s.sgov.gov", | ||
"*.dkr.ecr.*.cloud.adc-e.uk", | ||
"*.dkr.ecr.*.csp.hci.ic.gov", | ||
} | ||
kubeletVersion, err := GetKubeletVersion() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is now available in |
||
if err != nil { | ||
return nil, err | ||
} | ||
var cfg k8sruntime.Object | ||
if semver.Compare(kubeletVersion, "v1.27.0") < 0 { | ||
templateVars.ConfigApiVersion = "kubelet.config.k8s.io/v1alpha1" | ||
templateVars.ProviderApiVersion = "credentialprovider.kubelet.k8s.io/v1alpha1" | ||
err = k8sconfigv1alpha1.AddToScheme(scheme) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you should move the |
||
if err != nil { | ||
return nil, err | ||
} | ||
cfg = &k8sconfigv1alpha1.CredentialProviderConfig{ | ||
Providers: []k8sconfigv1alpha1.CredentialProvider{ | ||
{ | ||
Name: providerName, | ||
MatchImages: matchImages, | ||
DefaultCacheDuration: defaultCacheDuration, | ||
APIVersion: "credentialprovider.kubelet.k8s.io/v1alpha1", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be set by the scheme encoder |
||
}, | ||
}, | ||
} | ||
} else { | ||
templateVars.ConfigApiVersion = "kubelet.config.k8s.io/v1" | ||
templateVars.ProviderApiVersion = "credentialprovider.kubelet.k8s.io/v1" | ||
err = k8sconfigv1.AddToScheme(scheme) | ||
if err != nil { | ||
return nil, err | ||
} | ||
cfg = &k8sconfigv1.CredentialProviderConfig{ | ||
Providers: []k8sconfigv1.CredentialProvider{ | ||
{ | ||
Name: providerName, | ||
MatchImages: append(matchImages, "public.ecr.aws"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need a comment here explaining why this is only for v1/1.27+ |
||
DefaultCacheDuration: defaultCacheDuration, | ||
APIVersion: "credentialprovider.kubelet.k8s.io/v1", | ||
}, | ||
}, | ||
} | ||
} | ||
gvk, err := apiutil.GVKForObject(cfg, scheme) | ||
if err != nil { | ||
return nil, err | ||
} | ||
cfg.GetObjectKind().SetGroupVersionKind(gvk) | ||
Comment on lines
+110
to
+114
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this looks like a no-op, what's it for? |
||
serializer := k8sjson.NewSerializerWithOptions(k8sjson.DefaultMetaFactory, scheme, scheme, k8sjson.SerializerOptions{Pretty: true}) | ||
|
||
var buf bytes.Buffer | ||
if err := imageCredentialProviderTemplate.Execute(&buf, templateVars); err != nil { | ||
err = serializer.Encode(cfg, &buf) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return buf.Bytes(), nil | ||
|
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can also remove the force import of
embed
, I think?