Skip to content

Commit

Permalink
Use Tekton V1 API when working with TaskRuns
Browse files Browse the repository at this point in the history
  • Loading branch information
SaschaSchwarze0 committed Nov 20, 2023
1 parent 40a23bc commit 5bb47a6
Show file tree
Hide file tree
Showing 41 changed files with 500 additions and 465 deletions.
37 changes: 24 additions & 13 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"time"

"github.com/prometheus/client_golang/prometheus"
pipeline "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
corev1 "k8s.io/api/core/v1"
"k8s.io/utils/pointer"
)
Expand Down Expand Up @@ -93,10 +92,10 @@ var (
// can be set to use on the Build controllers
type Config struct {
CtxTimeOut time.Duration
GitContainerTemplate pipeline.Step
ImageProcessingContainerTemplate pipeline.Step
BundleContainerTemplate pipeline.Step
WaiterContainerTemplate pipeline.Step
GitContainerTemplate Step
ImageProcessingContainerTemplate Step
BundleContainerTemplate Step
WaiterContainerTemplate Step
RemoteArtifactsContainerImage string
TerminationLogPath string
Prometheus PrometheusConfig
Expand Down Expand Up @@ -141,6 +140,17 @@ type KubeAPIOptions struct {
Burst int
}

type Step struct {
Args []string `json:"args,omitempty"`
Command []string `json:"command,omitempty"`
Env []corev1.EnvVar `json:"env,omitempty"`
Image string `json:"image,omitempty"`
ImagePullPolicy corev1.PullPolicy `json:"imagePullPolicy,omitempty"`
Resources corev1.ResourceRequirements `json:"resources,omitempty"`
SecurityContext *corev1.SecurityContext `json:"securityContext,omitempty"`
WorkingDir string `json:"workingDir,omitempty"`
}

// NewDefaultConfig returns a new Config, with context timeout and default Kaniko image.
func NewDefaultConfig() *Config {
return &Config{
Expand All @@ -149,7 +159,7 @@ func NewDefaultConfig() *Config {
TerminationLogPath: terminationLogPathDefault,
GitRewriteRule: false,

GitContainerTemplate: pipeline.Step{
GitContainerTemplate: Step{
Image: gitDefaultImage,
Command: []string{
"/ko-app/git",
Expand All @@ -173,7 +183,7 @@ func NewDefaultConfig() *Config {
},
},

BundleContainerTemplate: pipeline.Step{
BundleContainerTemplate: Step{
Image: bundleDefaultImage,
Command: []string{
"/ko-app/bundle",
Expand All @@ -196,7 +206,8 @@ func NewDefaultConfig() *Config {
RunAsGroup: nonRoot,
},
},
ImageProcessingContainerTemplate: pipeline.Step{

ImageProcessingContainerTemplate: Step{
Image: imageProcessingDefaultImage,
Command: []string{
"/ko-app/image-processing",
Expand Down Expand Up @@ -228,7 +239,7 @@ func NewDefaultConfig() *Config {
},
},

WaiterContainerTemplate: pipeline.Step{
WaiterContainerTemplate: Step{
Image: waiterDefaultImage,
Command: []string{
"/ko-app/waiter",
Expand Down Expand Up @@ -298,7 +309,7 @@ func (c *Config) SetConfigFromEnv() error {
}

if gitContainerTemplate := os.Getenv(gitContainerTemplateEnvVar); gitContainerTemplate != "" {
c.GitContainerTemplate = pipeline.Step{}
c.GitContainerTemplate = Step{}
if err := json.Unmarshal([]byte(gitContainerTemplate), &c.GitContainerTemplate); err != nil {
return err
}
Expand All @@ -313,7 +324,7 @@ func (c *Config) SetConfigFromEnv() error {
}

if imageProcessingContainerTemplate := os.Getenv(imageProcessingContainerTemplateEnvVar); imageProcessingContainerTemplate != "" {
c.ImageProcessingContainerTemplate = pipeline.Step{}
c.ImageProcessingContainerTemplate = Step{}
if err := json.Unmarshal([]byte(imageProcessingContainerTemplate), &c.ImageProcessingContainerTemplate); err != nil {
return err
}
Expand All @@ -334,7 +345,7 @@ func (c *Config) SetConfigFromEnv() error {
}

if bundleContainerTemplate := os.Getenv(bundleContainerTemplateEnvVar); bundleContainerTemplate != "" {
c.BundleContainerTemplate = pipeline.Step{}
c.BundleContainerTemplate = Step{}
if err := json.Unmarshal([]byte(bundleContainerTemplate), &c.BundleContainerTemplate); err != nil {
return err
}
Expand All @@ -349,7 +360,7 @@ func (c *Config) SetConfigFromEnv() error {
}

if waiterContainerTemplate := os.Getenv(waiterContainerTemplateEnvVar); waiterContainerTemplate != "" {
c.WaiterContainerTemplate = pipeline.Step{}
c.WaiterContainerTemplate = Step{}
if err := json.Unmarshal([]byte(waiterContainerTemplate), &c.WaiterContainerTemplate); err != nil {
return err
}
Expand Down
17 changes: 8 additions & 9 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
pipeline "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/utils/pointer"
Expand Down Expand Up @@ -108,7 +107,7 @@ var _ = Describe("Config", func() {
}

configWithEnvVariableOverrides(overrides, func(config *Config) {
Expect(config.GitContainerTemplate).To(Equal(pipeline.Step{
Expect(config.GitContainerTemplate).To(Equal(Step{
Image: "myregistry/custom/git-image",
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{
Expand All @@ -127,7 +126,7 @@ var _ = Describe("Config", func() {

configWithEnvVariableOverrides(overrides, func(config *Config) {
nonRoot := pointer.Int64(1000)
Expect(config.GitContainerTemplate).To(Equal(pipeline.Step{
Expect(config.GitContainerTemplate).To(Equal(Step{
Image: "myregistry/custom/git-image",
Command: []string{
"/ko-app/git",
Expand All @@ -154,7 +153,7 @@ var _ = Describe("Config", func() {
}

configWithEnvVariableOverrides(overrides, func(config *Config) {
Expect(config.GitContainerTemplate).To(Equal(pipeline.Step{
Expect(config.GitContainerTemplate).To(Equal(Step{
Image: "myregistry/custom/git-image:override",
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{
Expand All @@ -172,7 +171,7 @@ var _ = Describe("Config", func() {
}

configWithEnvVariableOverrides(overrides, func(config *Config) {
Expect(config.ImageProcessingContainerTemplate).To(Equal(pipeline.Step{
Expect(config.ImageProcessingContainerTemplate).To(Equal(Step{
Image: "myregistry/custom/image-processing",
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{
Expand All @@ -190,7 +189,7 @@ var _ = Describe("Config", func() {
}

configWithEnvVariableOverrides(overrides, func(config *Config) {
Expect(config.WaiterContainerTemplate).To(Equal(pipeline.Step{
Expect(config.WaiterContainerTemplate).To(Equal(Step{
Image: "myregistry/custom/image",
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{
Expand All @@ -209,7 +208,7 @@ var _ = Describe("Config", func() {
}

configWithEnvVariableOverrides(overrides, func(config *Config) {
Expect(config.ImageProcessingContainerTemplate).To(Equal(pipeline.Step{
Expect(config.ImageProcessingContainerTemplate).To(Equal(Step{
Image: "myregistry/custom/image-processing:override",
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{
Expand All @@ -229,7 +228,7 @@ var _ = Describe("Config", func() {

configWithEnvVariableOverrides(overrides, func(config *Config) {
nonRoot := pointer.Int64(1000)
Expect(config.WaiterContainerTemplate).To(Equal(pipeline.Step{
Expect(config.WaiterContainerTemplate).To(Equal(Step{
Image: "myregistry/custom/image",
Command: []string{"/ko-app/waiter"},
Args: []string{"start"},
Expand All @@ -255,7 +254,7 @@ var _ = Describe("Config", func() {
}

configWithEnvVariableOverrides(overrides, func(config *Config) {
Expect(config.WaiterContainerTemplate).To(Equal(pipeline.Step{
Expect(config.WaiterContainerTemplate).To(Equal(Step{
Image: "myregistry/custom/image:override",
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package controller
import (
"context"

pipelinev1beta1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
pipelineapi "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"

"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/manager"
Expand Down Expand Up @@ -39,7 +39,7 @@ func NewManager(ctx context.Context, config *config.Config, cfg *rest.Config, op

ctxlog.Info(ctx, "Registering Components.")

if err := pipelinev1beta1.AddToScheme(mgr.GetScheme()); err != nil {
if err := pipelineapi.AddToScheme(mgr.GetScheme()); err != nil {
return nil, err
}

Expand Down
16 changes: 8 additions & 8 deletions pkg/reconciler/buildrun/buildrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"strconv"
"strings"

"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
pipelineapi "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
"knative.dev/pkg/apis"

corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -81,7 +81,7 @@ func (r *ReconcileBuildRun) Reconcile(ctx context.Context, request reconcile.Req
// so we can no longer assume that a build run event will not come in after the build run has a task run ref in its status
buildRun = &buildv1alpha1.BuildRun{}
getBuildRunErr := r.GetBuildRunObject(ctx, request.Name, request.Namespace, buildRun)
lastTaskRun := &v1beta1.TaskRun{}
lastTaskRun := &pipelineapi.TaskRun{}
getTaskRunErr := r.client.Get(ctx, types.NamespacedName{Name: request.Name, Namespace: request.Namespace}, lastTaskRun)

if getBuildRunErr != nil && getTaskRunErr != nil {
Expand Down Expand Up @@ -372,7 +372,7 @@ func (r *ReconcileBuildRun) Reconcile(ctx context.Context, request reconcile.Req
ctxlog.Info(ctx, "buildRun marked for cancellation, patching task run", namespace, request.Namespace, name, request.Name)
// patch tekton taskrun a la tkn to start tekton's cancelling logic
trueParam := true
if err := r.patchTaskRun(ctx, lastTaskRun, "replace", "/spec/status", v1beta1.TaskRunSpecStatusCancelled, metav1.PatchOptions{Force: &trueParam}); err != nil {
if err := r.patchTaskRun(ctx, lastTaskRun, "replace", "/spec/status", pipelineapi.TaskRunSpecStatusCancelled, metav1.PatchOptions{Force: &trueParam}); err != nil {
return reconcile.Result{}, err
}
}
Expand All @@ -386,9 +386,9 @@ func (r *ReconcileBuildRun) Reconcile(ctx context.Context, request reconcile.Req
return reconcile.Result{}, nil
}

if len(lastTaskRun.Status.TaskRunResults) > 0 {
if len(lastTaskRun.Status.Results) > 0 {
ctxlog.Info(ctx, "surfacing taskRun results to BuildRun status", namespace, request.Namespace, name, request.Name)
resources.UpdateBuildRunUsingTaskResults(ctx, buildRun, lastTaskRun.Status.TaskRunResults, request)
resources.UpdateBuildRunUsingTaskResults(ctx, buildRun, lastTaskRun.Status.Results, request)
}

trCondition := lastTaskRun.Status.GetCondition(apis.ConditionSucceeded)
Expand Down Expand Up @@ -550,9 +550,9 @@ func (r *ReconcileBuildRun) getReferencedStrategy(ctx context.Context, build *bu
return strategy, err
}

func (r *ReconcileBuildRun) createTaskRun(ctx context.Context, serviceAccount *corev1.ServiceAccount, strategy buildv1alpha1.BuilderStrategy, build *buildv1alpha1.Build, buildRun *buildv1alpha1.BuildRun) (*v1beta1.TaskRun, error) {
func (r *ReconcileBuildRun) createTaskRun(ctx context.Context, serviceAccount *corev1.ServiceAccount, strategy buildv1alpha1.BuilderStrategy, build *buildv1alpha1.Build, buildRun *buildv1alpha1.BuildRun) (*pipelineapi.TaskRun, error) {
var (
generatedTaskRun *v1beta1.TaskRun
generatedTaskRun *pipelineapi.TaskRun
)

generatedTaskRun, err := resources.GenerateTaskRun(r.config, build, buildRun, serviceAccount.Name, strategy)
Expand Down Expand Up @@ -582,7 +582,7 @@ type patchStringValue struct {
Value string `json:"value"`
}

func (r *ReconcileBuildRun) patchTaskRun(ctx context.Context, tr *v1beta1.TaskRun, op, path, value string, opts metav1.PatchOptions) error {
func (r *ReconcileBuildRun) patchTaskRun(ctx context.Context, tr *pipelineapi.TaskRun, op, path, value string, opts metav1.PatchOptions) error {
payload := []patchStringValue{{
Op: op,
Path: path,
Expand Down
32 changes: 16 additions & 16 deletions pkg/reconciler/buildrun/buildrun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

v1beta1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
pipelineapi "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -46,7 +46,7 @@ var _ = Describe("Reconcile BuildRun", func() {
ctl test.Catalog
buildSample *build.Build
buildRunSample *build.BuildRun
taskRunSample *v1beta1.TaskRun
taskRunSample *pipelineapi.TaskRun
statusWriter *fakes.FakeStatusWriter
taskRunName, buildRunName, buildName, strategyName, ns string
)
Expand All @@ -71,7 +71,7 @@ var _ = Describe("Reconcile BuildRun", func() {
case *build.BuildRun:
buildRunSample.DeepCopyInto(object)
return nil
case *v1beta1.TaskRun:
case *pipelineapi.TaskRun:
taskRunSample.DeepCopyInto(object)
return nil
}
Expand Down Expand Up @@ -378,7 +378,7 @@ var _ = Describe("Reconcile BuildRun", func() {
statusWriter.UpdateCalls(stubUpdateCalls)
stubPatchCalls := func(_ context.Context, object crc.Object, patch crc.Patch, opts ...crc.PatchOption) error {
switch v := object.(type) {
case *v1beta1.TaskRun:
case *pipelineapi.TaskRun:
if v.Name == taskRunSample.Name {
cancelPatchCalled = true
}
Expand All @@ -394,11 +394,11 @@ var _ = Describe("Reconcile BuildRun", func() {

// actually set value the patch would have set (but we overrode above)
// for next call
taskRunSample.Spec.Status = v1beta1.TaskRunSpecStatusCancelled
taskRunSample.Spec.Status = pipelineapi.TaskRunSpecStatusCancelled
taskRunSample.Status.Conditions = knativev1.Conditions{
{
Type: knativeapi.ConditionSucceeded,
Reason: string(v1beta1.TaskRunReasonCancelled),
Reason: string(pipelineapi.TaskRunReasonCancelled),
Status: corev1.ConditionFalse,
},
}
Expand Down Expand Up @@ -463,15 +463,15 @@ var _ = Describe("Reconcile BuildRun", func() {

It("does not break the reconcile when a failed taskrun has a pod with no failed container", func() {
buildRunSample = ctl.BuildRunWithBuildSnapshot(buildRunName, buildName)
taskRunSample = &v1beta1.TaskRun{
taskRunSample = &pipelineapi.TaskRun{
ObjectMeta: metav1.ObjectMeta{
Name: taskRunName,
Namespace: ns,
Labels: map[string]string{"buildrun.shipwright.io/name": buildRunName},
},
Spec: v1beta1.TaskRunSpec{},
Status: v1beta1.TaskRunStatus{
TaskRunStatusFields: v1beta1.TaskRunStatusFields{
Spec: pipelineapi.TaskRunSpec{},
Status: pipelineapi.TaskRunStatus{
TaskRunStatusFields: pipelineapi.TaskRunStatusFields{
PodName: "foobar",
CompletionTime: &metav1.Time{
Time: time.Now(),
Expand All @@ -484,7 +484,7 @@ var _ = Describe("Reconcile BuildRun", func() {
Conditions: knativev1.Conditions{
{
Type: knativeapi.ConditionSucceeded,
Reason: string(v1beta1.TaskRunReasonFailed),
Reason: string(pipelineapi.TaskRunReasonFailed),
Status: corev1.ConditionFalse,
Message: "some message",
},
Expand Down Expand Up @@ -973,7 +973,7 @@ var _ = Describe("Reconcile BuildRun", func() {
// Stub the create calls for a TaskRun
client.CreateCalls(func(_ context.Context, object crc.Object, _ ...crc.CreateOption) error {
switch object := object.(type) {
case *v1beta1.TaskRun:
case *pipelineapi.TaskRun:
ctl.DefaultTaskRunWithStatus(taskRunName, buildRunName, ns, corev1.ConditionTrue, "Succeeded").DeepCopyInto(object)
}
return nil
Expand Down Expand Up @@ -1002,7 +1002,7 @@ var _ = Describe("Reconcile BuildRun", func() {
// Stub the create calls for a TaskRun
client.CreateCalls(func(_ context.Context, object crc.Object, _ ...crc.CreateOption) error {
switch object := object.(type) {
case *v1beta1.TaskRun:
case *pipelineapi.TaskRun:
ctl.DefaultTaskRunWithStatus(taskRunName, buildRunName, ns, corev1.ConditionTrue, "Succeeded").DeepCopyInto(object)
}
return nil
Expand Down Expand Up @@ -1085,7 +1085,7 @@ var _ = Describe("Reconcile BuildRun", func() {
// Stub the create calls for a TaskRun
client.CreateCalls(func(_ context.Context, object crc.Object, _ ...crc.CreateOption) error {
switch object := object.(type) {
case *v1beta1.TaskRun:
case *pipelineapi.TaskRun:
ctl.DefaultTaskRunWithStatus(taskRunName, buildRunName, ns, corev1.ConditionTrue, "Succeeded").DeepCopyInto(object)
}
return nil
Expand Down Expand Up @@ -1294,7 +1294,7 @@ var _ = Describe("Reconcile BuildRun", func() {
var taskRunCreates int
client.CreateCalls(func(_ context.Context, o crc.Object, _ ...crc.CreateOption) error {
switch o.(type) {
case *v1beta1.TaskRun:
case *pipelineapi.TaskRun:
taskRunCreates++
}

Expand Down Expand Up @@ -1466,7 +1466,7 @@ var _ = Describe("Reconcile BuildRun", func() {
var taskRunCreates int
client.CreateCalls(func(_ context.Context, o crc.Object, _ ...crc.CreateOption) error {
switch taskRun := o.(type) {
case *v1beta1.TaskRun:
case *pipelineapi.TaskRun:
taskRunCreates++

Expect(taskRun.Labels).ToNot(HaveKey(build.LabelBuild), "no build name label is suppose to be set")
Expand Down
Loading

0 comments on commit 5bb47a6

Please sign in to comment.