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 08117da
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 13 deletions.
4 changes: 2 additions & 2 deletions assets/knative/service.yaml.tmpl
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: {{ .Name }}
name: {{ .DockerImage.Name }}
namespace: default
spec:
template:
spec:
containers:
- image: {{ .RemoteTag }}
- image: {{ .DockerImage.RemoteTag }}@{{ .ImageDigest }}
imagePullPolicy: Always
7 changes: 6 additions & 1 deletion src/cli/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ func (c *icli) Deploy(cCtx *cli.Context) error {
return err
}

return knative.Apply(cCtx.String(namespaceFlag), config, project, c.dockerImage)
imageDigest, err := c.DockerService.GetImageDigest()
if err != nil {
return err
}

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

func (c icli) DeployCMD() *cli.Command {
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) GetImageDigest() (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
}
18 changes: 14 additions & 4 deletions src/services/k8s/knative.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ import (
servingv1client "knative.dev/serving/pkg/client/clientset/versioned/typed/serving/v1"
)

type Image struct {
DockerImage docker.DockerImage
ImageDigest string
}

func Config(endpoint string, token string, caCrt []byte) (*rest.Config, error) {
if _, err := certutil.NewPoolFromBytes(caCrt); err != nil {
return nil, fmt.Errorf("Expected to load root CA from bytes, but got err: %v", err)
Expand All @@ -37,7 +42,7 @@ func Config(endpoint string, token string, caCrt []byte) (*rest.Config, error) {
}, nil
}

func loadManifest(project *project.Project, dockerImage docker.DockerImage) (*servingv1.Service, error) {
func loadManifest(project *project.Project, image Image) (*servingv1.Service, error) {
knativeTemplate := path.Join("assets", "knative", "service.yaml.tmpl")
template, err := template.ParseFS(project.Resources, knativeTemplate)
if err != nil {
Expand All @@ -46,7 +51,7 @@ func loadManifest(project *project.Project, dockerImage docker.DockerImage) (*se

output := &bytes.Buffer{}
// TODO replace map[string]string{} with proper values
if err = template.Execute(output, dockerImage); err != nil {
if err = template.Execute(output, image); err != nil {
return nil, err
}

Expand All @@ -71,12 +76,17 @@ func loadManifest(project *project.Project, dockerImage docker.DockerImage) (*se
return service, nil
}

func Apply(namespace string, config *rest.Config, project *project.Project, dockerImage docker.DockerImage) error {
func Apply(namespace string, config *rest.Config, project *project.Project, dockerImage docker.DockerImage, imageDigest string) error {
log.Info("Deploying Knative service", "host", config.Host, "name", project.Name, "namespace", namespace)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*5)
defer cancel()

serviceManifest, err := loadManifest(project, dockerImage)
image := Image{
DockerImage: dockerImage,
ImageDigest: imageDigest,
}

serviceManifest, err := loadManifest(project, image)
if err != nil {
return err
}
Expand Down
15 changes: 9 additions & 6 deletions src/services/k8s/knative_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,17 @@ func TestLoadManifest(t *testing.T) {
Resources: os.DirFS(root),
}

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

_, err := loadManifest(proj_knative, docker_image)
_, err := loadManifest(proj_knative, image)

if err != nil {
t.Fatalf(fmt.Sprintf("Error: %v", err))
Expand Down

0 comments on commit 08117da

Please sign in to comment.