Skip to content

Commit 14224ca

Browse files
committed
Change User.PasswdFrom from *PasswdSource to PasswdSource + add omitzero, extend SSA patch helper to handle arrays
1 parent c641a21 commit 14224ca

File tree

14 files changed

+201
-81
lines changed

14 files changed

+201
-81
lines changed

.golangci-kal.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,10 @@ linters:
211211
- kubeapilinter
212212

213213
# TODO: Excludes that should be removed once https://github.com/kubernetes-sigs/kube-api-linter/issues/132 will be fixed
214+
- path: "api/.*"
215+
text: "optionalfields: field (.*) is optional and (should have the omitempty tag|should be a pointer)"
216+
linters:
217+
- kubeapilinter
214218
- path: "api/ipam/v1beta2/ipaddressclaim_types.go"
215219
text: "optionalfields: field AddressRef is optional and has a valid zero value \\({}\\), but the validation is not complete \\(e.g. min properties/adding required fields\\). The field should be a pointer to allow the zero value to be set. If the zero value is not a valid use case, complete the validation and remove the pointer."
216220
linters:

api/bootstrap/kubeadm/v1beta1/conversion.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,31 @@ func Convert_v1beta2_ClusterConfiguration_To_v1beta1_ClusterConfiguration(in *bo
620620
return autoConvert_v1beta2_ClusterConfiguration_To_v1beta1_ClusterConfiguration(in, out, s)
621621
}
622622

