diff --git a/internal/agent/upgrade.go b/internal/agent/upgrade.go index e6980707..6b679241 100644 --- a/internal/agent/upgrade.go +++ b/internal/agent/upgrade.go @@ -3,6 +3,7 @@ package agent import ( "encoding/json" "fmt" + "path/filepath" "strings" hook "github.com/kairos-io/kairos-agent/v2/internal/agent/hooks" @@ -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" @@ -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) } } diff --git a/pkg/config/spec.go b/pkg/config/spec.go index 7e023ed7..842b6244 100644 --- a/pkg/config/spec.go +++ b/pkg/config/spec.go @@ -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" @@ -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) { diff --git a/pkg/config/spec_test.go b/pkg/config/spec_test.go index 2ba568ba..93072b7b 100644 --- a/pkg/config/spec_test.go +++ b/pkg/config/spec_test.go @@ -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) diff --git a/pkg/utils/k8s/common.go b/pkg/utils/k8s/common.go new file mode 100644 index 00000000..267c0972 --- /dev/null +++ b/pkg/utils/k8s/common.go @@ -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 "" + } +}