Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dev-setup): add option to specify image version #603

Merged
merged 8 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -209,23 +209,27 @@ $(KUSTOMIZE): $(LOCALBIN)
test -s $(LOCALBIN)/kustomize || curl -s $(KUSTOMIZE_INSTALL_SCRIPT) | bash -s -- $(subst v,,$(KUSTOMIZE_VERSION)) $(LOCALBIN)

.PHONY: action-controllergen
action-controllergen: $(CONTROLLER_GEN_ACTION) ## Download controller-gen locally if necessary.
$(CONTROLLER_GEN_ACTION): $(LOCALBIN)
GOPATH=$(shell pwd) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)
action-controllergen:: $(CONTROLLER_GEN_ACTION) ## Download controller-gen locally if necessary.
$(CONTROLLER_GEN_ACTION):: $(LOCALBIN)
GOMODCACHE=$(shell pwd)/tmp GOPATH=$(shell pwd) go install -modcacherw sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)
GOMODCACHE=$(shell pwd)/tmp go clean -modcache
rm -rf $(shell pwd)/pkg/sumdb/

.PHONY: controller-gen
controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary.
$(CONTROLLER_GEN): $(LOCALBIN)
controller-gen:: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary.
$(CONTROLLER_GEN):: $(LOCALBIN)
GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)

.PHONY: action-envtest
action-envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
$(ENVTEST_ACTION): $(LOCALBIN)
GOPATH=$(shell pwd) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
action-envtest:: $(ENVTEST) ## Download envtest-setup locally if necessary.
$(ENVTEST_ACTION):: $(LOCALBIN)
GOMODCACHE=$(shell pwd)/tmp GOPATH=$(shell pwd) go install -modcacherw sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
GOMODCACHE=$(shell pwd)/tmp go clean -modcache
rm -rf $(shell pwd)/pkg/sumdb/

.PHONY: envtest
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
$(ENVTEST): $(LOCALBIN)
envtest:: $(ENVTEST) ## Download envtest-setup locally if necessary.
$(ENVTEST):: $(LOCALBIN)
GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest

.PHONY: goimports
Expand Down
5 changes: 3 additions & 2 deletions pkg/internal/local/commands/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var (
Use: "create",
Short: "Create a kinD cluster",
Long: "Create a kinD cluster and setup the greenhouse namespace optionally",
Example: `greenhousectl dev cluster create --name <my-cluster-name> --namespace <my-namespace>`,
Example: `greenhousectl dev cluster create --name <my-cluster-name> --namespace <my-namespace> --version <v1.30.3>`,
DisableAutoGenTag: true,
PreRunE: func(cmd *cobra.Command, args []string) error {
return validateFlagInputs(cmd.Flags())
Expand Down Expand Up @@ -50,7 +50,7 @@ func processDeleteLocalCluster(_ *cobra.Command, _ []string) error {
}

func processCreateLocalCluster(_ *cobra.Command, _ []string) error {
err := setup.NewExecutionEnv().WithClusterSetup(clusterName, namespaceName).Run()
err := setup.NewExecutionEnv().WithClusterSetup(clusterName, namespaceName, clusterVersion).Run()
if err != nil {
return err
}
Expand All @@ -60,6 +60,7 @@ func processCreateLocalCluster(_ *cobra.Command, _ []string) error {
func init() {
createClusterCmd.Flags().StringVarP(&clusterName, "name", "c", "", "create a kind cluster with a name - e.g. -c <my-cluster>")
createClusterCmd.Flags().StringVarP(&namespaceName, "namespace", "n", "", "create a namespace in the cluster - e.g. -c <my-cluster> -n <my-namespace>")
createClusterCmd.Flags().StringVar(&clusterVersion, "version", "", "create the cluster with a specific version - e.g. -v <v1.30.3>")
deleteClusterCmd.Flags().StringVarP(&clusterName, "name", "c", "", "delete the kind cluster - e.g. -c <my-cluster>")
cobra.CheckErr(createClusterCmd.MarkFlagRequired("name"))
cobra.CheckErr(deleteClusterCmd.MarkFlagRequired("name"))
Expand Down
17 changes: 9 additions & 8 deletions pkg/internal/local/commands/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ import (
)

var (
clusterName string
namespaceName string
dockerFile string
releaseName string
chartPath string
valuesPath string
crdOnly bool
excludeKinds []string
clusterName string
namespaceName string
clusterVersion string
dockerFile string
releaseName string
chartPath string
valuesPath string
crdOnly bool
excludeKinds []string
)

func GetLocalSetupCommands() []*cobra.Command {
Expand Down
3 changes: 2 additions & 1 deletion pkg/internal/local/commands/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func processManifests(cmd *cobra.Command, _ []string) error {
Webhook: nil,
}
err := setup.NewExecutionEnv().
WithClusterSetup(clusterName, namespaceName).
WithClusterSetup(clusterName, namespaceName, clusterVersion).
WithLimitedManifests(ctx, manifest).
Run()
if err != nil {
Expand All @@ -61,6 +61,7 @@ func init() {
// required flags
manifestCmd.Flags().StringVarP(&clusterName, "name", "c", "", "Name of the kind cluster - e.g. greenhouse-123 (without the kind prefix)")
manifestCmd.Flags().StringVarP(&namespaceName, "namespace", "n", "", "namespace to install the resources")
manifestCmd.Flags().StringVar(&clusterVersion, "version", "", "create the cluster with a specific version - e.g. -v <v1.30.3>")
manifestCmd.Flags().StringVarP(&chartPath, "chart-path", "p", "", "local absolute chart path where manifests are located - e.g. <path>/<to>/charts/manager")
manifestCmd.Flags().StringVarP(&releaseName, "release", "r", "greenhouse", "Helm release name, Default value: greenhouse - e.g. your-release-name")
// optional flags
Expand Down
2 changes: 1 addition & 1 deletion pkg/internal/local/commands/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func processSetup(cmd *cobra.Command, _ []string) error {
namespace = *cfg.Cluster.Namespace
}
env := setup.NewExecutionEnv().
WithClusterSetup(cfg.Cluster.Name, namespace)
WithClusterSetup(cfg.Cluster.Name, namespace, cfg.Cluster.Version)
for _, dep := range cfg.Dependencies {
if dep.Manifest != nil && dep.Manifest.Webhook == nil {
env = env.WithLimitedManifests(ctx, dep.Manifest)
Expand Down
3 changes: 2 additions & 1 deletion pkg/internal/local/commands/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func processWebhook(cmd *cobra.Command, _ []string) error {
}

err := setup.NewExecutionEnv().
WithClusterSetup(clusterName, namespaceName).
WithClusterSetup(clusterName, namespaceName, clusterVersion).
WithWebhookDevelopment(ctx, manifest).
Run()
if err != nil {
Expand All @@ -71,6 +71,7 @@ func processWebhook(cmd *cobra.Command, _ []string) error {
func init() {
webhookCmd.Flags().StringVarP(&clusterName, "name", "c", "", "Name of the kind cluster - e.g. my-cluster (without the kind prefix)")
webhookCmd.Flags().StringVarP(&namespaceName, "namespace", "n", "", "namespace to install the resources")
webhookCmd.Flags().StringVar(&clusterVersion, "version", "", "create the cluster with a specific version - e.g. -v <v1.30.3>")
webhookCmd.Flags().StringVarP(&chartPath, "chart-path", "p", "", "local chart path where manifests are located - e.g. <path>/<to>/charts/manager")
webhookCmd.Flags().StringVarP(&valuesPath, "values-path", "v", "", "local absolute values file path - e.g. <path>/<to>/my-values.yaml")
webhookCmd.Flags().StringVarP(&dockerFile, "dockerfile", "f", "", "local path to the Dockerfile of greenhouse manager")
Expand Down
25 changes: 18 additions & 7 deletions pkg/internal/local/klient/kind.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

// CreateCluster - creates a kind cluster with the given name
// if the cluster already exists, it sets the context to the existing cluster
func CreateCluster(clusterName string) error {
func CreateCluster(clusterName, clusterVersion string) error {
exists, err := clusterExists(clusterName)
if err != nil {
return err
Expand All @@ -22,12 +22,23 @@ func CreateCluster(clusterName string) error {
utils.Logf("kind cluster with name %s already exists", clusterName)
return nil
}
return utils.Shell{
Cmd: "kind create cluster --name ${name}",
Vars: map[string]string{
"name": clusterName,
},
}.Exec()
if clusterVersion == "" {
return utils.Shell{
Cmd: "kind create cluster --name ${name}",
Vars: map[string]string{
"name": clusterName,
},
}.Exec()
} else {
version := "kindest/node:" + clusterVersion
return utils.Shell{
Cmd: "kind create cluster --name ${name} --image ${image}",
Vars: map[string]string{
"name": clusterName,
"image": version,
},
}.Exec()
}
}

// DeleteCluster - deletes a kind cluster with the given name
Expand Down
3 changes: 2 additions & 1 deletion pkg/internal/local/setup/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
type Cluster struct {
Name string `json:"name"`
Namespace *string `json:"namespace"`
Version string `json:"version"`
kubeConfigPath string
}

Expand All @@ -24,7 +25,7 @@ func clusterSetup(env *ExecutionEnv) error {
if env.cluster == nil {
return errors.New("cluster configuration is missing")
}
err := klient.CreateCluster(env.cluster.Name)
err := klient.CreateCluster(env.cluster.Name, env.cluster.Version)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/internal/local/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ func NewExecutionEnv() *ExecutionEnv {
}
}

func (env *ExecutionEnv) WithClusterSetup(name, namespace string) *ExecutionEnv {
func (env *ExecutionEnv) WithClusterSetup(name, namespace, version string) *ExecutionEnv {
env.cluster = &Cluster{
Name: name,
Namespace: nil,
Version: version,
}
if strings.TrimSpace(namespace) != "" {
env.cluster.Namespace = &namespace
Expand Down
Loading