623+
func Convert_v1beta1_User_To_v1beta2_User(in *User, out *bootstrapv1.User, s apimachineryconversion.Scope) error {
624+
if err := autoConvert_v1beta1_User_To_v1beta2_User(in, out, s); err != nil {
625+
return err
626+
}
627+
if in.PasswdFrom != nil {
628+
if err := Convert_v1beta1_PasswdSource_To_v1beta2_PasswdSource(in.PasswdFrom, &out.PasswdFrom, s); err != nil {
629+
return err
630+
}
631+
}
632+
return nil
633+
}
634+
635+
func Convert_v1beta2_User_To_v1beta1_User(in *bootstrapv1.User, out *User, s apimachineryconversion.Scope) error {
636+
if err := autoConvert_v1beta2_User_To_v1beta1_User(in, out, s); err != nil {
637+
return err
638+
}
639+
if in.PasswdFrom.IsDefined() {
640+
out.PasswdFrom = &PasswdSource{}
641+
if err := Convert_v1beta2_PasswdSource_To_v1beta1_PasswdSource(&in.PasswdFrom, out.PasswdFrom, s); err != nil {
642+
return err
643+
}
644+
}
645+
return nil
646+
}
647+
623648
func dropEmptyStringsKubeadmConfigSpec(dst *KubeadmConfigSpec) {
624649
for i, u := range dst.Users {
625650
dropEmptyString(&u.Gecos)

api/bootstrap/kubeadm/v1beta1/conversion_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,13 @@ func spokeKubeadmConfigSpec(in *KubeadmConfigSpec, c randfill.Continue) {
154154
in.UseExperimentalRetryJoin = false
155155

156156
dropEmptyStringsKubeadmConfigSpec(in)
157+
158+
for i, user := range in.Users {
159+
if user.PasswdFrom != nil && reflect.DeepEqual(user.PasswdFrom, &PasswdSource{}) {
160+
user.PasswdFrom = nil
161+
}
162+
in.Users[i] = user
163+
}
157164
}
158165

159166
func spokeClusterConfiguration(in *ClusterConfiguration, c randfill.Continue) {

api/bootstrap/kubeadm/v1beta1/zz_generated.conversion.go

Lines changed: 12 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/bootstrap/kubeadm/v1beta2/kubeadmconfig_types.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package v1beta2
1818

1919
import (
2020
"fmt"
21+
"reflect"
2122

2223
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2324
"k8s.io/apimachinery/pkg/util/validation/field"
@@ -266,7 +267,7 @@ func (c *KubeadmConfigSpec) validateUsers(pathPrefix *field.Path) field.ErrorLis
266267

267268
for i := range c.Users {
268269
user := c.Users[i]
269-
if user.Passwd != "" && user.PasswdFrom != nil {
270+
if user.Passwd != "" && user.PasswdFrom.IsDefined() {
270271
allErrs = append(
271272
allErrs,
272273
field.Invalid(
@@ -279,7 +280,7 @@ func (c *KubeadmConfigSpec) validateUsers(pathPrefix *field.Path) field.ErrorLis
279280
// n.b.: if we ever add types besides Secret as a PasswdFrom
280281
// Source, we must add webhook validation here for one of the
281282
// sources being non-nil.
282-
if user.PasswdFrom != nil {
283+
if user.PasswdFrom.IsDefined() {
283284
if user.PasswdFrom.Secret.Name == "" {
284285
allErrs = append(
285286
allErrs,
@@ -680,6 +681,11 @@ type PasswdSource struct {
680681
Secret SecretPasswdSource `json:"secret,omitempty,omitzero"`
681682
}
682683

684+
// IsDefined returns true if the PasswdSource is defined.
685+
func (r *PasswdSource) IsDefined() bool {
686+
return !reflect.DeepEqual(r, &PasswdSource{})
687+
}
688+
683689
// SecretPasswdSource adapts a Secret into a PasswdSource.
684690
//
685691
// The contents of the target Secret's Data field will be presented
@@ -742,7 +748,7 @@ type User struct {
742748

743749
// passwdFrom is a referenced source of passwd to populate the passwd.
744750
// +optional
745-
PasswdFrom *PasswdSource `json:"passwdFrom,omitempty"`
751+
PasswdFrom PasswdSource `json:"passwdFrom,omitempty,omitzero"`
746752

747753
// primaryGroup specifies the primary group for the user
748754
// +optional

api/bootstrap/kubeadm/v1beta2/zz_generated.deepcopy.go

Lines changed: 1 addition & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/controlplane/kubeadm/v1beta1/conversion_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,13 @@ func spokeKubeadmConfigSpec(in *bootstrapv1beta1.KubeadmConfigSpec, c randfill.C
255255
in.UseExperimentalRetryJoin = false
256256

257257
dropEmptyStringsKubeadmConfigSpec(in)
258+
259+
for i, user := range in.Users {
260+
if user.PasswdFrom != nil && reflect.DeepEqual(user.PasswdFrom, &bootstrapv1beta1.PasswdSource{}) {
261+
user.PasswdFrom = nil
262+
}
263+
in.Users[i] = user
264+
}
258265
}
259266

260267
func spokeClusterConfiguration(in *bootstrapv1beta1.ClusterConfiguration, c randfill.Continue) {

bootstrap/kubeadm/internal/controllers/kubeadmconfig_controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,12 +1010,12 @@ func (r *KubeadmConfigReconciler) resolveUsers(ctx context.Context, cfg *bootstr
10101010

10111011
for i := range cfg.Spec.Users {
10121012
in := cfg.Spec.Users[i]
1013-
if in.PasswdFrom != nil {
1013+
if in.PasswdFrom.IsDefined() {
10141014
data, err := r.resolveSecretPasswordContent(ctx, cfg.Namespace, in)
10151015
if err != nil {
10161016
return nil, errors.Wrapf(err, "failed to resolve passwd source")
10171017
}
1018-
in.PasswdFrom = nil
1018+
in.PasswdFrom = bootstrapv1.PasswdSource{}
10191019
passwdContent := string(data)
10201020
in.Passwd = passwdContent
10211021
}

bootstrap/kubeadm/internal/controllers/kubeadmconfig_controller_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2449,7 +2449,7 @@ func TestKubeadmConfigReconciler_ResolveUsers(t *testing.T) {
24492449
Users: []bootstrapv1.User{
24502450
{
24512451
Name: "foo",
2452-
PasswdFrom: &bootstrapv1.PasswdSource{
2452+
PasswdFrom: bootstrapv1.PasswdSource{
24532453
Secret: bootstrapv1.SecretPasswdSource{
24542454
Name: "source",
24552455
Key: "key",
@@ -2477,7 +2477,7 @@ func TestKubeadmConfigReconciler_ResolveUsers(t *testing.T) {
24772477
},
24782478
{
24792479
Name: "bar",
2480-
PasswdFrom: &bootstrapv1.PasswdSource{
2480+
PasswdFrom: bootstrapv1.PasswdSource{
24812481
Secret: bootstrapv1.SecretPasswdSource{
24822482
Name: "source",
24832483
Key: "key",
@@ -2518,7 +2518,7 @@ func TestKubeadmConfigReconciler_ResolveUsers(t *testing.T) {
25182518
// from secret still are.
25192519
passwdFrom := map[string]bool{}
25202520
for _, user := range tc.cfg.Spec.Users {
2521-
if user.PasswdFrom != nil {
2521+
if user.PasswdFrom.IsDefined() {
25222522
passwdFrom[user.Name] = true
25232523
}
25242524
}
@@ -2528,7 +2528,7 @@ func TestKubeadmConfigReconciler_ResolveUsers(t *testing.T) {
25282528
g.Expect(users).To(BeComparableTo(tc.expect))
25292529
for _, user := range tc.cfg.Spec.Users {
25302530
if passwdFrom[user.Name] {
2531-
g.Expect(user.PasswdFrom).NotTo(BeNil())
2531+
g.Expect(user.PasswdFrom.IsDefined()).To(BeTrue())
25322532
g.Expect(user.Passwd).To(BeEmpty())
25332533
}
25342534
}

bootstrap/kubeadm/internal/webhooks/kubeadmconfig_test.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ func TestKubeadmConfigValidate(t *testing.T) {
174174
Spec: bootstrapv1.KubeadmConfigSpec{
175175
Users: []bootstrapv1.User{
176176
{
177-
PasswdFrom: &bootstrapv1.PasswdSource{
177+
PasswdFrom: bootstrapv1.PasswdSource{
178178
Secret: bootstrapv1.SecretPasswdSource{
179179
Name: "foo",
180180
Key: "bar",
@@ -194,8 +194,12 @@ func TestKubeadmConfigValidate(t *testing.T) {
194194
Spec: bootstrapv1.KubeadmConfigSpec{
195195
Users: []bootstrapv1.User{
196196
{
197-
PasswdFrom: &bootstrapv1.PasswdSource{},
198-
Passwd: "foo",
197+
PasswdFrom: bootstrapv1.PasswdSource{
198+
Secret: bootstrapv1.SecretPasswdSource{
199+
Name: "secret",
200+
},
201+
},
202+
Passwd: "foo",
199203
},
200204
},
201205
},
@@ -211,7 +215,7 @@ func TestKubeadmConfigValidate(t *testing.T) {
211215
Spec: bootstrapv1.KubeadmConfigSpec{
212216
Users: []bootstrapv1.User{
213217
{
214-
PasswdFrom: &bootstrapv1.PasswdSource{
218+
PasswdFrom: bootstrapv1.PasswdSource{
215219
Secret: bootstrapv1.SecretPasswdSource{
216220
Key: "bar",
217221
},
@@ -232,7 +236,7 @@ func TestKubeadmConfigValidate(t *testing.T) {
232236
Spec: bootstrapv1.KubeadmConfigSpec{
233237
Users: []bootstrapv1.User{
234238
{
235-
PasswdFrom: &bootstrapv1.PasswdSource{
239+
PasswdFrom: bootstrapv1.PasswdSource{
236240
Secret: bootstrapv1.SecretPasswdSource{
237241
Name: "foo",
238242
},

0 commit comments

Comments
 (0)