Skip to content

Commit

Permalink
feat: Add raw disk build generation to SeedImage
Browse files Browse the repository at this point in the history
If SeedImageSpec.Type is set to 'raw' we now try to run elemental
build-disk to generate the disk image.

Signed-off-by: Fredrik Lönnegren <[email protected]>
  • Loading branch information
frelon committed Oct 30, 2023
1 parent eb80053 commit 8f913d0
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 90 deletions.
11 changes: 11 additions & 0 deletions charts/crds/templates/crds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2642,12 +2642,23 @@ spec:
anyOf:
- type: integer
- type: string
default: 6442450944
description: Size specifies the size of the volume used to store the
image. Defaults to 6Gi
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
type:
default: iso
description: Type specifies the type of seed image to built. Valid
values are iso|raw Defaults to "iso"
enum:
- iso
- raw
type: string
required:
- registrationRef
- size
- type
type: object
status:
properties:
Expand Down
171 changes: 81 additions & 90 deletions controllers/seedimage_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ func (r *SeedImageReconciler) reconcileConfigMapObject(ctx context.Context, seed
return fmt.Errorf("failed marshalling cloud-config: %w", err)
}

isoName := fmt.Sprintf("elemental-%s-%s.iso", seedImg.Spec.MachineRegistrationRef.Name, time.Now().Format(time.RFC3339))
outputName := fmt.Sprintf("elemental-%s-%s.%s", seedImg.Spec.MachineRegistrationRef.Name, time.Now().Format(time.RFC3339), seedImg.Spec.Type)

if err := r.Get(ctx, types.NamespacedName{
Name: seedImg.Name,
Expand All @@ -324,7 +324,7 @@ func (r *SeedImageReconciler) reconcileConfigMapObject(ctx context.Context, seed
}
logger.V(5).Info("ConfigMap is out of date", "configmap", podConfigMap.Namespace+"/"+podConfigMap.Name)

isoName = podConfigMap.Data[configMapKeyOutputName]
outputName = podConfigMap.Data[configMapKeyOutputName]

// ...but values are not up-to-date
if err := r.Delete(ctx, podConfigMap); err != nil {
Expand All @@ -344,7 +344,7 @@ func (r *SeedImageReconciler) reconcileConfigMapObject(ctx context.Context, seed
configMapKeyRegistrationURL: mRegistration.Status.RegistrationURL,
configMapKeyDevice: mRegistration.Spec.Config.Elemental.Install.Device,
configMapKeyBaseImage: seedImg.Spec.BaseImage,
configMapKeyOutputName: isoName,
configMapKeyOutputName: outputName,
}
conf := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -625,6 +625,38 @@ func fillBuildImagePod(seedImg *elementalv1.SeedImage, buildImg string, pullPoli
}

func defaultInitContainers(seedImg *elementalv1.SeedImage, buildImg string, pullPolicy corev1.PullPolicy) []corev1.Container {
if seedImg.Spec.Type == elementalv1.TypeRaw {
return defaultRawInitContainers(seedImg, buildImg, pullPolicy)
}

return defaultIsoInitContainers(seedImg, buildImg, pullPolicy)
}

func defaultRawInitContainers(seedImg *elementalv1.SeedImage, buildImg string, pullPolicy corev1.PullPolicy) []corev1.Container {
buildCommands := []string{"/usr/bin/elemental --debug build-disk --unprivileged --expandable --squash-no-compression -n elemental -o /iso $(ELEMENTAL_BASE_IMAGE)", "mv /iso/elemental.raw /iso/$(ELEMENTAL_OUTPUT_NAME)"}

return []corev1.Container{
{
Name: "build",
Image: buildImg,
ImagePullPolicy: pullPolicy,
Command: []string{"/bin/bash", "-c"},
Args: []string{strings.Join(buildCommands, " && ")},
VolumeMounts: []corev1.VolumeMount{
{
Name: "iso-storage",
MountPath: "/iso",
}, {
Name: "config-map",
MountPath: "/overlay",
},
},
Env: defaultEnvVars(seedImg.Name),
},
}
}

func defaultIsoInitContainers(seedImg *elementalv1.SeedImage, buildImg string, pullPolicy corev1.PullPolicy) []corev1.Container {
const baseIsoPath = "/iso/base.iso"

containers := []corev1.Container{}
Expand Down Expand Up @@ -670,52 +702,7 @@ func defaultInitContainers(seedImg *elementalv1.SeedImage, buildImg string, pull
MountPath: "/overlay",
},
},
Env: []corev1.EnvVar{
{
Name: "ELEMENTAL_DEVICE",
ValueFrom: &corev1.EnvVarSource{
ConfigMapKeyRef: &corev1.ConfigMapKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: seedImg.Name,
},
Key: configMapKeyDevice,
},
},
},
{
Name: "ELEMENTAL_REGISTRATION_URL",
ValueFrom: &corev1.EnvVarSource{
ConfigMapKeyRef: &corev1.ConfigMapKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: seedImg.Name,
},
Key: configMapKeyRegistrationURL,
},
},
},
{
Name: "ELEMENTAL_BASE_IMAGE",
ValueFrom: &corev1.EnvVarSource{
ConfigMapKeyRef: &corev1.ConfigMapKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: seedImg.Name,
},
Key: configMapKeyBaseImage,
},
},
},
{
Name: "ELEMENTAL_OUTPUT_NAME",
ValueFrom: &corev1.EnvVarSource{
ConfigMapKeyRef: &corev1.ConfigMapKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: seedImg.Name,
},
Key: configMapKeyOutputName,
},
},
},
},
Env: defaultEnvVars(seedImg.Name),
},
)

