Skip to content

Commit

Permalink
Use v1 Tekton PipelineRun and v1beta1 Build and BuildRun
Browse files Browse the repository at this point in the history
  • Loading branch information
SaschaSchwarze0 committed Nov 24, 2023
1 parent 51d29f9 commit 0138cfd
Show file tree
Hide file tree
Showing 50 changed files with 4,216 additions and 295 deletions.
6 changes: 6 additions & 0 deletions .github/actions/setup/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,9 @@ runs:
cache: true
check-latest: true
- uses: ko-build/[email protected]
- name: install-goml
run: |
TAG_NAME="$(curl -s https://api.github.com/repos/herrjulz/goml/releases/latest | jq -r '.tag_name')"
curl -o /usr/local/bin/goml -L -s "https://github.com/herrjulz/goml/releases/download/${TAG_NAME}/goml-$(uname -s | tr '[:upper:]' '[:lower:]')-$(uname -m | sed -e 's/x86_64/amd64/')"
chmod +x /usr/local/bin/goml
goml -v
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ CHART_DIR ?= ./chart
MANIFEST_DIR ?= $(CHART_DIR)/generated

# shipwright and tekton target versions to download upstream crd resources
SHIPWRIGHT_VERSION ?= v0.11.0
TEKTON_VERSION ?= v0.44.0
SHIPWRIGHT_VERSION ?= v0.12.0
TEKTON_VERSION ?= v0.53.2

# full path to the directory where the crds are downloaded
CRD_DIR ?= $(LOCAL_BIN)/crds
Expand Down
31 changes: 15 additions & 16 deletions controllers/customrun_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
"context"
"fmt"

"github.com/shipwright-io/build/pkg/apis/build/v1alpha1"
buildapi "github.com/shipwright-io/build/pkg/apis/build/v1beta1"
"github.com/shipwright-io/triggers/pkg/filter"

