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

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
c39b6e5
Kubernetes support for Hub XML-RPC
nadvornik May 24, 2024
0482408
Backup the pgsql data after rsyncing them
cbosdo Aug 7, 2024
e9a44a0
Refresh the connection after the k8s migration
cbosdo Aug 7, 2024
e3b2544
Wait for 3 hours when running a pod
cbosdo Aug 7, 2024
7560bc1
Add deployments reason for helm history
cbosdo Aug 7, 2024
38864fb
Add crds.keep value for cert-manager to keep feature parity with inst…
cbosdo Aug 8, 2024
152ed66
Reorganize the kubernetes SSL CA deployment code for readability
cbosdo Sep 5, 2024
823bff1
Ports internal data description refactoring
cbosdo Sep 27, 2024
f6a60ce
Refactor the upgrade SanityCheck to not exec in a running container
cbosdo Oct 23, 2024
475be63
Use one data structure for install, migrate, upgrade flags
cbosdo Oct 2, 2024
a04242d
Kubernetes migration refactoring
cbosdo Aug 8, 2024
0a7d697
Move the RunSetup function to shared
cbosdo Oct 9, 2024
92c7c29
Refactor kubernetes uninstall
cbosdo Sep 30, 2024
6ae8f22
Merge kubernetes install/upgrade/migrate into one entry point
cbosdo Sep 24, 2024
bf0def5
Migration script improvements
cbosdo Nov 8, 2024
5c44006
Change the --helm-* parameters into --kubernetes-*
cbosdo Nov 13, 2024
1409477
Disable CGO build for Debian i586 to avoid cross-compiling
cbosdo Nov 14, 2024
3a48af0
Handle traefik helm chart breaking change to expose ports
cbosdo Nov 14, 2024
5d4058b
Run the first user creation from the setup script
cbosdo Nov 15, 2024
f65f29a
Remove the line end in the local timezone
cbosdo Nov 15, 2024
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, "uyuni")
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
5 changes: 1 addition & 4 deletions mgradm/cmd/inspect/podman.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ 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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
shared_podman "github.com/uyuni-project/uyuni-tools/shared/podman"
"github.com/uyuni-project/uyuni-tools/shared/podman"

A nitpick, probably due to various refactoring we keep this, but it is no longer needed, there is no namespace clash anymore.
Ofc the rest of the code will need to drop shared_ prefix.

This will also make it the same between inspect and install.

"github.com/uyuni-project/uyuni-tools/shared/types"
Expand All @@ -31,8 +29,7 @@ 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, shared_podman.ServerContainerName)
serverImage, err = shared_podman.GetRunningImage(shared_podman.ServerContainerName)
if err != nil {
return utils.Errorf(err, L("failed to find the image of the currently running server container"))
}
Expand Down
15 changes: 6 additions & 9 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,17 +33,18 @@ 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.ServerFlags.Coco.IsChanged = v.IsSet("coco.replicas")
flags.ServerFlags.HubXmlrpc.IsChanged = v.IsSet("hubxmlrpc.replicas")
}
return utils.CommandHelper(globalFlags, cmd, args, &flags, flagsUpdater, run)
},
}

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

