Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Registry config command #2220

Merged
merged 3 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions internal/cmd/alpha/alpha.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package alpha

import (
"context"
"github.com/kyma-project/cli.v3/internal/cmd/alpha/registry"

"github.com/kyma-project/cli.v3/internal/cmd/alpha/access"
"github.com/kyma-project/cli.v3/internal/cmd/alpha/add"
Expand Down Expand Up @@ -38,6 +39,7 @@ func NewAlphaCMD() *cobra.Command {
cmd.AddCommand(modules.NewModulesCMD(config))
cmd.AddCommand(add.NewAddCMD(config))
cmd.AddCommand(remove.NewRemoveCMD(config))
cmd.AddCommand(registry.NewRegistryCMD(config))

return cmd
}
65 changes: 65 additions & 0 deletions internal/cmd/alpha/registry/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package config

import (
"fmt"
"github.com/kyma-project/cli.v3/internal/clierror"
"github.com/kyma-project/cli.v3/internal/cmdcommon"
"github.com/kyma-project/cli.v3/internal/registry"
"github.com/spf13/cobra"
"os"
)

type cfgConfig struct {
*cmdcommon.KymaConfig
cmdcommon.KubeClientConfig

dockerconfig bool
externalurl bool
output string
}

func NewConfigCMD(kymaConfig *cmdcommon.KymaConfig) *cobra.Command {
cfg := cfgConfig{
KymaConfig: kymaConfig,
KubeClientConfig: cmdcommon.KubeClientConfig{},
}

cmd := &cobra.Command{
Use: "config",
Short: "Saves Kyma registry dockerconfig to a file",
Long: "Use this command to save Kyma registry dockerconfig to a file",
PreRun: func(_ *cobra.Command, _ []string) {
clierror.Check(cfg.KubeClientConfig.Complete())
},
Run: func(_ *cobra.Command, _ []string) {
clierror.Check(runConfig(&cfg))
},
}

cfg.KubeClientConfig.AddFlag(cmd)
cmd.Flags().BoolVar(&cfg.dockerconfig, "dockerconfig", false, "Generate a docker config.json file for the Kyma registry")
Cortey marked this conversation as resolved.
Show resolved Hide resolved
cmd.Flags().BoolVar(&cfg.externalurl, "externalurl", false, "External URL for the Kyma registry")
Cortey marked this conversation as resolved.
Show resolved Hide resolved
cmd.Flags().StringVar(&cfg.output, "output", "config.json", "Path where the output file should be saved to")

return cmd
}

func runConfig(cfg *cfgConfig) clierror.Error {
registryConfig, err := registry.GetConfig(cfg.Ctx, cfg.KubeClient)
Cortey marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return clierror.WrapE(err, clierror.New("failed to load in-cluster registry configuration"))
}

if cfg.dockerconfig {
writeErr := os.WriteFile(cfg.output, []byte(registryConfig.SecretData.DockerConfigJSON), os.ModePerm)
if writeErr != nil {
return clierror.New("failed to write docker config to file")
}
fmt.Print("Docker config saved to ", cfg.output)
}

if cfg.externalurl {
fmt.Print(registryConfig.SecretData.PushRegAddr)
Cortey marked this conversation as resolved.
Show resolved Hide resolved
}
return nil
}
20 changes: 20 additions & 0 deletions internal/cmd/alpha/registry/registry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package registry

import (
"github.com/kyma-project/cli.v3/internal/cmd/alpha/registry/config"
"github.com/kyma-project/cli.v3/internal/cmdcommon"
"github.com/spf13/cobra"
)

func NewRegistryCMD(kymaConfig *cmdcommon.KymaConfig) *cobra.Command {

cmd := &cobra.Command{
Use: "registry",
Short: "Set of commands for Kyma registry",
Long: `Use this command manage resources related to Kyma registry`,
}

cmd.AddCommand(config.NewConfigCMD(kymaConfig))

return cmd
}