-
Notifications
You must be signed in to change notification settings - Fork 204
/
from.go
94 lines (75 loc) · 2.76 KB
/
from.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package stage
import (
"context"
"fmt"
"path"
"path/filepath"
"strings"
"github.com/werf/werf/pkg/config"
"github.com/werf/werf/pkg/container_runtime"
"github.com/werf/werf/pkg/stapel"
"github.com/werf/werf/pkg/util"
)
func GenerateFromStage(imageBaseConfig *config.StapelImageBase, baseImageRepoId string, baseStageOptions *NewBaseStageOptions) *FromStage {
var baseImageRepoIdOrNone string
if imageBaseConfig.FromLatest {
baseImageRepoIdOrNone = baseImageRepoId
}
var fromImageOrArtifactImageName string
if imageBaseConfig.FromImageName != "" {
fromImageOrArtifactImageName = imageBaseConfig.FromImageName
} else if imageBaseConfig.FromImageArtifactName != "" {
fromImageOrArtifactImageName = imageBaseConfig.FromImageArtifactName
}
return newFromStage(fromImageOrArtifactImageName, baseImageRepoIdOrNone, imageBaseConfig.FromCacheVersion, baseStageOptions)
}
func newFromStage(fromImageOrArtifactImageName, baseImageRepoIdOrNone, cacheVersion string, baseStageOptions *NewBaseStageOptions) *FromStage {
s := &FromStage{}
s.cacheVersion = cacheVersion
s.fromImageOrArtifactImageName = fromImageOrArtifactImageName
s.baseImageRepoIdOrNone = baseImageRepoIdOrNone
s.BaseStage = newBaseStage(From, baseStageOptions)
return s
}
type FromStage struct {
*BaseStage
fromImageOrArtifactImageName string
baseImageRepoIdOrNone string
cacheVersion string
}
func (s *FromStage) GetDependencies(_ context.Context, c Conveyor, prevImage, _ container_runtime.ImageInterface) (string, error) {
var args []string
if s.cacheVersion != "" {
args = append(args, s.cacheVersion)
}
if s.baseImageRepoIdOrNone != "" {
args = append(args, s.baseImageRepoIdOrNone)
}
for _, mount := range s.configMounts {
args = append(args, filepath.ToSlash(filepath.Clean(mount.From)), path.Clean(mount.To), mount.Type)
}
if s.fromImageOrArtifactImageName != "" {
args = append(args, c.GetImageContentSignature(s.fromImageOrArtifactImageName))
} else {
args = append(args, prevImage.Name())
}
return util.Sha256Hash(args...), nil
}
func (s *FromStage) PrepareImage(ctx context.Context, c Conveyor, prevBuiltImage, image container_runtime.ImageInterface) error {
if err := s.addProjectRepoCommitToLabels(ctx, c, image); err != nil {
return err
}
serviceMounts := s.getServiceMounts(prevBuiltImage)
s.addServiceMountsLabels(serviceMounts, image)
customMounts := s.getCustomMounts(prevBuiltImage)
s.addCustomMountLabels(customMounts, image)
var mountpoints []string
for _, mountCfg := range s.configMounts {
mountpoints = append(mountpoints, mountCfg.To)
}
if len(mountpoints) != 0 {
mountpointsStr := strings.Join(mountpoints, " ")
image.Container().AddServiceRunCommands(fmt.Sprintf("%s -rf %s", stapel.RmBinPath(), mountpointsStr))
}
return nil
}