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

[Spike] Fix controller generating cilium manifests with registry mirror #7139

Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions pkg/dependencies/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
SnowValidator *snow.Validator
IPValidator *validator.IPValidator
UnAuthKubectlClient KubeClients
HelmFactory *HelmFactory
CreateClusterDefaulter cli.CreateClusterDefaulter
UpgradeClusterDefaulter cli.UpgradeClusterDefaulter
}
Expand Down Expand Up @@ -770,6 +771,24 @@
return f
}

func (f *Factory) WithHelmFactory(opts ...executables.HelmOpt) *Factory {

Check warning on line 774 in pkg/dependencies/factory.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported method Factory.WithHelmFactory should have comment or be unexported (revive)
f.WithExecutableBuilder()

f.buildSteps = append(f.buildSteps, func(ctx context.Context) error {
Copy link
Member

Choose a reason for hiding this comment

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

Overall, factory is not really a preferred abstract in golang. As we have adopted it, we should try not making it more complicated.

This method introduces something feels like "nested factory", which makes the abstract harder to understand. I believe we can achieve the target without this "helmFactory"

if f.dependencies.HelmFactory != nil {
return nil
}

f.dependencies.HelmFactory = NewHelmFactory(f.executablesConfig.builder).
WithRegistryMirror(f.registryMirror).
WithProxyConfigurations(f.proxyConfiguration).
WithInsecure()
return nil
})

return f
}

// WithNetworking builds a Networking.
func (f *Factory) WithNetworking(clusterConfig *v1alpha1.Cluster) *Factory {
var networkingBuilder func() clustermanager.Networking
Expand Down Expand Up @@ -845,13 +864,13 @@
}

func (f *Factory) WithCiliumTemplater() *Factory {
f.WithHelm(executables.WithInsecure())
f.WithHelmFactory()

f.buildSteps = append(f.buildSteps, func(ctx context.Context) error {
if f.dependencies.CiliumTemplater != nil {
return nil
}
f.dependencies.CiliumTemplater = cilium.NewTemplater(f.dependencies.Helm)
f.dependencies.CiliumTemplater = cilium.NewTemplater(f.dependencies.HelmFactory)

return nil
})
Expand Down
68 changes: 68 additions & 0 deletions pkg/dependencies/helm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package dependencies

import (
"sync"

"github.com/aws/eks-anywhere/pkg/executables"
"github.com/aws/eks-anywhere/pkg/registrymirror"
)

type ExecutableBuilder interface {

Check warning on line 10 in pkg/dependencies/helm.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported type ExecutableBuilder should have comment or be unexported (revive)
BuildHelmExecutable(...executables.HelmOpt) *executables.Helm
}

type HelmFactory struct {

Check warning on line 14 in pkg/dependencies/helm.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported type HelmFactory should have comment or be unexported (revive)
mu sync.Mutex
builder ExecutableBuilder
helm *executables.Helm
registryMirror *registrymirror.RegistryMirror
proxyConfiguration map[string]string
insecure bool
}

// WithRegistryMirror configures the factory to use registry mirror wherever applicable.
func (f *HelmFactory) WithRegistryMirror(registryMirror *registrymirror.RegistryMirror) *HelmFactory {
f.registryMirror = registryMirror

return f
}

// WithProxyConfigurations configures the factory to use proxy configurations wherever applicable.
func (f *HelmFactory) WithProxyConfigurations(proxyConfiguration map[string]string) *HelmFactory {
f.proxyConfiguration = proxyConfiguration

return f
}

// WithInsecure configures the factory to configure helm to use to allow connections to TLS registry without certs or with self-signed certs

Check failure on line 37 in pkg/dependencies/helm.go

View workflow job for this annotation

GitHub Actions / lint

Comment should end in a period (godot)
func (f *HelmFactory) WithInsecure() *HelmFactory {
f.insecure = true

return f
}

func NewHelmFactory(builder ExecutableBuilder) *HelmFactory {

Check warning on line 44 in pkg/dependencies/helm.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported function NewHelmFactory should have comment or be unexported (revive)
return &HelmFactory{
builder: builder,
}
}

func (f *HelmFactory) GetInstance(opts ...executables.HelmOpt) *executables.Helm {

Check warning on line 50 in pkg/dependencies/helm.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported method HelmFactory.GetInstance should have comment or be unexported (revive)
f.mu.Lock()
Copy link
Member

Choose a reason for hiding this comment

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

Curious, do we really need a lock here?

defer f.mu.Unlock()

if f.registryMirror != nil {
opts = append(opts, executables.WithRegistryMirror(f.registryMirror))
}

if f.proxyConfiguration != nil {
opts = append(opts, executables.WithEnv(f.proxyConfiguration))
}

if f.insecure {
opts = append(opts, executables.WithInsecure())
}

f.helm = f.builder.BuildHelmExecutable(opts...)
return f.helm
}
53 changes: 39 additions & 14 deletions pkg/networking/cilium/templater.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
anywherev1 "github.com/aws/eks-anywhere/pkg/api/v1alpha1"
"github.com/aws/eks-anywhere/pkg/cluster"
"github.com/aws/eks-anywhere/pkg/config"
"github.com/aws/eks-anywhere/pkg/executables"
"github.com/aws/eks-anywhere/pkg/registrymirror"
"github.com/aws/eks-anywhere/pkg/retrier"
"github.com/aws/eks-anywhere/pkg/semver"
"github.com/aws/eks-anywhere/pkg/templater"
Expand All @@ -29,13 +31,17 @@
RegistryLogin(ctx context.Context, registry, username, password string) error
}

