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

fix: improve k3s upgrade, more cli opts, updates k3s env #60

Merged
merged 1 commit into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 10 additions & 0 deletions internal/cli/local/setup/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ type Flags struct {
RemoteVersion string
}

Iface string
Copy link
Contributor Author

Choose a reason for hiding this comment

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

those flags moved to common


TLSCert string // TLS certificate file
TLSKey string // TLS key file

Debug bool
}

Expand Down Expand Up @@ -75,4 +80,9 @@ func Use(cmd *cobra.Command, config *Flags) {
cmd.Flags().BoolVarP(&config.Chart.RemoteDownload, "remote-download", "r", false, "Enable downloading chart from remote repository")
cmd.Flags().StringVar(&config.Chart.RemoteVersion, "remote-version", "", "Version of the chart to download from remote repository")
cmd.MarkFlagsMutuallyExclusive("local-chart", "remote-download")

cmd.Flags().StringVar(&config.Iface, "iface", "", "interface to use for internal networking")

cmd.Flags().StringVar(&config.TLSCert, "tls-cert", "", "TLS certificate file")
cmd.Flags().StringVar(&config.TLSKey, "tls-key", "", "TLS secret key file")
}
2 changes: 1 addition & 1 deletion internal/cli/local/setup/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func runSetup(cmd *cobra.Command, args []string) (err error) {
}
}()

err = k3s.Install(cmd.Context(), k3s.InstallConfig{
err = k3s.Install(cmd.Context(), k3s.Config{
Copy link
Contributor Author

Choose a reason for hiding this comment

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

now install config and upgrade config are the same

Configuration: &setupConfig.Configuration,
Iface: setupConfig.Iface,
ProxyKubernetes: setupConfig.ProxyKubernetes,
Expand Down
10 changes: 0 additions & 10 deletions internal/cli/local/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ var (
type setup struct {
config_v1.Configuration
setup_flags.Flags

Iface string

TLSCert string // TLS certificate file
TLSKey string // TLS key file
}

func (s setup) Validate() error {
Expand Down Expand Up @@ -70,11 +65,6 @@ func init() {
setup_flags.Use(setupCmd, &setupConfig.Flags)

setupCmd.Flags().StringVar(&setupConfig.Host, "host", "", "public host or IP address for LWH (default: interface address)")

setupCmd.Flags().StringVar(&setupConfig.IP, "ip", "0.0.0.0", "internal IP address to use for cluster")
setupCmd.Flags().StringVar(&setupConfig.Iface, "iface", "", "interface to use for internal networking")

setupCmd.Flags().StringVar(&setupConfig.TLSCert, "tls-cert", "", "TLS certificate file")
setupCmd.Flags().StringVar(&setupConfig.TLSKey, "tls-key", "", "TLS secret key file")
})
}
31 changes: 30 additions & 1 deletion internal/cli/local/upgrade/run.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package upgrade

import (
"os"

"github.com/spf13/cobra"

"github.com/weka/gohomecli/internal/local/bundle"
"github.com/weka/gohomecli/internal/local/chart"
config_v1 "github.com/weka/gohomecli/internal/local/config/v1"
"github.com/weka/gohomecli/internal/local/k3s"
)

Expand All @@ -15,7 +18,12 @@ func runUpgrade(cmd *cobra.Command, args []string) error {
}
}

err := k3s.Upgrade(cmd.Context(), k3s.UpgradeConfig{Debug: upgradeConfig.Debug})
err := k3s.Upgrade(cmd.Context(), k3s.Config{
Configuration: &upgradeConfig.Configuration,
Iface: upgradeConfig.Iface,
ProxyKubernetes: upgradeConfig.ProxyKubernetes,
Debug: upgradeConfig.Debug,
})
if err != nil {
return err
}
Expand Down Expand Up @@ -56,3 +64,24 @@ func runUpgrade(cmd *cobra.Command, args []string) error {

return chart.Upgrade(cmd.Context(), helmOptions, upgradeConfig.Debug)
}

func readTLS(certFile, keyFile string, config *config_v1.Configuration) error {
if certFile == "" || keyFile == "" {
return nil
}

cert, err := os.ReadFile(certFile)
if err != nil {
return err
}
config.TLS.Cert = string(cert)

key, err := os.ReadFile(keyFile)
if err != nil {
return err
}

config.TLS.Key = string(key)

return nil
}
7 changes: 7 additions & 0 deletions internal/cli/local/upgrade/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ var upgradeCmd = &cobra.Command{
upgradeConfig.Configuration.Proxy.URL = upgradeConfig.Flags.ProxyURL
}

if err := readTLS(upgradeConfig.TLSCert, upgradeConfig.TLSKey, &upgradeConfig.Configuration); err != nil {
return err
}

return upgradeConfig.Validate()
},
RunE: runUpgrade,
Expand All @@ -53,5 +57,8 @@ func init() {
appCmd.AddCommand(upgradeCmd)

setup_flags.Use(upgradeCmd, &upgradeConfig.Flags)

upgradeCmd.Flags().StringVar(&upgradeConfig.Host, "host", "", "public host or IP address for LWH (default: interface address)")
upgradeCmd.Flags().StringVar(&upgradeConfig.IP, "ip", "0.0.0.0", "internal IP address to use for cluster")
})
}
122 changes: 3 additions & 119 deletions internal/local/k3s/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,60 +3,17 @@ package k3s
import (
"context"
"errors"
"fmt"
"io"
"io/fs"
"net/url"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"strings"

"github.com/weka/gohomecli/internal/local/bundle"
config_v1 "github.com/weka/gohomecli/internal/local/config/v1"
"github.com/weka/gohomecli/internal/utils"
)

