Skip to content

Commit

Permalink
Fix skipping paths under kubernetes
Browse files Browse the repository at this point in the history
Signed-off-by: Itxaka <itxaka@kairos.io>
  • Loading branch information
Itxaka committed Sep 14, 2024
1 parent e23d099 commit c549aeb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
12 changes: 8 additions & 4 deletions pkg/config/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions pkg/config/spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
})
})

0 comments on commit c549aeb

Please sign in to comment.