Add logic to controller to build disks

This commit is contained in:
Ettore Di Giacinto 2022-11-03 23:42:09 +00:00
parent dc6fb2c6be
commit 4f87e2329c
4 changed files with 81 additions and 13 deletions

View File

@ -30,7 +30,12 @@ type OSArtifactSpec struct {
// Foo is an example field of OSArtifact. Edit osartifact_types.go to remove/update // Foo is an example field of OSArtifact. Edit osartifact_types.go to remove/update
ImageName string `json:"imageName,omitempty"` ImageName string `json:"imageName,omitempty"`
// This needs to be revisited
ISO bool `json:"iso,omitempty"` ISO bool `json:"iso,omitempty"`
CloudImage bool `json:"cloudImage,omitempty"`
AzureImage bool `json:"azureImage,omitempty"`
GCEImage bool `json:"gceImage,omitempty"`
// TODO: treat cloudconfig as a secret, and take a secretRef where to store it (optionally) // TODO: treat cloudconfig as a secret, and take a secretRef where to store it (optionally)
CloudConfig string `json:"cloudConfig,omitempty"` CloudConfig string `json:"cloudConfig,omitempty"`
GRUBConfig string `json:"grubConfig,omitempty"` GRUBConfig string `json:"grubConfig,omitempty"`

View File

@ -140,12 +140,19 @@ func (r *OSArtifactReconciler) genDeployment(artifact buildv1alpha1.OSArtifact)
}) })
} }
cloudImgCmd := fmt.Sprintf(
"/raw-images.sh /rootfs /public/%s.raw",
artifact.Name,
)
if artifact.Spec.CloudConfig != "" { if artifact.Spec.CloudConfig != "" {
volumeMounts = append(volumeMounts, v1.VolumeMount{ volumeMounts = append(volumeMounts, v1.VolumeMount{
Name: "config", Name: "config",
MountPath: "/iso/iso-overlay/cloud_config.yaml", MountPath: "/iso/iso-overlay/cloud_config.yaml",
SubPath: "config", SubPath: "config",
}) })
cloudImgCmd += " /iso/iso-overlay/cloud_config.yaml"
} }
if artifact.Spec.CloudConfig != "" || artifact.Spec.GRUBConfig != "" { if artifact.Spec.CloudConfig != "" || artifact.Spec.GRUBConfig != "" {
@ -167,6 +174,48 @@ func (r *OSArtifactReconciler) genDeployment(artifact buildv1alpha1.OSArtifact)
VolumeMounts: volumeMounts, VolumeMounts: volumeMounts,
} }
buildCloudImageContainer := v1.Container{
ImagePullPolicy: v1.PullAlways,
SecurityContext: &v1.SecurityContext{Privileged: &privileged},
Name: "build-cloud-image",
Image: r.ToolImage,
Command: []string{"/bin/bash", "-cxe"},
Args: []string{
cloudImgCmd,
},
VolumeMounts: volumeMounts,
}
buildAzureCloudImageContainer := v1.Container{
ImagePullPolicy: v1.PullAlways,
SecurityContext: &v1.SecurityContext{Privileged: &privileged},
Name: "build-azure-cloud-image",
Image: r.ToolImage,
Command: []string{"/bin/bash", "-cxe"},
Args: []string{
fmt.Sprintf(
"/azure.sh /public/%s.raw /public/%s-azure.raw",
artifact.Name,
),
},
VolumeMounts: volumeMounts,
}
buildGCECloudImageContainer := v1.Container{
ImagePullPolicy: v1.PullAlways,
SecurityContext: &v1.SecurityContext{Privileged: &privileged},
Name: "build-gce-cloud-image",
Image: r.ToolImage,
Command: []string{"/bin/bash", "-cxe"},
Args: []string{
fmt.Sprintf(
"/gce.sh /public/%s.raw /public/%s-azure.raw",
artifact.Name,
),
},
VolumeMounts: volumeMounts,
}
servingContainer := v1.Container{ servingContainer := v1.Container{
ImagePullPolicy: v1.PullAlways, ImagePullPolicy: v1.PullAlways,
SecurityContext: &v1.SecurityContext{Privileged: &privileged}, SecurityContext: &v1.SecurityContext{Privileged: &privileged},
@ -212,11 +261,24 @@ func (r *OSArtifactReconciler) genDeployment(artifact buildv1alpha1.OSArtifact)
} }
if artifact.Spec.ISO {
pod.InitContainers = append(pod.InitContainers, buildIsoContainer) pod.InitContainers = append(pod.InitContainers, buildIsoContainer)
}
if artifact.Spec.CloudImage || artifact.Spec.AzureImage || artifact.Spec.GCEImage {
pod.InitContainers = append(pod.InitContainers, buildCloudImageContainer)
}
if artifact.Spec.AzureImage {
pod.InitContainers = append(pod.InitContainers, buildAzureCloudImageContainer)
}
if artifact.Spec.GCEImage {
pod.InitContainers = append(pod.InitContainers, buildGCECloudImageContainer)
}
if pushImage { if pushImage {
pod.InitContainers = append(pod.InitContainers, createImageContainer(r.ToolImage, artifact.Spec.PushOptions)) pod.InitContainers = append(pod.InitContainers, createImageContainer(r.ToolImage, artifact.Spec.PushOptions))
} }
pod.Containers = []v1.Container{servingContainer} pod.Containers = []v1.Container{servingContainer}

