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 24, 2024
1 parent 847ea27 commit 8492b27
Show file tree
Hide file tree
Showing 18 changed files with 258 additions and 98 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
name: Unit, Integration, and E2E Tests
on:
on:
pull_request:
branches:
- main
push:
paths-ignore:
- 'README.md'
- 'docs/**'
branches:
branches:
- main

jobs:
Expand Down 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
14 changes: 14 additions & 0 deletions deploy/crds/shipwright.io_buildruns.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6334,6 +6334,13 @@ spec:
name:
description: Name is the name of source
type: string
timestamp:
description: Timestamp holds the timestamp of the source, which
depends on the actual source type and could range from being
the commit timestamp or the fileystem timestamp of the most
recent source file in the working directory
format: date-time
type: string
required:
- name
type: object
Expand Down Expand Up @@ -12552,6 +12559,13 @@ spec:
description: Digest hold the image digest result
type: string
type: object
timestamp:
description: Timestamp holds the timestamp of the source, which
depends on the actual source type and could range from being
the commit timestamp or the fileystem timestamp of the most
recent source file in the working directory
format: date-time
type: string
type: object
startTime:
description: StartTime is the time the build is actually started.
Expand Down
8 changes: 8 additions & 0 deletions pkg/apis/build/v1alpha1/buildrun_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ type SourceResult struct {
//
// +optional
Bundle *BundleSourceResult `json:"bundle,omitempty"`

// Timestamp holds the timestamp of the source, which
// depends on the actual source type and could range from
// being the commit timestamp or the fileystem timestamp
// of the most recent source file in the working directory
//
// +optional
Timestamp *metav1.Time `json:"timestamp,omitempty"`
}

// BundleSourceResult holds the results emitted from the bundle source
Expand Down
4 changes: 4 additions & 0 deletions pkg/apis/build/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions pkg/apis/build/v1beta1/buildrun_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@ type SourceResult struct {
//
// +optional
OciArtifact *OciArtifactSourceResult `json:"ociArtifact,omitempty"`

// Timestamp holds the timestamp of the source, which
// depends on the actual source type and could range from
// being the commit timestamp or the fileystem timestamp
// of the most recent source file in the working directory
//
// +optional
Timestamp *metav1.Time `json:"timestamp,omitempty"`
}

// OciArtifactSourceResult holds the results emitted from the bundle source
Expand Down
4 changes: 4 additions & 0 deletions pkg/apis/build/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 28 additions & 1 deletion pkg/reconciler/buildrun/resources/sources.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
package resources

