Skip to content

Commit

Permalink
More robust --kubelet-extra-args comparison, drop --env and --force
Browse files Browse the repository at this point in the history
Signed-off-by: Kimmo Lehto <[email protected]>
  • Loading branch information
kke committed Dec 2, 2024
1 parent fc6aa51 commit a918157
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 9 deletions.
13 changes: 13 additions & 0 deletions pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,16 @@ func (f Flags) Equals(b Flags) bool {
}
return true
}

// NewFlags shell-splits and parses a string and returns new Flags or an error if splitting fails
func NewFlags(s string) (Flags, error) {
var flags Flags
parts, err := shell.Split(s)
if err != nil {
return flags, fmt.Errorf("failed to split flags %q: %w", s, err)
}
for _, part := range parts {
flags.Add(part)
}
return flags, nil
}
7 changes: 7 additions & 0 deletions pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,10 @@ func TestEquals(t *testing.T) {
flags2 = Flags{"-f", "--flag2=foo", "--flag3=baz"}
require.False(t, flags1.Equals(flags2))
}

func TestNewFlags(t *testing.T) {
flags, err := NewFlags("--hello=world --bar=baz")
require.NoError(t, err)
require.Equal(t, "world", flags.GetValue("--hello"))
require.Equal(t, "baz", flags.GetValue("--bar"))
}
39 changes: 30 additions & 9 deletions pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/go-playground/validator/v10"
"github.com/jellydator/validation"
"github.com/jellydator/validation/is"
"github.com/k0sproject/k0sctl/internal/shell"
"github.com/k0sproject/rig"
"github.com/k0sproject/rig/exec"
"github.com/k0sproject/rig/os"
Expand Down Expand Up @@ -308,13 +307,11 @@ func (h *Host) K0sInstallFlags() (Flags, error) {
if strings.HasSuffix(h.Role, "worker") {
var extra Flags
if old := flags.GetValue("--kubelet-extra-args"); old != "" {
parts, err := shell.Split(old)
ex, err := NewFlags(old)
if err != nil {
return flags, fmt.Errorf("failed to split kubelet-extra-args: %w", err)
}
for _, part := range parts {
extra.Add(part)
}
extra = ex
}
// set worker's private address to --node-ip in --extra-kubelet-args if cloud ins't enabled
enableCloudProvider, err := h.InstallFlags.GetBoolean("--enable-cloud-provider")
Expand Down Expand Up @@ -581,17 +578,41 @@ func (h *Host) ExpandTokens(input string, k0sVersion *version.Version) string {

// FlagsChanged returns true when the flags have changed by comparing the host.Metadata.K0sStatusArgs to what host.InstallFlags would produce
func (h *Host) FlagsChanged() bool {
installFlags, err := h.K0sInstallFlags()
our, err := h.K0sInstallFlags()
if err != nil {
log.Warnf("%s: could not get install flags: %s", h, err)
installFlags = Flags{}
our = Flags{}
}
ex := our.GetValue("--kubelet-extra-args")
ourExtra, err := NewFlags(ex)
if err != nil {
log.Warnf("%s: could not parse local --kubelet-extra-args value %q: %s", h, ex, err)
}

var their Flags
their = append(their, h.Metadata.K0sStatusArgs...)
ex = their.GetValue("--kubelet-extra-args")
theirExtra, err := NewFlags(ex)
if err != nil {
log.Warnf("%s: could not parse remote --kubelet-extra-args value %q: %s", h, ex, err)
}

if !ourExtra.Equals(theirExtra) {
log.Debugf("%s: installFlags --kubelet-extra-args seem to have changed: %+v vs %+v", h, theirExtra.Map(), ourExtra.Map())
return true
}

// remove flags that are dropped by k0s or are handled specially
for _, f := range []string{"--force", "--kubelet-extra-args", "--env"} {
our.Delete(f)
their.Delete(f)
}

if installFlags.Equals(h.Metadata.K0sStatusArgs) {
if our.Equals(their) {
log.Debugf("%s: installFlags have not changed", h)
return false
}

log.Debugf("%s: installFlags seem to have changed. existing: %+v new: %+v", h, h.Metadata.K0sStatusArgs.Map(), installFlags.Map())
log.Debugf("%s: installFlags seem to have changed. existing: %+v new: %+v", h, their.Map(), our.Map())
return true
}

0 comments on commit a918157

Please sign in to comment.