From 2cf0ac21691747ead9c04ee2da88f943217c2485 Mon Sep 17 00:00:00 2001 From: Bohan Chen Date: Mon, 20 Nov 2023 16:41:18 -0500 Subject: [PATCH] move config options into dedicated struct this makes it easier to move them around as a whole instead of passing a bajillion arguments around. they're also annotated with json tags for the upcoming slsa stuff Signed-off-by: Bohan Chen --- cmd/controller/main.go | 64 ++++++++++++++-------------- pkg/apis/build/v1alpha2/build_pod.go | 10 ++--- pkg/config/config.go | 37 ++++++++++++++++ 3 files changed, 73 insertions(+), 38 deletions(-) create mode 100644 pkg/config/config.go diff --git a/cmd/controller/main.go b/cmd/controller/main.go index 70f258fd4..3d75fe100 100644 --- a/cmd/controller/main.go +++ b/cmd/controller/main.go @@ -4,19 +4,14 @@ import ( "context" "flag" "fmt" - "github.com/pivotal/kpack/pkg/buildchange" "log" "net/http" "os" "time" - "github.com/pivotal/kpack/pkg/secret" - - "github.com/pivotal/kpack/pkg/cosign" + "github.com/Masterminds/semver/v3" "github.com/sigstore/cosign/v2/cmd/cosign/cli/sign" ociremote "github.com/sigstore/cosign/v2/pkg/oci/remote" - - "github.com/Masterminds/semver/v3" "go.uber.org/zap" "golang.org/x/sync/errgroup" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -39,13 +34,14 @@ import ( "github.com/pivotal/kpack/cmd" _ "github.com/pivotal/kpack/internal/logrus/fatal" - buildapi "github.com/pivotal/kpack/pkg/apis/build/v1alpha2" "github.com/pivotal/kpack/pkg/blob" + "github.com/pivotal/kpack/pkg/buildchange" "github.com/pivotal/kpack/pkg/buildpod" "github.com/pivotal/kpack/pkg/client/clientset/versioned" "github.com/pivotal/kpack/pkg/client/informers/externalversions" "github.com/pivotal/kpack/pkg/cnb" "github.com/pivotal/kpack/pkg/config" + "github.com/pivotal/kpack/pkg/cosign" "github.com/pivotal/kpack/pkg/dockercreds/k8sdockercreds" "github.com/pivotal/kpack/pkg/duckbuilder" "github.com/pivotal/kpack/pkg/flaghelpers" @@ -62,6 +58,7 @@ import ( "github.com/pivotal/kpack/pkg/reconciler/lifecycle" "github.com/pivotal/kpack/pkg/reconciler/sourceresolver" "github.com/pivotal/kpack/pkg/registry" + "github.com/pivotal/kpack/pkg/secret" ) const ( @@ -69,23 +66,31 @@ const ( component = "controller" ) +var ( + images config.Images + cfg config.Config + featureFlags config.FeatureFlags +) + var ( kubeconfig = flag.String("kubeconfig", "", "Path to a kubeconfig. Only required if out-of-cluster.") masterURL = flag.String("master", "", "The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster.") - - buildInitImage = flag.String("build-init-image", os.Getenv("BUILD_INIT_IMAGE"), "The image used to initialize a build") - buildInitWindowsImage = flag.String("build-init-windows-image", os.Getenv("BUILD_INIT_WINDOWS_IMAGE"), "The image used to initialize a build on windows") - rebaseImage = flag.String("rebase-image", os.Getenv("REBASE_IMAGE"), "The image used to perform rebases") - completionImage = flag.String("completion-image", os.Getenv("COMPLETION_IMAGE"), "The image used to finish a build") - completionWindowsImage = flag.String("completion-windows-image", os.Getenv("COMPLETION_WINDOWS_IMAGE"), "The image used to finish a build on windows") - enablePriorityClasses = flag.Bool("enable-priority-classes", flaghelpers.GetEnvBool("ENABLE_PRIORITY_CLASSES", false), "if set to true, enables different pod priority classes for normal builds and automated builds") - maximumPlatformApiVersion = flag.String("maximum-platform-api-version", os.Getenv("MAXIMUM_PLATFORM_API_VERSION"), "The maximum allowed platform api version a build can utilize") - buildWaiterImage = flag.String("build-waiter-image", os.Getenv("BUILD_WAITER_IMAGE"), "The image used to initialize a build") - injectedSidecarSupport = flag.Bool("injected-sidecar-support", flaghelpers.GetEnvBool("INJECTED_SIDECAR_SUPPORT", false), "if set to true, all builds will execute in standard containers instead of init containers to support injected sidecars") - sshTrustUnknownHosts = flag.Bool("insecure-ssh-trust-unknown-hosts", flaghelpers.GetEnvBool("INSECURE_SSH_TRUST_UNKNOWN_HOSTS", true), "if set to true, automatically trust unknown hosts when using git ssh source") ) func main() { + flag.StringVar(&images.BuildInitImage, "build-init-image", os.Getenv("BUILD_INIT_IMAGE"), "The image used to initialize a build") + flag.StringVar(&images.BuildInitWindowsImage, "build-init-windows-image", os.Getenv("BUILD_INIT_WINDOWS_IMAGE"), "The image used to initialize a build on windows") + flag.StringVar(&images.RebaseImage, "rebase-image", os.Getenv("REBASE_IMAGE"), "The image used to perform rebases") + flag.StringVar(&images.CompletionImage, "completion-image", os.Getenv("COMPLETION_IMAGE"), "The image used to finish a build") + flag.StringVar(&images.CompletionWindowsImage, "completion-windows-image", os.Getenv("COMPLETION_WINDOWS_IMAGE"), "The image used to finish a build on windows") + flag.StringVar(&images.BuildWaiterImage, "build-waiter-image", os.Getenv("BUILD_WAITER_IMAGE"), "The image used to initialize a build") + + flag.BoolVar(&cfg.EnablePriorityClasses, "enable-priority-classes", flaghelpers.GetEnvBool("ENABLE_PRIORITY_CLASSES", false), "if set to true, enables different pod priority classes for normal builds and automated builds") + flag.StringVar(&cfg.MaximumPlatformApiVersion, "maximum-platform-api-version", os.Getenv("MAXIMUM_PLATFORM_API_VERSION"), "The maximum allowed platform api version a build can utilize") + flag.BoolVar(&cfg.SshTrustUnknownHosts, "insecure-ssh-trust-unknown-hosts", flaghelpers.GetEnvBool("INSECURE_SSH_TRUST_UNKNOWN_HOSTS", true), "if set to true, automatically trust unknown hosts when using git ssh source") + + flag.BoolVar(&featureFlags.InjectedSidecarSupport, "injected-sidecar-support", flaghelpers.GetEnvBool("INJECTED_SIDECAR_SUPPORT", false), "if set to true, all builds will execute in standard containers instead of init containers to support injected sidecars") + flag.Parse() clusterConfig, err := clientcmd.BuildConfigFromFlags(*masterURL, *kubeconfig) @@ -164,24 +169,17 @@ func main() { } buildpodGenerator := &buildpod.Generator{ - BuildPodConfig: buildapi.BuildPodImages{ - BuildInitImage: *buildInitImage, - BuildWaiterImage: *buildWaiterImage, - CompletionImage: *completionImage, - RebaseImage: *rebaseImage, - BuildInitWindowsImage: *buildInitWindowsImage, - CompletionWindowsImage: *completionWindowsImage, - }, + BuildPodConfig: images.ToBuildPodImages(), K8sClient: k8sClient, KeychainFactory: keychainFactory, ImageFetcher: ®istry.Client{}, DynamicClient: dynamicClient, MaximumPlatformApiVersion: maxPlatformApi, - InjectedSidecarSupport: *injectedSidecarSupport, - SSHTrustUnknownHost: *sshTrustUnknownHosts, + InjectedSidecarSupport: featureFlags.InjectedSidecarSupport, + SSHTrustUnknownHost: cfg.SshTrustUnknownHosts, } - gitResolver := git.NewResolver(k8sClient, *sshTrustUnknownHosts) + gitResolver := git.NewResolver(k8sClient, cfg.SshTrustUnknownHosts) blobResolver := &blob.Resolver{} registryResolver := ®istry.Resolver{} @@ -209,8 +207,8 @@ func main() { secretFetcher := &secret.Fetcher{Client: k8sClient} - buildController := build.NewController(ctx, options, k8sClient, buildInformer, podInformer, metadataRetriever, buildpodGenerator, podProgressLogger, keychainFactory, *injectedSidecarSupport) - imageController := image.NewController(ctx, options, k8sClient, imageInformer, buildInformer, duckBuilderInformer, sourceResolverInformer, pvcInformer, *enablePriorityClasses) + buildController := build.NewController(ctx, options, k8sClient, buildInformer, podInformer, metadataRetriever, buildpodGenerator, podProgressLogger, keychainFactory, featureFlags.InjectedSidecarSupport) + imageController := image.NewController(ctx, options, k8sClient, imageInformer, buildInformer, duckBuilderInformer, sourceResolverInformer, pvcInformer, cfg.EnablePriorityClasses) sourceResolverController := sourceresolver.NewController(ctx, options, sourceResolverInformer, gitResolver, blobResolver, registryResolver) builderController, builderResync := builder.NewController(ctx, options, builderInformer, builderCreator, keychainFactory, clusterStoreInformer, buildpackInformer, clusterBuildpackInformer, clusterStackInformer, secretFetcher) buildpackController := buildpack.NewController(ctx, options, keychainFactory, buildpackInformer, remoteStoreReader) @@ -322,8 +320,8 @@ func waitForSync(stopCh <-chan struct{}, indexFormers ...cache.SharedIndexInform } func parseMaxPlatformApiVersion() (*semver.Version, error) { - if *maximumPlatformApiVersion != "" { - return semver.NewVersion(*maximumPlatformApiVersion) + if cfg.MaximumPlatformApiVersion != "" { + return semver.NewVersion(cfg.MaximumPlatformApiVersion) } return nil, nil diff --git a/pkg/apis/build/v1alpha2/build_pod.go b/pkg/apis/build/v1alpha2/build_pod.go index ff12f99f2..183e5ffaf 100644 --- a/pkg/apis/build/v1alpha2/build_pod.go +++ b/pkg/apis/build/v1alpha2/build_pod.go @@ -71,11 +71,11 @@ const ( var ( PrepareCommand = "/cnb/process/build-init" - AnalyzeCommand = "/cnb/lifecycle/analyzer" - DetectCommand = "/cnb/lifecycle/detector" - RestoreCommand = "/cnb/lifecycle/restorer" - BuildCommand = "/cnb/lifecycle/builder" - ExportCommand = "/cnb/lifecycle/exporter" + AnalyzeCommand = "/cnb/lifecycle/analyzer" + DetectCommand = "/cnb/lifecycle/detector" + RestoreCommand = "/cnb/lifecycle/restorer" + BuildCommand = "/cnb/lifecycle/builder" + ExportCommand = "/cnb/lifecycle/exporter" CompletionCommand = "/cnb/process/completion" RebaseCommand = "/cnb/process/rebase" ) diff --git a/pkg/config/config.go b/pkg/config/config.go new file mode 100644 index 000000000..1cf6cb201 --- /dev/null +++ b/pkg/config/config.go @@ -0,0 +1,37 @@ +package config + +import "github.com/pivotal/kpack/pkg/apis/build/v1alpha2" + +type Config struct { + EnablePriorityClasses bool `json:"enablePriorityClasses"` + MaximumPlatformApiVersion string `json:"maximumPlatformApiVersion"` + SshTrustUnknownHosts bool `json:"sshTrustUnknownHosts"` +} + +type FeatureFlags struct { + InjectedSidecarSupport bool `json:"injectedSidecarSupport"` +} + +type Images struct { + BuildInitImage string `json:"buildInitImage"` + BuildInitWindowsImage string `json:"buildInitWindowsImage"` + BuildWaiterImage string `json:"buildWaiterImage"` + CompletionImage string `json:"completionImage"` + CompletionWindowsImage string `json:"completionWindowsImage"` + RebaseImage string `json:"rebaseImage"` +} + +// TODO: evaluate if we can move the lifecycle_provider stuff out of this config package +// Ideally v1alpha2.BuildPodImages should either just use config.Images directly or be an alias to it. However this +// doesn't work right now because lifecycle_provider.go imports pkg/cnb which imports pkg/apis/build/v1alpha2 and +// thus creating an import cycle. +func (i *Images) ToBuildPodImages() v1alpha2.BuildPodImages { + return v1alpha2.BuildPodImages{ + BuildInitImage: i.BuildInitImage, + BuildInitWindowsImage: i.BuildInitWindowsImage, + BuildWaiterImage: i.BuildWaiterImage, + CompletionImage: i.CompletionImage, + CompletionWindowsImage: i.CompletionWindowsImage, + RebaseImage: i.RebaseImage, + } +}