import (
"strconv"
"time"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

buildv1alpha1 "github.com/shipwright-io/build/pkg/apis/build/v1alpha1"
"github.com/shipwright-io/build/pkg/config"
"github.com/shipwright-io/build/pkg/reconciler/buildrun/resources/sources"
Expand All @@ -14,6 +19,8 @@ import (

const defaultSourceName = "default"

const sourceTimestampName = "source-timestamp"

// isLocalCopyBuildSource appends all "Sources" in a single slice, and if any entry is typed
// "LocalCopy" it returns first LocalCopy typed BuildSource found, or nil.
func isLocalCopyBuildSource(
Expand All @@ -33,6 +40,15 @@ func isLocalCopyBuildSource(
return nil
}

func appendSourceTimestampResult(taskSpec *pipelineapi.TaskSpec) {
taskSpec.Results = append(taskSpec.Results,
pipelineapi.TaskResult{
Name: sources.TaskResultName(defaultSourceName, sourceTimestampName),
Description: "The timestamp of the source.",
},
)
}

// AmendTaskSpecWithSources adds the necessary steps to either wait for user upload ("LocalCopy"), or
// alternatively, configures the Task steps to use bundle and "git clone".
func AmendTaskSpecWithSources(
Expand All @@ -47,8 +63,10 @@ func AmendTaskSpecWithSources(
// create the step for spec.source, either Git or Bundle
switch {
case build.Spec.Source.BundleContainer != nil:
appendSourceTimestampResult(taskSpec)
sources.AppendBundleStep(cfg, taskSpec, build.Spec.Source, defaultSourceName)
case build.Spec.Source.URL != nil:
appendSourceTimestampResult(taskSpec)
sources.AppendGitStep(cfg, taskSpec, build.Spec.Source, defaultSourceName)
}
}
Expand All @@ -65,6 +83,7 @@ func AmendTaskSpecWithSources(
func updateBuildRunStatusWithSourceResult(buildrun *buildv1alpha1.BuildRun, results []pipelineapi.TaskRunResult) {
buildSpec := buildrun.Status.BuildSpec

// no results for HTTP sources yet
switch {
case buildSpec.Source.BundleContainer != nil:
sources.AppendBundleResult(buildrun, defaultSourceName, results)
Expand All @@ -73,5 +92,13 @@ func updateBuildRunStatusWithSourceResult(buildrun *buildv1alpha1.BuildRun, resu
sources.AppendGitResult(buildrun, defaultSourceName, results)
}

// no results for HTTP sources yet
if sourceTimestamp := sources.FindResultValue(results, defaultSourceName, sourceTimestampName); sourceTimestamp != "" {
if sec, err := strconv.ParseInt(sourceTimestamp, 10, 64); err == nil {
for i := range buildrun.Status.Sources {
if buildrun.Status.Sources[i].Name == defaultSourceName {
buildrun.Status.Sources[i].Timestamp = &metav1.Time{Time: time.Unix(sec, 0)}
}
}
}
}
}
26 changes: 14 additions & 12 deletions pkg/reconciler/buildrun/resources/sources/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ package sources

import (
"fmt"
"strings"

core "k8s.io/api/core/v1"
corev1 "k8s.io/api/core/v1"

build "github.com/shipwright-io/build/pkg/apis/build/v1alpha1"
"github.com/shipwright-io/build/pkg/config"
Expand All @@ -24,10 +23,12 @@ 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.",
},
)

// initialize the step from the template and the build-specific arguments
bundleStep := pipelineapi.Step{
Expand All @@ -37,8 +38,9 @@ func AppendBundleStep(
Command: cfg.BundleContainerTemplate.Command,
Args: []string{
"--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),
"--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-source-timestamp", fmt.Sprintf("$(results.%s-source-%s-source-timestamp.path)", PrefixParamsResultsVolumes, name),
},
Env: cfg.BundleContainerTemplate.Env,
ComputeResources: cfg.BundleContainerTemplate.Resources,
Expand All @@ -50,10 +52,10 @@ func AppendBundleStep(
if source.Credentials != nil {
AppendSecretVolume(taskSpec, source.Credentials.Name)

secretMountPath := fmt.Sprintf("/workspace/%s-pull-secret", prefixParamsResultsVolumes)
secretMountPath := fmt.Sprintf("/workspace/%s-pull-secret", PrefixParamsResultsVolumes)

// define the volume mount on the container
bundleStep.VolumeMounts = append(bundleStep.VolumeMounts, core.VolumeMount{
bundleStep.VolumeMounts = append(bundleStep.VolumeMounts, corev1.VolumeMount{
Name: SanitizeVolumeNameForSecretName(source.Credentials.Name),
MountPath: secretMountPath,
ReadOnly: true,
Expand All @@ -75,9 +77,9 @@ 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))
imageDigest := FindResultValue(results, name, "image-digest")

if strings.TrimSpace(imageDigest) != "" {
if imageDigest != "" {
buildRun.Status.Sources = append(buildRun.Status.Sources, build.SourceResult{
Name: name,
Bundle: &build.BundleSourceResult{
Expand Down
61 changes: 30 additions & 31 deletions pkg/reconciler/buildrun/resources/sources/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ package sources

import (
"fmt"
"strings"

corev1 "k8s.io/api/core/v1"

buildv1alpha1 "github.com/shipwright-io/build/pkg/apis/build/v1alpha1"
"github.com/shipwright-io/build/pkg/config"

pipelineapi "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
corev1 "k8s.io/api/core/v1"
)

const (
Expand All @@ -28,16 +29,20 @@ 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, 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 +51,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-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),
"--result-file-source-timestamp", fmt.Sprintf("$(results.%s-source-%s-source-timestamp.path)", PrefixParamsResultsVolumes, name),
},
Env: cfg.GitContainerTemplate.Env,
ComputeResources: cfg.GitContainerTemplate.Resources,
Expand All @@ -86,7 +85,7 @@ func AppendGitStep(
// ensure the value is there
AppendSecretVolume(taskSpec, source.Credentials.Name)

secretMountPath := fmt.Sprintf("/workspace/%s-source-secret", prefixParamsResultsVolumes)
secretMountPath := fmt.Sprintf("/workspace/%s-source-secret", PrefixParamsResultsVolumes)

// define the volume mount on the container
gitStep.VolumeMounts = append(gitStep.VolumeMounts, corev1.VolumeMount{
Expand All @@ -109,11 +108,11 @@ func AppendGitStep(

// AppendGitResult append git source result to build run
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))
branchName := findResultValue(results, fmt.Sprintf("%s-source-%s-%s", prefixParamsResultsVolumes, name, branchName))
commitAuthor := FindResultValue(results, name, commitAuthorResult)
commitSha := FindResultValue(results, name, commitSHAResult)
branchName := FindResultValue(results, name, branchName)

if strings.TrimSpace(commitAuthor) != "" || strings.TrimSpace(commitSha) != "" || strings.TrimSpace(branchName) != "" {
if commitAuthor != "" || commitSha != "" || branchName != "" {
buildRun.Status.Sources = append(buildRun.Status.Sources, buildv1alpha1.SourceResult{
Name: name,
Git: &buildv1alpha1.GitSourceResult{
Expand Down
Loading

0 comments on commit 8492b27

Please sign in to comment.