From a9a463bc557835ff7ddd21ee4dfbbf88c427cf72 Mon Sep 17 00:00:00 2001 From: Patrick Derks Date: Fri, 11 Oct 2024 17:05:10 +0200 Subject: [PATCH 1/2] feat: use setupscript instead of hooks This makes it possible to use specific commands and check the output for the setup command. The default will be /setup. --- api/v1/store.go | 6 ++++++ internal/controller/store_controller.go | 3 ++- internal/job/migration.go | 13 ++----------- internal/job/setup.go | 13 ++----------- 4 files changed, 12 insertions(+), 23 deletions(-) diff --git a/api/v1/store.go b/api/v1/store.go index c4dd48d..c60a291 100644 --- a/api/v1/store.go +++ b/api/v1/store.go @@ -56,9 +56,15 @@ type StoreSpec struct { // +kubebuilder:default={username: "admin", password: ""} AdminCredentials Credentials `json:"adminCredentials"` + //+kubebuilder:deprecatedversion SetupHook Hook `json:"setupHook,omitempty"` + // +kubebuilder:default=/setup + SetupScript string `json:"setupScript,omitempty"` + //+kubebuilder:deprecatedversion MigrationHook Hook `json:"migrationHook,omitempty"` + // +kubebuilder:default=/setup + MigrationScript string `json:"migrationScript,omitempty"` } func init() { diff --git a/internal/controller/store_controller.go b/internal/controller/store_controller.go index 377a1df..c68c42d 100644 --- a/internal/controller/store_controller.go +++ b/internal/controller/store_controller.go @@ -141,6 +141,7 @@ func (r *StoreReconciler) doReconcile( WithValues("state", store.Status.State) log.Info("Do reconcile on store") + log.Info("reconcile app secrets") if err := r.ensureAppSecrets(ctx, store); err != nil { return fmt.Errorf("app secrets: %w", err) } @@ -318,7 +319,7 @@ func (r *StoreReconciler) reconcileServices(ctx context.Context, store *v1.Store store.Namespace, obj.Labels["app"])) if err := k8s.EnsureService(ctx, r.Client, store, obj, r.Scheme, true); err != nil { - return fmt.Errorf("reconcile unready deployment: %w", err) + return fmt.Errorf("reconcile unready services: %w", err) } } } diff --git a/internal/job/migration.go b/internal/job/migration.go index 6c9a9bd..e62f869 100644 --- a/internal/job/migration.go +++ b/internal/job/migration.go @@ -55,22 +55,13 @@ func MigrationJob(store *v1.Store) *batchv1.Job { } maps.Copy(annotations, store.Spec.Container.Annotations) - var commandString string - if store.Spec.SetupHook.Before != "" { - commandString = store.Spec.SetupHook.Before - } - commandString = fmt.Sprintf("%s %s", commandString, "/setup;") - if store.Spec.SetupHook.After != "" { - commandString = fmt.Sprintf("%s %s", commandString, store.Spec.SetupHook.After) - } - containers := append(store.Spec.Container.ExtraContainers, corev1.Container{ Name: CONTAINER_NAME_MIGRATION_JOB, VolumeMounts: store.Spec.Container.VolumeMounts, ImagePullPolicy: store.Spec.Container.ImagePullPolicy, Image: store.Spec.Container.Image, - Command: []string{"sh"}, - Args: []string{"-c", commandString}, + Command: []string{"sh", "-c"}, + Args: []string{store.Spec.MigrationScript}, Env: store.GetEnv(), }) diff --git a/internal/job/setup.go b/internal/job/setup.go index b70393a..30453dc 100644 --- a/internal/job/setup.go +++ b/internal/job/setup.go @@ -39,15 +39,6 @@ func SetupJob(store *v1.Store) *batchv1.Job { } maps.Copy(labels, util.GetDefaultLabels(store)) - var commandString string - if store.Spec.SetupHook.Before != "" { - commandString = store.Spec.SetupHook.Before - } - commandString = fmt.Sprintf("%s %s", commandString, "/setup;") - if store.Spec.SetupHook.After != "" { - commandString = fmt.Sprintf("%s %s", commandString, store.Spec.SetupHook.After) - } - envs := append(store.GetEnv(), corev1.EnvVar{ Name: "INSTALL_ADMIN_PASSWORD", @@ -71,8 +62,8 @@ func SetupJob(store *v1.Store) *batchv1.Job { VolumeMounts: store.Spec.Container.VolumeMounts, ImagePullPolicy: store.Spec.Container.ImagePullPolicy, Image: store.Spec.Container.Image, - Command: []string{"sh"}, - Args: []string{"-c", commandString}, + Command: []string{"sh", "-c"}, + Args: []string{store.Spec.SetupScript}, Env: envs, }) From 95f3a233c11335a4853146240edb620afc13eb59 Mon Sep 17 00:00:00 2001 From: Patrick Derks Date: Fri, 11 Oct 2024 17:05:26 +0200 Subject: [PATCH 2/2] fix: cleanup for Makefile --- Makefile | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index bddeabb..d1a6a15 100644 --- a/Makefile +++ b/Makefile @@ -80,16 +80,8 @@ manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and Cust generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations. $(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..." -.PHONY: fmt -fmt: ## Run go fmt against code. - go fmt ./... - -.PHONY: vet -vet: ## Run go vet against code. - go vet ./... - .PHONY: test -test: manifests generate fmt vet envtest ## Run tests. +test: manifests generate envtest ## Run tests. KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... -coverprofile cover.out GOLANGCI_LINT = $(shell pwd)/bin/golangci-lint @@ -111,11 +103,11 @@ lint-fix: golangci-lint ## Run golangci-lint linter and perform fixes ##@ Build .PHONY: build -build: manifests generate fmt vet ## Build manager binary. +build: manifests generate ## Build manager binary. go build -o bin/manager cmd/manager.go .PHONY: run -run: manifests generate fmt vet zap-pretty ## Run a controller from your host. +run: manifests generate zap-pretty ## Run a controller from your host. go run ./cmd/manager.go --namespace ${NAMESPACE} --disable-checks --debug 2>&1 | $(ZAP_PRETTY) --all # If you wish to build the manager image targeting other platforms you can use the --platform flag. @@ -154,7 +146,7 @@ endif .PHONY: install install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config. - $(KUSTOMIZE) build config/crd | $(KUBECTL) apply -f - + $(KUSTOMIZE) build config/crd | $(KUBECTL) apply --namespace ${NAMESPACE} -f - .PHONY: uninstall uninstall: manifests kustomize ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.