Skip to content

Commit

Permalink
Add option to use native binaries in dep factory (#2683)
Browse files Browse the repository at this point in the history
* Add option to use native binaries in dep factory

Until now, the executable builder always decided if docker needed to be
used or not, based on an env var. This made the constructor take all the
arguments, even when some of them aren't needed. Moreover, in order to
build local native executables programmatically through the factory, we
needed to set an env var and force the use of a "fake" tools image.

Now the factory has an specific option to use docker or not. If it's not
set, it will default to the value of the env var, keeping current
behavior. But it allows to override it.

* Fix typo in factory method name

* Fixed typo in doc comment

Co-authored-by: Joey Wang <jiayiwang7@yahoo.com>

* Fixed typo in doc comment

* Nest executables fields in factory

* Remove unnecessary tests

Co-authored-by: Joey Wang <jiayiwang7@yahoo.com>
  • Loading branch information
g-gaston and jiayiwang7 authored Jul 11, 2022
1 parent 9a8147f commit 8b89b4d
Show file tree
Hide file tree
Showing 18 changed files with 418 additions and 160 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ mocks: export PATH := $(GO_VERSION):$(PATH)
mocks: ## Generate mocks
$(GO) install github.com/golang/mock/mockgen@v1.6.0
${GOPATH}/bin/mockgen -destination=pkg/providers/mocks/providers.go -package=mocks "github.com/aws/eks-anywhere/pkg/providers" Provider,DatacenterConfig,MachineConfig
${GOPATH}/bin/mockgen -destination=pkg/executables/mocks/executables.go -package=mocks "github.com/aws/eks-anywhere/pkg/executables" Executable,DockerClient
${GOPATH}/bin/mockgen -destination=pkg/executables/mocks/executables.go -package=mocks "github.com/aws/eks-anywhere/pkg/executables" Executable,DockerClient,DockerContainer
${GOPATH}/bin/mockgen -destination=pkg/providers/docker/mocks/client.go -package=mocks "github.com/aws/eks-anywhere/pkg/providers/docker" ProviderClient,ProviderKubectlClient
${GOPATH}/bin/mockgen -destination=pkg/providers/tinkerbell/mocks/client.go -package=mocks "github.com/aws/eks-anywhere/pkg/providers/tinkerbell" ProviderKubectlClient,SSHAuthKeyGenerator
${GOPATH}/bin/mockgen -destination=pkg/providers/cloudstack/mocks/client.go -package=mocks "github.com/aws/eks-anywhere/pkg/providers/cloudstack" ProviderCmkClient,ProviderKubectlClient
Expand Down
2 changes: 1 addition & 1 deletion cmd/eks-a-tool/cmd/validatecluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func init() {
}

func validateCluster(ctx context.Context, cluster *types.Cluster, clusterName string) error {
executableBuilder, close, err := executables.NewExecutableBuilder(ctx, executables.DefaultEksaImage())
executableBuilder, close, err := executables.InitInDockerExecutablesBuilder(ctx, executables.DefaultEksaImage())
if err != nil {
return fmt.Errorf("unable to initialize executables: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/eks-a-tool/cmd/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func init() {
}

func versions(ctx context.Context) error {
executableBuilder, close, err := executables.NewExecutableBuilder(ctx, executables.DefaultEksaImage())
executableBuilder, close, err := executables.InitInDockerExecutablesBuilder(ctx, executables.DefaultEksaImage())
if err != nil {
return fmt.Errorf("unable to initialize executables: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/eks-a-tool/cmd/vspherermvms.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func init() {
}

func vsphereRmVms(ctx context.Context, clusterName string, dryRun bool) error {
executableBuilder, close, err := executables.NewExecutableBuilder(ctx, executables.DefaultEksaImage())
executableBuilder, close, err := executables.InitInDockerExecutablesBuilder(ctx, executables.DefaultEksaImage())
if err != nil {
return fmt.Errorf("unable to initialize executables: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/eksctl-anywhere/cmd/importimages.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func importImages(ctx context.Context, spec string) error {
de := executables.BuildDockerExecutable()

bundle := clusterSpec.VersionsBundle
executableBuilder, closer, err := executables.NewExecutableBuilder(ctx, bundle.Eksa.CliTools.VersionedImage())
executableBuilder, closer, err := executables.InitInDockerExecutablesBuilder(ctx, bundle.Eksa.CliTools.VersionedImage())
if err != nil {
return fmt.Errorf("unable to initialize executables: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/pkg/vsphere/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type NetworkMapping struct {

func DeployTemplate(envMap map[string]string, library, templateName, vmName, deployFolder, datacenter, datastore, resourcePool string, opts OVFDeployOptions) error {
context := context.Background()
executableBuilder, close, err := executables.NewExecutableBuilder(context, executables.DefaultEksaImage())
executableBuilder, close, err := executables.InitInDockerExecutablesBuilder(context, executables.DefaultEksaImage())
if err != nil {
return fmt.Errorf("unable to initialize executables: %v", err)
}
Expand All @@ -57,7 +57,7 @@ func DeployTemplate(envMap map[string]string, library, templateName, vmName, dep

func TagVirtualMachine(envMap map[string]string, vmPath, tag string) error {
context := context.Background()
executableBuilder, close, err := executables.NewExecutableBuilder(context, executables.DefaultEksaImage())
executableBuilder, close, err := executables.InitInDockerExecutablesBuilder(context, executables.DefaultEksaImage())
if err != nil {
return fmt.Errorf("unable to initialize executables: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/test/cleanup/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func CleanUpVsphereTestResources(ctx context.Context, clusterName string) error

func VsphereRmVms(ctx context.Context, clusterName string, opts ...executables.GovcOpt) error {
logger.V(1).Info("Deleting vsphere vcenter vms")
executableBuilder, close, err := executables.NewExecutableBuilder(ctx, executables.DefaultEksaImage())
executableBuilder, close, err := executables.InitInDockerExecutablesBuilder(ctx, executables.DefaultEksaImage())
if err != nil {
return fmt.Errorf("unable to initialize executables: %v", err)
}
Expand All @@ -86,7 +86,7 @@ func VsphereRmVms(ctx context.Context, clusterName string, opts ...executables.G
}

func CleanUpCloudstackTestResources(ctx context.Context, clusterName string, dryRun bool) error {
executableBuilder, close, err := executables.NewExecutableBuilder(ctx, executables.DefaultEksaImage())
executableBuilder, close, err := executables.InitInDockerExecutablesBuilder(ctx, executables.DefaultEksaImage())
if err != nil {
return fmt.Errorf("unable to initialize executables: %v", err)
}
Expand Down
6 changes: 1 addition & 5 deletions manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,7 @@ func main() {

func setupReconcilers(ctx context.Context, mgr ctrl.Manager) {
if features.IsActive(features.FullLifecycleAPI()) {
// This feature doesn't support running the binaries through docker on the controller image so relying on the
// binaries built within the controller image instead. We also can specify a fake executable image for now in
// order to get the dependencies to build.
os.Setenv("MR_TOOLS_DISABLE", "true")
factory := dependencies.NewFactory().UseExecutableImage("test.com/fake-image:1.0")
factory := dependencies.NewFactory().WithLocalExecutables()
deps, err := factory.WithGovc().Build(ctx)
if err != nil {
setupLog.Error(err, "unable to build dependencies")
Expand Down
106 changes: 65 additions & 41 deletions pkg/dependencies/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,23 +104,32 @@ func ForSpec(ctx context.Context, clusterSpec *cluster.Spec) *Factory {
}

type Factory struct {
executableBuilder *executables.ExecutableBuilder
executablesImage string
executablesConfig *executablesConfig
registryMirror string
proxyConfiguration map[string]string
executablesMountDirs []string
writerFolder string
diagnosticCollectorImage string
buildSteps []buildStep
dependencies Dependencies
}

type executablesConfig struct {
builder *executables.ExecutablesBuilder
image string
useDockerContainer bool
dockerClient executables.DockerClient
mountDirs []string
}

type buildStep func(ctx context.Context) error

func NewFactory() *Factory {
return &Factory{
writerFolder: "./",
buildSteps: make([]buildStep, 0),
executablesConfig: &executablesConfig{
useDockerContainer: executables.ExecutablesInDocker(),
},
buildSteps: make([]buildStep, 0),
}
}

Expand Down Expand Up @@ -156,7 +165,7 @@ func (f *Factory) WithProxyConfiguration(proxyConfig map[string]string) *Factory
}

func (f *Factory) UseExecutableImage(image string) *Factory {
f.executablesImage = image
f.executablesConfig.image = image
return f
}

Expand All @@ -169,7 +178,7 @@ func (f *Factory) WithExecutableImage() *Factory {
f.WithManifestReader()

f.buildSteps = append(f.buildSteps, func(ctx context.Context) error {
if f.executablesImage != "" {
if f.executablesConfig.image != "" {
return nil
}

Expand All @@ -178,35 +187,63 @@ func (f *Factory) WithExecutableImage() *Factory {
return fmt.Errorf("retrieving executable tools image from bundle in dependency factory: %v", err)
}

f.executablesImage = bundles.DefaultEksAToolsImage().VersionedImage()
f.executablesConfig.image = bundles.DefaultEksAToolsImage().VersionedImage()
return nil
})

return f
}

func (f *Factory) WithExecutableMountDirs(mountDirs ...string) *Factory {
f.executablesMountDirs = mountDirs
f.executablesConfig.mountDirs = mountDirs
return f
}

func (f *Factory) WithLocalExecutables() *Factory {
f.executablesConfig.useDockerContainer = false
return f
}

// UseExecutablesDockerClient forces a specific DockerClient to build
// Executables as opposed to follow the normal building flow
// This is only for testing
func (f *Factory) UseExecutablesDockerClient(client executables.DockerClient) *Factory {
f.executablesConfig.dockerClient = client
return f
}

func (f *Factory) WithExecutableBuilder() *Factory {
f.WithExecutableImage()
if f.executablesConfig.useDockerContainer {
f.WithExecutableImage().WithDocker()
}

f.buildSteps = append(f.buildSteps, func(ctx context.Context) error {
if f.executableBuilder != nil {
if f.executablesConfig.builder != nil {
return nil
}

image := urls.ReplaceHost(f.executablesImage, f.registryMirror)
b, close, err := executables.NewExecutableBuilder(ctx, image, f.executablesMountDirs...)
if f.executablesConfig.useDockerContainer {
image := urls.ReplaceHost(f.executablesConfig.image, f.registryMirror)
b, err := executables.NewInDockerExecutablesBuilder(
f.executablesConfig.dockerClient,
image,
f.executablesConfig.mountDirs...,
)
if err != nil {
return err
}

f.executablesConfig.builder = b
} else {
f.executablesConfig.builder = executables.NewLocalExecutablesBuilder()
}

closer, err := f.executablesConfig.builder.Init(ctx)
if err != nil {
return err
}
f.dependencies.closers = append(f.dependencies.closers, closer)

f.dependencies.closers = append(f.dependencies.closers, close)

f.executableBuilder = b
return nil
})

Expand Down Expand Up @@ -349,30 +386,17 @@ func (f *Factory) WithProvider(clusterConfigFile string, clusterConfig *v1alpha1
return f
}

func (f *Factory) WithClusterAwsCli() *Factory {
f.WithExecutableBuilder()

f.buildSteps = append(f.buildSteps, func(ctx context.Context) error {
if f.dependencies.ClusterAwsCli != nil {
return nil
}

f.dependencies.ClusterAwsCli = f.executableBuilder.BuildClusterAwsAdmExecutable()
return nil
})

return f
}

func (f *Factory) WithDocker() *Factory {
f.WithExecutableBuilder()

f.buildSteps = append(f.buildSteps, func(ctx context.Context) error {
if f.dependencies.DockerClient != nil {
return nil
}

f.dependencies.DockerClient = executables.BuildDockerExecutable()
if f.executablesConfig.dockerClient == nil {
f.executablesConfig.dockerClient = f.dependencies.DockerClient
}

return nil
})

Expand All @@ -387,7 +411,7 @@ func (f *Factory) WithKubectl() *Factory {
return nil
}

f.dependencies.Kubectl = f.executableBuilder.BuildKubectlExecutable()
f.dependencies.Kubectl = f.executablesConfig.builder.BuildKubectlExecutable()
return nil
})

Expand All @@ -402,7 +426,7 @@ func (f *Factory) WithGovc() *Factory {
return nil
}

f.dependencies.Govc = f.executableBuilder.BuildGovcExecutable(f.dependencies.Writer)
f.dependencies.Govc = f.executablesConfig.builder.BuildGovcExecutable(f.dependencies.Writer)
f.dependencies.closers = append(f.dependencies.closers, f.dependencies.Govc)

return nil
Expand All @@ -424,7 +448,7 @@ func (f *Factory) WithCmk() *Factory {
return fmt.Errorf("building cmk executable: %v", err)
}

f.dependencies.Cmk = f.executableBuilder.BuildCmkExecutable(f.dependencies.Writer, execConfig.Profiles)
f.dependencies.Cmk = f.executablesConfig.builder.BuildCmkExecutable(f.dependencies.Writer, execConfig.Profiles)
f.dependencies.closers = append(f.dependencies.closers, f.dependencies.Cmk)

return nil
Expand Down Expand Up @@ -515,7 +539,7 @@ func (f *Factory) WithKind() *Factory {
return nil
}

f.dependencies.Kind = f.executableBuilder.BuildKindExecutable(f.dependencies.Writer)
f.dependencies.Kind = f.executablesConfig.builder.BuildKindExecutable(f.dependencies.Writer)
return nil
})

Expand All @@ -530,7 +554,7 @@ func (f *Factory) WithClusterctl() *Factory {
return nil
}

f.dependencies.Clusterctl = f.executableBuilder.BuildClusterCtlExecutable(f.dependencies.Writer)
f.dependencies.Clusterctl = f.executablesConfig.builder.BuildClusterCtlExecutable(f.dependencies.Writer)
return nil
})

Expand All @@ -545,7 +569,7 @@ func (f *Factory) WithFlux() *Factory {
return nil
}

f.dependencies.Flux = f.executableBuilder.BuildFluxExecutable()
f.dependencies.Flux = f.executablesConfig.builder.BuildFluxExecutable()
return nil
})

Expand All @@ -560,7 +584,7 @@ func (f *Factory) WithTroubleshoot() *Factory {
return nil
}

f.dependencies.Troubleshoot = f.executableBuilder.BuildTroubleshootExecutable()
f.dependencies.Troubleshoot = f.executablesConfig.builder.BuildTroubleshootExecutable()
return nil
})

Expand All @@ -584,7 +608,7 @@ func (f *Factory) WithHelmSecure() *Factory {
opts = append(opts, executables.WithEnv(f.proxyConfiguration))
}

f.dependencies.HelmSecure = f.executableBuilder.BuildHelmExecutable(opts...)
f.dependencies.HelmSecure = f.executablesConfig.builder.BuildHelmExecutable(opts...)
return nil
})

Expand All @@ -608,7 +632,7 @@ func (f *Factory) WithHelmInsecure() *Factory {
opts = append(opts, executables.WithEnv(f.proxyConfiguration))
}

f.dependencies.HelmInsecure = f.executableBuilder.BuildHelmExecutable(opts...)
f.dependencies.HelmInsecure = f.executablesConfig.builder.BuildHelmExecutable(opts...)
return nil
})

Expand Down
Loading

0 comments on commit 8b89b4d

Please sign in to comment.