View File

@ -3,13 +3,15 @@
# Transform a raw image disk to azure vhd # Transform a raw image disk to azure vhd
RAWIMAGE="$1" RAWIMAGE="$1"
VHDDISK="${2:-disk.vhd}" VHDDISK="${2:-disk.vhd}"
cp -rf $RAWIMAGE $VHDDISK.work
MB=$((1024*1024)) MB=$((1024*1024))
size=$(qemu-img info -f raw --output json "$RAWIMAGE" | gawk 'match($0, /"virtual-size": ([0-9]+),/, val) {print val[1]}') size=$(qemu-img info -f raw --output json "$RAWIMAGE" | gawk 'match($0, /"virtual-size": ([0-9]+),/, val) {print val[1]}')
# shellcheck disable=SC2004 # shellcheck disable=SC2004
ROUNDED_SIZE=$(((($size+$MB-1)/$MB)*$MB)) ROUNDED_SIZE=$(((($size+$MB-1)/$MB)*$MB))
echo "Resizing raw image to $ROUNDED_SIZE" echo "Resizing raw image to $ROUNDED_SIZE"
qemu-img resize -f raw "$RAWIMAGE" $ROUNDED_SIZE qemu-img resize -f raw "$VHDDISK.work" $ROUNDED_SIZE
echo "Converting $RAWIMAGE to $VHDDISK" echo "Converting $RAWIMAGE to $VHDDISK"
qemu-img convert -f raw -o subformat=fixed,force_size -O vpc "$RAWIMAGE" "$VHDDISK" qemu-img convert -f raw -o subformat=fixed,force_size -O vpc "$VHDDISK.work" "$VHDDISK"
echo "Done" echo "Done"
rm -rf "$VHDDISK.work"

View File

@ -2,15 +2,14 @@
# Transform a raw image disk to gce compatible # Transform a raw image disk to gce compatible
RAWIMAGE="$1" RAWIMAGE="$1"
OUT="${2:-$RAWIMAGE.gce.raw}"
cp -rf $RAWIMAGE $OUT
GB=$((1024*1024*1024)) GB=$((1024*1024*1024))
size=$(qemu-img info -f raw --output json "$RAWIMAGE" | gawk 'match($0, /"virtual-size": ([0-9]+),/, val) {print val[1]}') size=$(qemu-img info -f raw --output json "$OUT" | gawk 'match($0, /"virtual-size": ([0-9]+),/, val) {print val[1]}')
# shellcheck disable=SC2004 # shellcheck disable=SC2004
ROUNDED_SIZE=$(echo "$size/$GB+1"|bc) ROUNDED_SIZE=$(echo "$size/$GB+1"|bc)
echo "Resizing raw image from \"$size\"MB to \"$ROUNDED_SIZE\"GB" echo "Resizing raw image from \"$size\"MB to \"$ROUNDED_SIZE\"GB"
qemu-img resize -f raw "$RAWIMAGE" "$ROUNDED_SIZE"G qemu-img resize -f raw "$OUT" "$ROUNDED_SIZE"G
echo "Compressing raw image $RAWIMAGE to $RAWIMAGE.tar.gz" echo "Compressing raw image $OUT to $OUT.tar.gz"
tar -c -z --format=oldgnu -f "$RAWIMAGE".tar.gz $RAWIMAGE tar -c -z --format=oldgnu -f "$OUT".tar.gz $OUT
echo "Restoring size to original raw image"
qemu-img resize -f raw "$RAWIMAGE" --shrink "$size"
echo "Done"