From 88e4a4371df62ae3ec0078686a655e44b534f930 Mon Sep 17 00:00:00 2001 From: fabriziopandini Date: Thu, 28 Dec 2023 14:36:56 +0100 Subject: [PATCH 1/3] small improvements to tilt --- Tiltfile | 2 +- docs/book/src/developer/tilt.md | 8 +++++- hack/tools/internal/tilt-prepare/main.go | 34 +++++++++++++++++++----- 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/Tiltfile b/Tiltfile index 7187f3793862..fa5b97684163 100644 --- a/Tiltfile +++ b/Tiltfile @@ -366,7 +366,7 @@ def enable_provider(name, debug): k8s_yaml(p.get("context") + "/" + resource) additional_objs = additional_objs + decode_yaml_stream(read_file(p.get("context") + "/" + resource)) - if p.get("kustomize_config", True): + if p.get("apply_provider_yaml", True): yaml = read_file("./.tiltbuild/yaml/{}.provider.yaml".format(name)) k8s_yaml(yaml, allow_duplicates = True) objs = decode_yaml_stream(yaml) diff --git a/docs/book/src/developer/tilt.md b/docs/book/src/developer/tilt.md index 1b632738a064..c333ffe98769 100644 --- a/docs/book/src/developer/tilt.md +++ b/docs/book/src/developer/tilt.md @@ -429,7 +429,13 @@ COPY --from=tilt-helper /usr/bin/docker /usr/bin/docker COPY --from=tilt-helper /go/kubernetes/client/bin/kubectl /usr/bin/kubectl ``` -**kustomize_config** (Bool, default=true): Whether or not running kustomize on the ./config folder of the provider. +**kustomize_folder** (String, default=config/default): The folder where the kustomize file for a provider +are defined; the path is relative to the provider root folder. + +**kustomize_options** (String, default="""): Options to be applied when running kustomize for genereating +yaml manifest for a provider. e.g. `"kustomize_options": "--load-restrictor=LoadRestrictionsNone"` + +**apply_provider_yaml** (Bool, default=true): Whether or not apply the provider yaml. Set to `false` if your provider does not have a ./config folder or you do not want it to be applied in the cluster. **go_main** (String, default="main.go"): The go main file if not located at the root of the folder diff --git a/hack/tools/internal/tilt-prepare/main.go b/hack/tools/internal/tilt-prepare/main.go index 0c825b94a9c6..d033c14d6f5d 100644 --- a/hack/tools/internal/tilt-prepare/main.go +++ b/hack/tools/internal/tilt-prepare/main.go @@ -28,6 +28,7 @@ import ( "io/fs" "os" "os/exec" + "path" "path/filepath" "strings" "sync" @@ -36,6 +37,7 @@ import ( "github.com/pkg/errors" "github.com/spf13/pflag" appsv1 "k8s.io/api/apps/v1" + corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" kerrors "k8s.io/apimachinery/pkg/util/errors" @@ -44,6 +46,7 @@ import ( "k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/tools/clientcmd" "k8s.io/klog/v2" + "k8s.io/utils/pointer" "k8s.io/utils/ptr" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/kustomize/api/types" @@ -129,8 +132,11 @@ type tiltProvider struct { } type tiltProviderConfig struct { - Context *string `json:"context,omitempty"` - Version *string `json:"version,omitempty"` + Context *string `json:"context,omitempty"` + Version *string `json:"version,omitempty"` + ApplyProviderYaml *bool `json:"apply_provider_yaml,omitempty"` + KustomizeFolder *string `json:"kustomize_folder,omitempty"` + KustomizeOptions *string `json:"kustomize_options,omitempty"` } func init() { @@ -339,7 +345,11 @@ func tiltResources(ctx context.Context, ts *tiltSettings) error { if !ok { return errors.Errorf("failed to obtain config for the provider %s, please add the providers path to the provider_repos list in tilt-settings.yaml/json file", providerName) } - tasks[providerName] = workloadTask(providerName, "provider", "manager", "manager", ts, fmt.Sprintf("%s/config/default", *config.Context), getProviderObj(config.Version)) + if ptr.Deref(config.ApplyProviderYaml, true) { + kustomize_folder := path.Join(*config.Context, ptr.Deref(config.KustomizeFolder, "config/default")) + kustomize_options := ptr.Deref(config.KustomizeOptions, "") + tasks[providerName] = workloadTask(providerName, "provider", "manager", "manager", ts, kustomize_folder, kustomize_options, getProviderObj(config.Version)) + } } return runTaskGroup(ctx, "resources", tasks) @@ -361,8 +371,11 @@ func loadTiltProvider(providerRepository string) (map[string]tiltProviderConfig, contextPath := filepath.Join(providerRepository, ptr.Deref(p.Config.Context, ".")) ret[p.Name] = tiltProviderConfig{ - Context: &contextPath, - Version: p.Config.Version, + Context: &contextPath, + Version: p.Config.Version, + ApplyProviderYaml: p.Config.ApplyProviderYaml, + KustomizeFolder: p.Config.KustomizeFolder, + KustomizeOptions: p.Config.KustomizeOptions, } } return ret, nil @@ -674,9 +687,13 @@ func kustomizeTask(path, out string) taskFunction { // workloadTask generates a task for creating the component yaml for a workload and saving the output on a file. // NOTE: This task has several sub steps including running kustomize, envsubst, fixing components for debugging, // and adding the workload resource mimicking what clusterctl init does. -func workloadTask(name, workloadType, binaryName, containerName string, ts *tiltSettings, path string, getAdditionalObject func(string, []unstructured.Unstructured) (*unstructured.Unstructured, error)) taskFunction { +func workloadTask(name, workloadType, binaryName, containerName string, ts *tiltSettings, path, options string, getAdditionalObject func(string, []unstructured.Unstructured) (*unstructured.Unstructured, error)) taskFunction { return func(ctx context.Context, prefix string, errCh chan error) { - kustomizeCmd := exec.CommandContext(ctx, kustomizePath, "build", path) + args := []string{"build", path} + if options != "" { + args = []string{"build", options, path} + } + kustomizeCmd := exec.CommandContext(ctx, kustomizePath, args...) var stdout1, stderr1 bytes.Buffer kustomizeCmd.Dir = rootPath kustomizeCmd.Stdout = &stdout1 @@ -823,11 +840,14 @@ func prepareWorkload(name, prefix, binaryName, containerName string, objs []unst container.LivenessProbe = nil container.ReadinessProbe = nil + + container.Resources = corev1.ResourceRequirements{} } container.Command = cmd container.Args = args deployment.Spec.Template.Spec.Containers[j] = container + deployment.Spec.Replicas = pointer.Int32(1) } }) } From 48eaa76760aa67873b3286cd4bfde427d17b2bf5 Mon Sep 17 00:00:00 2001 From: fabriziopandini Date: Thu, 28 Dec 2023 15:47:05 +0100 Subject: [PATCH 2/3] fix linter issues --- hack/tools/internal/tilt-prepare/main.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/hack/tools/internal/tilt-prepare/main.go b/hack/tools/internal/tilt-prepare/main.go index d033c14d6f5d..562d022b968c 100644 --- a/hack/tools/internal/tilt-prepare/main.go +++ b/hack/tools/internal/tilt-prepare/main.go @@ -46,7 +46,6 @@ import ( "k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/tools/clientcmd" "k8s.io/klog/v2" - "k8s.io/utils/pointer" "k8s.io/utils/ptr" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/kustomize/api/types" @@ -346,9 +345,9 @@ func tiltResources(ctx context.Context, ts *tiltSettings) error { return errors.Errorf("failed to obtain config for the provider %s, please add the providers path to the provider_repos list in tilt-settings.yaml/json file", providerName) } if ptr.Deref(config.ApplyProviderYaml, true) { - kustomize_folder := path.Join(*config.Context, ptr.Deref(config.KustomizeFolder, "config/default")) - kustomize_options := ptr.Deref(config.KustomizeOptions, "") - tasks[providerName] = workloadTask(providerName, "provider", "manager", "manager", ts, kustomize_folder, kustomize_options, getProviderObj(config.Version)) + kustomizeFolder := path.Join(*config.Context, ptr.Deref(config.KustomizeFolder, "config/default")) + kustomizeOptions := ptr.Deref(config.KustomizeOptions, "") + tasks[providerName] = workloadTask(providerName, "provider", "manager", "manager", ts, kustomizeFolder, kustomizeOptions, getProviderObj(config.Version)) } } @@ -693,7 +692,7 @@ func workloadTask(name, workloadType, binaryName, containerName string, ts *tilt if options != "" { args = []string{"build", options, path} } - kustomizeCmd := exec.CommandContext(ctx, kustomizePath, args...) + kustomizeCmd := exec.CommandContext(ctx, kustomizePath, args...) //nolint: gosec var stdout1, stderr1 bytes.Buffer kustomizeCmd.Dir = rootPath kustomizeCmd.Stdout = &stdout1 @@ -847,7 +846,7 @@ func prepareWorkload(name, prefix, binaryName, containerName string, objs []unst container.Command = cmd container.Args = args deployment.Spec.Template.Spec.Containers[j] = container - deployment.Spec.Replicas = pointer.Int32(1) + deployment.Spec.Replicas = ptr.To[int32](1) } }) } From bd252f351f64c0f0cdd43ce81658983973917332 Mon Sep 17 00:00:00 2001 From: fabriziopandini Date: Mon, 8 Jan 2024 18:14:49 +0100 Subject: [PATCH 3/3] address comments --- docs/book/src/developer/tilt.md | 8 ++++---- hack/tools/internal/tilt-prepare/main.go | 23 +++++++++++------------ 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/docs/book/src/developer/tilt.md b/docs/book/src/developer/tilt.md index c333ffe98769..037a518721ac 100644 --- a/docs/book/src/developer/tilt.md +++ b/docs/book/src/developer/tilt.md @@ -430,12 +430,12 @@ COPY --from=tilt-helper /go/kubernetes/client/bin/kubectl /usr/bin/kubectl ``` **kustomize_folder** (String, default=config/default): The folder where the kustomize file for a provider -are defined; the path is relative to the provider root folder. +is defined; the path is relative to the provider root folder. -**kustomize_options** (String, default="""): Options to be applied when running kustomize for genereating -yaml manifest for a provider. e.g. `"kustomize_options": "--load-restrictor=LoadRestrictionsNone"` +**kustomize_options** ([]String, default=[]): Options to be applied when running kustomize for generating the +yaml manifest for a provider. e.g. `"kustomize_options": [ "--load-restrictor=LoadRestrictionsNone" ]` -**apply_provider_yaml** (Bool, default=true): Whether or not apply the provider yaml. +**apply_provider_yaml** (Bool, default=true): Whether to apply the provider yaml. Set to `false` if your provider does not have a ./config folder or you do not want it to be applied in the cluster. **go_main** (String, default="main.go"): The go main file if not located at the root of the folder diff --git a/hack/tools/internal/tilt-prepare/main.go b/hack/tools/internal/tilt-prepare/main.go index 562d022b968c..07b34a85f3c4 100644 --- a/hack/tools/internal/tilt-prepare/main.go +++ b/hack/tools/internal/tilt-prepare/main.go @@ -131,11 +131,11 @@ type tiltProvider struct { } type tiltProviderConfig struct { - Context *string `json:"context,omitempty"` - Version *string `json:"version,omitempty"` - ApplyProviderYaml *bool `json:"apply_provider_yaml,omitempty"` - KustomizeFolder *string `json:"kustomize_folder,omitempty"` - KustomizeOptions *string `json:"kustomize_options,omitempty"` + Context *string `json:"context,omitempty"` + Version *string `json:"version,omitempty"` + ApplyProviderYaml *bool `json:"apply_provider_yaml,omitempty"` + KustomizeFolder *string `json:"kustomize_folder,omitempty"` + KustomizeOptions []string `json:"kustomize_options,omitempty"` } func init() { @@ -346,7 +346,7 @@ func tiltResources(ctx context.Context, ts *tiltSettings) error { } if ptr.Deref(config.ApplyProviderYaml, true) { kustomizeFolder := path.Join(*config.Context, ptr.Deref(config.KustomizeFolder, "config/default")) - kustomizeOptions := ptr.Deref(config.KustomizeOptions, "") + kustomizeOptions := config.KustomizeOptions tasks[providerName] = workloadTask(providerName, "provider", "manager", "manager", ts, kustomizeFolder, kustomizeOptions, getProviderObj(config.Version)) } } @@ -686,13 +686,12 @@ func kustomizeTask(path, out string) taskFunction { // workloadTask generates a task for creating the component yaml for a workload and saving the output on a file. // NOTE: This task has several sub steps including running kustomize, envsubst, fixing components for debugging, // and adding the workload resource mimicking what clusterctl init does. -func workloadTask(name, workloadType, binaryName, containerName string, ts *tiltSettings, path, options string, getAdditionalObject func(string, []unstructured.Unstructured) (*unstructured.Unstructured, error)) taskFunction { +func workloadTask(name, workloadType, binaryName, containerName string, ts *tiltSettings, path string, options []string, getAdditionalObject func(string, []unstructured.Unstructured) (*unstructured.Unstructured, error)) taskFunction { return func(ctx context.Context, prefix string, errCh chan error) { - args := []string{"build", path} - if options != "" { - args = []string{"build", options, path} - } - kustomizeCmd := exec.CommandContext(ctx, kustomizePath, args...) //nolint: gosec + args := []string{"build"} + args = append(args, options...) + args = append(args, path) + kustomizeCmd := exec.CommandContext(ctx, kustomizePath, args...) var stdout1, stderr1 bytes.Buffer kustomizeCmd.Dir = rootPath kustomizeCmd.Stdout = &stdout1