Skip to content

Commit

Permalink
wip: skip-layers for analyzer
Browse files Browse the repository at this point in the history
Signed-off-by: Javier Romero <jromero@pivotal.io>
  • Loading branch information
ameyer-pivotal authored and jromero committed Jun 27, 2019
1 parent af83d98 commit fa7d238
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 43 deletions.
90 changes: 47 additions & 43 deletions analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import (
)

type Analyzer struct {
Buildpacks []*Buildpack
AppDir string
AnalyzedPath string
LayersDir string
AppDir string
Buildpacks []*Buildpack
GID, UID int
In []byte
LayersDir string
Out, Err *log.Logger
UID int
GID int
SkipLayers bool
}

func (a *Analyzer) Analyze(image imgutil.Image) error {
Expand All @@ -27,51 +27,55 @@ func (a *Analyzer) Analyze(image imgutil.Image) error {
return err
}

for _, buildpack := range a.Buildpacks {
cache, err := readBuildpackLayersDir(a.LayersDir, *buildpack)
if err != nil {
return err
}
if !a.SkipLayers {
for _, buildpack := range a.Buildpacks {
bpLayersDir, err := readBuildpackLayersDir(a.LayersDir, *buildpack)
if err != nil {
return err
}

metadataLayers := data.MetadataForBuildpack(buildpack.ID).Layers
for _, cachedLayer := range cache.layers {
cacheType := cachedLayer.classifyCache(metadataLayers)
switch cacheType {
case cacheStaleNoMetadata:
a.Out.Printf("removing stale cached launch layer '%s', not in metadata \n", cachedLayer.Identifier())
if err := cachedLayer.remove(); err != nil {
return err
}
case cacheStaleWrongSHA:
a.Out.Printf("removing stale cached launch layer '%s'", cachedLayer.Identifier())
if err := cachedLayer.remove(); err != nil {
return err
}
case cacheMalformed:
a.Out.Printf("removing malformed cached layer '%s'", cachedLayer.Identifier())
if err := cachedLayer.remove(); err != nil {
return err
}
case cacheNotForLaunch:
a.Out.Printf("using cached layer '%s'", cachedLayer.Identifier())
case cacheValid:
a.Out.Printf("using cached launch layer '%s'", cachedLayer.Identifier())
a.Out.Printf("rewriting metadata for layer '%s'", cachedLayer.Identifier())
if err := cachedLayer.writeMetadata(metadataLayers); err != nil {
return err
metadataLayers := data.MetadataForBuildpack(buildpack.ID).Layers
for _, cachedLayer := range bpLayersDir.layers {
cacheType := cachedLayer.classifyCache(metadataLayers)
switch cacheType {
case cacheStaleNoMetadata:
a.Out.Printf("removing stale cached launch layer '%s', not in metadata \n", cachedLayer.Identifier())
if err := cachedLayer.remove(); err != nil {
return err
}
case cacheStaleWrongSHA:
a.Out.Printf("removing stale cached launch layer '%s'", cachedLayer.Identifier())
if err := cachedLayer.remove(); err != nil {
return err
}
case cacheMalformed:
a.Out.Printf("removing malformed cached layer '%s'", cachedLayer.Identifier())
if err := cachedLayer.remove(); err != nil {
return err
}
case cacheNotForLaunch:
a.Out.Printf("using cached layer '%s'", cachedLayer.Identifier())
case cacheValid:
a.Out.Printf("using cached launch layer '%s'", cachedLayer.Identifier())
a.Out.Printf("rewriting metadata for layer '%s'", cachedLayer.Identifier())
if err := cachedLayer.writeMetadata(metadataLayers); err != nil {
return err
}
}
}
}

for lmd, data := range metadataLayers {
if !data.Build && !data.Cache {
layer := cache.newBPLayer(lmd)
a.Out.Printf("writing metadata for uncached layer '%s'", layer.Identifier())
if err := layer.writeMetadata(metadataLayers); err != nil {
return err
for lmd, data := range metadataLayers {
if !data.Build && !data.Cache {
layer := bpLayersDir.newBPLayer(lmd)
a.Out.Printf("writing metadata for uncached layer '%s'", layer.Identifier())
if err := layer.writeMetadata(metadataLayers); err != nil {
return err
}
}
}
}
} else {
a.Out.Printf("Skipping buildpack layer analysis")
}

// if analyzer is running as root it needs to fix the ownership of the layers dir
Expand Down
29 changes: 29 additions & 0 deletions analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,35 @@ func testAnalyzer(t *testing.T, when spec.G, it spec.S) {
h.AssertContains(t, entries, `[metadata]`)
})
})

when("skip-layers is true", func() {
it.Before(func() {
analyzer.SkipLayers = true
})

it("should write analyzed TOML", func() {
err := analyzer.Analyze(image)
h.AssertNil(t, err)

b, err := ioutil.ReadFile(analyzer.AnalyzedPath)
h.AssertNil(t, err)

entries := strings.Split(string(b), "\n")
h.AssertContains(t, entries, `repository = "image-repo-name"`)
h.AssertContains(t, entries, `digest = "s0m3D1g3sT"`)
h.AssertContains(t, entries, `[metadata]`)
})

it("does not write buildpack layers", func() {
err := analyzer.Analyze(image)
h.AssertNil(t, err)

fileInfos, err := ioutil.ReadDir(layerDir)
h.AssertNil(t, err)

h.AssertEq(t, len(fileInfos), 0)
})
})
})
})

Expand Down
3 changes: 3 additions & 0 deletions cmd/analyzer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var (
useHelpers bool
uid int
gid int
skipLayers bool
)

func init() {
Expand All @@ -41,6 +42,7 @@ func init() {
cmd.FlagUID(&uid)
cmd.FlagUseDaemon(&useDaemon)
cmd.FlagUseCredHelpers(&useHelpers)
cmd.FlagSkipLayers(&skipLayers)
}

func main() {
Expand Down Expand Up @@ -79,6 +81,7 @@ func analyzer() error {
Err: log.New(os.Stderr, "", 0),
UID: uid,
GID: gid,
SkipLayers: skipLayers,
}

var err error
Expand Down
5 changes: 5 additions & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const (
EnvUID = "CNB_USER_ID"
EnvGID = "CNB_GROUP_ID"
EnvRegistryAuth = "CNB_REGISTRY_AUTH"
EnvSkipLayers = "CNB_SKIP_LAYERS" // defaults to false
)

func FlagAnalyzedPath(dir *string) {
Expand Down Expand Up @@ -108,6 +109,10 @@ func FlagUseDaemon(use *bool) {
flag.BoolVar(use, "daemon", boolEnv(EnvUseDaemon), "export to docker daemon")
}

func FlagSkipLayers(skip *bool) {
flag.BoolVar(skip, "skip-layers", boolEnv(EnvSkipLayers), "do not provide layer metadata to buildpacks")
}

const (
CodeFailed = 1
// 2: reserved
Expand Down

0 comments on commit fa7d238

Please sign in to comment.