Skip to content

Commit

Permalink
WIP. Create default passwords when dev mode is set. cnoe-io#441
Browse files Browse the repository at this point in the history
Signed-off-by: cmoulliard <[email protected]>
  • Loading branch information
cmoulliard committed Nov 8, 2024
1 parent 8c6cd97 commit 3a974cb
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
1 change: 1 addition & 0 deletions api/v1alpha1/localbuild_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions pkg/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var (

type Build struct {
name string
devMode bool
cfg v1alpha1.BuildCustomizationSpec
kindConfigPath string
kubeConfigPath string
Expand All @@ -43,6 +44,7 @@ type Build struct {

type NewBuildOptions struct {
Name string
DevMode bool
TemplateData v1alpha1.BuildCustomizationSpec
KindConfigPath string
KubeConfigPath string
Expand All @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions pkg/cmd/create/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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\")."
Expand All @@ -40,6 +41,7 @@ var (
// Flags
recreateCluster bool
buildName string
devMode bool
kubeVersion string
extraPortsMapping string
kindConfigPath string
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
17 changes: 12 additions & 5 deletions pkg/controllers/localbuild/gitea.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
)

const (
giteaDevModePassword = "developer"
// hardcoded values from what we have in the yaml installation file.
giteaNamespace = "gitea"
giteaAdminSecret = "gitea-credential"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 3a974cb

Please sign in to comment.