Skip to content

Commit

Permalink
🌱 Small improvements to tilt (#9936)
Browse files Browse the repository at this point in the history
* small improvements to tilt

* fix linter issues

* address comments
  • Loading branch information
fabriziopandini committed Jan 8, 2024
1 parent 6d1cd8d commit 04cdfa6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Tiltfile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 7 additions & 1 deletion docs/book/src/developer/tilt.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
is defined; the path is relative to the provider root folder.

**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 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
Expand Down
32 changes: 25 additions & 7 deletions hack/tools/internal/tilt-prepare/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"io/fs"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"sync"
Expand All @@ -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"
Expand Down Expand Up @@ -129,8 +131,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() {
Expand Down Expand Up @@ -339,7 +344,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) {
kustomizeFolder := path.Join(*config.Context, ptr.Deref(config.KustomizeFolder, "config/default"))
kustomizeOptions := config.KustomizeOptions
tasks[providerName] = workloadTask(providerName, "provider", "manager", "manager", ts, kustomizeFolder, kustomizeOptions, getProviderObj(config.Version))
}
}

return runTaskGroup(ctx, "resources", tasks)
Expand All @@ -361,8 +370,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
Expand Down Expand Up @@ -674,9 +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 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) {
kustomizeCmd := exec.CommandContext(ctx, kustomizePath, "build", path)
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
Expand Down Expand Up @@ -823,11 +838,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 = ptr.To[int32](1)
}
})
}
Expand Down

0 comments on commit 04cdfa6

Please sign in to comment.