Skip to content

Commit

Permalink
[KOGITO-9702] Fix container-builder kaniko build context to consider …
Browse files Browse the repository at this point in the history
…external resources (#231)

* [KOGITO-9702] Fix container-builder kaniko build context to consider external resources

Signed-off-by: Ricardo Zanini <[email protected]>

* Mount with subpath to avoid hidden files in the build context

Signed-off-by: Ricardo Zanini <[email protected]>

* Add SonataFlow resources to kaniko container builder

Signed-off-by: Ricardo Zanini <[email protected]>

* Enable devmode e2e testing

Signed-off-by: Ricardo Zanini <[email protected]>

* Fix OpenShift build context files permissions

Signed-off-by: Ricardo Zanini <[email protected]>

---------

Signed-off-by: Ricardo Zanini <[email protected]>
  • Loading branch information
ricardozanini authored Aug 23, 2023
1 parent 32dcd71 commit 907abe5
Show file tree
Hide file tree
Showing 22 changed files with 497 additions and 125 deletions.
6 changes: 3 additions & 3 deletions api/v1alpha08/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@ func ToCNCFWorkflow(workflowCR *SonataFlow, context context.Context) (*cncfmodel
// Clearly this will be reviewed once we support 0.9.
func warnIfSpecVersionNotSupported(workflow *cncfmodel.Workflow, context context.Context) {
// simple guard to avoid polluting user's log.
if len(workflow.Version) == 0 {
workflow.Version = metadata.SpecVersion
if len(workflow.SpecVersion) == 0 {
workflow.SpecVersion = metadata.SpecVersion
return
}
if metadata.SpecVersion != workflow.Version {
if metadata.SpecVersion != workflow.SpecVersion {
controllerruntime.LoggerFrom(context).Info("SpecVersion not supported", "Workflow SpecVersion", workflow.Version)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ data:
DEFAULT_BUILDER_RESOURCE_NAME: Dockerfile
DEFAULT_WORKFLOW_EXTENSION: .sw.json
Dockerfile: "FROM quay.io/kiegroup/kogito-swf-builder-nightly:latest AS builder\n
\ \n # Copy from build context to skeleton resources project\nCOPY * ./resources/\n\nRUN
/home/kogito/launch/build-app.sh ./resources\n \n #=============================\n
\ \n # Copy from build context to skeleton resources project\nCOPY --chmod=644
* ./resources/\n\nRUN /home/kogito/launch/build-app.sh ./resources\n \n #=============================\n
\ # Runtime Run\n #=============================\nFROM registry.access.redhat.com/ubi8/openjdk-11:latest\n\nENV
LANG='en_US.UTF-8' LANGUAGE='en_US:en'\n \n # We make four distinct layers so
if there are application changes the library layers can be re-used\nCOPY --from=builder
Expand Down
2 changes: 1 addition & 1 deletion config/manager/sonataflow_builder_dockerfile.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FROM quay.io/kiegroup/kogito-swf-builder-nightly:latest AS builder

# Copy from build context to skeleton resources project
COPY * ./resources/
COPY --chmod=644 * ./resources/

RUN /home/kogito/launch/build-app.sh ./resources

Expand Down
4 changes: 3 additions & 1 deletion container-builder/api/build_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ type ContainerBuildStatus struct {
// https://github.com/OAI/OpenAPI-Specification/issues/845
Duration string `json:"duration,omitempty"`
// reference to where the build resources are located
ResourceVolume *ContainerBuildResourceVolume `json:"resourceVolume,omitempty"`
ResourceVolumes []ContainerBuildResourceVolume `json:"resourceVolumes,omitempty"`
}

// ContainerBuildFailure represent a message specifying the reason and the time of an event failure
Expand Down Expand Up @@ -232,4 +232,6 @@ type ContainerBuildResourceVolume struct {
ReferenceName string `json:"referenceName"`
// ReferenceType type of the resource holding the reference
ReferenceType ContainerBuildResourceReferenceType `json:"referenceType"`
// DestinationDir where to mount the given volume in the build context
DestinationDir string `json:"destinationDir,omitempty"`
}
8 changes: 4 additions & 4 deletions container-builder/api/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 30 additions & 14 deletions container-builder/builder/kubernetes/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ type ContainerBuilderInfo struct {
type resource struct {
Target string
Content []byte
Path string
}

type resourceConfigMap struct {
Ref corev1.LocalObjectReference
Path string
}

type containerBuildContext struct {
Expand All @@ -54,9 +60,9 @@ type builder struct {
}

type scheduler struct {
Scheduler
builder builder
Resources []resource
builder builder
Resources []resource
ResourceConfigMaps []resourceConfigMap
}

var _ Scheduler = &scheduler{}
Expand All @@ -69,8 +75,11 @@ var schedulers = map[string]schedulerHandler{

// Scheduler provides an interface to add resources and schedule a new build
type Scheduler interface {
// WithResource the actual file/resource to add to the builder. Might be called multiple times.
// WithResource the actual file/resource to add to the build context in the relative root path. Might be called multiple times.
WithResource(target string, content []byte) Scheduler
// WithConfigMapResource the configMap to add to the build context. Might be called multiple times.
// This ConfigMap is a Kubernetes LocalObjectReference, meaning that must be within the Platform namespace.
WithConfigMapResource(configMap corev1.LocalObjectReference, path string) Scheduler
WithClient(client client.Client) Scheduler
// WithResourceRequirements Kubernetes resource requirements to be passed to the underlying builder if necessary. For example, a builder pod might require specific resources underneath.
WithResourceRequirements(res corev1.ResourceRequirements) Scheduler
Expand Down Expand Up @@ -118,35 +127,42 @@ func NewBuild(info ContainerBuilderInfo) Scheduler {

func (s *scheduler) WithClient(client client.Client) Scheduler {
s.builder.WithClient(client)
return s.Scheduler
return s
}

func (s *scheduler) WithResource(target string, content []byte) Scheduler {
s.Resources = append(s.Resources, resource{target, content})
return s.Scheduler
s.Resources = append(s.Resources, resource{target, content, ""})
return s
}

func (s *scheduler) WithConfigMapResource(configMap corev1.LocalObjectReference, path string) Scheduler {
s.ResourceConfigMaps = append(s.ResourceConfigMaps, resourceConfigMap{configMap, path})
return s
}

func (s *scheduler) WithResourceRequirements(res corev1.ResourceRequirements) Scheduler {
// no default implementation.
return s.Scheduler
return s
}

func (s *scheduler) WithAdditionalArgs(args []string) Scheduler {
// no default implementation.
return s.Scheduler
return s
}

func (s *scheduler) WithProperty(property BuilderProperty, object interface{}) Scheduler {
// no default implementation
return s.Scheduler
return s
}

// Schedule schedules a new build in the platform
func (s *scheduler) Schedule() (*api.ContainerBuild, error) {
// TODO: create a handler to mount the resources according to the platform/context options (for now we only have CM, PoC level)
if err := mountResourcesWithConfigMap(&s.builder.Context, &s.Resources); err != nil {
// TODO: create a handler to mount the resources according to the platform/context options, for now only CM
if err := mountResourcesBinaryWithConfigMapToBuild(&s.builder.Context, &s.Resources); err != nil {
return nil, err
}
// Add the CMs to the build volume
mountResourcesConfigMapToBuild(&s.builder.Context, &s.ResourceConfigMaps)
return s.builder.Reconcile()
}

Expand Down Expand Up @@ -202,6 +218,6 @@ func (b *builder) Reconcile() (*api.ContainerBuild, error) {
}

func (b *builder) CancelBuild() (*api.ContainerBuild, error) {
//TODO implement me
panic("implement me")
// TODO: do the actual implementation if that makes sense
panic("CancelBuild: Operation Not Supported")
}
4 changes: 2 additions & 2 deletions container-builder/builder/kubernetes/builder_kaniko.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"github.com/kiegroup/kogito-serverless-operator/container-builder/api"
)

var _ Scheduler = &kanikoScheduler{}

type kanikoScheduler struct {
*scheduler
KanikoTask *api.KanikoTask
Expand Down Expand Up @@ -64,8 +66,6 @@ func (k kanikoSchedulerHandler) CreateScheduler(info ContainerBuilderInfo, build
},
&kanikoTask,
}
// we hold our own reference for the default methods to return the right object
sched.Scheduler = sched
return sched
}

Expand Down
3 changes: 1 addition & 2 deletions container-builder/builder/kubernetes/builder_kaniko_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ import (
// Test that verify we are able to create a Kaniko build with cache enabled, a specific set of resources and additional flags
func TestNewBuildWithKanikoCustomizations(t *testing.T) {
ns := "test"
c, err := test.NewFakeClient()
assert.NoError(t, err)
c := test.NewFakeClient()

dockerFile, err := os.ReadFile("testdata/Dockerfile")
assert.NoError(t, err)
Expand Down
3 changes: 1 addition & 2 deletions container-builder/builder/kubernetes/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ import (

func TestNewBuild(t *testing.T) {
ns := "test"
c, err := test.NewFakeClient()
assert.NoError(t, err)
c := test.NewFakeClient()

dockerFile, err := os.ReadFile("testdata/Dockerfile")
assert.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion container-builder/builder/kubernetes/kaniko.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func addKanikoTaskToPod(ctx context.Context, c client.Client, build *api.Contain
}

// TODO: should be handled by a mount build context handler instead since we can have many possibilities
if err := addResourcesToVolume(ctx, c, task.PublishTask, build, &volumes, &volumeMounts); err != nil {
if err := addResourcesToBuilderContextVolume(ctx, c, task.PublishTask, build, &volumes, &volumeMounts); err != nil {
return err
}

Expand Down
Loading

0 comments on commit 907abe5

Please sign in to comment.