Skip to content

Commit

Permalink
Merge all the proxy install and upgrade flags into a single struct
Browse files Browse the repository at this point in the history
In order to share code and bring consistency to the code, use a single
structure for all the common proxy install and upgrade flags. The
structs for Kubernetes flags are also merged into one.
  • Loading branch information
cbosdo committed Nov 13, 2024
1 parent a24cc43 commit 2a9f8f0
Show file tree
Hide file tree
Showing 13 changed files with 38 additions and 38 deletions.
10 changes: 2 additions & 8 deletions mgrpxy/cmd/install/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,7 @@ import (
"github.com/uyuni-project/uyuni-tools/shared/utils"
)

type kubernetesProxyInstallFlags struct {
pxy_utils.ProxyImageFlags `mapstructure:",squash"`
Helm kubernetes.HelmFlags
SCC types.SCCCredentials
}

func newCmd(globalFlags *types.GlobalFlags, run utils.CommandFunc[kubernetesProxyInstallFlags]) *cobra.Command {
func newCmd(globalFlags *types.GlobalFlags, run utils.CommandFunc[kubernetes.ProxyFlags]) *cobra.Command {
cmd := &cobra.Command{
Use: "kubernetes [path/to/config.tar.gz]",
Short: L("Install a new proxy on a running kubernetes cluster"),
Expand All @@ -34,7 +28,7 @@ NOTE: for now installing on a remote kubernetes cluster is not supported!
`),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
var flags kubernetesProxyInstallFlags
var flags kubernetes.ProxyFlags
return utils.CommandHelper(globalFlags, cmd, args, &flags, nil, run)
},
}
Expand Down
5 changes: 3 additions & 2 deletions mgrpxy/cmd/install/kubernetes/kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

"github.com/spf13/cobra"
"github.com/uyuni-project/uyuni-tools/mgrpxy/shared/kubernetes"
"github.com/uyuni-project/uyuni-tools/shared/testutils"
"github.com/uyuni-project/uyuni-tools/shared/testutils/flagstests"
"github.com/uyuni-project/uyuni-tools/shared/types"
Expand All @@ -23,8 +24,8 @@ func TestParamsParsing(t *testing.T) {
args = append(args, flagstests.SCCFlagTestArgs...)

// Test function asserting that the args are properly parsed
tester := func(globalFlags *types.GlobalFlags, flags *kubernetesProxyInstallFlags,
cmd *cobra.Command, args []string,
tester := func(_ *types.GlobalFlags, flags *kubernetes.ProxyFlags,
cmd *cobra.Command, _ []string,
) error {
flagstests.AssertProxyImageFlags(t, cmd, &flags.ProxyImageFlags)
flagstests.AssertProxyHelmFlags(t, cmd, &flags.Helm)
Expand Down
4 changes: 3 additions & 1 deletion mgrpxy/cmd/install/kubernetes/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
)

func installForKubernetes(globalFlags *types.GlobalFlags,
flags *kubernetesProxyInstallFlags, cmd *cobra.Command, args []string,
flags *kubernetes.ProxyFlags, cmd *cobra.Command, args []string,
) error {
for _, binary := range []string{"kubectl", "helm"} {
if _, err := exec.LookPath(binary); err != nil {
Expand All @@ -28,6 +28,7 @@ func installForKubernetes(globalFlags *types.GlobalFlags,
}

// Unpack the tarball
// This would not be in an operator as the configuration would be expected to be set up before.
configPath := utils.GetConfigPath(args)

tmpDir, cleaner, err := shared_utils.TempDir()
Expand All @@ -47,6 +48,7 @@ func installForKubernetes(globalFlags *types.GlobalFlags,
}

// If installing on k3s, install the traefik helm config in manifests
// In an operator, this would be delegated to the cluster admin.
isK3s := clusterInfos.IsK3s()
IsRke2 := clusterInfos.IsRke2()
ports := shared_utils.GetProxyPorts()
Expand Down
8 changes: 2 additions & 6 deletions mgrpxy/cmd/support/ptf/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ import (
"github.com/uyuni-project/uyuni-tools/shared/utils"
)

type kubernetesPTFFlags struct {
UpgradeFlags kubernetes.KubernetesProxyUpgradeFlags `mapstructure:",squash"`
}

func newCmd(globalFlags *types.GlobalFlags, run utils.CommandFunc[kubernetesPTFFlags]) *cobra.Command {
func newCmd(globalFlags *types.GlobalFlags, run utils.CommandFunc[kubernetes.ProxyFlags]) *cobra.Command {
kubernetesCmd := &cobra.Command{
Use: "kubernetes",
Short: L("Install a PTF or Test package on a kubernetes cluster"),
Expand All @@ -35,7 +31,7 @@ NOTE: installing on a remote cluster is not supported yet!

Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
var flags kubernetesPTFFlags
var flags kubernetes.ProxyFlags
return utils.CommandHelper(globalFlags, cmd, args, &flags, nil, run)
},
}
Expand Down
9 changes: 5 additions & 4 deletions mgrpxy/cmd/support/ptf/kubernetes/kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"testing"

"github.com/spf13/cobra"
"github.com/uyuni-project/uyuni-tools/mgrpxy/shared/kubernetes"
"github.com/uyuni-project/uyuni-tools/shared/testutils"
"github.com/uyuni-project/uyuni-tools/shared/testutils/flagstests"
"github.com/uyuni-project/uyuni-tools/shared/types"
Expand All @@ -21,11 +22,11 @@ func TestParamsParsing(t *testing.T) {
args = append(args, flagstests.ProxyHelmFlagsTestArgs...)

// Test function asserting that the args are properly parsed
tester := func(globalFlags *types.GlobalFlags, flags *kubernetesPTFFlags,
cmd *cobra.Command, args []string,
tester := func(_ *types.GlobalFlags, flags *kubernetes.ProxyFlags,
cmd *cobra.Command, _ []string,
) error {
flagstests.AssertProxyImageFlags(t, cmd, &flags.UpgradeFlags.ProxyImageFlags)
flagstests.AssertProxyHelmFlags(t, cmd, &flags.UpgradeFlags.Helm)
flagstests.AssertProxyImageFlags(t, cmd, &flags.ProxyImageFlags)
flagstests.AssertProxyHelmFlags(t, cmd, &flags.Helm)
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions mgrpxy/cmd/support/ptf/kubernetes/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
)

func ptfForKubernetes(globalFlags *types.GlobalFlags,
flags *kubernetesPTFFlags,
flags *kubernetes.ProxyFlags,
cmd *cobra.Command,
args []string,
) error {
return kubernetes.Upgrade(&flags.UpgradeFlags, cmd, args)
return kubernetes.Upgrade(flags, cmd, args)
}
4 changes: 2 additions & 2 deletions mgrpxy/cmd/upgrade/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

func newCmd(
globalFlags *types.GlobalFlags,
run utils.CommandFunc[kubernetes.KubernetesProxyUpgradeFlags],
run utils.CommandFunc[kubernetes.ProxyFlags],
) *cobra.Command {
cmd := &cobra.Command{
Use: "kubernetes",
Expand All @@ -28,7 +28,7 @@ NOTE: for now upgrading on a remote kubernetes cluster is not supported!
`),
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
var flags kubernetes.KubernetesProxyUpgradeFlags
var flags kubernetes.ProxyFlags
return utils.CommandHelper(globalFlags, cmd, args, &flags, nil, run)
},
}
Expand Down
4 changes: 2 additions & 2 deletions mgrpxy/cmd/upgrade/kubernetes/kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ func TestParamsParsing(t *testing.T) {
args = append(args, flagstests.ProxyHelmFlagsTestArgs...)

// Test function asserting that the args are properly parsed
tester := func(globalFlags *types.GlobalFlags, flags *kubernetes.KubernetesProxyUpgradeFlags,
cmd *cobra.Command, args []string,
tester := func(_ *types.GlobalFlags, flags *kubernetes.ProxyFlags,
cmd *cobra.Command, _ []string,
) error {
flagstests.AssertProxyImageFlags(t, cmd, &flags.ProxyImageFlags)
flagstests.AssertProxyHelmFlags(t, cmd, &flags.Helm)
Expand Down
2 changes: 1 addition & 1 deletion mgrpxy/cmd/upgrade/kubernetes/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func upgradeKubernetes(globalFlags *types.GlobalFlags,
flags *kubernetes.KubernetesProxyUpgradeFlags, cmd *cobra.Command, args []string,
flags *kubernetes.ProxyFlags, cmd *cobra.Command, args []string,
) error {
return kubernetes.Upgrade(flags, cmd, args)
}
7 changes: 7 additions & 0 deletions mgrpxy/shared/kubernetes/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,18 @@ import (
"fmt"

"github.com/spf13/cobra"
pxy_utils "github.com/uyuni-project/uyuni-tools/mgrpxy/shared/utils"
. "github.com/uyuni-project/uyuni-tools/shared/l10n"
"github.com/uyuni-project/uyuni-tools/shared/types"
"github.com/uyuni-project/uyuni-tools/shared/utils"
)

// ProxyFlags is storing all the kubernetes install and upgrade flags for the proxy.
type ProxyFlags struct {
pxy_utils.ProxyFlags `mapstructure:",squash"`
Helm HelmFlags
}

// HelmFlags it's used for helm chart flags.
type HelmFlags struct {
Proxy types.ChartFlags
Expand Down
8 changes: 1 addition & 7 deletions mgrpxy/shared/kubernetes/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ import (

const helmAppName = "uyuni-proxy"

// KubernetesProxyUpgradeFlags represents the flags for the mgrpxy upgrade kubernetes command.
type KubernetesProxyUpgradeFlags struct {
utils.ProxyImageFlags `mapstructure:",squash"`
Helm HelmFlags
}

// Deploy will deploy proxy in kubernetes.
func Deploy(imageFlags *utils.ProxyImageFlags, helmFlags *HelmFlags, configDir string,
kubeconfig string, helmArgs ...string,
Expand Down Expand Up @@ -146,7 +140,7 @@ func getConfigYaml(directory string) (string, error) {
}

// Upgrade will upgrade the current kubernetes proxy.
func Upgrade(flags *KubernetesProxyUpgradeFlags, cmd *cobra.Command, args []string,
func Upgrade(flags *ProxyFlags, cmd *cobra.Command, args []string,
) error {
for _, binary := range []string{"kubectl", "helm"} {
if _, err := exec.LookPath(binary); err != nil {
Expand Down
5 changes: 2 additions & 3 deletions mgrpxy/shared/podman/podman.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ import (

// PodmanProxyFlags are the flags used by podman proxy install and upgrade command.
type PodmanProxyFlags struct {
utils.ProxyImageFlags `mapstructure:",squash"`
SCC types.SCCCredentials
Podman podman.PodmanFlags
utils.ProxyFlags `mapstructure:",squash"`
Podman podman.PodmanFlags
}

// GenerateSystemdService generates all the systemd files required by proxy.
Expand Down
6 changes: 6 additions & 0 deletions mgrpxy/shared/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ import (
"github.com/uyuni-project/uyuni-tools/shared/utils"
)

// ProxyFlags is the object holding all the common proxy installation and upgrade flags.
type ProxyFlags struct {
ProxyImageFlags `mapstructure:",squash"`
SCC types.SCCCredentials
}

// ProxyImageFlags are the flags used by install proxy command.
type ProxyImageFlags struct {
Registry string `mapstructure:"registry"`
Expand Down

0 comments on commit 2a9f8f0

Please sign in to comment.