Skip to content

Commit 0b36e8f

Browse files
committed
Only use a Key for TrustedCA, not for BMCCA
Signed-off-by: Dmitry Tantsur <dtantsur@protonmail.com>
1 parent ef7da9a commit 0b36e8f

File tree

12 files changed

+125
-116
lines changed

12 files changed

+125
-116
lines changed

.golangci.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,12 @@ linters:
134134
- linters:
135135
- tagliatelle
136136
text: CA
137+
- linters:
138+
- staticcheck
139+
text: 'SA1019: tls\.(TrustedCAName|BMCCAName) is deprecated: .*'
140+
paths:
141+
- pkg/ironic/validation.go
142+
- pkg/ironic/utils.go
137143
paths:
138144
- zz_generated.*\.go$
139145
- .*conversion.*\.go$

api/v1alpha1/common.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,24 @@ var (
1818
IronicServiceLabel = IronicLabelPrefix + "/ironic"
1919
IronicVersionLabel = IronicLabelPrefix + "/version"
2020
)
21+
22+
// ResourceReference references a ConfigMap or Secret resource.
23+
type ResourceReference struct {
24+
// Name of the resource.
25+
Name string `json:"name"`
26+
27+
// Kind of the resource (ConfigMap or Secret).
28+
// +kubebuilder:validation:Enum=ConfigMap;Secret
29+
Kind string `json:"kind"`
30+
}
31+
32+
// ResourceReferenceWithKey references a ConfigMap or Secret resource and
33+
// targets a specific key from it.
34+
type ResourceReferenceWithKey struct {
35+
ResourceReference `json:",inline"`
36+
37+
// Key within the resource to use. If not specified and the resource contains multiple keys,
38+
// the first key will be used and a warning will be logged for other keys.
39+
// +optional
40+
Key string `json:"key,omitempty"`
41+
}

api/v1alpha1/ironic_types.go

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -60,21 +60,6 @@ type Inspection struct {
6060
VLANInterfaces []string `json:"vlanInterfaces,omitempty"`
6161
}
6262

