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

Kubernetes refactoring #450

Merged
merged 19 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
30 changes: 26 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,38 @@ module github.com/uyuni-project/uyuni-tools

go 1.21

toolchain go1.21.1

require (
github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2
github.com/briandowns/spinner v1.23.0
github.com/chai2010/gettext-go v1.0.2
github.com/spf13/cobra v1.8.0
k8s.io/api v0.29.7
k8s.io/apimachinery v0.29.7
k8s.io/cli-runtime v0.29.7
)

require (
github.com/creack/pty v1.1.17 // indirect
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/creack/pty v1.1.18 // indirect
github.com/fatih/color v1.7.0 // indirect
github.com/go-logr/logr v1.3.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect
github.com/moby/term v0.0.0-20221205130635-1aeaba878587 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
golang.org/x/net v0.23.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
k8s.io/client-go v0.29.7 // indirect
k8s.io/klog/v2 v2.110.1 // indirect
k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)

require (
Expand All @@ -30,9 +52,9 @@ require (
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.7.0
github.com/subosito/gotenv v1.2.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/term v0.22.0
golang.org/x/text v0.16.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/term v0.18.0
golang.org/x/text v0.14.0 // indirect
gopkg.in/ini.v1 v1.51.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1
gopkg.in/yaml.v2 v2.4.0
Expand Down
94 changes: 83 additions & 11 deletions go.sum

Large diffs are not rendered by default.

16 changes: 11 additions & 5 deletions mgradm/cmd/inspect/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ import (
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"

adm_utils "github.com/uyuni-project/uyuni-tools/mgradm/shared/utils"
"github.com/uyuni-project/uyuni-tools/shared"
shared_kubernetes "github.com/uyuni-project/uyuni-tools/shared/kubernetes"
"github.com/uyuni-project/uyuni-tools/shared/kubernetes"
. "github.com/uyuni-project/uyuni-tools/shared/l10n"
"github.com/uyuni-project/uyuni-tools/shared/types"
"github.com/uyuni-project/uyuni-tools/shared/utils"
Expand All @@ -32,11 +31,11 @@ func kuberneteInspect(
return utils.Errorf(err, L("failed to determine image"))
}

cnx := shared.NewConnection("kubectl", "", shared_kubernetes.ServerFilter)
cnx := shared.NewConnection("kubectl", "", kubernetes.ServerFilter)
if len(serverImage) <= 0 {
log.Debug().Msg("Use deployed image")

serverImage, err = adm_utils.RunningImage(cnx)
serverImage, err = kubernetes.GetRunningImage("uyuni")
if err != nil {
return errors.New(L("failed to find the image of the currently running server container: %s"))
}
Expand All @@ -46,7 +45,14 @@ func kuberneteInspect(
if err != nil {
return utils.Errorf(err, L("failed retrieving namespace"))
}
inspectResult, err := shared_kubernetes.InspectKubernetes(namespace, serverImage, flags.Image.PullPolicy)

// Get the SCC credentials secret if existing
pullSecret, err := kubernetes.GetSCCSecret(namespace, &types.SCCCredentials{}, kubernetes.ServerApp)
if err != nil {
return err
}

inspectResult, err := kubernetes.InspectServer(namespace, serverImage, flags.Image.PullPolicy, pullSecret)
if err != nil {
return utils.Errorf(err, L("inspect command failed"))
}
Expand Down
9 changes: 3 additions & 6 deletions mgradm/cmd/inspect/podman.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ import (

"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
adm_utils "github.com/uyuni-project/uyuni-tools/mgradm/shared/utils"
"github.com/uyuni-project/uyuni-tools/shared"
. "github.com/uyuni-project/uyuni-tools/shared/l10n"
shared_podman "github.com/uyuni-project/uyuni-tools/shared/podman"
"github.com/uyuni-project/uyuni-tools/shared/podman"
"github.com/uyuni-project/uyuni-tools/shared/types"
"github.com/uyuni-project/uyuni-tools/shared/utils"
)
Expand All @@ -31,13 +29,12 @@ func podmanInspect(
if len(serverImage) <= 0 {
log.Debug().Msg("Use deployed image")

cnx := shared.NewConnection("podman", shared_podman.ServerContainerName, "")
serverImage, err = adm_utils.RunningImage(cnx)
serverImage, err = podman.GetRunningImage(podman.ServerContainerName)
if err != nil {
return utils.Errorf(err, L("failed to find the image of the currently running server container"))
}
}
inspectResult, err := shared_podman.Inspect(serverImage, flags.Image.PullPolicy, flags.SCC)
inspectResult, err := podman.Inspect(serverImage, flags.Image.PullPolicy, flags.SCC)
if err != nil {
return utils.Errorf(err, L("inspect command failed"))
}
Expand Down
27 changes: 17 additions & 10 deletions mgradm/cmd/install/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,14 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/uyuni-project/uyuni-tools/mgradm/cmd/install/shared"
"github.com/uyuni-project/uyuni-tools/mgradm/shared/kubernetes"
cmd_utils "github.com/uyuni-project/uyuni-tools/mgradm/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"
)

type kubernetesInstallFlags struct {
shared.InstallFlags `mapstructure:",squash"`
Helm cmd_utils.HelmFlags
}

func newCmd(globalFlags *types.GlobalFlags, run utils.CommandFunc[kubernetesInstallFlags]) *cobra.Command {
func newCmd(globalFlags *types.GlobalFlags, run utils.CommandFunc[kubernetes.KubernetesServerFlags]) *cobra.Command {
cmd := &cobra.Command{
Use: "kubernetes [fqdn]",
Short: L("Install a new server on a kubernetes cluster"),
Expand All @@ -37,22 +33,33 @@ NOTE: installing on a remote cluster is not supported yet!
`),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
var flags kubernetesInstallFlags
var flags kubernetes.KubernetesServerFlags
flagsUpdater := func(v *viper.Viper) {
flags.InstallFlags.Coco.IsChanged = v.IsSet("coco.replicas")
flags.InstallFlags.HubXmlrpc.IsChanged = v.IsSet("hubxmlrpc.replicas")
flags.InstallFlags.Saline.IsChanged = v.IsSet("saline.replicas") || v.IsSet("saline.port")
flags.ServerFlags.Coco.IsChanged = v.IsSet("coco.replicas")
flags.ServerFlags.HubXmlrpc.IsChanged = v.IsSet("hubxmlrpc.replicas")
flags.ServerFlags.Saline.IsChanged = v.IsSet("saline.replicas") || v.IsSet("saline.port")
}
return utils.CommandHelper(globalFlags, cmd, args, &flags, flagsUpdater, run)
},
}

shared.AddInstallFlags(cmd)
cmd_utils.AddHelmInstallFlag(cmd)
cmd_utils.AddVolumesFlags(cmd)
return cmd
}

// NewCommand for kubernetes installation.
func NewCommand(globalFlags *types.GlobalFlags) *cobra.Command {
return newCmd(globalFlags, installForKubernetes)
}

func installForKubernetes(
_ *types.GlobalFlags,
flags *kubernetes.KubernetesServerFlags,
cmd *cobra.Command,
args []string,
) error {
flags.Installation.CheckParameters(cmd, "kubectl")
return kubernetes.Reconcile(flags, args[0])
}
11 changes: 7 additions & 4 deletions mgradm/cmd/install/kubernetes/kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,25 @@ import (
"testing"

"github.com/spf13/cobra"
"github.com/uyuni-project/uyuni-tools/mgradm/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"
)

func TestParamsParsing(t *testing.T) {
args := flagstests.InstallFlagsTestArgs()
args = append(args, flagstests.ServerHelmFlagsTestArgs...)
args = append(args, flagstests.ServerKubernetesFlagsTestArgs...)
args = append(args, flagstests.VolumesFlagsTestExpected...)
args = append(args, "srv.fq.dn")

// Test function asserting that the args are properly parsed
tester := func(_ *types.GlobalFlags, flags *kubernetesInstallFlags,
tester := func(_ *types.GlobalFlags, flags *kubernetes.KubernetesServerFlags,
_ *cobra.Command, args []string,
) error {
flagstests.AssertInstallFlags(t, &flags.InstallFlags)
flagstests.AssertServerHelmFlags(t, &flags.Helm)
flagstests.AssertInstallFlags(t, &flags.ServerFlags)
flagstests.AssertServerKubernetesFlags(t, &flags.Kubernetes)
flagstests.AssertVolumesFlags(t, &flags.Volumes)
testutils.AssertEquals(t, "Wrong FQDN", "srv.fq.dn", args[0])
return nil
}
Expand Down
106 changes: 0 additions & 106 deletions mgradm/cmd/install/kubernetes/utils.go

This file was deleted.

11 changes: 6 additions & 5 deletions mgradm/cmd/install/podman/podman.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/uyuni-project/uyuni-tools/mgradm/cmd/install/shared"
adm_utils "github.com/uyuni-project/uyuni-tools/mgradm/shared/utils"
. "github.com/uyuni-project/uyuni-tools/shared/l10n"
"github.com/uyuni-project/uyuni-tools/shared/podman"
"github.com/uyuni-project/uyuni-tools/shared/types"
"github.com/uyuni-project/uyuni-tools/shared/utils"
)

type podmanInstallFlags struct {
shared.InstallFlags `mapstructure:",squash"`
Podman podman.PodmanFlags
adm_utils.ServerFlags `mapstructure:",squash"`
Podman podman.PodmanFlags
}

func newCmd(globalFlags *types.GlobalFlags, run utils.CommandFunc[podmanInstallFlags]) *cobra.Command {
Expand All @@ -33,9 +34,9 @@ NOTE: installing on a remote podman is not supported yet!
RunE: func(cmd *cobra.Command, args []string) error {
var flags podmanInstallFlags
flagsUpdater := func(v *viper.Viper) {
flags.InstallFlags.Coco.IsChanged = v.IsSet("coco.replicas")
flags.InstallFlags.HubXmlrpc.IsChanged = v.IsSet("hubxmlrpc.replicas")
flags.InstallFlags.Saline.IsChanged = v.IsSet("saline.replicas") || v.IsSet("saline.port")
flags.ServerFlags.Coco.IsChanged = v.IsSet("coco.replicas")
flags.ServerFlags.HubXmlrpc.IsChanged = v.IsSet("hubxmlrpc.replicas")
flags.ServerFlags.Saline.IsChanged = v.IsSet("saline.replicas") || v.IsSet("saline.port")
}
return utils.CommandHelper(globalFlags, cmd, args, &flags, flagsUpdater, run)
},
Expand Down
6 changes: 4 additions & 2 deletions mgradm/cmd/install/podman/podman_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ func TestParamsParsing(t *testing.T) {
args = append(args, "srv.fq.dn")

// Test function asserting that the args are properly parsed
tester := func(_ *types.GlobalFlags, flags *podmanInstallFlags, _ *cobra.Command, args []string) error {
flagstests.AssertInstallFlags(t, &flags.InstallFlags)
tester := func(_ *types.GlobalFlags, flags *podmanInstallFlags,
_ *cobra.Command, args []string,
) error {
flagstests.AssertInstallFlags(t, &flags.ServerFlags)
flagstests.AssertPodmanInstallFlags(t, &flags.Podman)
testutils.AssertEquals(t, "Wrong FQDN", "srv.fq.dn", args[0])
return nil
Expand Down
Loading
Loading