Skip to content

Commit 5e6575d

Browse files
feat(cleanup): keep dependencies (#6704)
Signed-off-by: Yaroslav Pershin <62902094+iapershin@users.noreply.github.com> Signed-off-by: Aleksei Igrychev <aleksei.igrychev@palark.com> Co-authored-by: Aleksei Igrychev <aleksei.igrychev@palark.com>
1 parent 1f5613a commit 5e6575d

File tree

7 files changed

+67
-25
lines changed

7 files changed

+67
-25
lines changed

Diff for: pkg/build/stage/dependencies.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ func (s *DependenciesStage) GetDependencies(ctx context.Context, c Conveyor, cb
9696
}
9797

9898
func (s *DependenciesStage) prepareImageWithLegacyStapelBuilder(ctx context.Context, c Conveyor, cr container_backend.ContainerBackend, _, stageImage *StageImage) error {
99+
imageServiceCommitChangeOptions := stageImage.Builder.LegacyStapelStageBuilder().Container().ServiceCommitChangeOptions()
99100
for _, elm := range s.imports {
100101
sourceImageName := getSourceImageName(elm)
101102
srv, err := c.GetImportServer(ctx, s.targetPlatform, sourceImageName, elm.Stage)
@@ -106,8 +107,6 @@ func (s *DependenciesStage) prepareImageWithLegacyStapelBuilder(ctx context.Cont
106107
command := srv.GetCopyCommand(ctx, elm)
107108
stageImage.Builder.LegacyStapelStageBuilder().Container().AddServiceRunCommands(command)
108109

109-
imageServiceCommitChangeOptions := stageImage.Builder.LegacyStapelStageBuilder().Container().ServiceCommitChangeOptions()
110-
111110
checksumLabelKey := imagePkg.WerfImportChecksumLabelPrefix + getImportID(elm)
112111
sourceStageIDLabelKey := imagePkg.WerfImportSourceStageIDLabelPrefix + getImportID(elm)
113112

@@ -157,6 +156,11 @@ func (s *DependenciesStage) prepareImageWithLegacyStapelBuilder(ctx context.Cont
157156
})
158157
}
159158
}
159+
160+
depStageID := c.GetStageIDForLastImageStage(s.targetPlatform, dep.ImageName)
161+
imageServiceCommitChangeOptions.AddLabel(map[string]string{
162+
dependencyLabelKey(depStageID): depStageID,
163+
})
160164
}
161165

162166
return nil
@@ -220,6 +224,10 @@ func (s *DependenciesStage) prepareImage(ctx context.Context, c Conveyor, cr con
220224
})
221225
}
222226
}
227+
depStageID := c.GetStageIDForLastImageStage(s.targetPlatform, dep.ImageName)
228+
stageImage.Builder.StapelStageBuilder().AddLabels(map[string]string{
229+
dependencyLabelKey(depStageID): depStageID,
230+
})
223231
}
224232

225233
return nil

Diff for: pkg/build/stage/dependencies_utils.go

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package stage
22

33
import (
4+
"fmt"
5+
6+
"github.com/werf/common-go/pkg/util"
47
"github.com/werf/werf/v2/pkg/config"
58
"github.com/werf/werf/v2/pkg/image"
69
)
@@ -41,3 +44,7 @@ func ResolveDependenciesArgs(targetPlatform string, dependencies []*config.Depen
4144

4245
return resolved
4346
}
47+
48+
func dependencyLabelKey(depStageID string) string {
49+
return fmt.Sprintf("%s%s", image.WerfDependencySourceStageIDLabelPrefix, util.Sha256Hash(depStageID))
50+
}

Diff for: pkg/build/stage/full_dockerfile.go

