Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SeedImage.status.checksumURL #827

Merged
merged 1 commit into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .obs/chartfile/crds/templates/crds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3815,6 +3815,10 @@ spec:
type: object
status:
properties:
checksumURL:
description: ChecksumURL the URL from which the SeedImage checksum
can be downloaded once the image is built.
type: string
conditions:
description: Conditions describe the state of the machine registration
object.
Expand Down
3 changes: 3 additions & 0 deletions api/v1beta1/seedimage_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ type SeedImageStatus struct {
// DownloadURL the URL from which the SeedImage can be downloaded once built.
// +optional
DownloadURL string `json:"downloadURL,omitempty"`
// ChecksumURL the URL from which the SeedImage checksum can be downloaded once the image is built.
// +optional
ChecksumURL string `json:"checksumURL,omitempty"`
// State reflect the state of the seed image build process.
// +kubebuilder:validation:Enum=Initialized;Started;Completed;Failed;NotStarted
// +optional
Expand Down
4 changes: 4 additions & 0 deletions config/crd/bases/elemental.cattle.io_seedimages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ spec:
type: object
status:
properties:
checksumURL:
description: ChecksumURL the URL from which the SeedImage checksum
can be downloaded once the image is built.
type: string
conditions:
description: Conditions describe the state of the machine registration
object.
Expand Down
29 changes: 21 additions & 8 deletions controllers/seedimage_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ func (r *SeedImageReconciler) reconcileConfigMapObject(ctx context.Context, seed
return fmt.Errorf("failed marshalling cloud-config: %w", err)
}

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

if err := r.Get(ctx, types.NamespacedName{
Name: seedImg.Name,
Expand Down Expand Up @@ -448,13 +448,21 @@ func (r *SeedImageReconciler) updateStatusFromPod(ctx context.Context, seedImg *
return errMsg
}

// Use the registration name or else default to "elemental" for the image file name
imageName := "elemental"
if seedImg.Spec.MachineRegistrationRef != nil && len(seedImg.Spec.MachineRegistrationRef.Name) > 0 {
imageName = seedImg.Spec.MachineRegistrationRef.Name
podConfigMap := &corev1.ConfigMap{}
if err := r.Get(ctx, types.NamespacedName{
Name: seedImg.Name,
Namespace: seedImg.Namespace,
}, podConfigMap); err != nil {
return fmt.Errorf("getting ConfigMap '%s': %w", seedImg.Name, err)
}
outputName, found := podConfigMap.Data[configMapKeyOutputName]
if !found {
return fmt.Errorf("Could not find '%s' value in ConfigMap '%s'", configMapKeyOutputName, seedImg.Name)
}

seedImg.Status.DownloadToken = token
seedImg.Status.DownloadURL = fmt.Sprintf("https://%s/elemental/seedimage/%s/%s.%s", rancherURL, token, imageName, seedImg.Spec.Type)
seedImg.Status.DownloadURL = fmt.Sprintf("https://%s/elemental/seedimage/%s/%s", rancherURL, token, outputName)
seedImg.Status.ChecksumURL = fmt.Sprintf("%s.sha256", seedImg.Status.DownloadURL)
meta.SetStatusCondition(&seedImg.Status.Conditions, metav1.Condition{
Type: elementalv1.SeedImageConditionReady,
Status: metav1.ConditionTrue,
Expand Down Expand Up @@ -482,6 +490,7 @@ func (r *SeedImageReconciler) updateStatusFromPod(ctx context.Context, seedImg *
return errMsg
}
seedImg.Status.DownloadURL = ""
seedImg.Status.ChecksumURL = ""
meta.SetStatusCondition(&seedImg.Status.Conditions, metav1.Condition{
Type: elementalv1.SeedImageConditionReady,
Status: metav1.ConditionTrue,
Expand Down Expand Up @@ -519,6 +528,7 @@ func (r *SeedImageReconciler) deleteChildResources(ctx context.Context, seedImg
}
}
seedImg.Status.DownloadURL = ""
seedImg.Status.ChecksumURL = ""

foundSvc := &corev1.Service{}
svcName := seedImg.Name
Expand Down Expand Up @@ -593,14 +603,13 @@ func fillBuildImagePod(seedImg *elementalv1.SeedImage, buildImg string, pullPoli
ContainerPort: 80,
},
},
Args: []string{"-d", "$(ELEMENTAL_OUTPUT_NAME)", "-t", fmt.Sprintf("%d", deadline*60)},
Args: []string{"-d", "/srv", "-t", fmt.Sprintf("%d", deadline*60)},
VolumeMounts: []corev1.VolumeMount{
{
Name: "iso-storage",
MountPath: "/srv",
},
},
WorkingDir: "/srv",
Env: []corev1.EnvVar{
{
Name: "ELEMENTAL_OUTPUT_NAME",
Expand Down Expand Up @@ -672,6 +681,8 @@ func defaultRawInitContainers(seedImg *elementalv1.SeedImage, buildImg string, p
--system $(ELEMENTAL_BASE_IMAGE)`, platformArg),

"mv /iso/elemental.raw /iso/$(ELEMENTAL_OUTPUT_NAME)",

"cd /iso && sha256sum $(ELEMENTAL_OUTPUT_NAME) > $(ELEMENTAL_OUTPUT_NAME).sha256 && cd ../",
}

return []corev1.Container{
Expand Down Expand Up @@ -735,6 +746,8 @@ func defaultIsoInitContainers(seedImg *elementalv1.SeedImage, buildImg string, p
)
}

buildCommands = append(buildCommands, "cd /iso && sha256sum $(ELEMENTAL_OUTPUT_NAME) > $(ELEMENTAL_OUTPUT_NAME).sha256 && cd ../")

containers = append(
containers, corev1.Container{
Name: "build",
Expand Down
5 changes: 3 additions & 2 deletions pkg/server/api_seedimage.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ func (i *InventoryServer) apiSeedImage(resp http.ResponseWriter, req *http.Reque
var seedImg *elementalv1.SeedImage

// expected splittedPath = {"seedimage", {token}, {imgName.imgType}}
if len(splittedPath) < 2 {
if len(splittedPath) < 3 {
err = fmt.Errorf("unexpected path: %v", splittedPath)
http.Error(resp, err.Error(), http.StatusNotFound)
return err
}
token := splittedPath[1]
fileName := splittedPath[2]

if seedImg, err = i.getSeedImage(token); err != nil {
http.Error(resp, err.Error(), http.StatusNotFound)
Expand All @@ -54,7 +55,7 @@ func (i *InventoryServer) apiSeedImage(resp http.ResponseWriter, req *http.Reque
return errMsg
}

rawURL := fmt.Sprintf("http://%s", svc.Spec.ClusterIP)
rawURL := fmt.Sprintf("http://%s/%s", svc.Spec.ClusterIP, fileName)
seedImgURL, err := url.Parse(rawURL)
if err != nil {
errMsg := fmt.Errorf("failed to parse url '%s'", rawURL)
Expand Down
4 changes: 2 additions & 2 deletions pkg/server/api_seedimage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ func TestApiSeedImage(t *testing.T) {
t.Logf("Testing token %s\n", test.token)
req := httptest.NewRequest(
"GET",
fmt.Sprintf("%s/%s", "http://localhost/elemental/seedimage/", test.token),
fmt.Sprintf("%s/%s/foo.bar", "http://localhost/elemental/seedimage/", test.token),
nil)
resp := httptest.NewRecorder()
splittedPath := test.splittedPath
if splittedPath == nil {
splittedPath = []string{"seedimage", test.token}
splittedPath = []string{"seedimage", test.token, "foo.bar"}
}

err := server.apiSeedImage(resp, req, splittedPath)
Expand Down