Expand Down
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(globalFlags *types.GlobalFlags, flags *kubernetesInstallFlags,
tester := func(globalFlags *types.GlobalFlags, flags *kubernetes.KubernetesServerFlags,
cmd *cobra.Command, args []string,
) error {
flagstests.AssertInstallFlags(t, cmd, &flags.InstallFlags)
flagstests.AssertServerHelmFlags(t, cmd, &flags.Helm)
flagstests.AssertInstallFlags(t, cmd, &flags.ServerFlags)
flagstests.AssertServerKubernetesFlags(t, cmd, &flags.Kubernetes)
flagstests.AssertVolumesFlags(t, cmd, &flags.Volumes)
testutils.AssertEquals(t, "Wrong FQDN", "srv.fq.dn", args[0])
return nil
}
Expand Down
93 changes: 5 additions & 88 deletions mgradm/cmd/install/kubernetes/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,100 +7,17 @@
package kubernetes

import (
"fmt"
"os/exec"

"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
install_shared "github.com/uyuni-project/uyuni-tools/mgradm/cmd/install/shared"
"github.com/uyuni-project/uyuni-tools/mgradm/shared/kubernetes"
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/l10n"
"github.com/uyuni-project/uyuni-tools/shared/types"
shared_utils "github.com/uyuni-project/uyuni-tools/shared/utils"
)

func installForKubernetes(globalFlags *types.GlobalFlags,
flags *kubernetesInstallFlags,
func installForKubernetes(
globalFlags *types.GlobalFlags,
flags *kubernetes.KubernetesServerFlags,
cmd *cobra.Command,
args []string,
) error {
Comment on lines +15 to 20
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think for a clarity of code, as this is now just couple lines, can they be moved from utils.go directly to kubernetes.go and drop utils.go?

Seems strange to me to have utils file with single 2 lines command.

for _, binary := range []string{"kubectl", "helm"} {
if _, err := exec.LookPath(binary); err != nil {
return fmt.Errorf(L("install %s before running this command"), binary)
}
}

flags.CheckParameters(cmd, "kubectl")
cnx := shared.NewConnection("kubectl", "", shared_kubernetes.ServerFilter)

fqdn := args[0]

if err := shared_utils.IsValidFQDN(fqdn); err != nil {
return err
}

helmArgs := []string{"--set", "timezone=" + flags.TZ}
if flags.Mirror != "" {
// TODO Handle claims for multi-node clusters
helmArgs = append(helmArgs, "--set", "mirror.hostPath="+flags.Mirror)
}
if flags.Debug.Java {
helmArgs = append(helmArgs, "--set", "exposeJavaDebug=true")
}

// Check the kubernetes cluster setup
clusterInfos, err := shared_kubernetes.CheckCluster()
if err != nil {
return err
}

// Deploy the SSL CA or server certificate
ca := types.SSLPair{}
sslArgs, err := kubernetes.DeployCertificate(&flags.Helm, &flags.SSL, "", &ca, clusterInfos.GetKubeconfig(), fqdn,
flags.Image.PullPolicy)
if err != nil {
return shared_utils.Errorf(err, L("cannot deploy certificate"))
}
helmArgs = append(helmArgs, sslArgs...)

// Create a secret using SCC credentials if any are provided
helmArgs, err = shared_kubernetes.AddSCCSecret(helmArgs, flags.Helm.Uyuni.Namespace, &flags.SCC)
if err != nil {
return err
}

// Deploy Uyuni and wait for it to be up
if err := kubernetes.Deploy(cnx, flags.Image.Registry, &flags.Image, &flags.Helm, &flags.SSL,
clusterInfos, fqdn, flags.Debug.Java, false, helmArgs...,
); err != nil {
return shared_utils.Errorf(err, L("cannot deploy uyuni"))
}

// Create setup script + env variables and copy it to the container
envs := map[string]string{
"NO_SSL": "Y",
}

if err := install_shared.RunSetup(cnx, &flags.InstallFlags, args[0], envs); err != nil {
namespace, err := cnx.GetNamespace("")
if err != nil {
return shared_utils.Errorf(err, L("failed to stop service"))
}
if stopErr := shared_kubernetes.Stop(namespace, shared_kubernetes.ServerApp); stopErr != nil {
log.Error().Err(stopErr).Msg(L("failed to stop service"))
}
return err
}

// The CA needs to be added to the database for Kickstart use.
err = adm_utils.ExecCommand(zerolog.DebugLevel, cnx,
"/usr/bin/rhn-ssl-dbstore", "--ca-cert=/etc/pki/trust/anchors/LOCAL-RHN-ORG-TRUSTED-SSL-CERT")
if err != nil {
return shared_utils.Errorf(err, L("error storing the SSL CA certificate in database"))
}
return nil
flags.Installation.CheckParameters(cmd, "kubectl")
return kubernetes.Reconcile(flags, args[0])
}
9 changes: 5 additions & 4 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,8 +34,8 @@ 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.ServerFlags.Coco.IsChanged = v.IsSet("coco.replicas")
flags.ServerFlags.HubXmlrpc.IsChanged = v.IsSet("hubxmlrpc.replicas")
}
return utils.CommandHelper(globalFlags, cmd, args, &flags, flagsUpdater, run)
},
Expand Down
2 changes: 1 addition & 1 deletion mgradm/cmd/install/podman/podman_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestParamsParsing(t *testing.T) {
tester := func(globalFlags *types.GlobalFlags, flags *podmanInstallFlags,
cmd *cobra.Command, args []string,
) error {
flagstests.AssertInstallFlags(t, cmd, &flags.InstallFlags)
flagstests.AssertInstallFlags(t, cmd, &flags.ServerFlags)
flagstests.AssertPodmanInstallFlags(t, cmd, &flags.Podman)
testutils.AssertEquals(t, "Wrong FQDN", "srv.fq.dn", args[0])
return nil
Expand Down
Loading