Skip to content

Commit 37e5033

Browse files
authored
Merge pull request #12560 from sbueringer/pr-passwd-ssa
⚠️ Change User.PasswdFrom from *PasswdSource to PasswdSource + add omitzero, extend SSA patch helper to handle arrays
2 parents a4fb649 + aad7f3c commit 37e5033

File tree

13 files changed

+194
-81
lines changed

13 files changed

+194
-81
lines changed

api/bootstrap/kubeadm/v1beta1/conversion.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,31 @@ func Convert_v1beta2_ClusterConfiguration_To_v1beta1_ClusterConfiguration(in *bo
859859
return autoConvert_v1beta2_ClusterConfiguration_To_v1beta1_ClusterConfiguration(in, out, s)
860860
}
861861

862+
func Convert_v1beta1_User_To_v1beta2_User(in *User, out *bootstrapv1.User, s apimachineryconversion.Scope) error {
863+
if err := autoConvert_v1beta1_User_To_v1beta2_User(in, out, s); err != nil {
864+
return err
865+
}
866+
if in.PasswdFrom != nil {
867+
if err := Convert_v1beta1_PasswdSource_To_v1beta2_PasswdSource(in.PasswdFrom, &out.PasswdFrom, s); err != nil {
868+
return err
869+
}
870+
}
871+
return nil
872+
}
873+
874+
func Convert_v1beta2_User_To_v1beta1_User(in *bootstrapv1.User, out *User, s apimachineryconversion.Scope) error {
875+
if err := autoConvert_v1beta2_User_To_v1beta1_User(in, out, s); err != nil {
876+
return err
877+
}
878+
if in.PasswdFrom.IsDefined() {
879+
out.PasswdFrom = &PasswdSource{}
880+
if err := Convert_v1beta2_PasswdSource_To_v1beta1_PasswdSource(&in.PasswdFrom, out.PasswdFrom, s); err != nil {
881+
return err
882+
}
883+
}
884+
return nil
885+
}
886+
862887
func Convert_v1beta1_File_To_v1beta2_File(in *File, out *bootstrapv1.File, s apimachineryconversion.Scope) error {
863888
if err := autoConvert_v1beta1_File_To_v1beta2_File(in, out, s); err != nil {
864889
return err

api/bootstrap/kubeadm/v1beta1/conversion_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,12 @@ func spokeKubeadmConfigSpec(in *KubeadmConfigSpec, c randfill.Continue) {
168168
}
169169
in.Files[i] = file
170170
}
171+
for i, user := range in.Users {
172+
if user.PasswdFrom != nil && reflect.DeepEqual(user.PasswdFrom, &PasswdSource{}) {
173+
user.PasswdFrom = nil
174+
}
175+
in.Users[i] = user
176+
}
171177
}
172178

173179
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: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ func (c *KubeadmConfigSpec) validateUsers(pathPrefix *field.Path) field.ErrorLis
256256