"github.com/go-logr/logr"
tknv1beta1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
tektonapibeta "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -46,20 +46,19 @@ type CustomRunReconciler struct {
// BuildRun name is randomly generated using the Run's name as base.
func (r *CustomRunReconciler) generateBuildRun(
ctx context.Context,
customRun *tknv1beta1.CustomRun,
) (*v1alpha1.BuildRun, error) {
br := v1alpha1.BuildRun{
customRun *tektonapibeta.CustomRun,
) (*buildapi.BuildRun, error) {
br := buildapi.BuildRun{
ObjectMeta: metav1.ObjectMeta{
Namespace: customRun.GetNamespace(),
Name: names.SimpleNameGenerator.GenerateName(fmt.Sprintf("%s-", customRun.Name)),
Labels: map[string]string{
filter.OwnedByTektonCustomRun: customRun.Name,
},
},
Spec: v1alpha1.BuildRunSpec{
BuildRef: &v1alpha1.BuildRef{
APIVersion: &customRun.Spec.CustomRef.APIVersion,
Name: customRun.Spec.CustomRef.Name,
Spec: buildapi.BuildRunSpec{
Build: buildapi.ReferencedBuild{
Name: &customRun.Spec.CustomRef.Name,
},
ParamValues: filter.TektonCustomRunParamsToShipwrightParamValues(customRun),
Timeout: customRun.Spec.Timeout,
Expand All @@ -75,8 +74,8 @@ func (r *CustomRunReconciler) generateBuildRun(
// reflectBuildRunStatusOnTektonRun reflects the BuildRun status on the Run instance.
func (r *CustomRunReconciler) reflectBuildRunStatusOnTektonCustomRun(
logger logr.Logger,
customRun *tknv1beta1.CustomRun,
br *v1alpha1.BuildRun,
customRun *tektonapibeta.CustomRun,
br *buildapi.BuildRun,
) {
if br.Status.CompletionTime != nil {
customRun.Status.CompletionTime = br.Status.CompletionTime
Expand Down Expand Up @@ -127,7 +126,7 @@ func (r *CustomRunReconciler) Reconcile(
) (ctrl.Result, error) {
logger := log.FromContext(ctx)

var customRun tknv1beta1.CustomRun
var customRun tektonapibeta.CustomRun
err := r.Get(ctx, req.NamespacedName, &customRun)
if err != nil {
if !errors.IsNotFound(err) {
Expand All @@ -154,7 +153,7 @@ func (r *CustomRunReconciler) Reconcile(
return RequeueOnError(err)
}

var br = &v1alpha1.BuildRun{}
var br = &buildapi.BuildRun{}
// when the status extra-fields is empty, it means the BuildRun instance is not created yet, thus
// the first step is issuing the instance and later on watching over its status updates
if extraFields.IsEmpty() {
Expand Down Expand Up @@ -204,7 +203,7 @@ func (r *CustomRunReconciler) Reconcile(
logger.V(0).Info("Tekton CustomRun instance is cancelled, cancelling the BuildRun too")

originalBr := br.DeepCopy()
br.Spec.State = v1alpha1.BuildRunRequestedStatePtr(v1alpha1.BuildRunStateCancel)
br.Spec.State = buildapi.BuildRunRequestedStatePtr(buildapi.BuildRunStateCancel)
if err = r.Client.Patch(ctx, br, client.MergeFrom(originalBr)); err != nil {
logger.V(0).Error(err, "trying to patch BuildRun with cancellation state")
return RequeueOnError(err)
Expand Down Expand Up @@ -232,9 +231,9 @@ func (r *CustomRunReconciler) SetupWithManager(mgr ctrl.Manager) error {

return ctrl.NewControllerManagedBy(mgr).
// watches Tekton Run instances, that's the principal resource for this controller
For(&tknv1beta1.CustomRun{}).
For(&tektonapibeta.CustomRun{}).
// it also watches over BuildRun instances that are owned by this controller
Owns(&v1alpha1.BuildRun{}).
Owns(&buildapi.BuildRun{}).
// filtering out objects that aren't ready for reconciliation
WithEventFilter(predicate.NewPredicateFuncs(filter.CustomRunEventFilterPredicate)).
// making sure the controller reconciles one instance at the time in order to not create a
Expand Down
6 changes: 3 additions & 3 deletions controllers/inventory_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package controllers
import (
"context"

"github.com/shipwright-io/build/pkg/apis/build/v1alpha1"
buildapi "github.com/shipwright-io/build/pkg/apis/build/v1beta1"
"github.com/shipwright-io/triggers/pkg/inventory"

"k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -32,7 +32,7 @@ type InventoryReconciler struct {
func (r *InventoryReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
logger := log.FromContext(ctx)

var b v1alpha1.Build
var b buildapi.Build
if err := r.Get(ctx, req.NamespacedName, &b); err != nil {
if !errors.IsNotFound(err) {
logger.Error(err, "Unable to fetch Build, removing from the Inventory")
Expand All @@ -59,7 +59,7 @@ func (r *InventoryReconciler) SetupWithManager(mgr ctrl.Manager) error {
}

return ctrl.NewControllerManagedBy(mgr).
For(&v1alpha1.Build{}).
For(&buildapi.Build{}).
Complete(r)
}

Expand Down
24 changes: 12 additions & 12 deletions controllers/pipelinerun_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
"context"
"fmt"

"github.com/shipwright-io/build/pkg/apis/build/v1alpha1"
buildapi "github.com/shipwright-io/build/pkg/apis/build/v1beta1"
"github.com/shipwright-io/triggers/pkg/constants"
"github.com/shipwright-io/triggers/pkg/filter"
"github.com/shipwright-io/triggers/pkg/inventory"

tknv1beta1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
tektonapi "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -42,26 +42,26 @@ type PipelineRunReconciler struct {
// establish ownership. Only returns the created object name and error.
func (r *PipelineRunReconciler) createBuildRun(
ctx context.Context,
pipelineRun *tknv1beta1.PipelineRun,
pipelineRun *tektonapi.PipelineRun,
buildName string,
) (string, error) {
br := v1alpha1.BuildRun{
br := buildapi.BuildRun{
ObjectMeta: metav1.ObjectMeta{
Namespace: pipelineRun.GetNamespace(),
GenerateName: fmt.Sprintf("%s-", buildName),
Annotations: map[string]string{
filter.OwnedByTektonPipelineRun: pipelineRun.GetName(),
},
OwnerReferences: []metav1.OwnerReference{{
APIVersion: constants.TektonAPIv1beta1,
APIVersion: constants.TektonAPIv1,
Kind: "PipelineRun",
Name: pipelineRun.GetName(),
UID: pipelineRun.GetUID(),
}},
},
Spec: v1alpha1.BuildRunSpec{
BuildRef: &v1alpha1.BuildRef{
Name: buildName,
Spec: buildapi.BuildRunSpec{
Build: buildapi.ReferencedBuild{
Name: &buildName,
},
},
}
Expand All @@ -75,7 +75,7 @@ func (r *PipelineRunReconciler) createBuildRun(
// the PipelineRun annotations to documented the created BuildRuns.
func (r *PipelineRunReconciler) issueBuildRunsForPipelineRun(
ctx context.Context,
pipelineRun *tknv1beta1.PipelineRun,
pipelineRun *tektonapi.PipelineRun,
buildNames []string,
) ([]string, error) {
var created []string
Expand All @@ -99,7 +99,7 @@ func (r *PipelineRunReconciler) Reconcile(
) (ctrl.Result, error) {
logger := log.FromContext(ctx)

var pipelineRun tknv1beta1.PipelineRun
var pipelineRun tektonapi.PipelineRun
if err := r.Get(ctx, req.NamespacedName, &pipelineRun); err != nil {
if !errors.IsNotFound(err) {
logger.Error(err, "Unable to fetch PipelineRun")
Expand All @@ -123,7 +123,7 @@ func (r *PipelineRunReconciler) Reconcile(
)

// search for Builds with Pipeline triggers matching current ObjectRef criteria
buildsToBeIssued := r.buildInventory.SearchForObjectRef(v1alpha1.PipelineTrigger, objectRef)
buildsToBeIssued := r.buildInventory.SearchForObjectRef(buildapi.PipelineTrigger, objectRef)
if len(buildsToBeIssued) == 0 {
return Done()
}
Expand Down Expand Up @@ -197,7 +197,7 @@ func (r *PipelineRunReconciler) SetupWithManager(mgr ctrl.Manager) error {
}

return ctrl.NewControllerManagedBy(mgr).
For(&tknv1beta1.PipelineRun{}).
For(&tektonapi.PipelineRun{}).
WithEventFilter(predicate.NewPredicateFuncs(filter.PipelineRunEventFilterPredicate)).
WithOptions(controller.Options{MaxConcurrentReconciles: 1}).
Complete(r)
Expand Down
6 changes: 6 additions & 0 deletions hack/download-crds.sh
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ for f in ${SHIPWRIGHT_CRD_FILES[@]}; do
URL_BASE="https://${REPO_HOST}/${SHIPWRIGHT_REPO_PATH}"
echo "# - ${URL_BASE}/${f}"
do_curl "${URL_BASE}" "${f}"
# The integration tests run without Conversion, therefore disable it and make beta the stored version
goml delete -f "${CRD_DIR}/${f}" -p spec.conversion
goml set -f "${CRD_DIR}/${f}" -p spec.versions.name:v1alpha1.storage -v false
goml set -f "${CRD_DIR}/${f}" -p spec.versions.name:v1beta1.storage -v true
done

echo "# Tekton '${TEKTON_VERSION}' CRDs stored at: '${CRD_DIR}'"
Expand All @@ -74,4 +78,6 @@ for f in ${TEKTON_CRD_FILES[@]}; do
URL_BASE="https://${REPO_HOST}/${TEKTON_REPO_PATH}"
echo "# - ${URL_BASE}/${f}"
do_curl "${URL_BASE}" "${f}"
# The integration tests run without Conversion, therefore disable it
goml delete -f "${CRD_DIR}/${f}" -p spec.conversion 2>/dev/null || true
done
10 changes: 6 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import (
"flag"
"os"

"github.com/shipwright-io/build/pkg/apis/build/v1alpha1"
buildapi "github.com/shipwright-io/build/pkg/apis/build/v1beta1"
"github.com/shipwright-io/triggers/controllers"
"github.com/shipwright-io/triggers/pkg/inventory"

tknv1beta1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
tektonapi "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
tektonapibeta "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
Expand All @@ -33,8 +34,9 @@ var (

func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(v1alpha1.AddToScheme(scheme))
utilruntime.Must(tknv1beta1.AddToScheme(scheme))
utilruntime.Must(buildapi.AddToScheme(scheme))
utilruntime.Must(tektonapi.AddToScheme(scheme))
utilruntime.Must(tektonapibeta.AddToScheme(scheme))
}

func main() {
Expand Down
17 changes: 11 additions & 6 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,24 @@ package constants
import (
"fmt"

"github.com/shipwright-io/build/pkg/apis/build/v1alpha1"
buildapi "github.com/shipwright-io/build/pkg/apis/build/v1beta1"

tknv1beta1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
tektonapi "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
tektonapibeta "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
)

var (
TektonAPIv1 = fmt.Sprintf("%s/%s",
tektonapi.SchemeGroupVersion.Group,
tektonapi.SchemeGroupVersion.Version,
)
TektonAPIv1beta1 = fmt.Sprintf("%s/%s",
tknv1beta1.SchemeGroupVersion.Group,
tknv1beta1.SchemeGroupVersion.Version,
tektonapibeta.SchemeGroupVersion.Group,
tektonapibeta.SchemeGroupVersion.Version,
)
ShipwrightAPIVersion = fmt.Sprintf(
"%s/%s",
v1alpha1.SchemeGroupVersion.Group,
v1alpha1.SchemeGroupVersion.Version,
buildapi.SchemeGroupVersion.Group,
buildapi.SchemeGroupVersion.Version,
)
)
22 changes: 11 additions & 11 deletions pkg/filter/annotation.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,29 @@ import (
"encoding/json"
"reflect"

"github.com/shipwright-io/build/pkg/apis/build/v1alpha1"
buildapi "github.com/shipwright-io/build/pkg/apis/build/v1beta1"
"github.com/shipwright-io/triggers/pkg/util"
tknv1beta1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
tektonapi "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
)

// TriggeredBuild represents previously triggered builds by storing together the original build name
// and it's objectRef. Both are the criteria needed to find the Builds with matching triggers in the
// Inventory.
type TriggeredBuild struct {
BuildName string `json:"buildName"`
ObjectRef *v1alpha1.WhenObjectRef `json:"objectRef"`
ObjectRef *buildapi.WhenObjectRef `json:"objectRef"`
}

// PipelineRunGetAnnotations extract the annotations, return an empty map otherwise.
func PipelineRunGetAnnotations(pipelineRun *tknv1beta1.PipelineRun) map[string]string {
func PipelineRunGetAnnotations(pipelineRun *tektonapi.PipelineRun) map[string]string {
annotations := pipelineRun.GetAnnotations()
if annotations == nil {
annotations = map[string]string{}
}
return annotations
}

func PipelineRunAnnotatedNameMatchesObject(pipelineRun *tknv1beta1.PipelineRun) bool {
func PipelineRunAnnotatedNameMatchesObject(pipelineRun *tektonapi.PipelineRun) bool {
annotations := PipelineRunGetAnnotations(pipelineRun)
value, ok := annotations[TektonPipelineRunName]
if !ok {
Expand All @@ -40,7 +40,7 @@ func PipelineRunAnnotatedNameMatchesObject(pipelineRun *tknv1beta1.PipelineRun)
return pipelineRun.GetName() == value
}

func PipelineRunAnnotateName(pipelineRun *tknv1beta1.PipelineRun) {
func PipelineRunAnnotateName(pipelineRun *tektonapi.PipelineRun) {
annotations := PipelineRunGetAnnotations(pipelineRun)
annotations[TektonPipelineRunName] = pipelineRun.GetName()
pipelineRun.SetAnnotations(annotations)
Expand All @@ -64,7 +64,7 @@ func UnmarshalIntoTriggeredAnnotationSlice(payload string) ([]TriggeredBuild, er
// valid slice of the type. When the annotation is empty, or not present, an empty slice is returned
// instead.
func PipelineRunExtractTriggeredBuildsSlice(
pipelineRun *tknv1beta1.PipelineRun,
pipelineRun *tektonapi.PipelineRun,
) ([]TriggeredBuild, error) {
annotations := PipelineRunGetAnnotations(pipelineRun)
value, ok := annotations[TektonPipelineRunTriggeredBuilds]
Expand All @@ -78,7 +78,7 @@ func PipelineRunExtractTriggeredBuildsSlice(
func TriggereBuildsContainsObjectRef(
triggeredBuilds []TriggeredBuild,
buildNames []string,
objectRef *v1alpha1.WhenObjectRef,
objectRef *buildapi.WhenObjectRef,
) bool {
for _, entry := range triggeredBuilds {
// first of all, the build name must be the same
Expand All @@ -103,7 +103,7 @@ func TriggereBuildsContainsObjectRef(
func AppendIntoTriggeredBuildSliceAsAnnotation(
triggeredBuilds []TriggeredBuild,
buildNames []string,
objectRef *v1alpha1.WhenObjectRef,
objectRef *buildapi.WhenObjectRef,
) (string, error) {
for _, buildName := range buildNames {
entry := TriggeredBuild{
Expand All @@ -122,10 +122,10 @@ func AppendIntoTriggeredBuildSliceAsAnnotation(

// PipelineRunAppendTriggeredBuildsAnnotation set or update the triggered-builds annotation.
func PipelineRunAppendTriggeredBuildsAnnotation(
pipelineRun *tknv1beta1.PipelineRun,
pipelineRun *tektonapi.PipelineRun,
triggeredBuilds []TriggeredBuild,
buildNames []string,
objectRef *v1alpha1.WhenObjectRef,
objectRef *buildapi.WhenObjectRef,
) error {
annotations := pipelineRun.GetAnnotations()
if annotations == nil {
Expand Down
Loading

0 comments on commit 0138cfd

Please sign in to comment.