Skip to content

Commit

Permalink
Addition of Generic Remote Registry Flag to init commands to support …
Browse files Browse the repository at this point in the history
…Azure Quickstart (#1)
  • Loading branch information
BoLB23 authored Feb 7, 2024
1 parent 0eb9663 commit c72145b
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 32 deletions.
3 changes: 3 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
67 changes: 44 additions & 23 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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")
Expand Down Expand Up @@ -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()
}

Expand All @@ -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() {
Expand Down
17 changes: 10 additions & 7 deletions internal/config/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion internal/repo/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})

Expand Down
2 changes: 1 addition & 1 deletion internal/repo/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit c72145b

Please sign in to comment.