Skip to content

Commit 461535a

Browse files
committed
extend cluster iso images information
1 parent 390891f commit 461535a

File tree

7 files changed

+121
-11
lines changed

7 files changed

+121
-11
lines changed

api/v1alpha2/cloudconfig_types.go

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package v1alpha2
2+
3+
type CloudConfig struct {
4+
Users Users `json:"users,omitempty"`
5+
Plural Plural `json:"plural,omitempty"`
6+
}
7+
8+
type Users []User
9+
10+
func (in Users) FirstUser() *User {
11+
for _, user := range in {
12+
return &user
13+
}
14+
15+
return nil
16+
}
17+
18+
type User struct {
19+
Name string `json:"name"`
20+
Passwd string `json:"passwd"`
21+
}
22+
23+
func (in *User) GetName() *string {
24+
if in == nil {
25+
return nil
26+
}
27+
28+
return &in.Name
29+
}
30+
31+
func (in *User) GetPasswd() *string {
32+
if in == nil {
33+
return nil
34+
}
35+
36+
return &in.Passwd
37+
}
38+
39+
type Plural struct {
40+
Token string `json:"token"`
41+
URL string `json:"url"`
42+
}

api/v1alpha2/osartifact_types.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,24 @@ const (
3030

3131
// OSArtifactSpec defines the desired state of OSArtifact
3232
type OSArtifactSpec struct {
33-
// There are 3 ways to specify a Kairos image:
34-
3533
// Points to a prepared kairos image (e.g. a released one)
34+
// +optional
3635
ImageName string `json:"imageName,omitempty"`
3736

37+
// +optional
3838
ISO bool `json:"iso,omitempty"`
3939

4040
// +kubebuilder:validation:Type:=string
4141
// +kubebuilder:validation:Enum:=rpi3;rpi4
42+
// +optional
4243
Model *Model `json:"model,omitempty"`
4344

4445
// +optional
4546
CloudConfigRef *corev1.SecretKeySelector `json:"cloudConfigRef,omitempty"`
4647

48+
// +optional
49+
Project string `json:"project,omitempty"`
50+
4751
// +optional
4852
Bundles []string `json:"bundles,omitempty"`
4953

charts/osartifact/Chart.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ maintainers:
55
- name: Plural
66
77
type: application
8-
version: 0.8.2
8+
version: 0.9.0

charts/osartifact/templates/osartifact.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ spec:
2222
cloudConfigRef:
2323
name: {{ include "osartifact.fullname" . }}-config
2424
key: cloud-config.yaml
25+
{{- if .Values.project }}
26+
project: {{ .Values.project }}
27+
{{- end }}
2528
exporter:
2629
serviceAccount: {{ include "osartifact.fullname" . }}
2730
{{- with .Values.exporter.extraEnvVars }}

charts/osartifact/values.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ image: quay.io/kairos/alpine:3.19-standard-arm64-rpi4-v3.2.4-k3sv1.31.3-k3s1
77
# Target device. Currently supported values: rpi4
88
device: rpi4
99

10+
# Plural project name this image should be tied to.
11+
project: ~
12+
1013
# WiFi configuration. It will attempt the connection on the first boot.
1114
wifi:
1215
enabled: false

controllers/osartifact_controller.go

+46-6
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@ package controllers
1818

1919
import (
2020
"context"
21+
"encoding/json"
2122
"fmt"
2223
"time"
2324

24-
osbuilder "github.com/kairos-io/osbuilder/api/v1alpha2"
25-
consoleclient "github.com/kairos-io/osbuilder/pkg/client"
2625
console "github.com/pluralsh/console/go/client"
2726
batchv1 "k8s.io/api/batch/v1"
2827
corev1 "k8s.io/api/core/v1"
@@ -41,6 +40,9 @@ import (
4140
"sigs.k8s.io/controller-runtime/pkg/log"
4241
"sigs.k8s.io/controller-runtime/pkg/predicate"
4342
"sigs.k8s.io/controller-runtime/pkg/reconcile"
43+
44+
osbuilder "github.com/kairos-io/osbuilder/api/v1alpha2"
45+
consoleclient "github.com/kairos-io/osbuilder/pkg/client"
4446
)
4547

4648
const (
@@ -406,7 +408,7 @@ func (r *OSArtifactReconciler) checkExport(ctx context.Context, artifact *osbuil
406408
} else if job.Spec.Completions != nil {
407409
if job.Status.Succeeded > 0 && artifact.Status.Phase == osbuilder.Exporting {
408410
artifact.Status.Phase = osbuilder.Ready
409-
if err := r.upsertClusterIsoImage(artifact); err != nil {
411+
if err := r.upsertClusterIsoImage(ctx, artifact); err != nil {
410412
artifact.Status.Phase = osbuilder.Error
411413
meta.SetStatusCondition(&artifact.Status.Conditions, metav1.Condition{
412414
Type: "Ready",
@@ -444,11 +446,25 @@ func (r *OSArtifactReconciler) checkExport(ctx context.Context, artifact *osbuil
444446
return requeue, nil
445447
}
446448

447-
func (r *OSArtifactReconciler) upsertClusterIsoImage(artifact *osbuilder.OSArtifact) error {
449+
func (r *OSArtifactReconciler) upsertClusterIsoImage(ctx context.Context, artifact *osbuilder.OSArtifact) error {
450+
cloudConfig, err := r.getCloudConfig(ctx, artifact)
451+
if err != nil {
452+
return err
453+
}
454+
455+
var projectID *string = nil
456+
project, _ := r.ConsoleClient.GetProject(artifact.Spec.Project)
457+
if project != nil {
458+
projectID = &project.ID
459+
}
460+
448461
image := fmt.Sprintf("%s:%s", artifact.Spec.Exporter.Registry.Image.Repository, artifact.Spec.Exporter.Registry.Image.Tag)
449462
attr := console.ClusterIsoImageAttributes{
450-
Image: image,
451-
Registry: artifact.Spec.Exporter.Registry.Name,
463+
Image: image,
464+
Registry: artifact.Spec.Exporter.Registry.Name,
465+
User: cloudConfig.Users.FirstUser().GetName(),
466+
Password: cloudConfig.Users.FirstUser().GetPasswd(),
467+
ProjectID: projectID,
452468
}
453469

454470
getResponse, err := r.ConsoleClient.GetClusterIsoImage(&image)
@@ -518,3 +534,27 @@ func (r *OSArtifactReconciler) CreateConfigMap(ctx context.Context, artifact *os
518534

519535
return nil
520536
}
537+
538+
func (r *OSArtifactReconciler) getCloudConfig(ctx context.Context, artifact *osbuilder.OSArtifact) (*osbuilder.CloudConfig, error) {
539+
config := &osbuilder.CloudConfig{}
540+
secret := &corev1.Secret{}
541+
key := client.ObjectKey{
542+
Namespace: artifact.Namespace,
543+
Name: artifact.Spec.CloudConfigRef.Name,
544+
}
545+
546+
if err := r.Get(ctx, key, secret); err != nil {
547+
return config, err
548+
}
549+
550+
configBytes, exists := secret.Data[artifact.Spec.CloudConfigRef.Key]
551+
if !exists {
552+
return config, fmt.Errorf("could not find key %s in secret %s", artifact.Spec.CloudConfigRef.Key, artifact.Spec.CloudConfigRef.Name)
553+
}
554+
555+
if err := json.Unmarshal(configBytes, config); err != nil {
556+
return config, err
557+
}
558+
559+
return config, nil
560+
}

pkg/client/console.go

+20-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ import (
66
"net/http"
77

88
rawclient "github.com/Yamashou/gqlgenc/clientv2"
9-
internalerror "github.com/kairos-io/osbuilder/pkg/errors"
10-
"github.com/kairos-io/osbuilder/pkg/helpers"
119
"github.com/pkg/errors"
1210
console "github.com/pluralsh/console/go/client"
1311
apierrors "k8s.io/apimachinery/pkg/api/errors"
1412
"k8s.io/apimachinery/pkg/runtime/schema"
13+
14+
internalerror "github.com/kairos-io/osbuilder/pkg/errors"
15+
"github.com/kairos-io/osbuilder/pkg/helpers"
1516
)
1617

1718
type client struct {
@@ -37,6 +38,7 @@ type Client interface {
3738
UpdateClusterIsoImage(id string, attributes console.ClusterIsoImageAttributes) (*console.ClusterIsoImageFragment, error)
3839
GetClusterIsoImage(image *string) (*console.ClusterIsoImageFragment, error)
3940
DeleteClusterIsoImage(id string) (*console.ClusterIsoImageFragment, error)
41+
GetProject(name string) (*console.ProjectFragment, error)
4042
}
4143

4244
func (c *client) CreateClusterIsoImage(attributes console.ClusterIsoImageAttributes) (*console.ClusterIsoImageFragment, error) {
@@ -82,6 +84,22 @@ func (c *client) GetClusterIsoImage(image *string) (*console.ClusterIsoImageFrag
8284
return response.ClusterIsoImage, nil
8385
}
8486

87+
func (c *client) GetProject(name string) (*console.ProjectFragment, error) {
88+
response, err := c.consoleClient.GetProject(c.ctx, nil, &name)
89+
if internalerror.IsNotFound(err) {
90+
return nil, apierrors.NewNotFound(schema.GroupResource{}, name)
91+
}
92+
if err == nil && (response == nil || response.Project == nil) {
93+
return nil, apierrors.NewNotFound(schema.GroupResource{}, name)
94+
}
95+
96+
if response == nil {
97+
return nil, err
98+
}
99+
100+
return response.Project, nil
101+
}
102+
85103
func GetErrorResponse(err error, methodName string) error {
86104
if err == nil {
87105
return nil

0 commit comments

Comments
 (0)