Skip to content

Commit

Permalink
Add IgnitionSecretRef to IgnitionConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
defo89 committed Nov 13, 2024
1 parent 1a522ff commit 89ef9f3
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 10 deletions.
12 changes: 12 additions & 0 deletions hack/api-reference/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,18 @@ string
</tr>
<tr>
<td>
<code>ignitionSecret</code></br>
<em>
string
</em>
</td>
<td>
<em>(Optional)</em>
<p>IgnitionSecret is a reference to a secret containing the ignition config.</p>
</td>
</tr>
<tr>
<td>
<code>override</code></br>
<em>
bool
Expand Down
12 changes: 9 additions & 3 deletions hack/api-reference/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ string
<td>
<code>clientConnection</code></br>
<em>
invalid type
<a href="https://godoc.org/k8s.io/component-base/config/v1alpha1#ClientConnectionConfiguration">
Kubernetes v1alpha1.ClientConnectionConfiguration
</a>
</em>
</td>
<td>
Expand All @@ -72,7 +74,9 @@ ETCD
<td>
<code>healthCheckConfig</code></br>
<em>
invalid type
<a href="https://github.com/gardener/gardener/extensions/pkg/apis/config">
github.com/gardener/gardener/extensions/pkg/apis/config/v1alpha1.HealthCheckConfig
</a>
</em>
</td>
<td>
Expand Down Expand Up @@ -205,7 +209,9 @@ string
<td>
<code>capacity</code></br>
<em>
invalid type
<a href="https://godoc.org/k8s.io/apimachinery/pkg/api/resource#Quantity">
k8s.io/apimachinery/pkg/api/resource.Quantity
</a>
</em>
</td>
<td>
Expand Down
5 changes: 3 additions & 2 deletions pkg/apis/metal/types_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type MachineImage struct {

// IgnitionConfig contains ignition settings.
type IgnitionConfig struct {
Raw string
Override bool
Raw string
IgnitionSecret string
Override bool
}
4 changes: 4 additions & 0 deletions pkg/apis/metal/v1alpha1/types_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ type IgnitionConfig struct {
// +optional
Raw string `json:"raw,omitempty"`

// IgnitionSecret is a reference to a secret containing the ignition config.
// +optional
IgnitionSecret string `json:"ignitionSecret,omitempty"`

// Override configures, if ignition keys set by the os-extension are overridden
// by extra ignition.
// +optional
Expand Down
2 changes: 2 additions & 0 deletions pkg/apis/metal/v1alpha1/zz_generated.conversion.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 31 additions & 2 deletions pkg/controller/worker/machines.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,12 @@ func (w *workerDelegate) generateMachineClassAndSecrets(ctx context.Context) ([]
metal.ImageFieldName: machineImage,
metal.ServerLabelsFieldName: serverLabels,
}
if workerConfig.ExtraIgnition != nil {
machineClassProviderSpec[metal.IgnitionFieldName] = workerConfig.ExtraIgnition.Raw
if workerConfig.ExtraIgnition != nil && (workerConfig.ExtraIgnition.Raw != "" || workerConfig.ExtraIgnition.IgnitionSecret != "") {
mergedIgnition, err := w.getMergedIgnitionConfig(ctx, workerConfig)
if err != nil {
return nil, nil, err
}
machineClassProviderSpec[metal.IgnitionFieldName] = mergedIgnition
machineClassProviderSpec[metal.IgnitionOverrideFieldName] = workerConfig.ExtraIgnition.Override
}

Expand Down Expand Up @@ -227,3 +231,28 @@ func (w *workerDelegate) getServerLabelsForMachine(machineType string, workerCon
}
return combinedLabels, nil
}

func (w *workerDelegate) getMergedIgnitionConfig(ctx context.Context, workerConfig *metalv1alpha1.WorkerConfig) (string, error) {
var mergedIgnition string

if workerConfig.ExtraIgnition.Raw != "" {
mergedIgnition = workerConfig.ExtraIgnition.Raw
}

if workerConfig.ExtraIgnition.IgnitionSecret != "" {
secret := &corev1.Secret{}
secretKey := client.ObjectKey{Namespace: w.worker.Namespace, Name: workerConfig.ExtraIgnition.IgnitionSecret}
if err := w.client.Get(ctx, secretKey, secret); err != nil {
return "", fmt.Errorf("failed to get ignition secret %s: %w", workerConfig.ExtraIgnition.IgnitionSecret, err)
}

secretContent, ok := secret.Data["ignition"]
if !ok {
return "", fmt.Errorf("ignition key not found in secret %s", workerConfig.ExtraIgnition.IgnitionSecret)
}

mergedIgnition += string(secretContent)
}

return mergedIgnition, nil
}
2 changes: 1 addition & 1 deletion pkg/controller/worker/machines_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ var _ = Describe("Machines", func() {
"foo": "bar",
"foo1": "bar1",
},
metal.IgnitionFieldName: "abc",
metal.IgnitionFieldName: "abcdef",
metal.IgnitionOverrideFieldName: true,
}

Expand Down
18 changes: 16 additions & 2 deletions pkg/controller/worker/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ var _ = BeforeSuite(func() {
func SetupTest() (*corev1.Namespace, *gardener.ChartApplier) {
var chartApplier gardener.ChartApplier
ns := &corev1.Namespace{}
ign := &corev1.Secret{}

BeforeEach(func(ctx SpecContext) {
var err error
Expand All @@ -132,13 +133,26 @@ func SetupTest() (*corev1.Namespace, *gardener.ChartApplier) {
volumeName := "test-volume"
volumeType := "fast"

*ign = corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "testign-",
Namespace: ns.Name,
},
Data: map[string][]byte{
"ignition": []byte("def"),
},
}
Expect(k8sClient.Create(ctx, ign)).To(Succeed(), "failed to create test ignition secret")
DeferCleanup(k8sClient.Delete, ign)

workerConfig = &apiv1alpha1.WorkerConfig{
ExtraServerLabels: map[string]string{
"foo1": "bar1",
},
ExtraIgnition: &apiv1alpha1.IgnitionConfig{
Raw: "abc",
Override: true,
Raw: "abc",
IgnitionSecret: ign.Name,
Override: true,
},
}
workerConfigJSON, _ = json.Marshal(workerConfig)
Expand Down

0 comments on commit 89ef9f3

Please sign in to comment.