type HelmFactory interface {

Check warning on line 34 in pkg/networking/cilium/templater.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported type HelmFactory should have comment or be unexported (revive)
GetInstance(opts ...executables.HelmOpt) *executables.Helm
}

type Templater struct {
helm Helm
helmFactory HelmFactory
}

func NewTemplater(helm Helm) *Templater {
func NewTemplater(helmFactory HelmFactory) *Templater {

Check warning on line 42 in pkg/networking/cilium/templater.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported function NewTemplater should have comment or be unexported (revive)
return &Templater{
helm: helm,
helmFactory: helmFactory,
}
}

Expand All @@ -62,7 +68,16 @@
return nil, err
}

manifest, err := t.helm.Template(ctx, uri, version, namespace, v, kubeVersion)
r := registrymirror.FromCluster(spec.Cluster)
helm := t.helmFactory.GetInstance(executables.WithRegistryMirror(r))

if spec.Cluster.Spec.RegistryMirrorConfiguration != nil {
Copy link
Member

Choose a reason for hiding this comment

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

This if check is duplicated in registryLogin function

if err := t.registryLogin(ctx, helm, spec); err != nil {
return nil, err
}
}

manifest, err := helm.Template(ctx, uri, version, namespace, v, kubeVersion)
if err != nil {
return nil, fmt.Errorf("failed generating cilium upgrade preflight manifest: %v", err)
}
Expand Down Expand Up @@ -112,6 +127,20 @@
}
}

func (t *Templater) registryLogin(ctx context.Context, helm Helm, spec *cluster.Spec) error {
Copy link
Member

Choose a reason for hiding this comment

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

looks like this "t" is not used, it can be a function instead of a method of Templater

if spec.Cluster.Spec.RegistryMirrorConfiguration.Authenticate {
username, password, err := config.ReadCredentials()
if err != nil {
return err
}
endpoint := net.JoinHostPort(spec.Cluster.Spec.RegistryMirrorConfiguration.Endpoint, spec.Cluster.Spec.RegistryMirrorConfiguration.Port)
if err := helm.RegistryLogin(ctx, endpoint, username, password); err != nil {
return err
}
}
return nil
}

func (t *Templater) GenerateManifest(ctx context.Context, spec *cluster.Spec, opts ...ManifestOpt) ([]byte, error) {
versionsBundle := spec.RootVersionsBundle()
kubeVersion, err := getKubeVersionString(spec, versionsBundle)
Expand All @@ -131,21 +160,17 @@
uri, version := getChartURIAndVersion(versionsBundle)
var manifest []byte

r := registrymirror.FromCluster(spec.Cluster)
helm := t.helmFactory.GetInstance(executables.WithRegistryMirror(r))

if spec.Cluster.Spec.RegistryMirrorConfiguration != nil {
if spec.Cluster.Spec.RegistryMirrorConfiguration.Authenticate {
username, password, err := config.ReadCredentials()
if err != nil {
return nil, err
}
endpoint := net.JoinHostPort(spec.Cluster.Spec.RegistryMirrorConfiguration.Endpoint, spec.Cluster.Spec.RegistryMirrorConfiguration.Port)
if err := t.helm.RegistryLogin(ctx, endpoint, username, password); err != nil {
return nil, err
}
if err := t.registryLogin(ctx, helm, spec); err != nil {
return nil, err
}
}

err = c.retrier.Retry(func() error {
manifest, err = t.helm.Template(ctx, uri, version, namespace, c.values, c.kubeVersion)
manifest, err = helm.Template(ctx, uri, version, namespace, c.values, c.kubeVersion)
return err
})
if err != nil {
Expand Down
Loading