Skip to content

Commit

Permalink
Merge pull request #99 from winiciusallan/extraconfig
Browse files Browse the repository at this point in the history
✨ Allows to provide ExtraConfig for Ironic conf
  • Loading branch information
metal3-io-bot authored Dec 19, 2024
2 parents 76c580c + 926f6a5 commit 9f42616
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 0 deletions.
22 changes: 22 additions & 0 deletions api/v1alpha1/ironic_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,24 @@ type Images struct {
Keepalived string `json:"keepalived,omitempty"`
}

// ExtraConfig defines environment variables to override Ironic configuration
// More info at the end of description section
// https://github.com/metal3-io/ironic-image
type ExtraConfig struct {

// The group that config belongs to.
// +optional
Group string `json:"group,omitempty"`

// The name of the config.
// +optional
Name string `json:"name,omitempty"`

// The value of the config.
// +optional
Value string `json:"value,omitempty"`
}

// IronicSpec defines the desired state of Ironic
type IronicSpec struct {
// APICredentialsName is a reference to the secret with Ironic API credentials.
Expand Down Expand Up @@ -237,6 +255,10 @@ type IronicSpec struct {
// TLS defines TLS-related settings for various network interactions.
// +optional
TLS TLS `json:"tls,omitempty"`

// ExtraConfig defines extra options for Ironic configuration.
// +optional
ExtraConfig []ExtraConfig `json:"extraConfig,omitempty"`
}

// InstalledVersion identifies which version of Ironic was installed.
Expand Down
20 changes: 20 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

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

19 changes: 19 additions & 0 deletions config/crd/bases/ironic.metal3.io_ironics.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,25 @@ spec:
into the ramdisk for debugging purposes.
type: string
type: object
extraConfig:
description: ExtraConfig defines extra options for Ironic configuration.
items:
description: |-
ExtraConfig defines environment variables to override Ironic configuration
More info at the end of description section
https://github.com/metal3-io/ironic-image
properties:
group:
description: The group that config belongs to.
type: string
name:
description: The name of the config.
type: string
value:
description: The value of the config.
type: string
type: object
type: array
highAvailability:
description: |-
HighAvailability causes Ironic to be deployed as a DaemonSet on control plane nodes instead of a deployment with 1 replica.
Expand Down
29 changes: 29 additions & 0 deletions pkg/ironic/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,31 @@ func buildCommonEnvVars(ironic *metal3api.Ironic) []corev1.EnvVar {
return result
}

func buildExtraConfigVars(ironic *metal3api.Ironic) []corev1.EnvVar {
var result []corev1.EnvVar

for _, extraConfig := range ironic.Spec.ExtraConfig {
// Default group value
group := "DEFAULT"
if extraConfig.Group != "" {
group = extraConfig.Group
}

if extraConfig.Name != "" && extraConfig.Value != "" {
name := extraConfig.Name
value := extraConfig.Value

result = append(result,
corev1.EnvVar{
Name: fmt.Sprintf("OS_%s__%s", strings.ToUpper(group), strings.ToUpper(name)),
Value: value,
})
}
}

return result
}

func buildIronicEnvVars(ironic *metal3api.Ironic, db *metal3api.IronicDatabase, htpasswd string, domain string) []corev1.EnvVar {
result := buildCommonEnvVars(ironic)
result = append(result, []corev1.EnvVar{
Expand Down Expand Up @@ -188,6 +213,10 @@ func buildIronicEnvVars(ironic *metal3api.Ironic, db *metal3api.IronicDatabase,
)
}

if ironic.Spec.ExtraConfig != nil {
result = append(result, buildExtraConfigVars(ironic)...)
}

result = appendStringEnv(result, "IRONIC_EXTERNAL_IP", ironic.Spec.Networking.ExternalIP)

return result
Expand Down
59 changes: 59 additions & 0 deletions pkg/ironic/containers_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ironic

import (
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -153,3 +154,61 @@ func TestImageOverrides(t *testing.T) {
assert.Equal(t, expectedImages, images)
assert.Equal(t, "stable/x.y", actualBranch)
}

func TestExpectedExtraEnvVars(t *testing.T) {
cctx := ControllerContext{}
secret := &corev1.Secret{
Data: map[string][]byte{
"htpasswd": []byte("abcd"),
},
}

expectedExtraVars := map[string]string{
"OS_PXE__BOOT_RETRY_TIMEOUT": "1200",
"OS_CONDUCTOR__DEPLOY_CALLBACK_TIMEOUT": "4800",
"OS_CONDUCTOR__INSPECT_TIMEOUT": "1800",
}

ironic := &metal3api.Ironic{
ObjectMeta: metav1.ObjectMeta{
Namespace: "test",
Name: "test",
},
Spec: metal3api.IronicSpec{
Networking: metal3api.Networking{
Interface: "eth0",
IPAddress: "192.0.2.2",
IPAddressManager: metal3api.IPAddressManagerKeepalived,
},
ExtraConfig: []metal3api.ExtraConfig{
{
Group: "pxe",
Name: "boot_retry_timeout",
Value: "1200",
},
{
Group: "conductor",
Name: "deploy_callback_timeout",
Value: "4800",
},
{
Group: "conductor",
Name: "inspect_timeout",
Value: "1800",
},
},
},
}

podTemplate, err := newIronicPodTemplate(cctx, ironic, nil, secret, "test-domain.example.com")
assert.NoError(t, err)

extraVars := make(map[string]string, len(expectedExtraVars))
for _, env := range podTemplate.Spec.Containers[0].Env {
if strings.Contains(env.Name, "OS_") {
extraVars[env.Name] = env.Value
}
}

assert.Equal(t, expectedExtraVars, extraVars)
}

0 comments on commit 9f42616

Please sign in to comment.