Skip to content

Commit

Permalink
Read actual system configs during k8s upgrade (#579)
Browse files Browse the repository at this point in the history
  • Loading branch information
Itxaka authored Oct 16, 2024
1 parent d5ded90 commit e2c3a0e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 11 deletions.
15 changes: 13 additions & 2 deletions internal/agent/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package agent
import (
"encoding/json"
"fmt"
"path/filepath"
"strings"

hook "github.com/kairos-io/kairos-agent/v2/internal/agent/hooks"
Expand All @@ -13,6 +14,7 @@ import (
config "github.com/kairos-io/kairos-agent/v2/pkg/config"
"github.com/kairos-io/kairos-agent/v2/pkg/uki"
internalutils "github.com/kairos-io/kairos-agent/v2/pkg/utils"
k8sutils "github.com/kairos-io/kairos-agent/v2/pkg/utils/k8s"
events "github.com/kairos-io/kairos-sdk/bus"
"github.com/kairos-io/kairos-sdk/collector"
"github.com/kairos-io/kairos-sdk/utils"
Expand Down Expand Up @@ -68,10 +70,19 @@ func Upgrade(
source string, force, strictValidations bool, dirs []string, upgradeEntry string, preReleases bool) error {
bus.Manager.Initialize()

fixedDirs := make([]string, len(dirs))
// Check and fix dirs if we are under k8s, so we read the actual running system configs instead of only
// the container configs
// we can run it blindly as it will return an empty string if not under k8s
hostdir := k8sutils.GetHostDirForK8s()
for _, dir := range dirs {
fixedDirs = append(fixedDirs, filepath.Join(hostdir, dir))
}

if internalutils.UkiBootMode() == internalutils.UkiHDD {
return upgradeUki(source, dirs, upgradeEntry, strictValidations)
return upgradeUki(source, fixedDirs, upgradeEntry, strictValidations)
} else {
return upgrade(source, dirs, upgradeEntry, strictValidations)
return upgrade(source, fixedDirs, upgradeEntry, strictValidations)
}
}

Expand Down
11 changes: 3 additions & 8 deletions pkg/config/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/kairos-io/kairos-agent/v2/pkg/constants"
v1 "github.com/kairos-io/kairos-agent/v2/pkg/types/v1"
fsutils "github.com/kairos-io/kairos-agent/v2/pkg/utils/fs"
k8sutils "github.com/kairos-io/kairos-agent/v2/pkg/utils/k8s"
"github.com/kairos-io/kairos-agent/v2/pkg/utils/partitions"
"github.com/kairos-io/kairos-sdk/collector"
"github.com/kairos-io/kairos-sdk/ghw"
Expand Down Expand Up @@ -849,17 +850,11 @@ func GetSourceSize(config *Config, source *v1.ImageSource) (int64, error) {
// , which mounts the host root into $HOST_DIR
// we should skip that dir when calculating the size as we would be doubling the calculated size
// Plus we will hit the usual things when checking a running system. Processes that go away, tmpfiles, etc...

// This is always set for pods running under kubernetes
_, underKubernetes := os.LookupEnv("KUBERNETES_SERVICE_HOST")
config.Logger.Logger.Info().Bool("status", underKubernetes).Msg("Running under kubernetes")
// Try to get the HOST_DIR in case we are not using the default one
hostDir := os.Getenv("HOST_DIR")
// If we are under kubernetes but the HOST_DIR var is empty, default to /host as system-upgrade-controller mounts
// the host in that dir by default
if underKubernetes && hostDir == "" {
hostDir = "/host"
}
hostDir := k8sutils.GetHostDirForK8s()
config.Logger.Logger.Debug().Bool("status", underKubernetes).Str("hostdir", hostDir).Msg("Kubernetes check")
err = fsutils.WalkDirFs(config.Fs, source.Value(), func(path string, d fs.DirEntry, err error) error {
// If its empty we are just not setting it, so probably out of the k8s upgrade path
if hostDir != "" && strings.HasPrefix(path, hostDir) {
Expand Down
3 changes: 2 additions & 1 deletion pkg/config/spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,8 @@ var _ = Describe("GetSourceSize", Label("GetSourceSize"), func() {

Expect(os.Mkdir(filepath.Join(tempDir, "host"), os.ModePerm)).ToNot(HaveOccurred())
Expect(createFileOfSizeInMB(filepath.Join(tempDir, "host", "what.txt"), 200)).ToNot(HaveOccurred())
// Set env var like the suc upgrade does
// Set env var like the suc upgrade and k8s does to trigger the skip
Expect(os.Setenv("KUBERNETES_SERVICE_HOST", "10.0.0.1")).ToNot(HaveOccurred())
Expect(os.Setenv("HOST_DIR", filepath.Join(tempDir, "host"))).ToNot(HaveOccurred())

sizeAfter, err := config.GetSourceSize(conf, imageSource)
Expand Down
27 changes: 27 additions & 0 deletions pkg/utils/k8s/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package k8s

import "os"

// GetHostDirForK8s returns the base dir where the system is
// This is because under k8s, we mount the actual system under a dir
// So we need to know which paths we need to read the configs from
// if we read them from the root directly, we are actually reading the
// configs of the upgrade container
// If not found returns an empty string
func GetHostDirForK8s() string {
_, underKubernetes := os.LookupEnv("KUBERNETES_SERVICE_HOST")
// Try to get the HOST_DIR in case we are not using the default one
hostDirEnv := os.Getenv("HOST_DIR")
// If we are under kubernetes but the HOST_DIR var is empty, default to /host as system-upgrade-controller mounts
// the host in that dir by default
if underKubernetes {
if hostDirEnv != "" {
return hostDirEnv
} else {
return "/host"
}
} else {
// We return an empty string so any filepath.join does not alter the paths
return ""
}
}

0 comments on commit e2c3a0e

Please sign in to comment.