Skip to content

Commit

Permalink
Move to v1beta1
Browse files Browse the repository at this point in the history
pkg/reconciler/build
pkg/validate/
pkg/reconciler/buildrun
  • Loading branch information
qu1queee committed Jan 9, 2024
1 parent 3054897 commit 6a770c9
Show file tree
Hide file tree
Showing 40 changed files with 542 additions and 699 deletions.
15 changes: 15 additions & 0 deletions pkg/apis/build/v1beta1/build_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,18 @@ type BuildRetention struct {
func init() {
SchemeBuilder.Register(&Build{}, &BuildList{})
}

// GetSourceCredentials returns the secret name for a Build Source
func (b Build) GetSourceCredentials() *string {
switch b.Spec.Source.Type {
case OCIArtifactType:
if b.Spec.Source.OCIArtifact != nil && b.Spec.Source.OCIArtifact.PullSecret != nil {
return b.Spec.Source.OCIArtifact.PullSecret
}
default:
if b.Spec.Source.GitSource != nil && b.Spec.Source.GitSource.CloneSecret != nil {
return b.Spec.Source.GitSource.CloneSecret
}
}
return nil
}
2 changes: 1 addition & 1 deletion pkg/reconciler/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

build "github.com/shipwright-io/build/pkg/apis/build/v1alpha1"
build "github.com/shipwright-io/build/pkg/apis/build/v1beta1"
"github.com/shipwright-io/build/pkg/config"
"github.com/shipwright-io/build/pkg/ctxlog"
buildmetrics "github.com/shipwright-io/build/pkg/metrics"
Expand Down
55 changes: 27 additions & 28 deletions pkg/reconciler/build/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"

build "github.com/shipwright-io/build/pkg/apis/build/v1alpha1"
build "github.com/shipwright-io/build/pkg/apis/build/v1beta1"
"github.com/shipwright-io/build/pkg/config"
"github.com/shipwright-io/build/pkg/ctxlog"
)
Expand Down Expand Up @@ -62,27 +62,31 @@ func add(ctx context.Context, mgr manager.Manager, r reconcile.Reconciler, maxCo
o := e.ObjectOld.(*build.Build)
n := e.ObjectNew.(*build.Build)

buildRunDeletionAnnotation := false
// Check if the AnnotationBuildRunDeletion annotation is updated
oldAnnot := o.GetAnnotations()
newAnnot := n.GetAnnotations()
if !reflect.DeepEqual(oldAnnot, newAnnot) {
if oldAnnot[build.AnnotationBuildRunDeletion] != newAnnot[build.AnnotationBuildRunDeletion] {
ctxlog.Debug(
ctx,
"updating predicated passed, the annotation was modified.",
namespace,
n.GetNamespace(),
name,
n.GetName(),
)
buildRunDeletionAnnotation = true
buildAtBuildDeletion := false

// Check if the Build retention AtBuildDeletion is updated
oldBuildRetention := o.Spec.Retention
newBuildRetention := n.Spec.Retention

if o.Spec.Retention != nil && n.Spec.Retention != nil {
if !reflect.DeepEqual(oldBuildRetention, newBuildRetention) {
if o.Spec.Retention.AtBuildDeletion != n.Spec.Retention.AtBuildDeletion {
ctxlog.Debug(
ctx,
"updating predicated passed, the build retention AtBuildDeletion was modified.",
namespace,
n.GetNamespace(),
name,
n.GetName(),
)
buildAtBuildDeletion = true
}
}
}

// Ignore updates to CR status in which case metadata.Generation does not change
// or BuildRunDeletion annotation does not change
return o.GetGeneration() != n.GetGeneration() || buildRunDeletionAnnotation
return o.GetGeneration() != n.GetGeneration() || buildAtBuildDeletion
},
DeleteFunc: func(e event.DeleteEvent) bool {
// Never reconcile on deletion, there is nothing we have to do
Expand Down Expand Up @@ -153,21 +157,16 @@ func add(ctx context.Context, mgr manager.Manager, r reconcile.Reconciler, maxCo
flagReconcile := false

for _, build := range buildList.Items {
if build.Spec.Source.Credentials != nil {
if build.Spec.Source.Credentials.Name == secret.Name {
flagReconcile = true
}
if build.GetSourceCredentials() != nil && build.GetSourceCredentials() == &secret.Name {
flagReconcile = true
}
if build.Spec.Output.Credentials != nil {
if build.Spec.Output.Credentials.Name == secret.Name {
flagReconcile = true
}
}
if build.Spec.Builder != nil && build.Spec.Builder.Credentials != nil {
if build.Spec.Builder.Credentials.Name == secret.Name {

if build.Spec.Output.PushSecret != nil {
if build.Spec.Output.PushSecret == &secret.Name {
flagReconcile = true
}
}

if flagReconcile {
reconcileList = append(reconcileList, reconcile.Request{
NamespacedName: types.NamespacedName{
Expand Down
84 changes: 43 additions & 41 deletions pkg/reconciler/buildrun/buildrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

buildv1alpha1 "github.com/shipwright-io/build/pkg/apis/build/v1alpha1"
buildv1beta1 "github.com/shipwright-io/build/pkg/apis/build/v1beta1"
"github.com/shipwright-io/build/pkg/config"
"github.com/shipwright-io/build/pkg/ctxlog"
buildmetrics "github.com/shipwright-io/build/pkg/metrics"
Expand Down Expand Up @@ -66,8 +66,8 @@ func NewReconciler(c *config.Config, mgr manager.Manager, ownerRef setOwnerRefer
// Reconcile reads that state of the cluster for a Build object and makes changes based on the state read
// and what is in the Build.Spec
func (r *ReconcileBuildRun) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
var buildRun *buildv1alpha1.BuildRun
var build *buildv1alpha1.Build
var buildRun *buildv1beta1.BuildRun
var build *buildv1beta1.Build

updateBuildRunRequired := false

Expand All @@ -79,7 +79,7 @@ func (r *ReconcileBuildRun) Reconcile(ctx context.Context, request reconcile.Req

// with build run cancel, it is now possible for a build run update to stem from something other than a task run update,
// 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{}
buildRun = &buildv1beta1.BuildRun{}
getBuildRunErr := r.GetBuildRunObject(ctx, request.Name, request.Namespace, buildRun)
lastTaskRun := &pipelineapi.TaskRun{}
getTaskRunErr := r.client.Get(ctx, types.NamespacedName{Name: request.Name, Namespace: request.Namespace}, lastTaskRun)
Expand Down Expand Up @@ -124,16 +124,16 @@ func (r *ReconcileBuildRun) Reconcile(ctx context.Context, request reconcile.Req
}

// if this is a build run event after we've set the task run ref, get the task run using the task run name stored in the build run
if getBuildRunErr == nil && apierrors.IsNotFound(getTaskRunErr) && buildRun.Status.LatestTaskRunRef != nil {
getTaskRunErr = r.client.Get(ctx, types.NamespacedName{Name: *buildRun.Status.LatestTaskRunRef, Namespace: request.Namespace}, lastTaskRun)
if getBuildRunErr == nil && apierrors.IsNotFound(getTaskRunErr) && buildRun.Status.TaskRunName != nil {
getTaskRunErr = r.client.Get(ctx, types.NamespacedName{Name: *buildRun.Status.TaskRunName, Namespace: request.Namespace}, lastTaskRun)
}

// for existing TaskRuns update the BuildRun Status, if there is no TaskRun, then create one
if getTaskRunErr != nil {
if apierrors.IsNotFound(getTaskRunErr) {
build = &buildv1alpha1.Build{}
build = &buildv1beta1.Build{}
if err := resources.GetBuildObject(ctx, r.client, buildRun, build); err != nil {
if !resources.IsClientStatusUpdateError(err) && buildRun.Status.IsFailed(buildv1alpha1.Succeeded) {
if !resources.IsClientStatusUpdateError(err) && buildRun.Status.IsFailed(buildv1beta1.Succeeded) {
return reconcile.Result{}, nil
}
// system call failure, reconcile again
Expand All @@ -147,17 +147,17 @@ func (r *ReconcileBuildRun) Reconcile(ctx context.Context, request reconcile.Req
// an actual resource in the cluster and _should_ have been
// validated and registered by now ...
// reconcile again until it gets a registration value
case buildRun.Spec.BuildRef != nil:
case buildRun.Spec.Build.Name != nil:
return reconcile.Result{}, fmt.Errorf("the Build is not yet validated, build: %s", build.Name)

// When the build(spec) is embedded in the buildrun, the now
// transient/volatile build resource needs to be validated first
case buildRun.Spec.BuildSpec != nil:
case buildRun.Spec.Build.Build != nil:
err := validate.All(ctx,
validate.NewSourceURL(r.client, build),
validate.NewCredentials(r.client, build),
validate.NewStrategies(r.client, build),
validate.NewSourcesRef(build),
// validate.NewSourcesRef(build),
validate.NewBuildName(build),
validate.NewEnv(build),
)
Expand All @@ -180,16 +180,16 @@ func (r *ReconcileBuildRun) Reconcile(ctx context.Context, request reconcile.Req
}

// mark transient build as "registered" and validated
build.Status.Registered = buildv1alpha1.ConditionStatusPtr(corev1.ConditionTrue)
build.Status.Reason = buildv1alpha1.BuildReasonPtr(buildv1alpha1.SucceedStatus)
build.Status.Message = pointer.String(buildv1alpha1.AllValidationsSucceeded)
build.Status.Registered = buildv1beta1.ConditionStatusPtr(corev1.ConditionTrue)
build.Status.Reason = buildv1beta1.BuildReasonPtr(buildv1beta1.SucceedStatus)
build.Status.Message = pointer.String(buildv1beta1.AllValidationsSucceeded)
}
}

if *build.Status.Registered != corev1.ConditionTrue {
// stop reconciling and mark the BuildRun as Failed
// we only reconcile again if the status.Update call fails
var reason buildv1alpha1.BuildReason
var reason buildv1beta1.BuildReason

if build.Status.Reason != nil {
reason = *build.Status.Reason
Expand All @@ -210,31 +210,33 @@ func (r *ReconcileBuildRun) Reconcile(ctx context.Context, request reconcile.Req

// make sure the BuildRun has not already been cancelled
if buildRun.IsCanceled() {
if updateErr := resources.UpdateConditionWithFalseStatus(ctx, r.client, buildRun, "the BuildRun is marked canceled.", buildv1alpha1.BuildRunStateCancel); updateErr != nil {
if updateErr := resources.UpdateConditionWithFalseStatus(ctx, r.client, buildRun, "the BuildRun is marked canceled.", buildv1beta1.BuildRunStateCancel); updateErr != nil {
return reconcile.Result{}, updateErr
}
return reconcile.Result{}, nil
}

// Set OwnerReference for Build and BuildRun only when build.shipwright.io/build-run-deletion is set "true"
if build.GetAnnotations()[buildv1alpha1.AnnotationBuildRunDeletion] == "true" && !resources.IsOwnedByBuild(build, buildRun.OwnerReferences) {
if err := r.setOwnerReferenceFunc(build, buildRun, r.scheme); err != nil {
build.Status.Reason = buildv1alpha1.BuildReasonPtr(buildv1alpha1.SetOwnerReferenceFailed)
build.Status.Message = pointer.String(fmt.Sprintf("unexpected error when trying to set the ownerreference: %v", err))
if err := r.client.Status().Update(ctx, build); err != nil {
return reconcile.Result{}, err
// Set OwnerReference for Build and BuildRun only when build retention AtBuildDeletion is set to "true"
if build.Spec.Retention != nil && build.Spec.Retention.AtBuildDeletion != nil {
if *build.Spec.Retention.AtBuildDeletion && !resources.IsOwnedByBuild(build, buildRun.OwnerReferences) {
if err := r.setOwnerReferenceFunc(build, buildRun, r.scheme); err != nil {
build.Status.Reason = buildv1beta1.BuildReasonPtr(buildv1beta1.SetOwnerReferenceFailed)
build.Status.Message = pointer.String(fmt.Sprintf("unexpected error when trying to set the ownerreference: %v", err))
if err := r.client.Status().Update(ctx, build); err != nil {
return reconcile.Result{}, err
}
}
ctxlog.Info(ctx, fmt.Sprintf("updating BuildRun %s OwnerReferences, owner is Build %s", buildRun.Name, build.Name), namespace, request.Namespace, name, request.Name)
updateBuildRunRequired = true
}
ctxlog.Info(ctx, fmt.Sprintf("updating BuildRun %s OwnerReferences, owner is Build %s", buildRun.Name, build.Name), namespace, request.Namespace, name, request.Name)
updateBuildRunRequired = true
}

// Add missing build name and generation labels to BuildRun (unless it is an embedded build)
if build.Name != "" && build.Generation != 0 {
buildGeneration := strconv.FormatInt(build.Generation, 10)
if buildRun.GetLabels()[buildv1alpha1.LabelBuild] != build.Name || buildRun.GetLabels()[buildv1alpha1.LabelBuildGeneration] != buildGeneration {
buildRun.Labels[buildv1alpha1.LabelBuild] = build.Name
buildRun.Labels[buildv1alpha1.LabelBuildGeneration] = buildGeneration
if buildRun.GetLabels()[buildv1beta1.LabelBuild] != build.Name || buildRun.GetLabels()[buildv1beta1.LabelBuildGeneration] != buildGeneration {
buildRun.Labels[buildv1beta1.LabelBuild] = build.Name
buildRun.Labels[buildv1beta1.LabelBuildGeneration] = buildGeneration
ctxlog.Info(ctx, "updating BuildRun labels", namespace, request.Namespace, name, request.Name)
updateBuildRunRequired = true
}
Expand All @@ -257,7 +259,7 @@ func (r *ReconcileBuildRun) Reconcile(ctx context.Context, request reconcile.Req
// Choose a service account to use
svcAccount, err := resources.RetrieveServiceAccount(ctx, r.client, build, buildRun)
if err != nil {
if !resources.IsClientStatusUpdateError(err) && buildRun.Status.IsFailed(buildv1alpha1.Succeeded) {
if !resources.IsClientStatusUpdateError(err) && buildRun.Status.IsFailed(buildv1beta1.Succeeded) {
return reconcile.Result{}, nil
}
// system call failure, reconcile again
Expand All @@ -266,7 +268,7 @@ func (r *ReconcileBuildRun) Reconcile(ctx context.Context, request reconcile.Req

strategy, err := r.getReferencedStrategy(ctx, build, buildRun)
if err != nil {
if !resources.IsClientStatusUpdateError(err) && buildRun.Status.IsFailed(buildv1alpha1.Succeeded) {
if !resources.IsClientStatusUpdateError(err) && buildRun.Status.IsFailed(buildv1beta1.Succeeded) {
return reconcile.Result{}, nil
}
return reconcile.Result{}, err
Expand All @@ -293,7 +295,7 @@ func (r *ReconcileBuildRun) Reconcile(ctx context.Context, request reconcile.Req
// Create the TaskRun, this needs to be the last step in this block to be idempotent
generatedTaskRun, err := r.createTaskRun(ctx, svcAccount, strategy, build, buildRun)
if err != nil {
if !resources.IsClientStatusUpdateError(err) && buildRun.Status.IsFailed(buildv1alpha1.Succeeded) {
if !resources.IsClientStatusUpdateError(err) && buildRun.Status.IsFailed(buildv1beta1.Succeeded) {
ctxlog.Info(ctx, "taskRun generation failed", namespace, request.Namespace, name, request.Name)
return reconcile.Result{}, nil
}
Expand All @@ -305,7 +307,7 @@ func (r *ReconcileBuildRun) Reconcile(ctx context.Context, request reconcile.Req
// if resource is not found, fais the build run
if err != nil {
if apierrors.IsNotFound(err) {
if err := resources.UpdateConditionWithFalseStatus(ctx, r.client, buildRun, err.Error(), string(buildv1alpha1.VolumeDoesNotExist)); err != nil {
if err := resources.UpdateConditionWithFalseStatus(ctx, r.client, buildRun, err.Error(), string(buildv1beta1.VolumeDoesNotExist)); err != nil {
return reconcile.Result{}, err
}

Expand All @@ -324,7 +326,7 @@ func (r *ReconcileBuildRun) Reconcile(ctx context.Context, request reconcile.Req
}

// Set the LastTaskRunRef in the BuildRun status
buildRun.Status.LatestTaskRunRef = &generatedTaskRun.Name
buildRun.Status.TaskRunName = &generatedTaskRun.Name
ctxlog.Info(ctx, "updating BuildRun status with TaskRun name", namespace, request.Namespace, name, request.Name, "TaskRun", generatedTaskRun.Name)
if err = r.client.Status().Update(ctx, buildRun); err != nil {
// we ignore the error here to prevent another reconciliation that would create another TaskRun,
Expand Down Expand Up @@ -359,7 +361,7 @@ func (r *ReconcileBuildRun) Reconcile(ctx context.Context, request reconcile.Req
return reconcile.Result{}, getBuildRunErr
} else if apierrors.IsNotFound(getBuildRunErr) {
// this is a TR event, try getting the br from the label on the tr
err := r.GetBuildRunObject(ctx, lastTaskRun.Labels[buildv1alpha1.LabelBuildRun], request.Namespace, buildRun)
err := r.GetBuildRunObject(ctx, lastTaskRun.Labels[buildv1beta1.LabelBuildRun], request.Namespace, buildRun)
if err != nil && !apierrors.IsNotFound(err) {
return reconcile.Result{}, err
}
Expand Down Expand Up @@ -408,7 +410,7 @@ func (r *ReconcileBuildRun) Reconcile(ctx context.Context, request reconcile.Req
}
}

buildRun.Status.LatestTaskRunRef = &lastTaskRun.Name
buildRun.Status.TaskRunName = &lastTaskRun.Name

if buildRun.Status.StartTime == nil && lastTaskRun.Status.StartTime != nil {
buildRun.Status.StartTime = lastTaskRun.Status.StartTime
Expand Down Expand Up @@ -479,13 +481,13 @@ func (r *ReconcileBuildRun) Reconcile(ctx context.Context, request reconcile.Req
}

// GetBuildRunObject retrieves an existing BuildRun based on a name and namespace
func (r *ReconcileBuildRun) GetBuildRunObject(ctx context.Context, objectName string, objectNS string, buildRun *buildv1alpha1.BuildRun) error {
func (r *ReconcileBuildRun) GetBuildRunObject(ctx context.Context, objectName string, objectNS string, buildRun *buildv1beta1.BuildRun) error {
return r.client.Get(ctx, types.NamespacedName{Name: objectName, Namespace: objectNS}, buildRun)
}

// VerifyRequestName parse a Reconcile request name and looks for an associated BuildRun name
// If the BuildRun object exists and is not yet completed, it will update it with an error.
func (r *ReconcileBuildRun) VerifyRequestName(ctx context.Context, request reconcile.Request, buildRun *buildv1alpha1.BuildRun) {
func (r *ReconcileBuildRun) VerifyRequestName(ctx context.Context, request reconcile.Request, buildRun *buildv1beta1.BuildRun) {

regxBuildRun, _ := regexp.Compile(generatedNameRegex)

Expand All @@ -506,7 +508,7 @@ func (r *ReconcileBuildRun) VerifyRequestName(ctx context.Context, request recon
}
}

func (r *ReconcileBuildRun) getReferencedStrategy(ctx context.Context, build *buildv1alpha1.Build, buildRun *buildv1alpha1.BuildRun) (strategy buildv1alpha1.BuilderStrategy, err error) {
func (r *ReconcileBuildRun) getReferencedStrategy(ctx context.Context, build *buildv1beta1.Build, buildRun *buildv1beta1.BuildRun) (strategy buildv1beta1.BuilderStrategy, err error) {
if build.Spec.Strategy.Kind == nil {
// If the strategy Kind is not specified, we default to a namespaced-scope strategy
ctxlog.Info(ctx, "missing strategy Kind, defaulting to a namespaced-scope one", buildRun.Name, build.Name, namespace)
Expand All @@ -522,7 +524,7 @@ func (r *ReconcileBuildRun) getReferencedStrategy(ctx context.Context, build *bu
}

switch *build.Spec.Strategy.Kind {
case buildv1alpha1.NamespacedBuildStrategyKind:
case buildv1beta1.NamespacedBuildStrategyKind:
strategy, err = resources.RetrieveBuildStrategy(ctx, r.client, build)
if err != nil {
if apierrors.IsNotFound(err) {
Expand All @@ -531,7 +533,7 @@ func (r *ReconcileBuildRun) getReferencedStrategy(ctx context.Context, build *bu
}
}
}
case buildv1alpha1.ClusterBuildStrategyKind:
case buildv1beta1.ClusterBuildStrategyKind:
strategy, err = resources.RetrieveClusterBuildStrategy(ctx, r.client, build)
if err != nil {
if apierrors.IsNotFound(err) {
Expand All @@ -550,7 +552,7 @@ 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) (*pipelineapi.TaskRun, error) {
func (r *ReconcileBuildRun) createTaskRun(ctx context.Context, serviceAccount *corev1.ServiceAccount, strategy buildv1beta1.BuilderStrategy, build *buildv1beta1.Build, buildRun *buildv1beta1.BuildRun) (*pipelineapi.TaskRun, error) {
var (
generatedTaskRun *pipelineapi.TaskRun
)
Expand Down
Loading

0 comments on commit 6a770c9

Please sign in to comment.