Skip to content

Commit

Permalink
feat(core): Refactoring S2I BC and IS build code
Browse files Browse the repository at this point in the history
Ref #4297
  • Loading branch information
gansheer authored and squakez committed May 29, 2023
1 parent eaee49d commit bac85e1
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 64 deletions.
28 changes: 4 additions & 24 deletions pkg/builder/s2i.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,13 @@ import (
"strings"

corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer"

ctrl "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
ctrlutil "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

buildv1 "github.com/openshift/api/build/v1"
imagev1 "github.com/openshift/api/image/v1"
Expand Down Expand Up @@ -90,25 +88,16 @@ func (t *s2iTask) Do(ctx context.Context) v1.BuildStatus {
},
}

err := t.c.Delete(ctx, bc)
if err != nil && !apierrors.IsNotFound(err) {
return status.Failed(fmt.Errorf("cannot delete build config: %w", err))
}

// Set the build controller as owner reference
owner := t.getControllerReference()
if owner == nil {
// Default to the Build if no controller reference is present
owner = t.build
}

if err := ctrlutil.SetOwnerReference(owner, bc, t.c.GetScheme()); err != nil {
return status.Failed(fmt.Errorf("cannot set owner reference on BuildConfig: %s: %w", bc.Name, err))
}

err = t.c.Create(ctx, bc)
err := s2i.BuildConfig(ctx, t.c, bc, owner)
if err != nil {
return status.Failed(fmt.Errorf("cannot create build config: %w", err))
return status.Failed(err)
}

is := &imagev1.ImageStream{
Expand All @@ -128,18 +117,9 @@ func (t *s2iTask) Do(ctx context.Context) v1.BuildStatus {
},
}

err = t.c.Delete(ctx, is)
if err != nil && !apierrors.IsNotFound(err) {
return status.Failed(fmt.Errorf("cannot delete image stream: %w", err))
}

if err := ctrlutil.SetOwnerReference(owner, is, t.c.GetScheme()); err != nil {
return status.Failed(fmt.Errorf("cannot set owner reference on ImageStream: %s: %w", is.Name, err))
}

err = t.c.Create(ctx, is)
err = s2i.ImageStream(ctx, t.c, is, owner)
if err != nil {
return status.Failed(fmt.Errorf("cannot create image stream: %w", err))
return status.Failed(err)
}

