diff --git a/api/v1alpha1/osartifact_types.go b/api/v1alpha1/osartifact_types.go index 41b200b..e15fdb0 100644 --- a/api/v1alpha1/osartifact_types.go +++ b/api/v1alpha1/osartifact_types.go @@ -30,7 +30,12 @@ type OSArtifactSpec struct { // Foo is an example field of OSArtifact. Edit osartifact_types.go to remove/update ImageName string `json:"imageName,omitempty"` - ISO bool `json:"iso,omitempty"` + // This needs to be revisited + 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) CloudConfig string `json:"cloudConfig,omitempty"` GRUBConfig string `json:"grubConfig,omitempty"` diff --git a/controllers/deployment.go b/controllers/deployment.go index 264260e..f80877f 100644 --- a/controllers/deployment.go +++ b/controllers/deployment.go @@ -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 != "" { volumeMounts = append(volumeMounts, v1.VolumeMount{ Name: "config", MountPath: "/iso/iso-overlay/cloud_config.yaml", SubPath: "config", }) + + cloudImgCmd += " /iso/iso-overlay/cloud_config.yaml" } if artifact.Spec.CloudConfig != "" || artifact.Spec.GRUBConfig != "" { @@ -167,6 +174,48 @@ func (r *OSArtifactReconciler) genDeployment(artifact buildv1alpha1.OSArtifact) 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{ ImagePullPolicy: v1.PullAlways, SecurityContext: &v1.SecurityContext{Privileged: &privileged}, @@ -212,11 +261,24 @@ func (r *OSArtifactReconciler) genDeployment(artifact buildv1alpha1.OSArtifact) } - pod.InitContainers = append(pod.InitContainers, buildIsoContainer) + if artifact.Spec.ISO { + 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 { pod.InitContainers = append(pod.InitContainers, createImageContainer(r.ToolImage, artifact.Spec.PushOptions)) - } pod.Containers = []v1.Container{servingContainer} diff --git a/tools-image/azure.sh b/tools-image/azure.sh index d6cf273..819f6f8 100755 --- a/tools-image/azure.sh +++ b/tools-image/azure.sh @@ -3,13 +3,15 @@ # Transform a raw image disk to azure vhd RAWIMAGE="$1" VHDDISK="${2:-disk.vhd}" +cp -rf $RAWIMAGE $VHDDISK.work MB=$((1024*1024)) size=$(qemu-img info -f raw --output json "$RAWIMAGE" | gawk 'match($0, /"virtual-size": ([0-9]+),/, val) {print val[1]}') # shellcheck disable=SC2004 ROUNDED_SIZE=$(((($size+$MB-1)/$MB)*$MB)) 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" -qemu-img convert -f raw -o subformat=fixed,force_size -O vpc "$RAWIMAGE" "$VHDDISK" -echo "Done" \ No newline at end of file +qemu-img convert -f raw -o subformat=fixed,force_size -O vpc "$VHDDISK.work" "$VHDDISK" +echo "Done" +rm -rf "$VHDDISK.work" \ No newline at end of file diff --git a/tools-image/gce.sh b/tools-image/gce.sh index a3386a9..d3b1a53 100755 --- a/tools-image/gce.sh +++ b/tools-image/gce.sh @@ -2,15 +2,14 @@ # Transform a raw image disk to gce compatible RAWIMAGE="$1" +OUT="${2:-$RAWIMAGE.gce.raw}" +cp -rf $RAWIMAGE $OUT 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 ROUNDED_SIZE=$(echo "$size/$GB+1"|bc) echo "Resizing raw image from \"$size\"MB to \"$ROUNDED_SIZE\"GB" -qemu-img resize -f raw "$RAWIMAGE" "$ROUNDED_SIZE"G -echo "Compressing raw image $RAWIMAGE to $RAWIMAGE.tar.gz" -tar -c -z --format=oldgnu -f "$RAWIMAGE".tar.gz $RAWIMAGE -echo "Restoring size to original raw image" -qemu-img resize -f raw "$RAWIMAGE" --shrink "$size" -echo "Done" \ No newline at end of file +qemu-img resize -f raw "$OUT" "$ROUNDED_SIZE"G +echo "Compressing raw image $OUT to $OUT.tar.gz" +tar -c -z --format=oldgnu -f "$OUT".tar.gz $OUT