Skip to content

Commit

Permalink
update cilium tempater to use helm factory
Browse files Browse the repository at this point in the history
  • Loading branch information
cxbrowne1207 committed Dec 7, 2023
1 parent 938bcf0 commit fb6fcd0
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 16 deletions.
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 @@ type Dependencies struct {
SnowValidator *snow.Validator
IPValidator *validator.IPValidator
UnAuthKubectlClient KubeClients
HelmFactory *HelmFactory
CreateClusterDefaulter cli.CreateClusterDefaulter
UpgradeClusterDefaulter cli.UpgradeClusterDefaulter
}
Expand Down Expand Up @@ -770,6 +771,24 @@ func (f *Factory) WithHelm(opts ...executables.HelmOpt) *Factory {
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 {
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) WithCNIInstaller(spec *cluster.Spec, provider providers.Provid
}

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()
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 @@ import (
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 @@ type Helm interface {
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 @@ func (t *Templater) GenerateUpgradePreflightManifest(ctx context.Context, spec *
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 {
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 WithPolicyAllowedNamespaces(namespaces []string) ManifestOpt {
}
}

func (t *Templater) registryLogin(ctx context.Context, helm Helm, spec *cluster.Spec) error {
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 @@ func (t *Templater) GenerateManifest(ctx context.Context, spec *cluster.Spec, op
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

0 comments on commit fb6fcd0

Please sign in to comment.