63-
// ResourceReference references a ConfigMap or Secret resource.
64-
type ResourceReference struct {
65-
// Name of the resource.
66-
Name string `json:"name"`
67-
68-
// Kind of the resource (ConfigMap or Secret).
69-
// +kubebuilder:validation:Enum=ConfigMap;Secret
70-
Kind string `json:"kind"`
71-
72-
// Key within the resource to use. If not specified and the resource contains multiple keys,
73-
// the first key will be used and a warning will be logged for other keys.
74-
// +optional
75-
Key string `json:"key,omitempty"`
76-
}
77-
7863
type DHCP struct {
7964
// DNSAddress is the IP address of the DNS server to pass to hosts via DHCP.
8065
// Must not be set together with ServeDNS.
@@ -232,10 +217,8 @@ type TLS struct {
232217
// TrustedCA is a reference to a ConfigMap or Secret containing the CA certificate(s)
233218
// to use when validating TLS connections to image servers and other services.
234219
// The resource should contain one or more CA certificates in PEM format.
235-
// If the resource contains multiple keys, only the first key will be used and
236-
// a warning will be logged.
237220
// +optional
238-
TrustedCA *ResourceReference `json:"trustedCA,omitempty"`
221+
TrustedCA *ResourceReferenceWithKey `json:"trustedCA,omitempty"`
239222

240223
// TrustedCAName is a reference to the configmap with the CA certificate(s)
241224
// to use when validating TLS connections to image servers and other services.
@@ -261,36 +244,6 @@ type TLS struct {
261244
InsecureRPC *bool `json:"insecureRPC,omitempty"`
262245
}
263246

264-
// GetBMCCA returns the effective BMC CA resource reference.
265-
// It prefers the new BMCCA field over the deprecated BMCCAName field.
266-
func (t *TLS) GetBMCCA() *ResourceReference {
267-
if t.BMCCA != nil {
268-
return t.BMCCA
269-
}
270-
if t.BMCCAName != "" {
271-
return &ResourceReference{
272-
Name: t.BMCCAName,
273-
Kind: ResourceKindSecret,
274-
}
275-
}
276-
return nil
277-
}
278-
279-
// GetTrustedCA returns the effective Trusted CA resource reference.
280-
// It prefers the new TrustedCA field over the deprecated TrustedCAName field.
281-
func (t *TLS) GetTrustedCA() *ResourceReference {
282-
if t.TrustedCA != nil {
283-
return t.TrustedCA
284-
}
285-
if t.TrustedCAName != "" {
286-
return &ResourceReference{
287-
Name: t.TrustedCAName,
288-
Kind: ResourceKindConfigMap,
289-
}
290-
}
291-
return nil
292-
}
293-
294247
type Images struct {
295248
// DeployRamdiskBranch is the branch of IPA to download. The main branch is used by default.
296249
// Not used if deployRamdisk.disableDownloader is true.

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 17 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/ironic.metal3.io_ironics.yaml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3431,11 +3431,6 @@ spec:
34313431
to use when validating TLS connections to BMCs.
34323432
Supported in Ironic 32.0 or newer.
34333433
properties:
3434-
key:
3435-
description: |-
3436-
Key within the resource to use. If not specified and the resource contains multiple keys,
3437-
the first key will be used and a warning will be logged for other keys.
3438-
type: string
34393434
kind:
34403435
description: Kind of the resource (ConfigMap or Secret).
34413436
enum:
@@ -3480,8 +3475,6 @@ spec:
34803475
TrustedCA is a reference to a ConfigMap or Secret containing the CA certificate(s)
34813476
to use when validating TLS connections to image servers and other services.
34823477
The resource should contain one or more CA certificates in PEM format.
3483-
If the resource contains multiple keys, only the first key will be used and
3484-
a warning will be logged.
34853478
properties:
34863479
key:
34873480
description: |-

docs/api.md

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6941,9 +6941,7 @@ HighAvailability feature gate to be set.<br/>
69416941
<td>
69426942
TrustedCA is a reference to a ConfigMap or Secret containing the CA certificate(s)
69436943
to use when validating TLS connections to image servers and other services.
6944-
The resource should contain one or more CA certificates in PEM format.
6945-
If the resource contains multiple keys, only the first key will be used and
6946-
a warning will be logged.<br/>
6944+
The resource should contain one or more CA certificates in PEM format.<br/>
69476945
</td>
69486946
<td>false</td>
69496947
</tr><tr>
@@ -6997,14 +6995,6 @@ Supported in Ironic 32.0 or newer.
69976995
Name of the resource.<br/>
69986996
</td>
69996997
<td>true</td>
7000-
</tr><tr>
7001-
<td><b>key</b></td>
7002-
<td>string</td>
7003-
<td>
7004-
Key within the resource to use. If not specified and the resource contains multiple keys,
7005-
the first key will be used and a warning will be logged for other keys.<br/>
7006-
</td>
7007-
<td>false</td>
70086998
</tr></tbody>
70096999
</table>
70107000

@@ -7017,8 +7007,6 @@ the first key will be used and a warning will be logged for other keys.<br/>
70177007
TrustedCA is a reference to a ConfigMap or Secret containing the CA certificate(s)
70187008
to use when validating TLS connections to image servers and other services.
70197009
The resource should contain one or more CA certificates in PEM format.
7020-
If the resource contains multiple keys, only the first key will be used and
7021-
a warning will be logged.
70227010

70237011
<table>
70247012
<thead>

internal/controller/ironic_controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ func (r *IronicReconciler) handleIronic(cctx ironic.ControllerContext, ironicCon
168168

169169
var bmcCASecret *corev1.Secret
170170
var bmcCAConfigMap *corev1.ConfigMap
171-
if bmcCARef := ironicConf.Spec.TLS.GetBMCCA(); bmcCARef != nil {
171+
if bmcCARef := ironic.GetBMCCA(&ironicConf.Spec.TLS); bmcCARef != nil {
172172
switch bmcCARef.Kind {
173173
case metal3api.ResourceKindSecret:
174174
bmcCASecret, requeue, err = r.getAndUpdateSecret(cctx, ironicConf, bmcCARef.Name)
@@ -185,7 +185,7 @@ func (r *IronicReconciler) handleIronic(cctx ironic.ControllerContext, ironicCon
185185

186186
var trustedCASecret *corev1.Secret
187187
var trustedCAConfigMap *corev1.ConfigMap
188-
if trustedCARef := ironicConf.Spec.TLS.GetTrustedCA(); trustedCARef != nil {
188+
if trustedCARef := ironic.GetTrustedCA(&ironicConf.Spec.TLS); trustedCARef != nil {
189189
switch trustedCARef.Kind {
190190
case metal3api.ResourceKindSecret:
191191
trustedCASecret, requeue, err = r.getAndUpdateSecret(cctx, ironicConf, trustedCARef.Name)

pkg/ironic/containers.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -211,16 +211,19 @@ func buildTrustedCAEnvVars(cctx ControllerContext, resources Resources) []corev1
211211
}
212212

213213
// Get the TrustedCA reference to check if a specific key was requested
214-
trustedCARef := resources.Ironic.Spec.TLS.GetTrustedCA()
214+
var requestedKey string
215+
if resources.Ironic.Spec.TLS.TrustedCA != nil {
216+
requestedKey = resources.Ironic.Spec.TLS.TrustedCA.Key
217+
}
215218
var selectedKey string
216219

217-
if trustedCARef != nil && trustedCARef.Key != "" {
220+
if requestedKey != "" {
218221
// User specified a key, use it if it exists
219-
if _, exists := keys[trustedCARef.Key]; exists {
220-
selectedKey = trustedCARef.Key
222+
if _, exists := keys[requestedKey]; exists {
223+
selectedKey = requestedKey
221224
} else {
222225
cctx.Logger.Info("specified key not found in Trusted CA "+resourceKind+", using first available key",
223-
"requestedKey", trustedCARef.Key, resourceKind, namespace+"/"+resourceName)
226+
"requestedKey", requestedKey, resourceKind, namespace+"/"+resourceName)
224227
// Fall through to select first key
225228
}
226229
}
@@ -459,8 +462,7 @@ func buildIronicVolumesAndMounts(resources Resources) (volumes []corev1.Volume,
459462
}
460463
}
461464

462-
if maybeVolume := volumeForSecretOrConfigMap(bmcCAVolumeName, resources.BMCCASecret, resources.BMCCAConfigMap,
463-
resources.Ironic.Spec.TLS.GetBMCCA()); maybeVolume != nil {
465+
if maybeVolume := volumeForSecretOrConfigMap(bmcCAVolumeName, resources.BMCCASecret, resources.BMCCAConfigMap); maybeVolume != nil {
464466
volumes = append(volumes, *maybeVolume)
465467
mounts = append(mounts,
466468
corev1.VolumeMount{
@@ -471,8 +473,7 @@ func buildIronicVolumesAndMounts(resources Resources) (volumes []corev1.Volume,
471473
)
472474
}
473475

474-
if maybeVolume := volumeForSecretOrConfigMap(trustedCAVolumeName, resources.TrustedCASecret, resources.TrustedCAConfigMap,
475-
resources.Ironic.Spec.TLS.GetTrustedCA()); maybeVolume != nil {
476+
if maybeVolume := volumeForSecretOrConfigMap(trustedCAVolumeName, resources.TrustedCASecret, resources.TrustedCAConfigMap); maybeVolume != nil {
476477
volumes = append(volumes, *maybeVolume)
477478
mounts = append(mounts,
478479
corev1.VolumeMount{

pkg/ironic/containers_test.go

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ func TestPrometheusExporterEnvVars(t *testing.T) {
529529
func TestBuildTrustedCAEnvVars(t *testing.T) {
530530
testCases := []struct {
531531
name string
532-
trustedCARef *metal3api.ResourceReference
532+
trustedCARef *metal3api.ResourceReferenceWithKey
533533
configMapData map[string]string
534534
secretData map[string][]byte
535535
expectedKey string
@@ -538,10 +538,12 @@ func TestBuildTrustedCAEnvVars(t *testing.T) {
538538
}{
539539
{
540540
name: "ConfigMap with specific key",
541-
trustedCARef: &metal3api.ResourceReference{
542-
Name: "trusted-ca",
543-
Kind: "ConfigMap",
544-
Key: "custom-ca.crt",
541+
trustedCARef: &metal3api.ResourceReferenceWithKey{
542+
ResourceReference: metal3api.ResourceReference{
543+
Name: "trusted-ca",
544+
Kind: "ConfigMap",
545+
},
546+
Key: "custom-ca.crt",
545547
},
546548
configMapData: map[string]string{
547549
"custom-ca.crt": "cert1",
@@ -552,10 +554,12 @@ func TestBuildTrustedCAEnvVars(t *testing.T) {
552554
},
553555
{
554556
name: "Secret with specific key",
555-
trustedCARef: &metal3api.ResourceReference{
556-
Name: "trusted-ca-secret",
557-
Kind: "Secret",
558-
Key: "tls.crt",
557+
trustedCARef: &metal3api.ResourceReferenceWithKey{
558+
ResourceReference: metal3api.ResourceReference{
559+
Name: "trusted-ca-secret",
560+
Kind: "Secret",
561+
},
562+
Key: "tls.crt",
559563
},
560564
secretData: map[string][]byte{
561565
"tls.crt": []byte("cert1"),
@@ -566,10 +570,12 @@ func TestBuildTrustedCAEnvVars(t *testing.T) {
566570
},
567571
{
568572
name: "Key specified but doesn't exist - ConfigMap",
569-
trustedCARef: &metal3api.ResourceReference{
570-
Name: "trusted-ca",
571-
Kind: "ConfigMap",
572-
Key: "nonexistent.crt",
573+
trustedCARef: &metal3api.ResourceReferenceWithKey{
574+
ResourceReference: metal3api.ResourceReference{
575+
Name: "trusted-ca",
576+
Kind: "ConfigMap",
577+
},
578+
Key: "nonexistent.crt",
573579
},
574580
configMapData: map[string]string{
575581
"actual-ca.crt": "cert1",
@@ -580,10 +586,12 @@ func TestBuildTrustedCAEnvVars(t *testing.T) {
580586
},
581587
{
582588
name: "Key specified but doesn't exist - Secret",
583-
trustedCARef: &metal3api.ResourceReference{
584-
Name: "trusted-ca-secret",
585-
Kind: "Secret",
586-
Key: "missing.crt",
589+
trustedCARef: &metal3api.ResourceReferenceWithKey{
590+
ResourceReference: metal3api.ResourceReference{
591+
Name: "trusted-ca-secret",
592+
Kind: "Secret",
593+
},
594+
Key: "missing.crt",
587595
},
588596
secretData: map[string][]byte{
589597
"available.crt": []byte("cert1"),
@@ -594,9 +602,11 @@ func TestBuildTrustedCAEnvVars(t *testing.T) {
594602
},
595603
{
596604
name: "Multiple keys without Key specified - ConfigMap",
597-
trustedCARef: &metal3api.ResourceReference{
598-
Name: "trusted-ca",
599-
Kind: "ConfigMap",
605+
trustedCARef: &metal3api.ResourceReferenceWithKey{
606+
ResourceReference: metal3api.ResourceReference{
607+
Name: "trusted-ca",
608+
Kind: "ConfigMap",
609+
},
600610
},
601611
configMapData: map[string]string{
602612
"ca1.crt": "cert1",
@@ -712,10 +722,12 @@ func TestBuildTrustedCAEnvVarsKeySelection(t *testing.T) {
712722
Logger: logr.Discard(),
713723
}
714724

715-
trustedCARef := &metal3api.ResourceReference{
716-
Name: "test-ca",
717-
Kind: "ConfigMap",
718-
Key: tc.specifiedKey,
725+
trustedCARef := &metal3api.ResourceReferenceWithKey{
726+
ResourceReference: metal3api.ResourceReference{
727+
Name: "test-ca",
728+
Kind: "ConfigMap",
729+
},
730+
Key: tc.specifiedKey,
719731
}
720732

721733
data := make(map[string]string)

pkg/ironic/local.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ func ParseLocalIronic(inputFile string, scheme *runtime.Scheme) (*Resources, err
7575
}
7676

7777
// Get effective CA references
78-
bmcCARef := resources.Ironic.Spec.TLS.GetBMCCA()
79-
trustedCARef := resources.Ironic.Spec.TLS.GetTrustedCA()
78+
bmcCARef := GetBMCCA(&resources.Ironic.Spec.TLS)
79+
trustedCARef := GetTrustedCA(&resources.Ironic.Spec.TLS)
8080

8181
for _, secretObj := range secrets {
8282
// Determine secret type based on name patterns or labels

0 commit comments

Comments
 (0)