err = util.WithTempDir(t.build.Name+"-s2i-", func(tmpDir string) error {
Expand Down
58 changes: 18 additions & 40 deletions pkg/controller/catalog/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,20 @@ import (
"github.com/apache/camel-k/v2/pkg/util"
"github.com/apache/camel-k/v2/pkg/util/kubernetes"
"github.com/apache/camel-k/v2/pkg/util/s2i"

spectrum "github.com/container-tools/spectrum/pkg/builder"
gcrv1 "github.com/google/go-containerregistry/pkg/v1"

buildv1 "github.com/openshift/api/build/v1"
imagev1 "github.com/openshift/api/image/v1"

corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer"

ctrl "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
)
Expand Down Expand Up @@ -175,6 +180,8 @@ func initializeS2i(ctx context.Context, c client.Client, ip *v1.IntegrationPlatf
ADD /usr/share/maven/mvnw/ /usr/share/maven/mvnw/
`))

owner := catalogReference(catalog)

// BuildConfig
bc := &buildv1.BuildConfig{
TypeMeta: metav1.TypeMeta{
Expand All @@ -192,14 +199,6 @@ func initializeS2i(ctx context.Context, c client.Client, ip *v1.IntegrationPlatf
"camel.apache.org/runtime.version": catalog.Spec.Runtime.Version,
"camel.apache.org/runtime.provider": string(catalog.Spec.Runtime.Provider),
},
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: catalog.APIVersion,
Kind: catalog.Kind,
Name: catalog.Name,
UID: catalog.UID,
},
},
},
Spec: buildv1.BuildConfigSpec{
CommonSpec: buildv1.CommonSpec{
Expand Down Expand Up @@ -236,14 +235,6 @@ func initializeS2i(ctx context.Context, c client.Client, ip *v1.IntegrationPlatf
kubernetes.CamelCreatorLabelVersion: catalog.ResourceVersion,
"camel.apache.org/runtime.provider": string(catalog.Spec.Runtime.Provider),
},
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: catalog.APIVersion,
Kind: catalog.Kind,
Name: catalog.Name,
UID: catalog.UID,
},
},
},
Spec: imagev1.ImageStreamSpec{
LookupPolicy: imagev1.ImageLookupPolicy{
Expand All @@ -264,18 +255,7 @@ func initializeS2i(ctx context.Context, c client.Client, ip *v1.IntegrationPlatf
return target, nil
}

err := c.Delete(ctx, bc)
if err != nil && !k8serrors.IsNotFound(err) {
target.Status.Phase = v1.CamelCatalogPhaseError
target.Status.SetErrorCondition(
v1.CamelCatalogConditionReady,
"Builder Image",
err,
)
return target, err
}

err = c.Create(ctx, bc)
err := s2i.BuildConfig(ctx, c, bc, owner)
if err != nil {
target.Status.Phase = v1.CamelCatalogPhaseError
target.Status.SetErrorCondition(
Expand All @@ -286,18 +266,7 @@ func initializeS2i(ctx context.Context, c client.Client, ip *v1.IntegrationPlatf
return target, err
}

err = c.Delete(ctx, is)
if err != nil && !k8serrors.IsNotFound(err) {
target.Status.Phase = v1.CamelCatalogPhaseError
target.Status.SetErrorCondition(
v1.CamelCatalogConditionReady,
"Builder Image",
err,
)
return target, err
}

err = c.Create(ctx, is)
err = s2i.ImageStream(ctx, c, is, owner)
if err != nil {
target.Status.Phase = v1.CamelCatalogPhaseError
target.Status.SetErrorCondition(
Expand Down Expand Up @@ -569,3 +538,12 @@ func tarEntries(writer io.Writer, files ...string) error {
}
return nil
}

func catalogReference(catalog *v1.CamelCatalog) *unstructured.Unstructured {
owner := &unstructured.Unstructured{}
owner.SetName(catalog.Name)
owner.SetUID(catalog.UID)
owner.SetAPIVersion(catalog.APIVersion)
owner.SetKind(catalog.Kind)
return owner
}
38 changes: 38 additions & 0 deletions pkg/util/s2i/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,18 @@ package s2i
import (
"context"
"errors"
"fmt"
"time"

"github.com/apache/camel-k/v2/pkg/client"
buildv1 "github.com/openshift/api/build/v1"
imagev1 "github.com/openshift/api/image/v1"

apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

ctrl "sigs.k8s.io/controller-runtime/pkg/client"
ctrlutil "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)

// Cancel the s2i Build by updating its status.
Expand Down Expand Up @@ -68,3 +74,35 @@ func WaitForS2iBuildCompletion(ctx context.Context, c client.Client, build *buil
}
}
}

// Create the BuildConfig of the build with the right owner after having deleted it it already existed.
func BuildConfig(ctx context.Context, c client.Client, bc *buildv1.BuildConfig, owner metav1.Object) error {
if err := c.Delete(ctx, bc); err != nil && !apierrors.IsNotFound(err) {
return fmt.Errorf("cannot delete build config: %w", err)
}

if err := ctrlutil.SetOwnerReference(owner, bc, c.GetScheme()); err != nil {
return fmt.Errorf("cannot set owner reference on BuildConfig: %s: %w", bc.Name, err)
}

if err := c.Create(ctx, bc); err != nil {
return fmt.Errorf("cannot create build config: %w", err)
}
return nil
}

// Create the ImageStream for the builded image with the right owner after having deleted it it already existed.
func ImageStream(ctx context.Context, c client.Client, is *imagev1.ImageStream, owner metav1.Object) error {
if err := c.Delete(ctx, is); err != nil && !apierrors.IsNotFound(err) {
return fmt.Errorf("cannot delete image stream: %w", err)
}

if err := ctrlutil.SetOwnerReference(owner, is, c.GetScheme()); err != nil {
return fmt.Errorf("cannot set owner reference on ImageStream: %s: %w", is.Name, err)
}

if err := c.Create(ctx, is); err != nil {
return fmt.Errorf("cannot create image stream: %w", err)
}
return nil
}

0 comments on commit bac85e1

Please sign in to comment.