Skip to content

Commit

Permalink
move config options into dedicated struct
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
chenbh committed Nov 20, 2023
1 parent 20bfcc5 commit 2cf0ac2
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 38 deletions.
64 changes: 31 additions & 33 deletions cmd/controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -62,30 +58,39 @@ 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 (
routinesPerController = 2
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)
Expand Down Expand Up @@ -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: &registry.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 := &registry.Resolver{}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions pkg/apis/build/v1alpha2/build_pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down
37 changes: 37 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -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,
}
}

0 comments on commit 2cf0ac2

Please sign in to comment.