Skip to content

Commit

Permalink
Add timestamp fields to resource objects
Browse files Browse the repository at this point in the history
Update CRDs to include timestamp fields.

Add timestamp fields for Git and Bundle results.
  • Loading branch information
HeavyWombat committed Jan 18, 2024
1 parent e4f2965 commit 414bd83
Show file tree
Hide file tree
Showing 13 changed files with 245 additions and 88 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,9 @@ jobs:
# host.docker.internal does not work in a GitHub action
docker exec kind-control-plane bash -c "echo '172.17.0.1 host.docker.internal' >>/etc/hosts"
# Build and load the Git image
# Build and load the Git and Bundle image
export GIT_CONTAINER_IMAGE="$(KO_DOCKER_REPO=kind.local ko publish ./cmd/git)"
export BUNDLE_CONTAINER_IMAGE="$(KO_DOCKER_REPO=kind.local ko publish ./cmd/bundle)"
make test-integration
Expand Down
26 changes: 26 additions & 0 deletions deploy/crds/shipwright.io_buildruns.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6313,6 +6313,15 @@ spec:
digest:
description: Digest hold the image digest result
type: string
image_timestamp:
description: ImageTimestamp holds the (bundle) image timestamp
result
type: string
source_timestamp:
description: SourceTimestamp holds the source timestamp,
meaning the most recent timestamp of all files that are
used as source
type: string
type: object
git:
description: Git holds the results emitted from from the step
Expand All @@ -6330,6 +6339,10 @@ spec:
commitSha:
description: CommitSha holds the commit sha of git source
type: string
commitTimestamp:
description: CommitTimestamp holds the commit timestamp
of a git source
type: string
type: object
name:
description: Name is the name of source
Expand Down Expand Up @@ -12543,6 +12556,10 @@ spec:
commitSha:
description: CommitSha holds the commit sha of git source
type: string
commitTimestamp:
description: CommitTimestamp holds the commit timestamp of
a git source
type: string
type: object
ociArtifact:
description: OciArtifact holds the results emitted from the source
Expand All @@ -12551,6 +12568,15 @@ spec:
digest:
description: Digest hold the image digest result
type: string
image_timestamp:
description: ImageTimestamp holds the (bundle) image timestamp
result
type: string
source_timestamp:
description: SourceTimestamp holds the source timestamp, meaning
the most recent timestamp of all files that are used as
source
type: string
type: object
type: object
startTime:
Expand Down
10 changes: 10 additions & 0 deletions pkg/apis/build/v1alpha1/buildrun_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ type SourceResult struct {
type BundleSourceResult struct {
// Digest hold the image digest result
Digest string `json:"digest,omitempty"`

// ImageTimestamp holds the (bundle) image timestamp result
ImageTimestamp string `json:"image_timestamp,omitempty"`

// SourceTimestamp holds the source timestamp, meaning the most recent
// timestamp of all files that are used as source
SourceTimestamp string `json:"source_timestamp,omitempty"`
}

// GitSourceResult holds the results emitted from the git source
Expand All @@ -133,6 +140,9 @@ type GitSourceResult struct {
// CommitAuthor holds the commit author of a git source
CommitAuthor string `json:"commitAuthor,omitempty"`

// CommitTimestamp holds the commit timestamp of a git source
CommitTimestamp string `json:"commitTimestamp,omitempty"`

// BranchName holds the default branch name of the git source
// this will be set only when revision is not specified in Build object
BranchName string `json:"branchName,omitempty"`
Expand Down
10 changes: 10 additions & 0 deletions pkg/apis/build/v1beta1/buildrun_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,13 @@ type SourceResult struct {
type OciArtifactSourceResult struct {
// Digest hold the image digest result
Digest string `json:"digest,omitempty"`

// ImageTimestamp holds the (bundle) image timestamp result
ImageTimestamp string `json:"image_timestamp,omitempty"`

// SourceTimestamp holds the source timestamp, meaning the most recent
// timestamp of all files that are used as source
SourceTimestamp string `json:"source_timestamp,omitempty"`
}

// GitSourceResult holds the results emitted from the git source
Expand All @@ -138,6 +145,9 @@ type GitSourceResult struct {
// CommitAuthor holds the commit author of a git source
CommitAuthor string `json:"commitAuthor,omitempty"`

// CommitTimestamp holds the commit timestamp of a git source
CommitTimestamp string `json:"commitTimestamp,omitempty"`

// BranchName holds the default branch name of the git source
// this will be set only when revision is not specified in Build object
//
Expand Down
31 changes: 25 additions & 6 deletions pkg/reconciler/buildrun/resources/sources/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,20 @@ func AppendBundleStep(
name string,
) {
// append the result
taskSpec.Results = append(taskSpec.Results, pipelineapi.TaskResult{
Name: fmt.Sprintf("%s-source-%s-image-digest", prefixParamsResultsVolumes, name),
Description: "The digest of the bundle image.",
})
taskSpec.Results = append(taskSpec.Results,
pipelineapi.TaskResult{
Name: fmt.Sprintf("%s-source-%s-image-digest", prefixParamsResultsVolumes, name),
Description: "The digest of the bundle image.",
},
pipelineapi.TaskResult{
Name: fmt.Sprintf("%s-source-%s-image-timestamp", prefixParamsResultsVolumes, name),
Description: "The timestamp of the bundle image.",
},
pipelineapi.TaskResult{
Name: fmt.Sprintf("%s-source-%s-source-timestamp", prefixParamsResultsVolumes, name),
Description: "The timestamp of the most recent source file.",
},
)

// initialize the step from the template and the build-specific arguments
bundleStep := pipelineapi.Step{
Expand All @@ -39,6 +49,8 @@ func AppendBundleStep(
"--image", source.BundleContainer.Image,
"--target", fmt.Sprintf("$(params.%s-%s)", prefixParamsResultsVolumes, paramSourceRoot),
"--result-file-image-digest", fmt.Sprintf("$(results.%s-source-%s-image-digest.path)", prefixParamsResultsVolumes, name),
"--result-file-image-timestamp", fmt.Sprintf("$(results.%s-source-%s-image-timestamp.path)", prefixParamsResultsVolumes, name),
"--result-file-source-timestamp", fmt.Sprintf("$(results.%s-source-%s-source-timestamp.path)", prefixParamsResultsVolumes, name),
},
Env: cfg.BundleContainerTemplate.Env,
ComputeResources: cfg.BundleContainerTemplate.Resources,
Expand Down Expand Up @@ -76,12 +88,19 @@ func AppendBundleStep(
// AppendBundleResult append bundle source result to build run
func AppendBundleResult(buildRun *build.BuildRun, name string, results []pipelineapi.TaskRunResult) {
imageDigest := findResultValue(results, fmt.Sprintf("%s-source-%s-image-digest", prefixParamsResultsVolumes, name))
imageTimestamp := findResultValue(results, fmt.Sprintf("%s-source-%s-image-timestamp", prefixParamsResultsVolumes, name))
sourceTimestamp := findResultValue(results, fmt.Sprintf("%s-source-%s-source-timestamp", prefixParamsResultsVolumes, name))

if strings.TrimSpace(imageDigest) != "" ||
strings.TrimSpace(imageTimestamp) != "" ||
strings.TrimSpace(sourceTimestamp) != "" {

if strings.TrimSpace(imageDigest) != "" {
buildRun.Status.Sources = append(buildRun.Status.Sources, build.SourceResult{
Name: name,
Bundle: &build.BundleSourceResult{
Digest: imageDigest,
Digest: imageDigest,
ImageTimestamp: imageTimestamp,
SourceTimestamp: sourceTimestamp,
},
})
}
Expand Down
67 changes: 36 additions & 31 deletions pkg/reconciler/buildrun/resources/sources/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ import (
)

const (
commitSHAResult = "commit-sha"
commitAuthorResult = "commit-author"
branchName = "branch-name"
commitSHAResult = "commit-sha"
commitAuthorResult = "commit-author"
commitTimestampResult = "commit-timestamp"
branchName = "branch-name"
)

// AppendGitStep appends the Git step and results and volume if needed to the TaskSpec
Expand All @@ -28,16 +29,24 @@ func AppendGitStep(
name string,
) {
// append the result
taskSpec.Results = append(taskSpec.Results, pipelineapi.TaskResult{
Name: fmt.Sprintf("%s-source-%s-%s", prefixParamsResultsVolumes, name, commitSHAResult),
Description: "The commit SHA of the cloned source.",
}, pipelineapi.TaskResult{
Name: fmt.Sprintf("%s-source-%s-%s", prefixParamsResultsVolumes, name, commitAuthorResult),
Description: "The author of the last commit of the cloned source.",
}, pipelineapi.TaskResult{
Name: fmt.Sprintf("%s-source-%s-%s", prefixParamsResultsVolumes, name, branchName),
Description: "The name of the branch used of the cloned source.",
})
taskSpec.Results = append(taskSpec.Results,
pipelineapi.TaskResult{
Name: fmt.Sprintf("%s-source-%s-%s", prefixParamsResultsVolumes, name, commitSHAResult),
Description: "The commit SHA of the cloned source.",
},
pipelineapi.TaskResult{
Name: fmt.Sprintf("%s-source-%s-%s", prefixParamsResultsVolumes, name, commitAuthorResult),
Description: "The author of the last commit of the cloned source.",
},
pipelineapi.TaskResult{
Name: fmt.Sprintf("%s-source-%s-%s", prefixParamsResultsVolumes, name, commitTimestampResult),
Description: "The commit timestamp of the last commit of the cloned source.",
},
pipelineapi.TaskResult{
Name: fmt.Sprintf("%s-source-%s-%s", prefixParamsResultsVolumes, name, branchName),
Description: "The name of the branch used of the cloned source.",
},
)

// initialize the step from the template and the build-specific arguments
gitStep := pipelineapi.Step{
Expand All @@ -46,20 +55,14 @@ func AppendGitStep(
ImagePullPolicy: cfg.GitContainerTemplate.ImagePullPolicy,
Command: cfg.GitContainerTemplate.Command,
Args: []string{
"--url",
*source.URL,
"--target",
fmt.Sprintf("$(params.%s-%s)", prefixParamsResultsVolumes, paramSourceRoot),
"--result-file-commit-sha",
fmt.Sprintf("$(results.%s-source-%s-%s.path)", prefixParamsResultsVolumes, name, commitSHAResult),
"--result-file-commit-author",
fmt.Sprintf("$(results.%s-source-%s-%s.path)", prefixParamsResultsVolumes, name, commitAuthorResult),
"--result-file-branch-name",
fmt.Sprintf("$(results.%s-source-%s-%s.path)", prefixParamsResultsVolumes, name, branchName),
"--result-file-error-message",
fmt.Sprintf("$(results.%s-error-message.path)", prefixParamsResultsVolumes),
"--result-file-error-reason",
fmt.Sprintf("$(results.%s-error-reason.path)", prefixParamsResultsVolumes),
"--url", *source.URL,
"--target", fmt.Sprintf("$(params.%s-%s)", prefixParamsResultsVolumes, paramSourceRoot),
"--result-file-commit-sha", fmt.Sprintf("$(results.%s-source-%s-%s.path)", prefixParamsResultsVolumes, name, commitSHAResult),
"--result-file-commit-author", fmt.Sprintf("$(results.%s-source-%s-%s.path)", prefixParamsResultsVolumes, name, commitAuthorResult),
"--result-file-commit-timestamp", fmt.Sprintf("$(results.%s-source-%s-%s.path)", prefixParamsResultsVolumes, name, commitTimestampResult),
"--result-file-branch-name", fmt.Sprintf("$(results.%s-source-%s-%s.path)", prefixParamsResultsVolumes, name, branchName),
"--result-file-error-message", fmt.Sprintf("$(results.%s-error-message.path)", prefixParamsResultsVolumes),
"--result-file-error-reason", fmt.Sprintf("$(results.%s-error-reason.path)", prefixParamsResultsVolumes),
},
Env: cfg.GitContainerTemplate.Env,
ComputeResources: cfg.GitContainerTemplate.Resources,
Expand Down Expand Up @@ -111,15 +114,17 @@ func AppendGitStep(
func AppendGitResult(buildRun *buildv1alpha1.BuildRun, name string, results []pipelineapi.TaskRunResult) {
commitAuthor := findResultValue(results, fmt.Sprintf("%s-source-%s-%s", prefixParamsResultsVolumes, name, commitAuthorResult))
commitSha := findResultValue(results, fmt.Sprintf("%s-source-%s-%s", prefixParamsResultsVolumes, name, commitSHAResult))
commitTimestamp := findResultValue(results, fmt.Sprintf("%s-source-%s-%s", prefixParamsResultsVolumes, name, commitTimestampResult))
branchName := findResultValue(results, fmt.Sprintf("%s-source-%s-%s", prefixParamsResultsVolumes, name, branchName))

if strings.TrimSpace(commitAuthor) != "" || strings.TrimSpace(commitSha) != "" || strings.TrimSpace(branchName) != "" {
if strings.TrimSpace(commitAuthor) != "" || strings.TrimSpace(commitSha) != "" || strings.TrimSpace(branchName) != "" || strings.TrimSpace(commitTimestamp) != "" {
buildRun.Status.Sources = append(buildRun.Status.Sources, buildv1alpha1.SourceResult{
Name: name,
Git: &buildv1alpha1.GitSourceResult{
CommitAuthor: commitAuthor,
CommitSha: commitSha,
BranchName: branchName,
CommitAuthor: commitAuthor,
CommitSha: commitSha,
CommitTimestamp: commitTimestamp,
BranchName: branchName,
},
})
}
Expand Down
57 changes: 23 additions & 34 deletions pkg/reconciler/buildrun/resources/sources/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,31 +36,26 @@ var _ = Describe("Git", func() {
})

It("adds results for the commit sha, commit author and branch name", func() {
Expect(len(taskSpec.Results)).To(Equal(3))
Expect(len(taskSpec.Results)).To(Equal(4))
Expect(taskSpec.Results[0].Name).To(Equal("shp-source-default-commit-sha"))
Expect(taskSpec.Results[1].Name).To(Equal("shp-source-default-commit-author"))
Expect(taskSpec.Results[2].Name).To(Equal("shp-source-default-branch-name"))
Expect(taskSpec.Results[2].Name).To(Equal("shp-source-default-commit-timestamp"))
Expect(taskSpec.Results[3].Name).To(Equal("shp-source-default-branch-name"))
})

It("adds a step", func() {
Expect(len(taskSpec.Steps)).To(Equal(1))
Expect(taskSpec.Steps[0].Name).To(Equal("source-default"))
Expect(taskSpec.Steps[0].Image).To(Equal(cfg.GitContainerTemplate.Image))
Expect(taskSpec.Steps[0].Args).To(Equal([]string{
"--url",
"https://github.com/shipwright-io/build",
"--target",
"$(params.shp-source-root)",
"--result-file-commit-sha",
"$(results.shp-source-default-commit-sha.path)",
"--result-file-commit-author",
"$(results.shp-source-default-commit-author.path)",
"--result-file-branch-name",
"$(results.shp-source-default-branch-name.path)",
"--result-file-error-message",
"$(results.shp-error-message.path)",
"--result-file-error-reason",
"$(results.shp-error-reason.path)",
"--url", "https://github.com/shipwright-io/build",
"--target", "$(params.shp-source-root)",
"--result-file-commit-sha", "$(results.shp-source-default-commit-sha.path)",
"--result-file-commit-author", "$(results.shp-source-default-commit-author.path)",
"--result-file-commit-timestamp", "$(results.shp-source-default-commit-timestamp.path)",
"--result-file-branch-name", "$(results.shp-source-default-branch-name.path)",
"--result-file-error-message", "$(results.shp-error-message.path)",
"--result-file-error-reason", "$(results.shp-error-reason.path)",
}))
})
})
Expand All @@ -83,10 +78,11 @@ var _ = Describe("Git", func() {
})

It("adds results for the commit sha, commit author and branch name", func() {
Expect(len(taskSpec.Results)).To(Equal(3))
Expect(len(taskSpec.Results)).To(Equal(4))
Expect(taskSpec.Results[0].Name).To(Equal("shp-source-default-commit-sha"))
Expect(taskSpec.Results[1].Name).To(Equal("shp-source-default-commit-author"))
Expect(taskSpec.Results[2].Name).To(Equal("shp-source-default-branch-name"))
Expect(taskSpec.Results[2].Name).To(Equal("shp-source-default-commit-timestamp"))
Expect(taskSpec.Results[3].Name).To(Equal("shp-source-default-branch-name"))
})

It("adds a volume for the secret", func() {
Expand All @@ -101,22 +97,15 @@ var _ = Describe("Git", func() {
Expect(taskSpec.Steps[0].Name).To(Equal("source-default"))
Expect(taskSpec.Steps[0].Image).To(Equal(cfg.GitContainerTemplate.Image))
Expect(taskSpec.Steps[0].Args).To(Equal([]string{
"--url",
"git@github.com:shipwright-io/build.git",
"--target",
"$(params.shp-source-root)",
"--result-file-commit-sha",
"$(results.shp-source-default-commit-sha.path)",
"--result-file-commit-author",
"$(results.shp-source-default-commit-author.path)",
"--result-file-branch-name",
"$(results.shp-source-default-branch-name.path)",
"--result-file-error-message",
"$(results.shp-error-message.path)",
"--result-file-error-reason",
"$(results.shp-error-reason.path)",
"--secret-path",
"/workspace/shp-source-secret",
"--url", "git@github.com:shipwright-io/build.git",
"--target", "$(params.shp-source-root)",
"--result-file-commit-sha", "$(results.shp-source-default-commit-sha.path)",
"--result-file-commit-author", "$(results.shp-source-default-commit-author.path)",
"--result-file-commit-timestamp", "$(results.shp-source-default-commit-timestamp.path)",
"--result-file-branch-name", "$(results.shp-source-default-branch-name.path)",
"--result-file-error-message", "$(results.shp-error-message.path)",
"--result-file-error-reason", "$(results.shp-error-reason.path)",
"--secret-path", "/workspace/shp-source-secret",
}))
Expect(len(taskSpec.Steps[0].VolumeMounts)).To(Equal(1))
Expect(taskSpec.Steps[0].VolumeMounts[0].Name).To(Equal("shp-a-secret"))
Expand Down
22 changes: 8 additions & 14 deletions pkg/reconciler/buildrun/resources/taskrun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,14 @@ var _ = Describe("GenerateTaskrun", func() {
Expect(got.Steps[0].Name).To(Equal("source-default"))
Expect(got.Steps[0].Command[0]).To(Equal("/ko-app/git"))
Expect(got.Steps[0].Args).To(Equal([]string{
"--url",
*build.Spec.Source.URL,
"--target",
"$(params.shp-source-root)",
"--result-file-commit-sha",
"$(results.shp-source-default-commit-sha.path)",
"--result-file-commit-author",
"$(results.shp-source-default-commit-author.path)",
"--result-file-branch-name",
"$(results.shp-source-default-branch-name.path)",
"--result-file-error-message",
"$(results.shp-error-message.path)",
"--result-file-error-reason",
"$(results.shp-error-reason.path)",
"--url", *build.Spec.Source.URL,
"--target", "$(params.shp-source-root)",
"--result-file-commit-sha", "$(results.shp-source-default-commit-sha.path)",
"--result-file-commit-author", "$(results.shp-source-default-commit-author.path)",
"--result-file-commit-timestamp", "$(results.shp-source-default-commit-timestamp.path)",
"--result-file-branch-name", "$(results.shp-source-default-branch-name.path)",
"--result-file-error-message", "$(results.shp-error-message.path)",
"--result-file-error-reason", "$(results.shp-error-reason.path)",
}))
})

Expand Down
1 change: 1 addition & 0 deletions test/integration/build_to_buildruns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ var _ = Describe("Integration tests Build and BuildRuns", func() {
Expect(err).To(BeNil())

})

// Delete the ClusterBuildStrategies after each test case
AfterEach(func() {

Expand Down
Loading

0 comments on commit 414bd83

Please sign in to comment.