Skip to content

Commit

Permalink
feat: check pushed Dockerfile
Browse files Browse the repository at this point in the history
STONEBLD-2522

Signed-off-by: Chenxiong Qi <[email protected]>
  • Loading branch information
tkdchen committed Aug 8, 2024
1 parent 0612222 commit 1cd9396
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 1 deletion.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ require (
k8s.io/utils v0.0.0-20240102154912-e7106e64919e
knative.dev/pkg v0.0.0-20240219120257-9227ebb57a4e
sigs.k8s.io/controller-runtime v0.17.5
oras.land/oras-go/v2 v2.3.0
sigs.k8s.io/yaml v1.4.0
)

Expand Down Expand Up @@ -303,7 +304,6 @@ require (
k8s.io/kube-openapi v0.0.0-20240221221325-2ac9dc51f3f1 // indirect
k8s.io/kubectl v0.29.2 // indirect
k8s.io/kubernetes v1.29.2 // indirect
oras.land/oras-go/v2 v2.3.0 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/kustomize/api v0.13.5-0.20230601165947-6ce0bf390ce3 // indirect
sigs.k8s.io/kustomize/kyaml v0.14.3-0.20230601165947-6ce0bf390ce3 // indirect
Expand Down
52 changes: 52 additions & 0 deletions pkg/clients/oras/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package oras

import (
"context"
"os"

"github.com/openshift/library-go/pkg/image/reference"
oras "oras.land/oras-go/v2"
"oras.land/oras-go/v2/content/file"
"oras.land/oras-go/v2/registry/remote"
"oras.land/oras-go/v2/registry/remote/auth"
"oras.land/oras-go/v2/registry/remote/retry"
)

// PullArtifacts pulls artifacts from the given imagePullSpec.
// Pulled artifacts will be stored in a local directory, whose path is returned.
func PullArtifacts(imagePullSpec string) (string, error) {
storePath, err := os.MkdirTemp("", "pulled-artifacts")
if err != nil {
return "", err
}
fs, err := file.New(storePath)
if err != nil {
return "", err
}
defer fs.Close()

imageRef, err := reference.Parse(imagePullSpec)
if err != nil {
return "", err
}

repo, err := remote.NewRepository(imagePullSpec)
if err != nil {
return "", err
}
repo.Client = &auth.Client{
Client: retry.DefaultClient,
Cache: auth.NewCache(),
Credential: auth.StaticCredential(imageRef.Registry, auth.Credential{
AccessToken: os.Getenv("QUAY_TOKEN"),
}),
}

ctx := context.Background()
tag := imageRef.Tag
if _, err := oras.Copy(ctx, repo, tag, fs, tag, oras.DefaultCopyOptions); err != nil {
return "", err
}

return storePath, nil
}
49 changes: 49 additions & 0 deletions tests/build/build_templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"time"

Expand All @@ -15,6 +16,7 @@ import (
"github.com/konflux-ci/e2e-tests/pkg/clients/common"
"github.com/konflux-ci/e2e-tests/pkg/clients/has"
kubeapi "github.com/konflux-ci/e2e-tests/pkg/clients/kubernetes"
"github.com/konflux-ci/e2e-tests/pkg/clients/oras"
"github.com/konflux-ci/e2e-tests/pkg/constants"
"github.com/konflux-ci/e2e-tests/pkg/framework"
"github.com/konflux-ci/e2e-tests/pkg/utils"
Expand Down Expand Up @@ -687,6 +689,10 @@ var _ = framework.BuildSuiteDescribe("Build templates E2E test", Label("build",
})
}
})

It("should push Dockerfile to registry", Label(buildTemplatesTestLabel), func() {
ensureOriginalDockerfileIsPushed(kubeadminClient, pr)
})
}

It(fmt.Sprintf("pipelineRun should fail for symlink component with Git source URL %s", pythonComponentGitSourceURL), Label(buildTemplatesTestLabel, sourceBuildTestLabel), func() {
Expand Down Expand Up @@ -797,3 +803,46 @@ func applyAdditionalTagsInPipelineBundle(customDockerBuildBundle string, additio
}
return newDockerBuildPipeline.String(), nil
}

func ensureOriginalDockerfileIsPushed(hub *framework.ControllerHub, pr *tektonpipeline.PipelineRun) {
binaryImage := build.GetBinaryImage(pr)
Expect(binaryImage).ShouldNot(BeEmpty())

binaryImageRef, err := reference.Parse(binaryImage)
Expect(err).Should(Succeed())

tagInfo, err := build.GetImageTag(binaryImageRef.Namespace, binaryImageRef.Name, binaryImageRef.Tag)
Expect(err).Should(Succeed())

dockerfileImageTag := fmt.Sprintf("%s.dockerfile", strings.Replace(tagInfo.ManifestDigest, ":", "-", 1))

dockerfileImage := reference.DockerImageReference{
Registry: binaryImageRef.Registry,
Namespace: binaryImageRef.Namespace,
Name: binaryImageRef.Name,
Tag: dockerfileImageTag,
}.String()
exists, err := build.DoesTagExistsInQuay(dockerfileImage)
Expect(err).Should(Succeed())
Expect(exists).Should(BeTrue())

// Ensure the original Dockerfile used for build was pushed
c := hub.CommonController.KubeRest()
originDockerfileContent, err := build.ReadDockerfileUsedForBuild(c, hub.TektonController, pr)
Expect(err).Should(Succeed())

storePath, err := oras.PullArtifacts(dockerfileImage)
Expect(err).Should(Succeed())
entries, err := os.ReadDir(storePath)
Expect(err).Should(Succeed())
for _, entry := range entries {
if entry.Type().IsRegular() && entry.Name() == "Dockerfile" {
content, err := os.ReadFile(filepath.Join(storePath, entry.Name()))
Expect(err).Should(Succeed())
Expect(content).Should(Equal(originDockerfileContent))
return
}
}

Fail(fmt.Sprintf("Dockerfile is not found from the pulled artifacts for %s", dockerfileImage))
}
7 changes: 7 additions & 0 deletions tests/build/multi-platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,13 @@ var _ = framework.MultiPlatformBuildSuiteDescribe("Multi Platform Controller E2E
}, timeout, interval).Should(Succeed(), "timed out when verifying that the remote host was cleaned up correctly")
})

It("should push Dockerfile to registry", func() {
pr, err := f.AsKubeAdmin.HasController.GetComponentPipelineRun(
component.GetName(), component.Spec.Application, component.GetNamespace(), "")
Expect(err).Should(Succeed())
ensureOriginalDockerfileIsPushed(f.AsKubeAdmin, pr)
})

})
})
})
Expand Down

0 comments on commit 1cd9396

Please sign in to comment.