-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. moved to shared func |
||
}, | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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