+5
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,11 @@ func (s *FullDockerfileStage) PrepareImage(ctx context.Context, c Conveyor, cb c
606606

607607
stageImage.Builder.DockerfileBuilder().AppendLabels(fmt.Sprintf("%s=%s", image.WerfProjectRepoCommitLabel, c.GiterminismManager().HeadCommit()))
608608

609+
for _, dep := range s.dependencies {
610+
depStageID := c.GetStageIDForLastImageStage(s.targetPlatform, dep.ImageName)
611+
stageImage.Builder.DockerfileBuilder().AppendLabels(fmt.Sprintf("%s=%s", dependencyLabelKey(depStageID), depStageID))
612+
}
613+
609614
if c.GiterminismManager().Dev() {
610615
stageImage.Builder.DockerfileBuilder().AppendLabels(fmt.Sprintf("%s=true", image.WerfDevLabel))
611616
}

Diff for: pkg/build/stage/stubs.go

+16-2
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,15 @@ func (c *LegacyContainerStub) ServiceCommitChangeOptions() container_backend.Leg
4949
type LegacyContainerOptionsStub struct {
5050
container_backend.LegacyContainerOptions
5151

52-
Env map[string]string
52+
Env map[string]string
53+
Labels map[string]string
5354
}
5455

5556
func NewLegacyContainerOptionsStub() *LegacyContainerOptionsStub {
56-
return &LegacyContainerOptionsStub{Env: make(map[string]string)}
57+
return &LegacyContainerOptionsStub{
58+
Env: make(map[string]string),
59+
Labels: make(map[string]string),
60+
}
5761
}
5862

5963
func (opts *LegacyContainerOptionsStub) AddEnv(envs map[string]string) {
@@ -62,6 +66,12 @@ func (opts *LegacyContainerOptionsStub) AddEnv(envs map[string]string) {
6266
}
6367
}
6468

69+
func (opts *LegacyContainerOptionsStub) AddLabel(labels map[string]string) {
70+
for k, v := range labels {
71+
opts.Labels[k] = v
72+
}
73+
}
74+
6575
type ConveyorStub struct {
6676
Conveyor
6777

@@ -104,6 +114,10 @@ func (c *ConveyorStub) GiterminismManager() giterminism_manager.Interface {
104114
return c.giterminismManager
105115
}
106116

117+
func (c *ConveyorStub) GetStageIDForLastImageStage(targetPlatform, imageName string) string {
118+
return c.lastStageImageDigestByImageName[imageName]
119+
}
120+
107121
type GiterminismInspectorStub struct {
108122
giterminism_manager.Inspector
109123
}

Diff for: pkg/cleaning/cleanup.go

+13-7
Original file line numberDiff line numberDiff line change
@@ -716,9 +716,9 @@ func (m *cleanupManager) cleanupUnusedStages(ctx context.Context) error {
716716
{
717717
for protectionReason, stageDescToKeepSet := range m.stageManager.GetProtectedStageDescSetByReason() {
718718
// Git history based policy keeps import sources more effectively, other policies do not keep them.
719-
withImportSources := protectionReason != stage_manager.ProtectionReasonGitPolicy
719+
withImportOrDependencySources := protectionReason != stage_manager.ProtectionReasonGitPolicy
720720
for stageDescToKeep := range stageDescToKeepSet.Iter() {
721-
m.protectRelativeStageDescSetByStageDesc(stageDescToKeep, withImportSources)
721+
m.protectRelativeStageDescSetByStageDesc(stageDescToKeep, withImportOrDependencySources)
722722
}
723723
}
724724

@@ -882,7 +882,7 @@ func deleteImportsMetadata(ctx context.Context, projectName string, storageManag
882882
})
883883
}
884884

885-
func (m *cleanupManager) protectRelativeStageDescSetByStageDesc(targetStageDesc *image.StageDesc, withImportSource bool) {
885+
func (m *cleanupManager) protectRelativeStageDescSetByStageDesc(targetStageDesc *image.StageDesc, withImportOrDependencySources bool) {
886886
targetStageDescSet := image.NewStageDescSet()
887887
if targetStageDesc.Info.IsIndex {
888888
for _, platformImageInfo := range targetStageDesc.Info.Index {
@@ -909,11 +909,11 @@ func (m *cleanupManager) protectRelativeStageDescSetByStageDesc(targetStageDesc
909909
handledStageDescSet.Add(currentStageDesc)
910910
}
911911

912-
// Import source checking.
913-
if withImportSource {
914-
for label, checksum := range currentStageDesc.Info.Labels {
912+
// Import or Dependency source checking.
913+
if withImportOrDependencySources {
914+
for label, value := range currentStageDesc.Info.Labels {
915915
if strings.HasPrefix(label, image.WerfImportChecksumLabelPrefix) {
916-
sourceStageIDs, ok := m.checksumSourceStageIDs[checksum]
916+
sourceStageIDs, ok := m.checksumSourceStageIDs[value]
917917
if !ok {
918918
continue
919919
}
@@ -925,6 +925,12 @@ func (m *cleanupManager) protectRelativeStageDescSetByStageDesc(targetStageDesc
925925
m.stageManager.MarkStageDescAsProtected(sourceStageDesc, stage_manager.ProtectionReasonImportSource, false)
926926
}
927927
}
928+
} else if strings.HasPrefix(label, image.WerfDependencySourceStageIDLabelPrefix) {
929+
sourceStageDesc := m.stageManager.GetStageDescByStageID(value)
930+
if sourceStageDesc != nil {
931+
currentStageDescSet.Add(sourceStageDesc)
932+
m.stageManager.MarkStageDescAsProtected(sourceStageDesc, stage_manager.ProtectionReasonDependencySource, false)
933+
}
928934
}
929935
}
930936
}

Diff for: pkg/cleaning/stage_manager/stages.go

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ var (
3535
ProtectionReasonGitPolicy = newProtectionReason("git policy")
3636
ProtectionReasonBuiltWithinLastNHoursPolicy = newProtectionReason("built within last N hours")
3737
ProtectionReasonImportSource = newProtectionReason("import source")
38+
ProtectionReasonDependencySource = newProtectionReason("dependency source")
3839
ProtectionReasonAncestor = newProtectionReason("ancestor")
3940
ProtectionReasonNotFoundInRepo = newProtectionReason("not found in repo")
4041
)

Diff for: pkg/image/const.go

+15-14
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
package image
22

33
const (
4-
WerfLabelPrefix = "werf"
5-
WerfLabel = "werf"
6-
WerfVersionLabel = "werf-version"
7-
WerfCacheVersionLabel = "werf-cache-version"
8-
WerfImageLabel = "werf-image"
9-
WerfDevLabel = "werf-dev"
10-
WerfDockerImageName = "werf-docker-image-name"
11-
WerfStageDigestLabel = "werf-stage-digest"
12-
WerfStageContentDigestLabel = "werf-stage-content-digest"
13-
WerfProjectRepoCommitLabel = "werf-project-repo-commit"
14-
WerfImportChecksumLabelPrefix = "werf-import-checksum-"
15-
WerfImportSourceStageIDLabelPrefix = "werf-import-source-stage-id-"
16-
WerfBaseImageIDLabel = "werf.io/base-image-id"
17-
WerfParentStageID = "werf.io/parent-stage-id"
4+
WerfLabelPrefix = "werf"
5+
WerfLabel = "werf"
6+
WerfVersionLabel = "werf-version"
7+
WerfCacheVersionLabel = "werf-cache-version"
8+
WerfImageLabel = "werf-image"
9+
WerfDevLabel = "werf-dev"
10+
WerfDockerImageName = "werf-docker-image-name"
11+
WerfStageDigestLabel = "werf-stage-digest"
12+
WerfStageContentDigestLabel = "werf-stage-content-digest"
13+
WerfProjectRepoCommitLabel = "werf-project-repo-commit"
14+
WerfImportChecksumLabelPrefix = "werf-import-checksum-"
15+
WerfImportSourceStageIDLabelPrefix = "werf-import-source-stage-id-"
16+
WerfDependencySourceStageIDLabelPrefix = "werf-dependency-stage-id-"
17+
WerfBaseImageIDLabel = "werf.io/base-image-id"
18+
WerfParentStageID = "werf.io/parent-stage-id"
1819

1920
WerfImportMetadataChecksumLabel = "checksum"
2021
WerfImportMetadataSourceStageIDLabel = "source-stage-id"

0 commit comments

Comments
 (0)