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 22, 2024
1 parent 984448e commit 594deeb
Show file tree
Hide file tree
Showing 15 changed files with 263 additions and 103 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.

43 changes: 30 additions & 13 deletions pkg/reconciler/buildrun/resources/sources/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ package sources

import (
"fmt"
"strconv"
"strings"
"time"

core "k8s.io/api/core/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

build "github.com/shipwright-io/build/pkg/apis/build/v1alpha1"
"github.com/shipwright-io/build/pkg/config"
Expand All @@ -24,10 +27,16 @@ 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-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 +48,7 @@ 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-source-timestamp", fmt.Sprintf("$(results.%s-source-%s-source-timestamp.path)", prefixParamsResultsVolumes, name),
},
Env: cfg.BundleContainerTemplate.Env,
ComputeResources: cfg.BundleContainerTemplate.Resources,
Expand All @@ -53,7 +63,7 @@ func AppendBundleStep(
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,14 +85,21 @@ 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))
var sourceResult = build.SourceResult{Name: name}

imageDigest := findResultValue(results, fmt.Sprintf("%s-source-%s-image-digest", prefixParamsResultsVolumes, name))
if strings.TrimSpace(imageDigest) != "" {
buildRun.Status.Sources = append(buildRun.Status.Sources, build.SourceResult{
Name: name,
Bundle: &build.BundleSourceResult{
Digest: imageDigest,
},
})
sourceResult.Bundle = &build.BundleSourceResult{Digest: imageDigest}
}

sourceTimestamp := findResultValue(results, fmt.Sprintf("%s-source-%s-source-timestamp", prefixParamsResultsVolumes, name))
if strings.TrimSpace(sourceTimestamp) != "" {
if sec, err := strconv.ParseInt(sourceTimestamp, 10, 64); err == nil {
sourceResult.Timestamp = &metav1.Time{Time: time.Unix(sec, 0)}
}
}

if sourceResult.Bundle != nil || sourceResult.Timestamp != nil {
buildRun.Status.Sources = append(buildRun.Status.Sources, sourceResult)
}
}
92 changes: 55 additions & 37 deletions pkg/reconciler/buildrun/resources/sources/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,25 @@ package sources

import (
"fmt"
"strconv"
"strings"
"time"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

build "github.com/shipwright-io/build/pkg/apis/build/v1alpha1"
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 (
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 +35,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 +61,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 @@ -109,18 +118,27 @@ func AppendGitStep(

// AppendGitResult append git source result to build run
func AppendGitResult(buildRun *buildv1alpha1.BuildRun, name string, results []pipelineapi.TaskRunResult) {
var sourceResult = build.SourceResult{Name: name}

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))

if strings.TrimSpace(commitAuthor) != "" || strings.TrimSpace(commitSha) != "" || strings.TrimSpace(branchName) != "" {
buildRun.Status.Sources = append(buildRun.Status.Sources, buildv1alpha1.SourceResult{
Name: name,
Git: &buildv1alpha1.GitSourceResult{
CommitAuthor: commitAuthor,
CommitSha: commitSha,
BranchName: branchName,
},
})
sourceResult.Git = &buildv1alpha1.GitSourceResult{
CommitAuthor: commitAuthor,
CommitSha: commitSha,
BranchName: branchName,
}
}

commitTimestamp := findResultValue(results, fmt.Sprintf("%s-source-%s-%s", prefixParamsResultsVolumes, name, commitTimestampResult))
if strings.TrimSpace(commitTimestamp) != "" {
if sec, err := strconv.ParseInt(commitTimestamp, 10, 64); err == nil {
sourceResult.Timestamp = &metav1.Time{Time: time.Unix(sec, 0)}
}
}

if sourceResult.Git != nil || sourceResult.Timestamp != nil {
buildRun.Status.Sources = append(buildRun.Status.Sources, sourceResult)
}
}
Loading

0 comments on commit 594deeb

Please sign in to comment.