From 3a974cba169381c075be9563c10181ad7153280a Mon Sep 17 00:00:00 2001 From: cmoulliard Date: Fri, 8 Nov 2024 14:24:00 +0100 Subject: [PATCH] WIP. Create default passwords when dev mode is set. #441 Signed-off-by: cmoulliard --- api/v1alpha1/localbuild_types.go | 1 + pkg/build/build.go | 3 +++ pkg/cmd/create/root.go | 4 ++++ pkg/controllers/localbuild/gitea.go | 17 ++++++++++++----- 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/api/v1alpha1/localbuild_types.go b/api/v1alpha1/localbuild_types.go index 62d53ba6..0cc114f9 100644 --- a/api/v1alpha1/localbuild_types.go +++ b/api/v1alpha1/localbuild_types.go @@ -53,6 +53,7 @@ type BuildCustomizationSpec struct { Port string `json:"port,omitempty"` UsePathRouting bool `json:"usePathRouting,omitempty"` SelfSignedCert string `json:"selfSignedCert,omitempty"` + DevMode bool `json:"devMode,omitempty"` } type LocalbuildSpec struct { diff --git a/pkg/build/build.go b/pkg/build/build.go index 0da1cb4f..c6d4be99 100644 --- a/pkg/build/build.go +++ b/pkg/build/build.go @@ -28,6 +28,7 @@ var ( type Build struct { name string + devMode bool cfg v1alpha1.BuildCustomizationSpec kindConfigPath string kubeConfigPath string @@ -43,6 +44,7 @@ type Build struct { type NewBuildOptions struct { Name string + DevMode bool TemplateData v1alpha1.BuildCustomizationSpec KindConfigPath string KubeConfigPath string @@ -59,6 +61,7 @@ type NewBuildOptions struct { func NewBuild(opts NewBuildOptions) *Build { return &Build{ name: opts.Name, + devMode: opts.DevMode, kindConfigPath: opts.KindConfigPath, kubeConfigPath: opts.KubeConfigPath, kubeVersion: opts.KubeVersion, diff --git a/pkg/cmd/create/root.go b/pkg/cmd/create/root.go index 32212ccd..e3c15353 100644 --- a/pkg/cmd/create/root.go +++ b/pkg/cmd/create/root.go @@ -20,6 +20,7 @@ import ( const ( recreateClusterUsage = "Delete cluster first if it already exists." buildNameUsage = "Name for build (Prefix for kind cluster name, pod names, etc)." + devModeUsage = "When enabled, the platform will run the core packages with developer password." kubeVersionUsage = "Version of the kind kubernetes cluster to create." extraPortsMappingUsage = "List of extra ports to expose on the docker container and kubernetes cluster as nodePort " + "(e.g. \"22:32222,9090:39090,etc\")." @@ -40,6 +41,7 @@ var ( // Flags recreateCluster bool buildName string + devMode bool kubeVersion string extraPortsMapping string kindConfigPath string @@ -67,6 +69,7 @@ func init() { CreateCmd.PersistentFlags().StringVar(&buildName, "build-name", "localdev", buildNameUsage) CreateCmd.PersistentFlags().MarkDeprecated("build-name", "use --name instead.") CreateCmd.PersistentFlags().StringVar(&buildName, "name", "localdev", buildNameUsage) + CreateCmd.PersistentFlags().BoolVar(&devMode, "dev", false, devModeUsage) CreateCmd.PersistentFlags().StringVar(&kubeVersion, "kube-version", "v1.30.3", kubeVersionUsage) CreateCmd.PersistentFlags().StringVar(&extraPortsMapping, "extra-ports", "", extraPortsMappingUsage) CreateCmd.PersistentFlags().StringVar(&kindConfigPath, "kind-config", "", kindConfigPathUsage) @@ -132,6 +135,7 @@ func create(cmd *cobra.Command, args []string) error { opts := build.NewBuildOptions{ Name: buildName, + DevMode: devMode, KubeVersion: kubeVersion, KubeConfigPath: kubeConfigPath, KindConfigPath: kindConfigPath, diff --git a/pkg/controllers/localbuild/gitea.go b/pkg/controllers/localbuild/gitea.go index 79f844c8..7901a419 100644 --- a/pkg/controllers/localbuild/gitea.go +++ b/pkg/controllers/localbuild/gitea.go @@ -24,6 +24,7 @@ import ( ) const ( + giteaDevModePassword = "developer" // hardcoded values from what we have in the yaml installation file. giteaNamespace = "gitea" giteaAdminSecret = "gitea-credential" @@ -56,11 +57,17 @@ func giteaAdminSecretObject() corev1.Secret { } } -func newGiteaAdminSecret() (corev1.Secret, error) { - pass, err := util.GeneratePassword() - if err != nil { - return corev1.Secret{}, err +func newGiteaAdminSecret(devMode bool) (corev1.Secret, error) { + // Reuse the same password when dev mode is enabled + pass := giteaDevModePassword + if !devMode { + var err error + pass, err = util.GeneratePassword() + if err != nil { + return corev1.Secret{}, err + } } + obj := giteaAdminSecretObject() obj.StringData = map[string]string{ "username": v1alpha1.GiteaAdminUserName, @@ -93,7 +100,7 @@ func (r *LocalbuildReconciler) ReconcileGitea(ctx context.Context, req ctrl.Requ if err != nil { if k8serrors.IsNotFound(err) { - giteaCreds, err := newGiteaAdminSecret() + giteaCreds, err := newGiteaAdminSecret(resource.Spec.BuildCustomization.DevMode) if err != nil { return ctrl.Result{}, fmt.Errorf("generating gitea admin secret: %w", err) }