Skip to content
This repository was archived by the owner on Apr 11, 2024. It is now read-only.

Commit a1cd64f

Browse files
committed
Change Sudo field from pointer to value
The zero value (empty string) is not valid, so the field does not need to be a pointer.
1 parent 2e3ead5 commit a1cd64f

File tree

6 files changed

+50
-11
lines changed

6 files changed

+50
-11
lines changed

api/v1alpha1/clusterconfig_types.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,9 +378,10 @@ type User struct {
378378
// +optional
379379
SSHAuthorizedKeys []string `json:"sshAuthorizedKeys,omitempty"`
380380

381-
// Sudo specifies a sudo role for the user
381+
// Sudo specifies a sudo role for the user.
382+
// An empty string is not marshalled, because it is not a valid value.
382383
// +optional
383-
Sudo *string `json:"sudo,omitempty"`
384+
Sudo string `json:"sudo,omitempty"`
384385
}
385386

386387
func (User) VariableSchema() clusterv1.VariableSchema {

api/v1alpha1/zz_generated.deepcopy.go

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

pkg/handlers/generic/mutation/users/inject.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ func generateBootstrapUser(userFromVariable v1alpha1.User) bootstrapv1.User {
120120
bootstrapUser := bootstrapv1.User{
121121
Name: userFromVariable.Name,
122122
SSHAuthorizedKeys: userFromVariable.SSHAuthorizedKeys,
123-
Sudo: userFromVariable.Sudo,
124123
}
125124

126125
// LockPassword is not part of our API, because we can derive its value
@@ -140,5 +139,9 @@ func generateBootstrapUser(userFromVariable v1alpha1.User) bootstrapv1.User {
140139
bootstrapUser.Passwd = ptr.To(userFromVariable.HashedPassword)
141140
}
142141

142+
if userFromVariable.Sudo != "" {
143+
bootstrapUser.Sudo = ptr.To(userFromVariable.Sudo)
144+
}
145+
143146
return bootstrapUser
144147
}

pkg/handlers/generic/mutation/users/inject_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,47 @@ func Test_generateBootstrapUser(t *testing.T) {
6363
LockPassword: ptr.To(true),
6464
},
6565
},
66+
{
67+
name: "if user sets sudo, include it in the patch",
68+
args: args{
69+
userFromVariable: v1alpha1.User{
70+
Name: "example",
71+
Sudo: "example",
72+
},
73+
},
74+
want: bootstrapv1.User{
75+
Name: "example",
76+
Sudo: ptr.To("example"),
77+
LockPassword: ptr.To(true),
78+
},
79+
},
80+
{
81+
name: "if user does not set sudo, do not include in the patch",
82+
args: args{
83+
userFromVariable: v1alpha1.User{
84+
Name: "example",
85+
},
86+
},
87+
want: bootstrapv1.User{
88+
Name: "example",
89+
Sudo: nil,
90+
LockPassword: ptr.To(true),
91+
},
92+
},
93+
{
94+
name: "if user sets empty sudo, do not include in the patch",
95+
args: args{
96+
userFromVariable: v1alpha1.User{
97+
Name: "example",
98+
Sudo: "",
99+
},
100+
},
101+
want: bootstrapv1.User{
102+
Name: "example",
103+
Sudo: nil,
104+
LockPassword: ptr.To(true),
105+
},
106+
},
66107
}
67108
for _, tt := range tests {
68109
t.Run(tt.name, func(t *testing.T) {

pkg/handlers/generic/mutation/users/tests/generate_patches.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88

99
"github.com/onsi/gomega"
1010
"k8s.io/apiserver/pkg/storage/names"
11-
"k8s.io/utils/ptr"
1211
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
1312

1413
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
@@ -25,7 +24,7 @@ var (
2524
"key1",
2625
"key2",
2726
},
28-
Sudo: ptr.To("ALL=(ALL) NOPASSWD:ALL"),
27+
Sudo: "ALL=(ALL) NOPASSWD:ALL",
2928
}
3029
testUser2 = v1alpha1.User{
3130
Name: "onlyname",

pkg/handlers/generic/mutation/users/variables_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func TestVariableValidation(t *testing.T) {
3232
"key1",
3333
"key2",
3434
},
35-
Sudo: ptr.To("ALL=(ALL) NOPASSWD:ALL"),
35+
Sudo: "ALL=(ALL) NOPASSWD:ALL",
3636
},
3737
{
3838
Name: "onlyname",

0 commit comments

Comments
 (0)