Skip to content

Commit df02642

Browse files
ericzzzzzzzidsulik
andauthored
fix(lookupRemote): cherry-pick cache remote fix (#9301)
* fix(lookupRemote): fixed lookup.go lookupRemote to compare remote and cached digests (#9278) * fix(lookupRemote): fixed lookup.go lookupRemote to compare remote and cached digests * fixes * fixed lookup.go * fixes * feat: schema * lint --------- Co-authored-by: idsulik <[email protected]>
1 parent cbc665b commit df02642

File tree

7 files changed

+50
-52
lines changed

7 files changed

+50
-52
lines changed

deploy/skaffold/Dockerfile.deps

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,5 +163,5 @@ RUN apt-get update && apt-get install --no-install-recommends --no-install-sugge
163163
jq \
164164
apt-transport-https && \
165165
rm -rf /var/lib/apt/lists/*
166-
COPY --from=golang:1.21.0 /usr/local/go /usr/local/go
166+
COPY --from=golang:1.21.6 /usr/local/go /usr/local/go
167167
ENV PATH /usr/local/go/bin:/root/go/bin:$PATH

deploy/skaffold/Dockerfile.deps.slim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,5 +104,5 @@ RUN apt-get update && apt-get install --no-install-recommends --no-install-sugge
104104
jq \
105105
apt-transport-https && \
106106
rm -rf /var/lib/apt/lists/*
107-
COPY --from=golang:1.21.0 /usr/local/go /usr/local/go
107+
COPY --from=golang:1.21.6 /usr/local/go /usr/local/go
108108
ENV PATH /usr/local/go/bin:/root/go/bin:$PATH

hack/versions/pkg/schema/check.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828
"github.com/GoogleContainerTools/skaffold/v2/hack/versions/pkg/diff"
2929
)
3030

31-
const baseRef = "origin/main"
31+
const baseRef = "origin/release/v2.10"
3232

3333
func RunSchemaCheckOnChangedFiles() error {
3434
git, err := newGit(baseRef)

pkg/skaffold/build/cache/cache.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ type ImageDetails struct {
4242
ID string `yaml:"id,omitempty"`
4343
}
4444

45+
func (d ImageDetails) HasID() bool {
46+
return d.ID != ""
47+
}
48+
49+
func (d ImageDetails) HasDigest() bool {
50+
return d.Digest != ""
51+
}
52+
4553
// ArtifactCache is a map of [artifact dependencies hash : ImageDetails]
4654
type ArtifactCache map[string]ImageDetails
4755

pkg/skaffold/build/cache/lookup.go

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -119,38 +119,40 @@ func (c *cache) lookupLocal(ctx context.Context, hash, tag string, entry ImageDe
119119
}
120120

121121
func (c *cache) lookupRemote(ctx context.Context, hash, tag string, platforms []specs.Platform) cacheDetails {
122-
var cacheHit bool
123-
entry := ImageDetails{}
122+
c.cacheMutex.RLock()
123+
cachedEntry, cacheHit := c.artifactCache[hash]
124+
c.cacheMutex.RUnlock()
124125

125-
if digest, err := docker.RemoteDigest(tag, c.cfg, nil); err == nil {
126-
log.Entry(ctx).Debugf("Found %s remote", tag)
127-
entry.Digest = digest
126+
if remoteDigest, err := docker.RemoteDigest(tag, c.cfg, nil); err == nil {
127+
if cacheHit && remoteDigest == cachedEntry.Digest {
128+
log.Entry(ctx).Debugf("Found %s remote with the same digest", tag)
129+
return found{hash: hash}
130+
}
128131

129-
c.cacheMutex.Lock()
130-
c.artifactCache[hash] = entry
131-
c.cacheMutex.Unlock()
132-
return found{hash: hash}
132+
if !cacheHit {
133+
log.Entry(ctx).Debugf("Added digest for %s to cache entry", tag)
134+
cachedEntry.Digest = remoteDigest
135+
c.cacheMutex.Lock()
136+
c.artifactCache[hash] = cachedEntry
137+
c.cacheMutex.Unlock()
138+
}
133139
}
134140

135-
c.cacheMutex.RLock()
136-
entry, cacheHit = c.artifactCache[hash]
137-
c.cacheMutex.RUnlock()
138-
139-
if cacheHit {
141+
if cachedEntry.HasDigest() {
140142
// Image exists remotely with a different tag
141-
fqn := tag + "@" + entry.Digest // Actual tag will be ignored but we need the registry and the digest part of it.
142-
log.Entry(ctx).Debugf("Looking up %s tag with the full fqn %s", tag, entry.Digest)
143+
fqn := tag + "@" + cachedEntry.Digest // Actual tag will be ignored but we need the registry and the digest part of it.
144+
log.Entry(ctx).Debugf("Looking up %s tag with the full fqn %s", tag, cachedEntry.Digest)
143145
if remoteDigest, err := docker.RemoteDigest(fqn, c.cfg, nil); err == nil {
144146
log.Entry(ctx).Debugf("Found %s with the full fqn", tag)
145-
if remoteDigest == entry.Digest {
146-
return needsRemoteTagging{hash: hash, tag: tag, digest: entry.Digest, platforms: platforms}
147+
if remoteDigest == cachedEntry.Digest {
148+
return needsRemoteTagging{hash: hash, tag: tag, digest: cachedEntry.Digest, platforms: platforms}
147149
}
148150
}
151+
}
149152

150-
// Image exists locally
151-
if entry.ID != "" && c.client != nil && c.client.ImageExists(ctx, entry.ID) {
152-
return needsPushing{hash: hash, tag: tag, imageID: entry.ID}
153-
}
153+
// Image exists locally
154+
if cachedEntry.HasID() && c.client != nil && c.client.ImageExists(ctx, cachedEntry.ID) {
155+
return needsPushing{hash: hash, tag: tag, imageID: cachedEntry.ID}
154156
}
155157

156158
return needsBuilding{hash: hash}

pkg/skaffold/build/cache/retrieve_test.go

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -206,31 +206,23 @@ func TestCacheBuildRemote(t *testing.T) {
206206
Write("dep1", "content1").
207207
Write("dep2", "content2").
208208
Write("dep3", "content3").
209-
Write("dep4", "content4").
210-
Write("dep5", "content5").
211209
Chdir()
212210

213211
tags := map[string]string{
214-
"artifact1": "artifact1:tag1",
215-
"artifact2": "artifact2:tag2",
216-
"exist_artifact1": "exist_artifact1:tag1",
217-
"exist_artifact2": "exist_artifact2:tag2",
212+
"artifact1": "artifact1:tag1",
213+
"artifact2": "artifact2:tag2",
218214
}
219215
artifacts := []*latest.Artifact{
220216
{ImageName: "artifact1", ArtifactType: latest.ArtifactType{DockerArtifact: &latest.DockerArtifact{}}},
221217
{ImageName: "artifact2", ArtifactType: latest.ArtifactType{DockerArtifact: &latest.DockerArtifact{}}},
222-
{ImageName: "exist_artifact1", ArtifactType: latest.ArtifactType{DockerArtifact: &latest.DockerArtifact{}}},
223-
{ImageName: "exist_artifact2", ArtifactType: latest.ArtifactType{DockerArtifact: &latest.DockerArtifact{}}},
224218
}
225219
deps := depLister(map[string][]string{
226-
"artifact1": {"dep1", "dep2"},
227-
"artifact2": {"dep3"},
228-
"exist_artifact1": {"dep4"},
229-
"exist_artifact2": {"dep5"},
220+
"artifact1": {"dep1", "dep2"},
221+
"artifact2": {"dep3"},
230222
})
231223
tagToDigest := map[string]string{
232-
"exist_artifact1:tag1": "sha256:51ae7fa00c92525c319404a3a6d400e52ff9372c5a39cb415e0486fe425f3165",
233-
"exist_artifact2:tag2": "sha256:35bdf2619f59e6f2372a92cb5486f4a0bf9b86e0e89ee0672864db6ed9c51539",
224+
"artifact1:tag1": "sha256:51ae7fa00c92525c319404a3a6d400e52ff9372c5a39cb415e0486fe425f3165",
225+
"artifact2:tag2": "sha256:35bdf2619f59e6f2372a92cb5486f4a0bf9b86e0e89ee0672864db6ed9c51539",
234226
}
235227

236228
// Mock Docker
@@ -273,37 +265,29 @@ func TestCacheBuildRemote(t *testing.T) {
273265

274266
t.CheckNoError(err)
275267
t.CheckDeepEqual(2, len(builder.built))
276-
t.CheckDeepEqual(4, len(bRes))
268+
t.CheckDeepEqual(2, len(bRes))
277269
// Artifacts should always be returned in their original order
278270
t.CheckDeepEqual("artifact1", bRes[0].ImageName)
279271
t.CheckDeepEqual("artifact2", bRes[1].ImageName)
280272

281-
// Add the other tags to the remote cache
282-
tagToDigest["artifact1:tag1"] = tagToDigest["exist_artifact1:tag1"]
283-
tagToDigest["artifact2:tag2"] = tagToDigest["exist_artifact2:tag2"]
284-
api.Add("artifact1:tag1", tagToDigest["artifact1:tag1"])
285-
api.Add("artifact2:tag2", tagToDigest["artifact2:tag2"])
286-
287273
// Second build: both artifacts are read from cache
288274
builder = &mockBuilder{dockerDaemon: dockerDaemon, push: true, cache: artifactCache}
289275
bRes, err = artifactCache.Build(context.Background(), io.Discard, tags, artifacts, platform.Resolver{}, builder.Build)
290276

291277
t.CheckNoError(err)
292278
t.CheckEmpty(builder.built)
293-
t.CheckDeepEqual(4, len(bRes))
279+
t.CheckDeepEqual(2, len(bRes))
294280
t.CheckDeepEqual("artifact1", bRes[0].ImageName)
295281
t.CheckDeepEqual("artifact2", bRes[1].ImageName)
296282

297283
// Third build: change one artifact's dependencies
298284
tmpDir.Write("dep1", "new content")
299-
tags["artifact1"] = "artifact1:new_tag1"
300-
301285
builder = &mockBuilder{dockerDaemon: dockerDaemon, push: true, cache: artifactCache}
302286
bRes, err = artifactCache.Build(context.Background(), io.Discard, tags, artifacts, platform.Resolver{}, builder.Build)
303287

304288
t.CheckNoError(err)
305289
t.CheckDeepEqual(1, len(builder.built))
306-
t.CheckDeepEqual(4, len(bRes))
290+
t.CheckDeepEqual(2, len(bRes))
307291
t.CheckDeepEqual("artifact1", bRes[0].ImageName)
308292
t.CheckDeepEqual("artifact2", bRes[1].ImageName)
309293
})

pkg/skaffold/runner/new.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,12 @@ func isImageLocal(runCtx *runcontext.RunContext, imageName string) (bool, error)
232232
log.Entry(context.TODO()).Debugf("Didn't find pipeline for image %s. Using default pipeline!", imageName)
233233
pipeline = runCtx.DefaultPipeline()
234234
}
235-
if pipeline.Build.GoogleCloudBuild != nil || pipeline.Build.Cluster != nil {
236-
log.Entry(context.TODO()).Debugf("Image %s is remote because it has GoogleCloudBuild or pipeline.Build.Cluster", imageName)
235+
if pipeline.Build.GoogleCloudBuild != nil {
236+
log.Entry(context.TODO()).Debugf("Image %s is remote because it has pipeline.Build.GoogleCloudBuild", imageName)
237+
return false, nil
238+
}
239+
if pipeline.Build.Cluster != nil {
240+
log.Entry(context.TODO()).Debugf("Image %s is remote because it has pipeline.Build.Cluster", imageName)
237241
return false, nil
238242
}
239243

0 commit comments

Comments
 (0)