diff --git a/cmd/root.go b/cmd/root.go index 96bc45f..80a40d0 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -41,6 +41,9 @@ func init() { rootCmd.PersistentFlags().StringVarP(&cfg.Flags.OutFormat, "output", "o", "yaml", `output format, one of ["json", "yaml"]`) rootCmd.PersistentFlags().BoolVarP(&cfg.Flags.Info, "info", "i", false, "enable info output") rootCmd.PersistentFlags().BoolVarP(&cfg.Flags.Verbose, "verbose", "v", false, "enable verbose output") + rootCmd.PersistentFlags().StringVarP(&cfg.Flags.RegistryAddress, "registry-address", "", "", `address of your container registry`) + rootCmd.PersistentFlags().StringVarP(&cfg.Flags.RegistryToken, "registry-token", "", "", `access token for your container registry`) + rootCmd.PersistentFlags().StringVarP(&cfg.Flags.RegistryUsername, "registry-username", "", "", `username for your container registry`) } func initViper(cmd *cobra.Command, args []string) { diff --git a/internal/config/config.go b/internal/config/config.go index e5c5d76..ac58b3b 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -81,8 +81,9 @@ type Kind struct { } type ContainerRegistry struct { - Address string `json:"address" validate:"required"` - Token string `json:"token"` + Address string `json:"address" validate:"required"` + Token string `json:"token"` + Username string `json:"username"` } func (cfg *Config) IsRegistryLocal() bool { @@ -99,14 +100,14 @@ func (cfg *Config) Load() { log.Verbose("Loading Kubefox config from '%s'", cfg.path) b, err := os.ReadFile(cfg.path) + if errors.Is(err, fs.ErrNotExist) { if cfg.Flags.Quickstart { - cfg.setupKind("kind") + cfg.setupQuickstart("kind") cfg.Fresh = true cfg.Write() return } - log.Info("It looks like this is the first time you are using 🦊 Fox. Welcome!") log.InfoNewline() log.Info("🦊 Fox needs some information from you to configure itself. The setup process only") @@ -176,26 +177,14 @@ func (cfg *Config) Setup() { kindOnly := utils.YesNoPrompt("Are you only using KubeFox with local kind cluster?", false) if kindOnly { name := utils.NamePrompt("kind cluster", "kind", true) - cfg.setupKind(name) + cfg.setupQuickstart(name) log.InfoNewline() cfg.done() return } log.InfoNewline() - log.Info("Great! If you don't already have a container registry 🦊 Fox can help setup the") - log.Info("GitHub container registry (ghcr.io).") - useGH := utils.YesNoPrompt("Would you like to use ghcr.io?", true) + cfg.setupRegistry() log.InfoNewline() - if useGH { - cfg.setupGitHub() - } else { - log.Info("No problem. 🦊 Fox just needs to know which container registry to use. Please be") - log.Info("sure you have permissions to pull and push images to the registry.") - cfg.ContainerRegistry.Address = utils.InputPrompt("Enter the container registry you'd like to use", "", true) - cfg.ContainerRegistry.Token = utils.InputPrompt("Enter the container registry access token", "", false) - } - log.InfoNewline() - cfg.done() } @@ -209,11 +198,43 @@ func (cfg *Config) done() { log.Info("If you run into any problems please let us know on GitHub (https://github.com/xigxog/kubefox/issues).") } -func (cfg *Config) setupKind(name string) { - cfg.ContainerRegistry.Address = LocalRegistry - cfg.ContainerRegistry.Token = "" - cfg.Kind.ClusterName = name - cfg.Kind.AlwaysLoad = true +func (cfg *Config) setupQuickstart(name string) { + if cfg.Flags.RegistryAddress != "" { + cfg.ContainerRegistry.Address = cfg.Flags.RegistryAddress + cfg.Kind.AlwaysLoad = false + + } else { + cfg.ContainerRegistry.Address = LocalRegistry + cfg.Kind.ClusterName = name + cfg.Kind.AlwaysLoad = true + } + cfg.ContainerRegistry.Token = cfg.Flags.RegistryToken + cfg.ContainerRegistry.Username = cfg.Flags.RegistryUsername + +} + +func (cfg *Config) setupRegistry() { + if cfg.Flags.RegistryAddress != "" { + log.Info("Remote registry information provided. Setting the remote registry %s", cfg.Flags.RegistryAddress) + cfg.ContainerRegistry.Address = cfg.Flags.RegistryAddress + cfg.ContainerRegistry.Token = cfg.Flags.RegistryToken + cfg.ContainerRegistry.Username = cfg.Flags.RegistryUsername + return + } + log.Info("If you don't already have a container registry 🦊 Fox can help setup the") + log.Info("GitHub container registry (ghcr.io).") + useGH := utils.YesNoPrompt("Would you like to use ghcr.io?", true) + log.InfoNewline() + if useGH { + cfg.setupGitHub() + return + } + log.Info("🦊 Fox just needs to know which container registry to use. Please be") + log.Info("sure you have permissions to pull and push images to the registry.") + cfg.ContainerRegistry.Address = utils.InputPrompt("Enter the container registry endpoint you'd like to use", "", true) + cfg.ContainerRegistry.Username = utils.InputPrompt("Enter the container registry username (if required)", "", false) + cfg.ContainerRegistry.Token = utils.InputPrompt("Enter the container registry access token or password", "", true) + return } func (cfg *Config) setupGitHub() { diff --git a/internal/config/flags.go b/internal/config/flags.go index 83a90db..98034d4 100644 --- a/internal/config/flags.go +++ b/internal/config/flags.go @@ -20,13 +20,16 @@ type Flags struct { Verbose bool // flags used by subcommands - AppDeployment string - Builder string - Kind string - Namespace string - Platform string - Version string - VirtEnv string + AppDeployment string + Builder string + Kind string + Namespace string + Platform string + Version string + VirtEnv string + RegistryAddress string + RegistryUsername string + RegistryToken string CreateTag bool ForceBuild bool diff --git a/internal/repo/build.go b/internal/repo/build.go index 6b9f7aa..f56dc05 100644 --- a/internal/repo/build.go +++ b/internal/repo/build.go @@ -215,8 +215,12 @@ func (r *repo) GetRegAuth() string { if r.cfg.GitHub.Token != "" { token = r.cfg.GitHub.Token } + user := r.cfg.ContainerRegistry.Username + if user == "" { + user = "kubefox" + } authCfg, _ := json.Marshal(registry.AuthConfig{ - Username: "kubefox", + Username: user, Password: token, }) diff --git a/internal/repo/deploy.go b/internal/repo/deploy.go index a8dd261..aa4a257 100644 --- a/internal/repo/deploy.go +++ b/internal/repo/deploy.go @@ -109,7 +109,7 @@ func (r *repo) applyIPS(ctx context.Context, p *v1alpha1.Platform, spec *v1alpha if r.cfg.ContainerRegistry.Token != "" { cr := r.cfg.ContainerRegistry name := fmt.Sprintf("%s-image-pull-secret", spec.AppName) - dockerCfg := fmt.Sprintf(`{"auths":{"%s":{"username":"kubefox","password":"%s"}}}`, cr.Address, cr.Token) + dockerCfg := fmt.Sprintf(`{"auths":{"%s":{"username":"%s","password":"%s"}}}`, cr.Address, cr.Username, cr.Token) s := &corev1.Secret{ TypeMeta: metav1.TypeMeta{