257257
for i := range c.Users {
258258
user := c.Users[i]
259-
if user.Passwd != "" && user.PasswdFrom != nil {
259+
if user.Passwd != "" && user.PasswdFrom.IsDefined() {
260260
allErrs = append(
261261
allErrs,
262262
field.Invalid(
@@ -269,7 +269,7 @@ func (c *KubeadmConfigSpec) validateUsers(pathPrefix *field.Path) field.ErrorLis
269269
// n.b.: if we ever add types besides Secret as a PasswdFrom
270270
// Source, we must add webhook validation here for one of the
271271
// sources being non-nil.
272-
if user.PasswdFrom != nil {
272+
if user.PasswdFrom.IsDefined() {
273273
if user.PasswdFrom.Secret.Name == "" {
274274
allErrs = append(
275275
allErrs,
@@ -681,6 +681,11 @@ type PasswdSource struct {
681681
Secret SecretPasswdSource `json:"secret,omitempty,omitzero"`
682682
}
683683

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

744749
// passwdFrom is a referenced source of passwd to populate the passwd.
745750
// +optional
746-
PasswdFrom *PasswdSource `json:"passwdFrom,omitempty"`
751+
PasswdFrom PasswdSource `json:"passwdFrom,omitempty,omitzero"`
747752

748753
// primaryGroup specifies the primary group for the user
749754
// +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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,12 @@ func spokeKubeadmConfigSpec(in *bootstrapv1beta1.KubeadmConfigSpec, c randfill.C
291291
}
292292
in.Files[i] = file
293293
}
294+
for i, user := range in.Users {
295+
if user.PasswdFrom != nil && reflect.DeepEqual(user.PasswdFrom, &bootstrapv1beta1.PasswdSource{}) {
296+
user.PasswdFrom = nil
297+
}
298+
in.Users[i] = user
299+
}
294300
}
295301

296302
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
@@ -1026,12 +1026,12 @@ func (r *KubeadmConfigReconciler) resolveUsers(ctx context.Context, cfg *bootstr
10261026

10271027
for i := range cfg.Spec.Users {
10281028
in := cfg.Spec.Users[i]
1029-
if in.PasswdFrom != nil {
1029+
if in.PasswdFrom.IsDefined() {
10301030
data, err := r.resolveSecretPasswordContent(ctx, cfg.Namespace, in)
10311031
if err != nil {
10321032
return nil, errors.Wrapf(err, "failed to resolve passwd source")
10331033
}
1034-
in.PasswdFrom = nil
1034+
in.PasswdFrom = bootstrapv1.PasswdSource{}
10351035
passwdContent := string(data)
10361036
in.Passwd = passwdContent
10371037
}

bootstrap/kubeadm/internal/controllers/kubeadmconfig_controller_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2445,7 +2445,7 @@ func TestKubeadmConfigReconciler_ResolveUsers(t *testing.T) {
24452445
Users: []bootstrapv1.User{
24462446
{
24472447
Name: "foo",
2448-
PasswdFrom: &bootstrapv1.PasswdSource{
2448+
PasswdFrom: bootstrapv1.PasswdSource{
24492449
Secret: bootstrapv1.SecretPasswdSource{
24502450
Name: "source",
24512451
Key: "key",
@@ -2473,7 +2473,7 @@ func TestKubeadmConfigReconciler_ResolveUsers(t *testing.T) {
24732473
},
24742474
{
24752475
Name: "bar",
2476-
PasswdFrom: &bootstrapv1.PasswdSource{
2476+
PasswdFrom: bootstrapv1.PasswdSource{
24772477
Secret: bootstrapv1.SecretPasswdSource{
24782478
Name: "source",
24792479
Key: "key",
@@ -2514,7 +2514,7 @@ func TestKubeadmConfigReconciler_ResolveUsers(t *testing.T) {
25142514
// from secret still are.
25152515
passwdFrom := map[string]bool{}
25162516
for _, user := range tc.cfg.Spec.Users {
2517-
if user.PasswdFrom != nil {
2517+
if user.PasswdFrom.IsDefined() {
25182518
passwdFrom[user.Name] = true
25192519
}
25202520
}
@@ -2524,7 +2524,7 @@ func TestKubeadmConfigReconciler_ResolveUsers(t *testing.T) {
25242524
g.Expect(users).To(BeComparableTo(tc.expect))
25252525
for _, user := range tc.cfg.Spec.Users {
25262526
if passwdFrom[user.Name] {
2527-
g.Expect(user.PasswdFrom).NotTo(BeNil())
2527+
g.Expect(user.PasswdFrom.IsDefined()).To(BeTrue())
25282528
g.Expect(user.Passwd).To(BeEmpty())
25292529
}
25302530
}

bootstrap/kubeadm/internal/webhooks/kubeadmconfig_test.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ func TestKubeadmConfigValidate(t *testing.T) {
178178
Spec: bootstrapv1.KubeadmConfigSpec{
179179
Users: []bootstrapv1.User{
180180
{
181-
PasswdFrom: &bootstrapv1.PasswdSource{
181+
PasswdFrom: bootstrapv1.PasswdSource{
182182
Secret: bootstrapv1.SecretPasswdSource{
183183
Name: "foo",
184184
Key: "bar",
@@ -198,8 +198,12 @@ func TestKubeadmConfigValidate(t *testing.T) {
198198
Spec: bootstrapv1.KubeadmConfigSpec{
199199
Users: []bootstrapv1.User{
200200
{
201-
PasswdFrom: &bootstrapv1.PasswdSource{},
202-
Passwd: "foo",
201+
PasswdFrom: bootstrapv1.PasswdSource{
202+
Secret: bootstrapv1.SecretPasswdSource{
203+
Name: "secret",
204+
},
205+
},
206+
Passwd: "foo",
203207
},
204208
},
205209
},
@@ -215,7 +219,7 @@ func TestKubeadmConfigValidate(t *testing.T) {
215219
Spec: bootstrapv1.KubeadmConfigSpec{
216220
Users: []bootstrapv1.User{
217221
{
218-
PasswdFrom: &bootstrapv1.PasswdSource{
222+
PasswdFrom: bootstrapv1.PasswdSource{
219223
Secret: bootstrapv1.SecretPasswdSource{
220224
Key: "bar",
221225
},
@@ -236,7 +240,7 @@ func TestKubeadmConfigValidate(t *testing.T) {
236240
Spec: bootstrapv1.KubeadmConfigSpec{
237241
Users: []bootstrapv1.User{
238242
{
239-
PasswdFrom: &bootstrapv1.PasswdSource{
243+
PasswdFrom: bootstrapv1.PasswdSource{
240244
Secret: bootstrapv1.SecretPasswdSource{
241245
Name: "foo",
242246
},

internal/api/bootstrap/kubeadm/v1alpha3/conversion.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func RestoreKubeadmConfigSpec(dst *bootstrapv1.KubeadmConfigSpec, restored *boot
8686
dst.Users = restored.Users
8787
if restored.Users != nil {
8888
for i := range restored.Users {
89-
if restored.Users[i].PasswdFrom != nil {
89+
if restored.Users[i].PasswdFrom.IsDefined() {
9090
dst.Users[i].PasswdFrom = restored.Users[i].PasswdFrom
9191
}
9292
}

0 commit comments

Comments
 (0)