diff --git a/pkg/config/spec.go b/pkg/config/spec.go index f01a96fb..9e684c36 100644 --- a/pkg/config/spec.go +++ b/pkg/config/spec.go @@ -814,20 +814,24 @@ func GetSourceSize(config *Config, source *v1.ImageSource) (int64, error) { // 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.Getenv("KUBERNETES_SERVICE_HOST") + _, 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 == "" { + if underKubernetes && hostDir == "" { hostDir = "/host" } 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) { config.Logger.Logger.Debug().Str("path", path).Str("hostDir", hostDir).Msg("Skipping file as it is a host directory") - } else if strings.HasPrefix(path, "/proc") || strings.HasPrefix(path, "/dev") { - config.Logger.Logger.Debug().Str("path", path).Str("hostDir", hostDir).Msg("Skipping dir as it is a runtime directory (/proc or /dev)") + } else if underKubernetes && (strings.HasPrefix(path, "/proc") || strings.HasPrefix(path, "/dev") || strings.HasPrefix(path, "/run")) { + // If under kubernetes, the upgrade will check the size of / which includes the host dir mounted under /host + // But it also can bind the host mounts into / so we want to skip those runtime dirs + // During install or upgrade outside kubernetes, we dont care about those dirs as they are not expected to be in the source dir + config.Logger.Logger.Debug().Str("path", path).Str("hostDir", hostDir).Msg("Skipping dir as it is a runtime directory under kubernetes (/proc, /dev or /run)") } else { v := getSize(&size, filesVisited, path, d, err) return v diff --git a/pkg/config/spec_test.go b/pkg/config/spec_test.go index bb9892ad..1439fad5 100644 --- a/pkg/config/spec_test.go +++ b/pkg/config/spec_test.go @@ -730,4 +730,19 @@ var _ = Describe("GetSourceSize", Label("GetSourceSize"), func() { // what we get (/1000/1000) then we finish by adding and extra 100MB on top, like the GetSourceSize does internally Expect(sizeAfter).To(Equal(int64((400 * 1024 * 1024 / 1000 / 1000) + 100))) }) + It("Does not skip the dirs if outside of kubernetes", func() { + sizeBefore, err := config.GetSourceSize(conf, imageSource) + Expect(err).To(BeNil()) + Expect(sizeBefore).ToNot(BeZero()) + + // Not inside kubernetes so it should count this dir + Expect(os.Mkdir(filepath.Join(tempDir, "run"), os.ModePerm)).ToNot(HaveOccurred()) + Expect(createFileOfSizeInMB(filepath.Join(tempDir, "run", "what.txt"), 200)).ToNot(HaveOccurred()) + + sizeAfter, err := config.GetSourceSize(conf, imageSource) + Expect(err).ToNot(HaveOccurred()) + Expect(sizeAfter).ToNot(Equal(sizeBefore)) + Expect(sizeAfter).ToNot(BeZero()) + Expect(sizeAfter).To(Equal(int64((400 * 1024 * 1024 / 1000 / 1000) + 100))) + }) })