Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add build strategy labels to task run #465

Merged
merged 5 commits into from
Nov 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/apis/build/v1alpha1/build_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var (
const (
// LabelBuild is a label key for defining the build name
LabelBuild = "build.build.dev/name"

Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/build/v1alpha1/buildrun_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var (
const (
// LabelBuildRun is a label key for BuildRuns to define the name of the BuildRun
LabelBuildRun = "buildrun.build.dev/name"

Expand Down
57 changes: 57 additions & 0 deletions pkg/apis/build/v1alpha1/buildstrategy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright The Shipwright Contributors
//
// SPDX-License-Identifier: Apache-2.0

package v1alpha1

import (
corev1 "k8s.io/api/core/v1"
)

const (
// NamespacedBuildStrategyKind indicates that the buildstrategy type has a namespaced scope.
NamespacedBuildStrategyKind BuildStrategyKind = "BuildStrategy"

// ClusterBuildStrategyKind indicates that buildstrategy type has a cluster scope.
ClusterBuildStrategyKind BuildStrategyKind = "ClusterBuildStrategy"
)

// BuildStrategySpec defines the desired state of BuildStrategy
type BuildStrategySpec struct {
BuildSteps []BuildStep `json:"buildSteps,omitempty"`
}

// BuildStep defines a partial step that needs to run in container for
// building the image.
type BuildStep struct {
corev1.Container `json:",inline"`
}

// BuildStrategyStatus defines the observed state of BuildStrategy
type BuildStrategyStatus struct {
}

// BuildStrategyKind defines the type of BuildStrategy used by the build.
type BuildStrategyKind string

// StrategyRef can be used to refer to a specific instance of a buildstrategy.
// Copied from CrossVersionObjectReference: https://github.com/kubernetes/kubernetes/blob/169df7434155cbbc22f1532cba8e0a9588e29ad8/pkg/apis/autoscaling/types.go#L64
type StrategyRef struct {
// Name of the referent; More info: http://kubernetes.io/docs/user-guide/identifiers#names
Name string `json:"name"`

// BuildStrategyKind indicates the kind of the buildstrategy, namespaced or cluster scoped.
Kind *BuildStrategyKind `json:"kind,omitempty"`

// API version of the referent
// +optional
APIVersion string `json:"apiVersion,omitempty"`
}

// BuilderStrategy defines the common elements of build strategies
type BuilderStrategy interface {
GetName() string
GetGeneration() int64
GetResourceLabels() map[string]string
GetBuildSteps() []BuildStep
}
65 changes: 31 additions & 34 deletions pkg/apis/build/v1alpha1/buildstrategy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,18 @@
package v1alpha1

import (
corev1 "k8s.io/api/core/v1"
"strconv"

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

// BuildStrategySpec defines the desired state of BuildStrategy
type BuildStrategySpec struct {
BuildSteps []BuildStep `json:"buildSteps,omitempty"`
}

// BuildStep defines a partial step that needs to run in container for
// building the image.
type BuildStep struct {
corev1.Container `json:",inline"`
}
const (
// LabelBuildStrategyName is a label key for defining the build strategy name
LabelBuildStrategyName = "buildstrategy.build.dev/name"

// BuildStrategyStatus defines the observed state of BuildStrategy
type BuildStrategyStatus struct {
}
// LabelBuildStrategyGeneration is a label key for defining the build strategy generation
LabelBuildStrategyGeneration = "buildstrategy.build.dev/generation"
)

// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand All @@ -47,28 +41,31 @@ type BuildStrategyList struct {
Items []BuildStrategy `json:"items"`
}

func init() {
SchemeBuilder.Register(&BuildStrategy{}, &BuildStrategyList{})
// GetName returns the name of the build strategy
func (s BuildStrategy) GetName() string {
return s.Name
}

// StrategyRef can be used to refer to a specific instance of a buildstrategy.
// Copied from CrossVersionObjectReference: https://github.com/kubernetes/kubernetes/blob/169df7434155cbbc22f1532cba8e0a9588e29ad8/pkg/apis/autoscaling/types.go#L64
type StrategyRef struct {
// Name of the referent; More info: http://kubernetes.io/docs/user-guide/identifiers#names
Name string `json:"name"`
// BuildStrategyKind indicates the kind of the buildstrategy, namespaced or cluster scoped.
Kind *BuildStrategyKind `json:"kind,omitempty"`
// API version of the referent
// +optional
APIVersion string `json:"apiVersion,omitempty"`
// GetGeneration returns the current generation sequence number of the build
// strategy resource
func (s BuildStrategy) GetGeneration() int64 {
return s.Generation
}

// BuildStrategyKind defines the type of BuildStrategy used by the build.
type BuildStrategyKind string
// GetResourceLabels returns labels that define the build strategy name and
// generation to be used in labels map of a resource
func (s BuildStrategy) GetResourceLabels() map[string]string {
return map[string]string{
LabelBuildStrategyName: s.Name,
LabelBuildStrategyGeneration: strconv.FormatInt(s.Generation, 10),
}
}

const (
// NamespacedBuildStrategyKind indicates that the buildstrategy type has a namespaced scope.
NamespacedBuildStrategyKind BuildStrategyKind = "BuildStrategy"
// ClusterBuildStrategyKind indicates that buildstrategy type has a cluster scope.
ClusterBuildStrategyKind BuildStrategyKind = "ClusterBuildStrategy"
)
// GetBuildSteps returns the spec build steps of the build strategy
func (s BuildStrategy) GetBuildSteps() []BuildStep {
return s.Spec.BuildSteps
}

func init() {
SchemeBuilder.Register(&BuildStrategy{}, &BuildStrategyList{})
}
35 changes: 35 additions & 0 deletions pkg/apis/build/v1alpha1/clusterbuildstrategy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,19 @@
package v1alpha1

import (
"strconv"

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

const (
// LabelClusterBuildStrategyName is a label key for defining the cluster build strategy name
LabelClusterBuildStrategyName = "clusterbuildstrategy.build.dev/name"

// LabelClusterBuildStrategyGeneration is a label key for defining the cluster build strategy generation
LabelClusterBuildStrategyGeneration = "clusterbuildstrategy.build.dev/generation"
)

// +genclient
// +genclient:nonNamespaced
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand All @@ -32,6 +42,31 @@ type ClusterBuildStrategyList struct {
Items []ClusterBuildStrategy `json:"items"`
}

// GetName returns the name of the build strategy
func (s ClusterBuildStrategy) GetName() string {
return s.Name
}

// GetGeneration returns the current generation sequence number of the build
// strategy resource
func (s ClusterBuildStrategy) GetGeneration() int64 {
return s.Generation
}

// GetResourceLabels returns labels that define the build strategy name and
// generation to be used in labels map of a resource
func (s ClusterBuildStrategy) GetResourceLabels() map[string]string {
return map[string]string{
LabelClusterBuildStrategyName: s.Name,
LabelClusterBuildStrategyGeneration: strconv.FormatInt(s.Generation, 10),
}
}

// GetBuildSteps returns the spec build steps of the build strategy
func (s ClusterBuildStrategy) GetBuildSteps() []BuildStep {
return s.Spec.BuildSteps
}

func init() {
SchemeBuilder.Register(&ClusterBuildStrategy{}, &ClusterBuildStrategyList{})
}
4 changes: 2 additions & 2 deletions pkg/controller/buildrun/buildrun_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ func (r *ReconcileBuildRun) createTaskRun(ctx context.Context, build *buildv1alp
return nil, err
}
if buildStrategy != nil {
generatedTaskRun, err = GenerateTaskRun(r.config, build, buildRun, serviceAccount.Name, buildStrategy.Spec.BuildSteps)
generatedTaskRun, err = GenerateTaskRun(r.config, build, buildRun, serviceAccount.Name, buildStrategy)
if err != nil {
updateErr := r.updateBuildRunErrorStatus(ctx, buildRun, err.Error())
return nil, handleError("Failed to generate the taskrun with buildStrategy", err, updateErr)
Expand All @@ -566,7 +566,7 @@ func (r *ReconcileBuildRun) createTaskRun(ctx context.Context, build *buildv1alp
return nil, err
}
if clusterBuildStrategy != nil {
generatedTaskRun, err = GenerateTaskRun(r.config, build, buildRun, serviceAccount.Name, clusterBuildStrategy.Spec.BuildSteps)
generatedTaskRun, err = GenerateTaskRun(r.config, build, buildRun, serviceAccount.Name, clusterBuildStrategy)
if err != nil {
updateErr := r.updateBuildRunErrorStatus(ctx, buildRun, err.Error())
return nil, handleError("Failed to generate the taskrun with clusterBuildStrategy", err, updateErr)
Expand Down
8 changes: 6 additions & 2 deletions pkg/controller/buildrun/generate_taskrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func GenerateTaskRun(
build *buildv1alpha1.Build,
buildRun *buildv1alpha1.BuildRun,
serviceAccountName string,
buildSteps []buildv1alpha1.BuildStep,
strategy buildv1alpha1.BuilderStrategy,
) (*v1beta1.TaskRun, error) {

revision := "master"
Expand All @@ -194,7 +194,7 @@ func GenerateTaskRun(
ImageURL = build.Spec.Output.ImageURL
}

taskSpec, err := GenerateTaskSpec(cfg, build, buildRun, buildSteps)
taskSpec, err := GenerateTaskSpec(cfg, build, buildRun, strategy.GetBuildSteps())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -254,6 +254,10 @@ func GenerateTaskRun(
},
}

for label, value := range strategy.GetResourceLabels() {
expectedTaskRun.Labels[label] = value
}

// assign the timeout
if buildRun.Spec.Timeout != nil {
expectedTaskRun.Spec.Timeout = buildRun.Spec.Timeout
Expand Down
10 changes: 6 additions & 4 deletions pkg/controller/buildrun/generate_taskrun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ var _ = Describe("GenerateTaskrun", func() {
})

JustBeforeEach(func() {
got, err = buildrunCtl.GenerateTaskRun(config.NewDefaultConfig(), build, buildRun, serviceAccountName, buildStrategy.Spec.BuildSteps)
got, err = buildrunCtl.GenerateTaskRun(config.NewDefaultConfig(), build, buildRun, serviceAccountName, buildStrategy)
Expect(err).To(BeNil())
})

Expand All @@ -154,6 +154,8 @@ var _ = Describe("GenerateTaskrun", func() {
Expect(got.Spec.ServiceAccountName).To(Equal(buildpacks + "-serviceaccount"))
Expect(got.Labels[buildv1alpha1.LabelBuild]).To(Equal(build.Name))
Expect(got.Labels[buildv1alpha1.LabelBuildRun]).To(Equal(buildRun.Name))
Expect(got.Labels[buildv1alpha1.LabelBuildStrategyName]).To(Equal(build.Spec.StrategyRef.Name))
Expect(got.Labels[buildv1alpha1.LabelBuildStrategyGeneration]).To(Equal("0"))
})

It("should ensure generated TaskRun's input and output resources are correct", func() {
Expand Down Expand Up @@ -215,7 +217,7 @@ var _ = Describe("GenerateTaskrun", func() {
})

JustBeforeEach(func() {
got, err = buildrunCtl.GenerateTaskRun(config.NewDefaultConfig(), build, buildRun, serviceAccountName, buildStrategy.Spec.BuildSteps)
got, err = buildrunCtl.GenerateTaskRun(config.NewDefaultConfig(), build, buildRun, serviceAccountName, buildStrategy)
Expect(err).To(BeNil())
})

Expand Down Expand Up @@ -274,7 +276,7 @@ var _ = Describe("GenerateTaskrun", func() {
})

JustBeforeEach(func() {
got, err = buildrunCtl.GenerateTaskRun(config.NewDefaultConfig(), build, buildRun, serviceAccountName, buildStrategy.Spec.BuildSteps)
got, err = buildrunCtl.GenerateTaskRun(config.NewDefaultConfig(), build, buildRun, serviceAccountName, buildStrategy)
Expect(err).To(BeNil())
})

Expand All @@ -297,7 +299,7 @@ var _ = Describe("GenerateTaskrun", func() {
})

JustBeforeEach(func() {
got, err = buildrunCtl.GenerateTaskRun(config.NewDefaultConfig(), build, buildRun, serviceAccountName, buildStrategy.Spec.BuildSteps)
got, err = buildrunCtl.GenerateTaskRun(config.NewDefaultConfig(), build, buildRun, serviceAccountName, buildStrategy)
Expect(err).To(BeNil())
})

Expand Down