Reimplement iteration over fileInfos in getOrphan.
1. Reduce complexity due to nested if blocks by using early return/continue 2. Improve logging Changes suggested as a part of code review comments in 39748 Signed-off-by: Vikram bir Singh <vikrambir.singh@docker.com>
This commit is contained in:
parent
f505abb6a7
commit
ebf12dbda0
|
@ -14,7 +14,7 @@ import (
|
||||||
|
|
||||||
"github.com/docker/distribution"
|
"github.com/docker/distribution"
|
||||||
"github.com/docker/docker/pkg/ioutils"
|
"github.com/docker/docker/pkg/ioutils"
|
||||||
"github.com/opencontainers/go-digest"
|
digest "github.com/opencontainers/go-digest"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
@ -317,32 +317,40 @@ func (fms *fileMetadataStore) getOrphan() ([]roLayer, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, fi := range fileInfos {
|
for _, fi := range fileInfos {
|
||||||
if fi.IsDir() && strings.Contains(fi.Name(), "-removing") {
|
if !fi.IsDir() || !strings.HasSuffix(fi.Name(), "-removing") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// At this stage, fi.Name value looks like <digest>-<random>-removing
|
||||||
|
// Split on '-' to get the digest value.
|
||||||
nameSplit := strings.Split(fi.Name(), "-")
|
nameSplit := strings.Split(fi.Name(), "-")
|
||||||
dgst := digest.NewDigestFromEncoded(algorithm, nameSplit[0])
|
dgst := digest.NewDigestFromEncoded(algorithm, nameSplit[0])
|
||||||
if err := dgst.Validate(); err != nil {
|
if err := dgst.Validate(); err != nil {
|
||||||
logrus.Debugf("Ignoring invalid digest %s:%s", algorithm, nameSplit[0])
|
logrus.WithError(err).WithField("digest", string(algorithm)+":"+nameSplit[0]).Debug("ignoring invalid digest")
|
||||||
} else {
|
continue
|
||||||
chainID := ChainID(dgst)
|
}
|
||||||
|
|
||||||
chainFile := filepath.Join(fms.root, string(algorithm), fi.Name(), "cache-id")
|
chainFile := filepath.Join(fms.root, string(algorithm), fi.Name(), "cache-id")
|
||||||
contentBytes, err := ioutil.ReadFile(chainFile)
|
contentBytes, err := ioutil.ReadFile(chainFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.WithError(err).WithField("digest", dgst).Error("cannot get cache ID")
|
if !os.IsNotExist(err) {
|
||||||
|
logrus.WithError(err).WithField("digest", dgst).Error("failed to read cache ID")
|
||||||
|
}
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
cacheID := strings.TrimSpace(string(contentBytes))
|
cacheID := strings.TrimSpace(string(contentBytes))
|
||||||
if cacheID == "" {
|
if cacheID == "" {
|
||||||
logrus.Errorf("invalid cache id value")
|
logrus.Error("invalid cache ID")
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
l := &roLayer{
|
l := &roLayer{
|
||||||
chainID: chainID,
|
chainID: ChainID(dgst),
|
||||||
cacheID: cacheID,
|
cacheID: cacheID,
|
||||||
}
|
}
|
||||||
orphanLayers = append(orphanLayers, *l)
|
orphanLayers = append(orphanLayers, *l)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
return orphanLayers, nil
|
return orphanLayers, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue