Skip to content

Commit

Permalink
fix: pass manifest digest to ensure new docker image download
Browse files Browse the repository at this point in the history
  • Loading branch information
zaremba-tomasz committed Sep 28, 2023
1 parent 93f4be9 commit e104bc2
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 9 deletions.
6 changes: 6 additions & 0 deletions src/cli/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ func (c *icli) Deploy(cCtx *cli.Context) error {
return err
}

manifestDigest, err := c.DockerService.GetManifestDigest()
if err != nil {
return err
}
c.dockerImage.ManifestDigest = manifestDigest

return knative.Apply(cCtx.String(namespaceFlag), config, project, c.dockerImage)
}

Expand Down
1 change: 1 addition & 0 deletions src/cli/onbranch.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func (c icli) buildPushDeploy(cCtx *cli.Context) error {
if cCtx.Bool(stopOnPushFlag) {
return err
}

return c.Deploy(cCtx)
}

Expand Down
15 changes: 10 additions & 5 deletions src/services/docker/docker-image.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@ import (
)

type DockerImage struct {
Registry string
Name string
Directory string
Tag string
Registry string
Name string
Directory string
Tag string
ManifestDigest string
}

func (dockerImage DockerImage) RemoteTag() string {
return fmt.Sprintf("%s/%s:%s", strings.ToLower(dockerImage.Registry), dockerImage.Name, dockerImage.Tag)
if dockerImage.ManifestDigest != "" {
return fmt.Sprintf("%s/%s:%s@%s", strings.ToLower(dockerImage.Registry), dockerImage.Name, dockerImage.Tag, dockerImage.ManifestDigest)
} else {
return fmt.Sprintf("%s/%s:%s", strings.ToLower(dockerImage.Registry), dockerImage.Name, dockerImage.Tag)
}
}

func (dockerImage DockerImage) LocalTag() string {
Expand Down
21 changes: 21 additions & 0 deletions src/services/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,24 @@ func (ds DockerService) Push() error {

return logger.PrintStream(pushResponse)
}

func (ds DockerService) GetManifestDigest() (string, error) {
logger.PrintInfo("Getting manifest digest for " + ds.dockerImage.RemoteTag())
logger.PrintInfo("User: " + ds.AuthConfig.Username)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*120)
defer cancel()

encodedJSON, err := json.Marshal(ds.AuthConfig)
if err != nil {
return "", err
}

encodedRegistryAuth := base64.URLEncoding.EncodeToString(encodedJSON)
distributionInspect, err := ds.Client.DistributionInspect(ctx, ds.dockerImage.RemoteTag(), encodedRegistryAuth)
if err != nil {
return "", err
}

digest := distributionInspect.Descriptor.Digest
return fmt.Sprintf("%s", digest), nil
}
25 changes: 25 additions & 0 deletions src/services/docker/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,31 @@ func TestLocalTag(t *testing.T) {
}

func TestRemoteTag(t *testing.T) {
dockerImage := DockerImage{
Registry: "example.org",
Directory: defaults.ProjectDirectory,
Name: "test",
Tag: "v1.1.0",
ManifestDigest: "sha256:3556949350924148437d1c40a2bfd74335fcd045da8b298e4ec4b6d5c53b4dc8",
}

ds := DockerService{
project: project.Project{
Directory: defaults.ProjectDirectory,
Name: "test",
Version: "v1.1.0",
},
dockerImage: dockerImage,
}

remoteTag := ds.dockerImage.RemoteTag()
expected := "example.org/test:v1.1.0@sha256:3556949350924148437d1c40a2bfd74335fcd045da8b298e4ec4b6d5c53b4dc8"
if remoteTag != expected {
t.Fatalf("Expected '%s' got %s", expected, remoteTag)
}
}

func TestRemoteTagMissingDigest(t *testing.T) {
dockerImage := DockerImage{
Registry: "example.org",
Directory: defaults.ProjectDirectory,
Expand Down
9 changes: 5 additions & 4 deletions src/services/k8s/knative_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,11 @@ func TestLoadManifest(t *testing.T) {
}

docker_image := docker.DockerImage{
Registry: "example.com",
Directory: ".",
Name: "test",
Tag: "v1.1.0",
Registry: "example.com",
Directory: ".",
Name: "test",
Tag: "v1.1.0",
ManifestDigest: "sha256:3556949350924148437d1c40a2bfd74335fcd045da8b298e4ec4b6d5c53b4dc8",
}

_, err := loadManifest(proj_knative, docker_image)
Expand Down

0 comments on commit e104bc2

Please sign in to comment.