const dataDir = "/opt/wekahome/data/"

var (
ErrExists = errors.New("k3s already installed")

DefaultLocalStoragePath = filepath.Join(dataDir, "local-storage")

k3sBundleRegexp = regexp.MustCompile(`k3s.*\.(tar(\.gz)?)|(tgz)`)
k3sImagesPath = "/var/lib/rancher/k3s/agent/images/"
)

type InstallConfig struct {
*config_v1.Configuration

Iface string // interface for k3s network to work on
IfaceAddr string // ip addr for k3s to use as node ip

ProxyKubernetes bool // use proxy for k3s

Debug bool
}

func (c InstallConfig) k3sInstallArgs() []string {
k3sArgs := []string{
fmt.Sprintf("--flannel-iface=%s", c.Iface),
fmt.Sprintf("--node-ip=%s", c.IfaceAddr), // node ip needs to have ip address (not 0.0.0.0)
fmt.Sprintf("--kubelet-arg=address=%s", c.IP),
fmt.Sprintf("--bind-address=%s", c.IP),
fmt.Sprintf("--default-local-storage-path=%s", DefaultLocalStoragePath),
}

k3sArgs = append(k3sArgs, c.Configuration.K3SArgs...)

return k3sArgs
}

// Install runs K3S installation process
func Install(ctx context.Context, c InstallConfig) error {
func Install(ctx context.Context, c Config) error {
setupLogger(c.Debug)

if hasK3S() && !c.Debug {
Expand Down Expand Up @@ -171,84 +128,11 @@ func copyAirgapImages() bundle.TarCallback {
}
}

func runInstallScript(c InstallConfig) bundle.TarCallback {
func runInstallScript(c Config) bundle.TarCallback {
return bundle.TarCallback{
FileName: "install.sh",

Callback: func(ctx context.Context, fi fs.FileInfo, r io.Reader) error {
logger.Info().Msg("Starting k3s install")

if c.Host != "" {
logger.Debug().Str("hostname", c.Host).Msg("Using hostname")
os.Setenv("K3S_HOSTNAME", c.Host)
os.Setenv("K3S_NODE_NAME", c.Host)
}

overriden, err := resolvConfOverriden()
if err != nil {
return err
}
if overriden {
logger.Debug().Str("resolvconf", k3sResolvConfPath).Msg("Resolv.conf is overriden")
os.Setenv("K3S_RESOLV_CONF", k3sResolvConfPath)
}

logger.Debug().
Str("installPath", k3sInstallPath).
Msg("Setting env vars")

os.Setenv("INSTALL_K3S_BIN_DIR", k3sInstallPath)
os.Setenv("INSTALL_K3S_SKIP_DOWNLOAD", "true")
os.Setenv("INSTALL_K3S_SELINUX_WARN", "true")
os.Setenv("INSTALL_K3S_SKIP_SELINUX_RPM", "true")

if c.Proxy.URL != "" && c.ProxyKubernetes {
proxyURL, err := url.Parse(c.Proxy.URL)
if err != nil {
return fmt.Errorf("url parse: %w", err)
}

logger.Info().
Str("proxy", utils.URLSafe(proxyURL).String()).
Msg("Using proxy")

var noProxy = []string{
"127.0.0.0/8", "10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16",
fmt.Sprintf("%s/32", c.IP),
fmt.Sprintf("%s/32", c.IfaceAddr),
}

os.Setenv("NO_PROXY", strings.Join(noProxy, ","))

switch proxyURL.Scheme {
case "http":
os.Setenv("HTTPS_PROXY", proxyURL.String())
os.Setenv("HTTP_PROXY", proxyURL.String())
case "https":
os.Setenv("HTTPS_PROXY", proxyURL.String())
default:
logger.Warn().
Str("url", proxyURL.String()).
Msgf("Proxy scheme %s is not supported with K3S", proxyURL.Scheme)
}
}

cmd, err := utils.ExecCommand(ctx, "sh", append([]string{"-s", "-", "server"}, c.k3sInstallArgs()...),
utils.WithStdin(r),
utils.WithStdoutReader(k3sLogParser(utils.InfoLevel)),
utils.WithStderrReader(k3sLogParser(utils.InfoLevel)),
)
if err != nil {
return err
}

if err = cmd.Wait(); err != nil {
return fmt.Errorf("install.sh: %w", errors.Join(err, ctx.Err()))
}

logger.Info().Msg("Install completed")

return nil
return k3sInstall(ctx, c, fi, r)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

moved to shared func

},
}
}
Loading
Loading