Skip to content

Commit 61d6518

Browse files
authored
Add SeedImage.status.checksumURL (#827) (#828)
Signed-off-by: Andrea Mazzotti <[email protected]>
1 parent c6411a4 commit 61d6518

File tree

6 files changed

+37
-12
lines changed

6 files changed

+37
-12
lines changed

.obs/chartfile/crds/templates/crds.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3657,6 +3657,10 @@ spec:
36573657
type: object
36583658
status:
36593659
properties:
3660+
checksumURL:
3661+
description: ChecksumURL the URL from which the SeedImage checksum
3662+
can be downloaded once the image is built.
3663+
type: string
36603664
conditions:
36613665
description: Conditions describe the state of the machine registration
36623666
object.

api/v1beta1/seedimage_type.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ type SeedImageStatus struct {
114114
// DownloadURL the URL from which the SeedImage can be downloaded once built.
115115
// +optional
116116
DownloadURL string `json:"downloadURL,omitempty"`
117+
// ChecksumURL the URL from which the SeedImage checksum can be downloaded once the image is built.
118+
// +optional
119+
ChecksumURL string `json:"checksumURL,omitempty"`
117120
// State reflect the state of the seed image build process.
118121
// +kubebuilder:validation:Enum=Initialized;Started;Completed;Failed;NotStarted
119122
// +optional

config/crd/bases/elemental.cattle.io_seedimages.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ spec:
159159
type: object
160160
status:
161161
properties:
162+
checksumURL:
163+
description: ChecksumURL the URL from which the SeedImage checksum
164+
can be downloaded once the image is built.
165+
type: string
162166
conditions:
163167
description: Conditions describe the state of the machine registration
164168
object.

controllers/seedimage_controller.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ func (r *SeedImageReconciler) reconcileConfigMapObject(ctx context.Context, seed
317317
return fmt.Errorf("failed marshalling cloud-config: %w", err)
318318
}
319319

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

322322
if err := r.Get(ctx, types.NamespacedName{
323323
Name: seedImg.Name,
@@ -448,13 +448,21 @@ func (r *SeedImageReconciler) updateStatusFromPod(ctx context.Context, seedImg *
448448
return errMsg
449449
}
450450

451-
// Use the registration name or else default to "elemental" for the image file name
452-
imageName := "elemental"
453-
if seedImg.Spec.MachineRegistrationRef != nil && len(seedImg.Spec.MachineRegistrationRef.Name) > 0 {
454-
imageName = seedImg.Spec.MachineRegistrationRef.Name
451+
podConfigMap := &corev1.ConfigMap{}
452+
if err := r.Get(ctx, types.NamespacedName{
453+
Name: seedImg.Name,
454+
Namespace: seedImg.Namespace,
455+
}, podConfigMap); err != nil {
456+
return fmt.Errorf("getting ConfigMap '%s': %w", seedImg.Name, err)
457+
}
458+
outputName, found := podConfigMap.Data[configMapKeyOutputName]
459+
if !found {
460+
return fmt.Errorf("Could not find '%s' value in ConfigMap '%s'", configMapKeyOutputName, seedImg.Name)
455461
}
462+
456463
seedImg.Status.DownloadToken = token
457-
seedImg.Status.DownloadURL = fmt.Sprintf("https://%s/elemental/seedimage/%s/%s.%s", rancherURL, token, imageName, seedImg.Spec.Type)
464+
seedImg.Status.DownloadURL = fmt.Sprintf("https://%s/elemental/seedimage/%s/%s", rancherURL, token, outputName)
465+
seedImg.Status.ChecksumURL = fmt.Sprintf("%s.sha256", seedImg.Status.DownloadURL)
458466
meta.SetStatusCondition(&seedImg.Status.Conditions, metav1.Condition{
459467
Type: elementalv1.SeedImageConditionReady,
460468
Status: metav1.ConditionTrue,
@@ -482,6 +490,7 @@ func (r *SeedImageReconciler) updateStatusFromPod(ctx context.Context, seedImg *
482490
return errMsg
483491
}
484492
seedImg.Status.DownloadURL = ""
493+
seedImg.Status.ChecksumURL = ""
485494
meta.SetStatusCondition(&seedImg.Status.Conditions, metav1.Condition{
486495
Type: elementalv1.SeedImageConditionReady,
487496
Status: metav1.ConditionTrue,
@@ -519,6 +528,7 @@ func (r *SeedImageReconciler) deleteChildResources(ctx context.Context, seedImg
519528
}
520529
}
521530
seedImg.Status.DownloadURL = ""
531+
seedImg.Status.ChecksumURL = ""
522532

523533
foundSvc := &corev1.Service{}
524534
svcName := seedImg.Name
@@ -593,14 +603,13 @@ func fillBuildImagePod(seedImg *elementalv1.SeedImage, buildImg string, pullPoli
593603
ContainerPort: 80,
594604
},
595605
},
596-
Args: []string{"-d", "$(ELEMENTAL_OUTPUT_NAME)", "-t", fmt.Sprintf("%d", deadline*60)},
606+
Args: []string{"-d", "/srv", "-t", fmt.Sprintf("%d", deadline*60)},
597607
VolumeMounts: []corev1.VolumeMount{
598608
{
599609
Name: "iso-storage",
600610
MountPath: "/srv",
601611
},
602612
},
603-
WorkingDir: "/srv",
604613
Env: []corev1.EnvVar{
605614
{
606615
Name: "ELEMENTAL_OUTPUT_NAME",
@@ -672,6 +681,8 @@ func defaultRawInitContainers(seedImg *elementalv1.SeedImage, buildImg string, p
672681
--system $(ELEMENTAL_BASE_IMAGE)`, platformArg),
673682

674683
"mv /iso/elemental.raw /iso/$(ELEMENTAL_OUTPUT_NAME)",
684+
685+
"cd /iso && sha256sum $(ELEMENTAL_OUTPUT_NAME) > $(ELEMENTAL_OUTPUT_NAME).sha256 && cd ../",
675686
}
676687

677688
return []corev1.Container{
@@ -725,6 +736,8 @@ func defaultIsoInitContainers(seedImg *elementalv1.SeedImage, buildImg string, p
725736
)
726737
}
727738

739+
buildCommands = append(buildCommands, "cd /iso && sha256sum $(ELEMENTAL_OUTPUT_NAME) > $(ELEMENTAL_OUTPUT_NAME).sha256 && cd ../")
740+
728741
containers = append(
729742
containers, corev1.Container{
730743
Name: "build",

pkg/server/api_seedimage.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,13 @@ func (i *InventoryServer) apiSeedImage(resp http.ResponseWriter, req *http.Reque
3535
var seedImg *elementalv1.SeedImage
3636

3737
// expected splittedPath = {"seedimage", {token}, {imgName.imgType}}
38-
if len(splittedPath) < 2 {
38+
if len(splittedPath) < 3 {
3939
err = fmt.Errorf("unexpected path: %v", splittedPath)
4040
http.Error(resp, err.Error(), http.StatusNotFound)
4141
return err
4242
}
4343
token := splittedPath[1]
44+
fileName := splittedPath[2]
4445

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

57-
rawURL := fmt.Sprintf("http://%s", svc.Spec.ClusterIP)
58+
rawURL := fmt.Sprintf("http://%s/%s", svc.Spec.ClusterIP, fileName)
5859
seedImgURL, err := url.Parse(rawURL)
5960
if err != nil {
6061
errMsg := fmt.Errorf("failed to parse url '%s'", rawURL)

pkg/server/api_seedimage_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ func TestApiSeedImage(t *testing.T) {
5252
t.Logf("Testing token %s\n", test.token)
5353
req := httptest.NewRequest(
5454
"GET",
55-
fmt.Sprintf("%s/%s", "http://localhost/elemental/seedimage/", test.token),
55+
fmt.Sprintf("%s/%s/foo.bar", "http://localhost/elemental/seedimage/", test.token),
5656
nil)
5757
resp := httptest.NewRecorder()
5858
splittedPath := test.splittedPath
5959
if splittedPath == nil {
60-
splittedPath = []string{"seedimage", test.token}
60+
splittedPath = []string{"seedimage", test.token, "foo.bar"}
6161
}
6262

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

0 commit comments

Comments
 (0)