Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Use digest if one is provided in custom images #841

Merged
merged 1 commit into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions pkg/abstractions/image/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type BuildOpts struct {
BaseImageRegistry string
BaseImageName string
BaseImageTag string
BaseImageDigest string
BaseImageCreds string
ExistingImageUri string
ExistingImageCreds map[string]string
Expand All @@ -75,6 +76,7 @@ func (o *BuildOpts) String() string {
fmt.Fprintf(&b, " \"BaseImageRegistry\": %q,", o.BaseImageRegistry)
fmt.Fprintf(&b, " \"BaseImageName\": %q,", o.BaseImageName)
fmt.Fprintf(&b, " \"BaseImageTag\": %q,", o.BaseImageTag)
fmt.Fprintf(&b, " \"BaseImageDigest\": %q,", o.BaseImageDigest)
fmt.Fprintf(&b, " \"BaseImageCreds\": %q,", o.BaseImageCreds)
fmt.Fprintf(&b, " \"ExistingImageUri\": %q,", o.ExistingImageUri)
fmt.Fprintf(&b, " \"ExistingImageCreds\": %#v,", o.ExistingImageCreds)
Expand Down Expand Up @@ -106,6 +108,7 @@ func (o *BuildOpts) setCustomImageBuildOptions() error {
o.BaseImageRegistry = baseImage.Registry
o.BaseImageName = baseImage.Repo
o.BaseImageTag = baseImage.Tag
o.BaseImageDigest = baseImage.Digest

return nil
}
Expand Down Expand Up @@ -180,7 +183,7 @@ func (b *Builder) GetImageId(opts *BuildOpts) (string, error) {

bodyToHash := &ImageIdHash{
BaseImageName: opts.BaseImageName,
BaseImageTag: opts.BaseImageTag,
BaseImageTag: tagOrDigest(opts.BaseImageDigest, opts.BaseImageTag),
PythonVersion: opts.PythonVersion,
PythonPackages: opts.PythonPackages,
ExitingImageUri: opts.ExistingImageUri,
Expand Down Expand Up @@ -232,6 +235,7 @@ func (b *Builder) Build(ctx context.Context, opts *BuildOpts, outputChan chan co
BaseImageRegistry: opts.BaseImageRegistry,
BaseImageName: opts.BaseImageName,
BaseImageTag: opts.BaseImageTag,
BaseImageDigest: opts.BaseImageDigest,
ExistingImageUri: opts.ExistingImageUri,
EnvVars: opts.EnvVars,
Dockerfile: opts.Dockerfile,
Expand All @@ -242,7 +246,14 @@ func (b *Builder) Build(ctx context.Context, opts *BuildOpts, outputChan chan co
return err
}

sourceImage := fmt.Sprintf("%s/%s:%s", opts.BaseImageRegistry, opts.BaseImageName, opts.BaseImageTag)
var sourceImage string
switch {
case opts.BaseImageDigest != "":
sourceImage = fmt.Sprintf("%s/%s@%s", opts.BaseImageRegistry, opts.BaseImageName, opts.BaseImageDigest)
default:
sourceImage = fmt.Sprintf("%s/%s:%s", opts.BaseImageRegistry, opts.BaseImageName, opts.BaseImageTag)
}

containerId := b.genContainerId()

// Allow config to override default build container settings
Expand Down Expand Up @@ -748,3 +759,10 @@ func extractPackageName(pkg string) string {
// Handle regular packages
return strings.FieldsFunc(pkg, func(c rune) bool { return c == '=' || c == '>' || c == '<' || c == '[' || c == ';' })[0]
}

func tagOrDigest(digest string, tag string) string {
if tag != "" {
return tag
}
return digest
}
6 changes: 6 additions & 0 deletions pkg/abstractions/image/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ func TestExtractImageNameAndTag(t *testing.T) {
wantRepo: "gis-ops/docker-valhalla/valhalla",
wantRegistry: "ghcr.io",
},
{
ref: "us-east1-docker.pkg.dev/test/ds-us-east1/test-test-test@sha256:c31c45b6fdc3d01c131a6dcae1daed008e3df4001bb43e555e49d82ac8d779e4",
wantDigest: "sha256:c31c45b6fdc3d01c131a6dcae1daed008e3df4001bb43e555e49d82ac8d779e4",
wantRepo: "test/ds-us-east1/test-test-test",
wantRegistry: "us-east1-docker.pkg.dev",
},
}

for _, test := range tests {
Expand Down
Loading