Skip to content

Commit

Permalink
Moving Build Dockerfile to be defined in the ConfigMap (kubernetes-si…
Browse files Browse the repository at this point in the history
…gs#147)

Currently dockerfile is defined a multiline string in the Build
field of the KernelMapping. This PR does the following:
1) dockerfile is now defined in a dedicated configmap (created by customer)
   with the key being: dockerfile
2) Build struct now contains reference to the ConfigMap, instead of
   a string

Upstream-Commit: a3d7e28
  • Loading branch information
yevgeny-shnaidman authored Nov 28, 2022
1 parent 17c5baf commit a19b031
Show file tree
Hide file tree
Showing 15 changed files with 311 additions and 53 deletions.
3 changes: 2 additions & 1 deletion api/v1beta1/module_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ type Build struct {
// BuildArgs is an array of build variables that are provided to the image building backend.
BuildArgs []BuildArg `json:"buildArgs"`

Dockerfile string `json:"dockerfile"`
// ConfigMap that holds Dockerfile contents
DockerfileConfigMap *v1.LocalObjectReference `json:"dockerfileConfigMap"`

// +optional
// Pull contains settings determining how to pull the base images of the build process.
Expand Down
25 changes: 25 additions & 0 deletions api/v1beta1/zz_generated.deepcopy.go

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

29 changes: 23 additions & 6 deletions config/crd/bases/kmm.sigs.k8s.io_modules.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1890,8 +1890,16 @@ spec:
- value
type: object
type: array
dockerfile:
type: string
dockerfileConfigMap:
description: ConfigMap that holds Dockerfile contents
properties:
name:
description: 'Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names
TODO: Add other useful fields. apiVersion, kind,
uid?'
type: string
type: object
x-kubernetes-map-type: atomic
kanikoParams:
description: KanikoParams is used to customize the building
process of the image.
Expand Down Expand Up @@ -1947,7 +1955,7 @@ spec:
x-kubernetes-map-type: atomic
type: array
required:
- dockerfile
- dockerfileConfigMap
type: object
containerImage:
description: ContainerImage is a top-level field
Expand Down Expand Up @@ -1988,8 +1996,17 @@ spec:
- value
type: object
type: array
dockerfile:
type: string
dockerfileConfigMap:
description: ConfigMap that holds Dockerfile contents
properties:
name:
description: 'Name of the referent. More info:
https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names
TODO: Add other useful fields. apiVersion,
kind, uid?'
type: string
type: object
x-kubernetes-map-type: atomic
kanikoParams:
description: KanikoParams is used to customize the
building process of the image.
Expand Down Expand Up @@ -2049,7 +2066,7 @@ spec:
x-kubernetes-map-type: atomic
type: array
required:
- dockerfile
- dockerfileConfigMap
type: object
containerImage:
description: ContainerImage is the name of the DriverContainer
Expand Down
7 changes: 7 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ rules:
- list
- patch
- watch
- apiGroups:
- ""
resources:
- configmaps
verbs:
- get
- list
- apiGroups:
- ""
resources:
Expand Down
1 change: 1 addition & 0 deletions controllers/module_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ func NewModuleReconciler(
//+kubebuilder:rbac:groups=apps,resources=daemonsets,verbs=create;delete;get;list;patch;watch
//+kubebuilder:rbac:groups="core",resources=nodes,verbs=get;list;watch
//+kubebuilder:rbac:groups="core",resources=secrets,verbs=get;list;watch
//+kubebuilder:rbac:groups="core",resources=configmaps,verbs=get;list
//+kubebuilder:rbac:groups="core",resources=serviceaccounts,verbs=create;delete;get;list;patch;watch
//+kubebuilder:rbac:groups=security.openshift.io,resources=securitycontextconstraints,verbs=use,resourceNames=privileged
//+kubebuilder:rbac:groups="rbac.authorization.k8s.io",resources=rolebindings,verbs=create;delete;get;list;patch;watch
Expand Down
35 changes: 30 additions & 5 deletions internal/build/buildconfig/maker.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package buildconfig

import (
"context"
"fmt"
"strings"

Expand All @@ -10,10 +11,13 @@ import (
kmmbuild "github.com/rh-ecosystem-edge/kernel-module-management/internal/build"
"github.com/rh-ecosystem-edge/kernel-module-management/internal/constants"
"github.com/rh-ecosystem-edge/kernel-module-management/internal/syncronizedmap"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)

Expand All @@ -22,23 +26,25 @@ const dtkBuildArg = "DTK_AUTO"
//go:generate mockgen -source=maker.go -package=buildconfig -destination=mock_maker.go

type Maker interface {
MakeBuildTemplate(mod kmmv1beta1.Module, mapping kmmv1beta1.KernelMapping, targetKernel, containerImage string,
MakeBuildTemplate(ctx context.Context, mod kmmv1beta1.Module, mapping kmmv1beta1.KernelMapping, targetKernel, containerImage string,
pushImage bool, kernelOsDtkMapping syncronizedmap.KernelOsDtkMapping) (*buildv1.Build, error)
}

type maker struct {
client client.Client
helper kmmbuild.Helper
scheme *runtime.Scheme
}

func NewMaker(helper kmmbuild.Helper, scheme *runtime.Scheme) Maker {
func NewMaker(client client.Client, helper kmmbuild.Helper, scheme *runtime.Scheme) Maker {
return &maker{
client: client,
helper: helper,
scheme: scheme,
}
}

func (m *maker) MakeBuildTemplate(mod kmmv1beta1.Module, mapping kmmv1beta1.KernelMapping, targetKernel, containerImage string,
func (m *maker) MakeBuildTemplate(ctx context.Context, mod kmmv1beta1.Module, mapping kmmv1beta1.KernelMapping, targetKernel, containerImage string,
pushImage bool, kernelOsDtkMapping syncronizedmap.KernelOsDtkMapping) (*buildv1.Build, error) {

kmmBuild := m.helper.GetRelevantBuild(mod, mapping)
Expand All @@ -50,7 +56,12 @@ func (m *maker) MakeBuildTemplate(mod kmmv1beta1.Module, mapping kmmv1beta1.Kern
},
}

if strings.Contains(kmmBuild.Dockerfile, dtkBuildArg) {
dockerfileData, err := m.getDockerfileData(ctx, kmmBuild, mod.Namespace)
if err != nil {
return nil, fmt.Errorf("failed to get dockerfile data from configmap: %v", err)
}

if strings.Contains(dockerfileData, dtkBuildArg) {

dtkImage, err := kernelOsDtkMapping.GetImage(targetKernel)
if err != nil {
Expand All @@ -76,7 +87,7 @@ func (m *maker) MakeBuildTemplate(mod kmmv1beta1.Module, mapping kmmv1beta1.Kern
}

sourceConfig := buildv1.BuildSource{
Dockerfile: &kmmBuild.Dockerfile,
Dockerfile: &dockerfileData,
Type: buildv1.BuildSourceDockerfile,
}

Expand Down Expand Up @@ -117,6 +128,20 @@ func (m *maker) MakeBuildTemplate(mod kmmv1beta1.Module, mapping kmmv1beta1.Kern
return &bc, nil
}

func (m *maker) getDockerfileData(ctx context.Context, buildConfig *kmmv1beta1.Build, namespace string) (string, error) {
dockerfileCM := &corev1.ConfigMap{}
namespacedName := types.NamespacedName{Name: buildConfig.DockerfileConfigMap.Name, Namespace: namespace}
err := m.client.Get(ctx, namespacedName, dockerfileCM)
if err != nil {
return "", fmt.Errorf("failed to get dockerfile ConfigMap %s: %v", namespacedName, err)
}
data, ok := dockerfileCM.Data[constants.DockerfileCMKey]
if !ok {
return "", fmt.Errorf("invalid Dockerfile ConfigMap %s format, %s key is missing", namespacedName, constants.DockerfileCMKey)
}
return data, nil
}

func envVarsFromKMMBuildArgs(args []kmmv1beta1.BuildArg) []v1.EnvVar {
if args == nil {
return nil
Expand Down
Loading

0 comments on commit a19b031

Please sign in to comment.