Skip to content

Commit

Permalink
feat(staged-dockerfile): refine dockerfile instructions digests calcu…
Browse files Browse the repository at this point in the history
…lations

Signed-off-by: Timofey Kirillov <timofey.kirillov@flant.com>
  • Loading branch information
distorhead committed Oct 27, 2022
1 parent e558e1e commit d15d79f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 8 deletions.
5 changes: 2 additions & 3 deletions pkg/build/stage/instruction/env.go
Expand Up @@ -27,11 +27,10 @@ func (stg *Env) GetDependencies(ctx context.Context, c stage.Conveyor, cb contai
}

args = append(args, "Instruction", stg.instruction.Data.Name())
// FIXME(staged-dockerfile): sort envs
if len(stg.instruction.Data.Envs) > 0 {
args = append(args, "Envs")
for k, v := range stg.instruction.Data.Envs {
args = append(args, k, v)
for _, k := range util.SortedStringKeys(stg.instruction.Data.Envs) {
args = append(args, k, stg.instruction.Data.Envs[k])
}
}
return util.Sha256Hash(args...), nil
Expand Down
7 changes: 2 additions & 5 deletions pkg/build/stage/instruction/label.go
Expand Up @@ -27,14 +27,11 @@ func (stg *Label) GetDependencies(ctx context.Context, c stage.Conveyor, cb cont
}

args = append(args, "Instruction", stg.instruction.Data.Name())

// FIXME(staged-dockerfile): sort labels map
if len(stg.instruction.Data.Labels) > 0 {
args = append(args, "Labels")
for k, v := range stg.instruction.Data.Labels {
args = append(args, k, v)
for _, k := range util.SortedStringKeys(stg.instruction.Data.Labels) {
args = append(args, k, stg.instruction.Data.Labels[k])
}
}

return util.Sha256Hash(args...), nil
}
27 changes: 27 additions & 0 deletions pkg/build/stage/instruction/run.go
Expand Up @@ -2,6 +2,7 @@ package instruction

import (
"context"
"fmt"

"github.com/werf/werf/pkg/build/stage"
"github.com/werf/werf/pkg/config"
Expand All @@ -28,6 +29,32 @@ func (stg *Run) GetDependencies(ctx context.Context, c stage.Conveyor, cb contai

args = append(args, "Instruction", stg.instruction.Data.Name())
args = append(args, append([]string{"Command"}, stg.instruction.Data.Command...)...)
args = append(args, "PrependShell", fmt.Sprintf("%v", stg.instruction.Data.PrependShell))
args = append(args, "Network", string(stg.instruction.Data.Network))
args = append(args, "Security", string(stg.instruction.Data.Security))

if len(stg.instruction.Data.Mounts) > 0 {
args = append(args, "Mounts")
for _, mnt := range stg.instruction.Data.Mounts {
args = append(args, "Type", mnt.Type)
args = append(args, "From", mnt.From)
args = append(args, "Source", mnt.Source)
args = append(args, "Target", mnt.Target)
args = append(args, "ReadOnly", fmt.Sprintf("%v", mnt.ReadOnly))
args = append(args, "CacheID", mnt.CacheID)
args = append(args, "CacheSharing", mnt.CacheSharing)
args = append(args, "Required", fmt.Sprintf("%v", mnt.Required))
if mnt.Mode != nil {
args = append(args, "Mode", fmt.Sprintf("%d", *mnt.Mode))
}
if mnt.UID != nil {
args = append(args, "UID", fmt.Sprintf("%d", *mnt.UID))
}
if mnt.GID != nil {
args = append(args, "GID", fmt.Sprintf("%d", *mnt.GID))
}
}
}

// TODO(ilya-lesikov): should bind mount with context as src be counted as dependency?

Expand Down
13 changes: 13 additions & 0 deletions pkg/util/map.go
@@ -1,5 +1,9 @@
package util

import (
"sort"
)

// Dest has higher priority.
func MergeMaps[K comparable, V any](src, dest map[K]V) map[K]V {
result := make(map[K]V)
Expand All @@ -14,3 +18,12 @@ func MergeMaps[K comparable, V any](src, dest map[K]V) map[K]V {

return result
}

func SortedStringKeys(m map[string]string) []string {
var keys []string
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
return keys
}

0 comments on commit d15d79f

Please sign in to comment.