Expand Down Expand Up @@ -756,55 +743,59 @@ func userDefinedInitContainers(seedImg *elementalv1.SeedImage) []corev1.Containe
MountPath: "/overlay",
},
},
Env: []corev1.EnvVar{
{
Name: "ELEMENTAL_DEVICE",
ValueFrom: &corev1.EnvVarSource{
ConfigMapKeyRef: &corev1.ConfigMapKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: seedImg.Name,
},
Key: configMapKeyDevice,
},
},
Env: defaultEnvVars(seedImg.Name),
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceEphemeralStorage: seedImg.Spec.Size,
},
{
Name: "ELEMENTAL_REGISTRATION_URL",
ValueFrom: &corev1.EnvVarSource{
ConfigMapKeyRef: &corev1.ConfigMapKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: seedImg.Name,
},
Key: configMapKeyRegistrationURL,
},
},
},
}
}

func defaultEnvVars(seedImgName string) []corev1.EnvVar {
return []corev1.EnvVar{
{
Name: "ELEMENTAL_DEVICE",
ValueFrom: &corev1.EnvVarSource{
ConfigMapKeyRef: &corev1.ConfigMapKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: seedImgName,
},
Key: configMapKeyDevice,
},
{
Name: "ELEMENTAL_BASE_IMAGE",
ValueFrom: &corev1.EnvVarSource{
ConfigMapKeyRef: &corev1.ConfigMapKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: seedImg.Name,
},
Key: configMapKeyBaseImage,
},
},
},
{
Name: "ELEMENTAL_REGISTRATION_URL",
ValueFrom: &corev1.EnvVarSource{
ConfigMapKeyRef: &corev1.ConfigMapKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: seedImgName,
},
Key: configMapKeyRegistrationURL,
},
{
Name: "ELEMENTAL_OUTPUT_NAME",
ValueFrom: &corev1.EnvVarSource{
ConfigMapKeyRef: &corev1.ConfigMapKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: seedImg.Name,
},
Key: configMapKeyOutputName,
},
},
},
{
Name: "ELEMENTAL_BASE_IMAGE",
ValueFrom: &corev1.EnvVarSource{
ConfigMapKeyRef: &corev1.ConfigMapKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: seedImgName,
},
Key: configMapKeyBaseImage,
},
},
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceEphemeralStorage: seedImg.Spec.Size,
},
{
Name: "ELEMENTAL_OUTPUT_NAME",
ValueFrom: &corev1.EnvVarSource{
ConfigMapKeyRef: &corev1.ConfigMapKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: seedImgName,
},
Key: configMapKeyOutputName,
},
},
},
Expand Down

0 comments on commit 8f913d0

Please sign in to comment.