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 12, 2024
1 parent 3054897 commit 3c23b25
Show file tree
Hide file tree
Showing 56 changed files with 847 additions and 1,178 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
}
3 changes: 1 addition & 2 deletions 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 All @@ -28,7 +28,6 @@ var validationTypes = [...]string{
validate.SourceURL,
validate.Secrets,
validate.Strategies,
validate.Sources,
validate.BuildName,
validate.Envs,
validate.Triggers,
Expand Down
93 changes: 16 additions & 77 deletions pkg/reconciler/build/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import (
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"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/controller/fakes"
buildController "github.com/shipwright-io/build/pkg/reconciler/build"
test "github.com/shipwright-io/build/test/v1alpha1_samples"
test "github.com/shipwright-io/build/test/v1beta1_samples"
)

var _ = Describe("Reconcile Build", func() {
Expand Down Expand Up @@ -83,10 +83,9 @@ var _ = Describe("Reconcile Build", func() {
Describe("Reconcile", func() {
Context("when source secret is specified", func() {
It("fails when the secret does not exist", func() {
buildSample.Spec.Source.Credentials = &corev1.LocalObjectReference{
Name: "non-existing",
}
buildSample.Spec.Output.Credentials = nil
buildSample.Spec.Source.GitSource.CloneSecret = pointer.String("non-existing")

buildSample.Spec.Output.PushSecret = nil

statusCall := ctl.StubFunc(corev1.ConditionFalse, build.SpecSourceSecretRefNotFound, "referenced secret non-existing not found")
statusWriter.UpdateCalls(statusCall)
Expand All @@ -97,62 +96,8 @@ var _ = Describe("Reconcile Build", func() {
})

It("succeeds when the secret exists foobar", func() {
buildSample.Spec.Source.Credentials = &corev1.LocalObjectReference{
Name: "existing",
}
buildSample.Spec.Output.Credentials = nil

// Fake some client Get calls and ensure we populate all
// different resources we could get during reconciliation
client.GetCalls(func(_ context.Context, nn types.NamespacedName, object crc.Object, getOptions ...crc.GetOption) error {
switch object := object.(type) {
case *build.Build:
buildSample.DeepCopyInto(object)
case *build.ClusterBuildStrategy:
clusterBuildStrategySample.DeepCopyInto(object)
case *corev1.Secret:
secretSample = ctl.SecretWithoutAnnotation("existing", namespace)
secretSample.DeepCopyInto(object)
}
return nil
})

statusCall := ctl.StubFunc(corev1.ConditionTrue, build.SucceedStatus, "all validations succeeded")
statusWriter.UpdateCalls(statusCall)

result, err := reconciler.Reconcile(context.TODO(), request)
Expect(err).ToNot(HaveOccurred())
Expect(statusWriter.UpdateCallCount()).To(Equal(1))
Expect(reconcile.Result{}).To(Equal(result))
})
})

Context("when builder image secret is specified", func() {
It("fails when the secret does not exist", func() {
buildSample.Spec.Builder = &build.Image{
Image: "busybox",
Credentials: &corev1.LocalObjectReference{
Name: "non-existing",
},
}
buildSample.Spec.Output.Credentials = nil

statusCall := ctl.StubFunc(corev1.ConditionFalse, build.SpecBuilderSecretRefNotFound, "referenced secret non-existing not found")
statusWriter.UpdateCalls(statusCall)

_, err := reconciler.Reconcile(context.TODO(), request)
Expect(err).To(BeNil())
Expect(statusWriter.UpdateCallCount()).To(Equal(1))
})

It("succeeds when the secret exists", func() {
buildSample.Spec.Builder = &build.Image{
Image: "busybox",
Credentials: &corev1.LocalObjectReference{
Name: "existing",
},
}
buildSample.Spec.Output.Credentials = nil
buildSample.Spec.Source.GitSource.CloneSecret = pointer.String("existing")
buildSample.Spec.Output.PushSecret = nil

// Fake some client Get calls and ensure we populate all
// different resources we could get during reconciliation
Expand Down Expand Up @@ -217,12 +162,8 @@ var _ = Describe("Reconcile Build", func() {

Context("when source secret and output secret are specified", func() {
It("fails when both secrets do not exist", func() {
buildSample.Spec.Source.Credentials = &corev1.LocalObjectReference{
Name: "non-existing-source",
}
buildSample.Spec.Output.Credentials = &corev1.LocalObjectReference{
Name: "non-existing-output",
}
buildSample.Spec.Source.GitSource.CloneSecret = pointer.String("non-existing-source")
buildSample.Spec.Output.PushSecret = pointer.String("non-existing-output")

statusCall := ctl.StubFunc(corev1.ConditionFalse, build.MultipleSecretRefNotFound, "missing secrets are non-existing-output,non-existing-source")
statusWriter.UpdateCalls(statusCall)
Expand Down Expand Up @@ -371,7 +312,7 @@ var _ = Describe("Reconcile Build", func() {
Context("when source URL is specified", func() {
// validate file protocol
It("fails when source URL is invalid", func() {
buildSample.Spec.Source.URL = pointer.String("foobar")
buildSample.Spec.Source.GitSource.URL = "foobar"
buildSample.SetAnnotations(map[string]string{
build.AnnotationBuildVerifyRepository: "true",
})
Expand All @@ -385,7 +326,7 @@ var _ = Describe("Reconcile Build", func() {

// validate https protocol
It("fails when public source URL is unreachable", func() {
buildSample.Spec.Source.URL = pointer.String("https://github.com/shipwright-io/sample-go-fake")
buildSample.Spec.Source.GitSource.URL = "https://github.com/shipwright-io/sample-go-fake"
buildSample.SetAnnotations(map[string]string{
build.AnnotationBuildVerifyRepository: "true",
})
Expand All @@ -400,7 +341,7 @@ var _ = Describe("Reconcile Build", func() {

// skip validation because of empty sourceURL annotation
It("succeed when source URL is invalid because source annotation is empty", func() {
buildSample.Spec.Source.URL = pointer.String("foobar")
buildSample.Spec.Source.GitSource.URL = "foobar"

// Fake some client Get calls and ensure we populate all
// different resources we could get during reconciliation
Expand Down Expand Up @@ -451,8 +392,8 @@ var _ = Describe("Reconcile Build", func() {
// skip validation because build references a sourceURL secret
It("succeed when source URL is fake private URL because build reference a sourceURL secret", func() {
buildSample := ctl.BuildWithClusterBuildStrategyAndSourceSecret(buildName, namespace, buildStrategyName)
buildSample.Spec.Source.URL = pointer.String("https://github.yourco.com/org/build-fake")
buildSample.Spec.Source.Credentials.Name = registrySecret
buildSample.Spec.Source.GitSource.URL = "https://github.yourco.com/org/build-fake"
buildSample.Spec.Source.GitSource.CloneSecret = pointer.String(registrySecret)

// Fake some client Get calls and ensure we populate all
// different resources we could get during reconciliation
Expand All @@ -479,10 +420,8 @@ var _ = Describe("Reconcile Build", func() {

Context("when environment variables are specified", func() {
JustBeforeEach(func() {
buildSample.Spec.Source.Credentials = &corev1.LocalObjectReference{
Name: "existing",
}
buildSample.Spec.Output.Credentials = nil
buildSample.Spec.Source.GitSource.CloneSecret = pointer.String("existing")
buildSample.Spec.Output.PushSecret = nil

// Fake some client Get calls and ensure we populate all
// different resources we could get during reconciliation
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
Loading

0 comments on commit 3c23b25

Please sign in to comment.