Skip to content

Commit b83e3c4

Browse files
committedJul 13, 2024·
fix: Use controller-runtime's defaulting mechanism for serviceAccountName
1 parent a686a38 commit b83e3c4

7 files changed

+17
-17
lines changed
 

‎api/v1alpha1/objecttemplate_types.go

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type ObjectTemplateSpec struct {
4141
// ServiceAccountName specifies the name of the Kubernetes service account to impersonate
4242
// when reconciling this ObjectTemplate. If omitted, the "default" service account is used
4343
// +optional
44+
// +kubebuilder:default:="default"
4445
ServiceAccountName string `json:"serviceAccountName,omitempty"`
4546

4647
// Prune enables pruning of previously created objects when these disappear from the list of rendered objects

‎api/v1alpha1/texttemplate_types.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ type TextTemplateSpec struct {
3131
// +kubebuilder:default:=false
3232
Suspend bool `json:"suspend"`
3333

34-
// The name of the Kubernetes service account to impersonate
35-
// when reconciling this TextTemplate. If omitted, the "default" service account is used.
34+
// ServiceAccountName specifies the name of the Kubernetes service account to impersonate
35+
// when reconciling this TextTemplate. If omitted, the "default" service account is used
3636
// +optional
37+
// +kubebuilder:default:="default"
3738
ServiceAccountName string `json:"serviceAccountName,omitempty"`
3839

3940
// +optional

‎config/crd/bases/templates.kluctl.io_objecttemplates.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ spec:
108108
these disappear from the list of rendered objects
109109
type: boolean
110110
serviceAccountName:
111+
default: default
111112
description: |-
112113
ServiceAccountName specifies the name of the Kubernetes service account to impersonate
113114
when reconciling this ObjectTemplate. If omitted, the "default" service account is used

‎config/crd/bases/templates.kluctl.io_texttemplates.yaml

+3-2
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,10 @@ spec:
7171
type: object
7272
type: array
7373
serviceAccountName:
74+
default: default
7475
description: |-
75-
The name of the Kubernetes service account to impersonate
76-
when reconciling this TextTemplate. If omitted, the "default" service account is used.
76+
ServiceAccountName specifies the name of the Kubernetes service account to impersonate
77+
when reconciling this TextTemplate. If omitted, the "default" service account is used
7778
type: string
7879
suspend:
7980
default: false

‎controllers/base_template_reconciler.go

+4-8
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,17 @@ type BaseTemplateReconciler struct {
3131
mutex sync.Mutex
3232
}
3333

34-
func (r *BaseTemplateReconciler) getClientForObjects(serviceAccountName string, objNamespace string) (client.WithWatch, string, error) {
34+
func (r *BaseTemplateReconciler) getClientForObjects(serviceAccountName string, objNamespace string) (client.WithWatch, error) {
3535
restConfig := rest.CopyConfig(r.Manager.GetConfig())
3636

37-
name := "default"
38-
if serviceAccountName != "" {
39-
name = serviceAccountName
40-
}
41-
username := fmt.Sprintf("system:serviceaccount:%s:%s", objNamespace, name)
37+
username := fmt.Sprintf("system:serviceaccount:%s:%s", objNamespace, serviceAccountName)
4238
restConfig.Impersonate = rest.ImpersonationConfig{UserName: username}
4339

4440
c, err := client.NewWithWatch(restConfig, client.Options{Mapper: r.RESTMapper()})
4541
if err != nil {
46-
return nil, "", err
42+
return nil, err
4743
}
48-
return c, name, nil
44+
return c, nil
4945
}
5046

5147
func (r *BaseTemplateReconciler) buildBaseVars(templateObj runtime.Object, objVarName string) (map[string]any, error) {

‎controllers/objecttemplate_controller.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -184,13 +184,13 @@ func (r *ObjectTemplateReconciler) doReconcile(ctx context.Context, rt *template
184184
var wg sync.WaitGroup
185185
var mutex sync.Mutex
186186

187-
objClient, saName, err := r.getClientForObjects(rt.Spec.ServiceAccountName, rt.GetNamespace())
187+
objClient, err := r.getClientForObjects(rt.Spec.ServiceAccountName, rt.GetNamespace())
188188
if err != nil {
189189
return err
190190
}
191191

192192
wt := r.watchesUtil.getWatchesForTemplate(client.ObjectKeyFromObject(rt))
193-
wt.setClient(objClient, saName)
193+
wt.setClient(objClient, rt.Spec.ServiceAccountName)
194194
newObjects := map[templatesv1alpha1.ObjectRef]bool{}
195195
for _, me := range rt.Spec.Matrix {
196196
if me.Object != nil {
@@ -466,7 +466,7 @@ func (r *ObjectTemplateReconciler) doFinalize(ctx context.Context, obj *template
466466
return
467467
}
468468

469-
objClient, _, err := r.getClientForObjects(obj.Spec.ServiceAccountName, obj.GetNamespace())
469+
objClient, err := r.getClientForObjects(obj.Spec.ServiceAccountName, obj.GetNamespace())
470470
if err != nil {
471471
log.Error(err, "Failed to create objClient for deletion")
472472
return

‎controllers/texttemplate_controller.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,13 @@ func (r *TextTemplateReconciler) doReconcile(ctx context.Context, tt *templatesv
111111
}
112112
defer j2.Close()
113113

114-
objClient, saName, err := r.getClientForObjects(tt.Spec.ServiceAccountName, tt.GetNamespace())
114+
objClient, err := r.getClientForObjects(tt.Spec.ServiceAccountName, tt.GetNamespace())
115115
if err != nil {
116116
return err
117117
}
118118

119119
wt := r.watchesUtil.getWatchesForTemplate(client.ObjectKeyFromObject(tt))
120-
wt.setClient(objClient, saName)
120+
wt.setClient(objClient, tt.Spec.ServiceAccountName)
121121
newObjects := map[templatesv1alpha1.ObjectRef]bool{}
122122
if tt.Spec.TemplateRef != nil && tt.Spec.TemplateRef.ConfigMap != nil {
123123
ns := tt.Spec.TemplateRef.ConfigMap.Namespace

0 commit comments

Comments
 (0)
Please sign in to comment.