@@ -25,7 +25,6 @@ import (
25
25
"helm.sh/helm/v3/pkg/kube"
26
26
"helm.sh/helm/v3/pkg/storage"
27
27
"helm.sh/helm/v3/pkg/storage/driver"
28
- corev1 "k8s.io/api/core/v1"
29
28
"k8s.io/apimachinery/pkg/api/meta"
30
29
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
31
30
"k8s.io/client-go/discovery"
@@ -57,14 +56,25 @@ func NewActionConfigGetter(baseRestConfig *rest.Config, rm meta.RESTMapper, opts
57
56
if acg .objectToClientNamespace == nil {
58
57
acg .objectToClientNamespace = getObjectNamespace
59
58
}
60
- if acg .objectToStorageNamespace == nil {
61
- acg .objectToStorageNamespace = getObjectNamespace
59
+ if acg .objectToClientRestConfig == nil {
60
+ acg .objectToClientRestConfig = func (_ context.Context , _ client.Object , baseRestConfig * rest.Config ) (* rest.Config , error ) {
61
+ return rest .CopyConfig (baseRestConfig ), nil
62
+ }
62
63
}
63
- if acg .objectToRestConfig == nil {
64
- acg .objectToRestConfig = func (_ context.Context , _ client.Object , baseRestConfig * rest.Config ) (* rest.Config , error ) {
64
+ if acg .objectToStorageRestConfig == nil {
65
+ acg .objectToStorageRestConfig = func (_ context.Context , _ client.Object , baseRestConfig * rest.Config ) (* rest.Config , error ) {
65
66
return rest .CopyConfig (baseRestConfig ), nil
66
67
}
67
68
}
69
+ if acg .objectToStorageDriver == nil {
70
+ if acg .objectToStorageNamespace == nil {
71
+ acg .objectToStorageNamespace = getObjectNamespace
72
+ }
73
+ acg .objectToStorageDriver = DefaultSecretsStorageDriver (SecretsStorageDriverOpts {
74
+ DisableOwnerRefInjection : acg .disableStorageOwnerRefInjection ,
75
+ StorageNamespaceMapper : acg .objectToStorageNamespace ,
76
+ })
77
+ }
68
78
return acg , nil
69
79
}
70
80
@@ -73,28 +83,52 @@ var _ ActionConfigGetter = &actionConfigGetter{}
73
83
type ActionConfigGetterOption func (getter * actionConfigGetter )
74
84
75
85
type ObjectToStringMapper func (client.Object ) (string , error )
86
+ type ObjectToRestConfigMapper func (context.Context , client.Object , * rest.Config ) (* rest.Config , error )
87
+ type ObjectToStorageDriverMapper func (context.Context , client.Object , * rest.Config ) (driver.Driver , error )
88
+
89
+ func ClientRestConfigMapper (f ObjectToRestConfigMapper ) ActionConfigGetterOption { // nolint:revive
90
+ return func (getter * actionConfigGetter ) {
91
+ getter .objectToClientRestConfig = f
92
+ }
93
+ }
76
94
77
95
func ClientNamespaceMapper (m ObjectToStringMapper ) ActionConfigGetterOption { // nolint:revive
78
96
return func (getter * actionConfigGetter ) {
79
97
getter .objectToClientNamespace = m
80
98
}
81
99
}
82
100
101
+ func StorageRestConfigMapper (f ObjectToRestConfigMapper ) ActionConfigGetterOption {
102
+ return func (getter * actionConfigGetter ) {
103
+ getter .objectToStorageRestConfig = f
104
+ }
105
+ }
106
+
107
+ func StorageDriverMapper (f ObjectToStorageDriverMapper ) ActionConfigGetterOption {
108
+ return func (getter * actionConfigGetter ) {
109
+ getter .objectToStorageDriver = f
110
+ }
111
+ }
112
+
113
+ // Deprecated: use StorageDriverMapper(DefaultSecretsStorageDriver(SecretsStorageDriverOpts)) instead.
83
114
func StorageNamespaceMapper (m ObjectToStringMapper ) ActionConfigGetterOption {
84
115
return func (getter * actionConfigGetter ) {
85
116
getter .objectToStorageNamespace = m
86
117
}
87
118
}
88
119
120
+ // Deprecated: use StorageDriverMapper(DefaultSecretsStorageDriver(SecretsStorageDriverOpts)) instead.
89
121
func DisableStorageOwnerRefInjection (v bool ) ActionConfigGetterOption {
90
122
return func (getter * actionConfigGetter ) {
91
123
getter .disableStorageOwnerRefInjection = v
92
124
}
93
125
}
94
126
127
+ // Deprecated: use ClientRestConfigMapper and StorageRestConfigMapper instead.
95
128
func RestConfigMapper (f func (context.Context , client.Object , * rest.Config ) (* rest.Config , error )) ActionConfigGetterOption {
96
129
return func (getter * actionConfigGetter ) {
97
- getter .objectToRestConfig = f
130
+ getter .objectToClientRestConfig = f
131
+ getter .objectToStorageRestConfig = f
98
132
}
99
133
}
100
134
@@ -107,58 +141,53 @@ type actionConfigGetter struct {
107
141
restMapper meta.RESTMapper
108
142
discoveryClient discovery.CachedDiscoveryInterface
109
143
110
- objectToClientNamespace ObjectToStringMapper
111
- objectToStorageNamespace ObjectToStringMapper
112
- objectToRestConfig func (context.Context , client.Object , * rest.Config ) (* rest.Config , error )
144
+ objectToClientRestConfig ObjectToRestConfigMapper
145
+ objectToClientNamespace ObjectToStringMapper
146
+
147
+ objectToStorageRestConfig ObjectToRestConfigMapper
148
+ objectToStorageDriver ObjectToStorageDriverMapper
149
+
150
+ // Deprecated: only keep around for backward compatibility with StorageNamespaceMapper option.
151
+ objectToStorageNamespace ObjectToStringMapper
152
+ // Deprecated: only keep around for backward compatibility with DisableStorageOwnerRefInjection option.
113
153
disableStorageOwnerRefInjection bool
114
154
}
115
155
116
156
func (acg * actionConfigGetter ) ActionConfigFor (ctx context.Context , obj client.Object ) (* action.Configuration , error ) {
117
- storageNs , err := acg .objectToStorageNamespace ( obj )
157
+ clientRestConfig , err := acg .objectToClientRestConfig ( ctx , obj , acg . baseRestConfig )
118
158
if err != nil {
119
- return nil , fmt .Errorf ("get storage namespace for object: %v" , err )
120
- }
121
-
122
- restConfig , err := acg .objectToRestConfig (ctx , obj , acg .baseRestConfig )
123
- if err != nil {
124
- return nil , fmt .Errorf ("get rest config for object: %v" , err )
159
+ return nil , fmt .Errorf ("get client rest config for object: %v" , err )
125
160
}
126
161
127
162
clientNamespace , err := acg .objectToClientNamespace (obj )
128
163
if err != nil {
129
164
return nil , fmt .Errorf ("get client namespace for object: %v" , err )
130
165
}
131
166
132
- rcg := newRESTClientGetter (restConfig , acg .restMapper , acg .discoveryClient , clientNamespace )
133
- kc := kube .New (rcg )
134
- kc .Namespace = clientNamespace
135
-
136
- kcs , err := kc .Factory .KubernetesClientSet ()
137
- if err != nil {
138
- return nil , fmt .Errorf ("create kubernetes clientset: %v" , err )
139
- }
167
+ clientRCG := newRESTClientGetter (clientRestConfig , acg .restMapper , acg .discoveryClient , clientNamespace )
168
+ clientKC := kube .New (clientRCG )
169
+ clientKC .Namespace = clientNamespace
140
170
141
171
// Setup the debug log function that Helm will use
142
172
debugLog := getDebugLogger (ctx )
143
173
144
- secretClient := kcs .CoreV1 ().Secrets (storageNs )
145
- if ! acg .disableStorageOwnerRefInjection {
146
- ownerRef := metav1 .NewControllerRef (obj , obj .GetObjectKind ().GroupVersionKind ())
147
- secretClient = & ownerRefSecretClient {
148
- SecretInterface : secretClient ,
149
- refs : []metav1.OwnerReference {* ownerRef },
150
- }
174
+ storageRestConfig , err := acg .objectToStorageRestConfig (ctx , obj , acg .baseRestConfig )
175
+ if err != nil {
176
+ return nil , fmt .Errorf ("get storage rest config for object: %v" , err )
177
+ }
178
+
179
+ d , err := acg .objectToStorageDriver (ctx , obj , storageRestConfig )
180
+ if err != nil {
181
+ return nil , fmt .Errorf ("get storage driver for object: %v" , err )
151
182
}
152
- d := driver .NewSecrets (secretClient )
153
- d .Log = debugLog
154
183
155
184
// Initialize the storage backend
156
185
s := storage .Init (d )
157
186
158
187
return & action.Configuration {
159
- RESTClientGetter : rcg ,
188
+ RESTClientGetter : clientRCG ,
160
189
Releases : s ,
161
- KubeClient : kc ,
190
+ KubeClient : clientKC ,
162
191
Log : debugLog ,
163
192
}, nil
164
193
}
@@ -173,19 +202,32 @@ func getDebugLogger(ctx context.Context) func(format string, v ...interface{}) {
173
202
}
174
203
}
175
204
176
- var _ v1.SecretInterface = & ownerRefSecretClient {}
177
-
178
- type ownerRefSecretClient struct {
179
- v1.SecretInterface
180
- refs []metav1.OwnerReference
205
+ type SecretsStorageDriverOpts struct {
206
+ DisableOwnerRefInjection bool
207
+ StorageNamespaceMapper ObjectToStringMapper
181
208
}
182
209
183
- func (c * ownerRefSecretClient ) Create (ctx context.Context , in * corev1.Secret , opts metav1.CreateOptions ) (* corev1.Secret , error ) {
184
- in .OwnerReferences = append (in .OwnerReferences , c .refs ... )
185
- return c .SecretInterface .Create (ctx , in , opts )
186
- }
210
+ func DefaultSecretsStorageDriver (opts SecretsStorageDriverOpts ) ObjectToStorageDriverMapper {
211
+ if opts .StorageNamespaceMapper == nil {
212
+ opts .StorageNamespaceMapper = getObjectNamespace
213
+ }
214
+ return func (ctx context.Context , obj client.Object , restConfig * rest.Config ) (driver.Driver , error ) {
215
+ storageNamespace , err := opts .StorageNamespaceMapper (obj )
216
+ if err != nil {
217
+ return nil , fmt .Errorf ("get storage namespace for object: %v" , err )
218
+ }
219
+ secretsInterface , err := v1 .NewForConfig (restConfig )
220
+ if err != nil {
221
+ return nil , fmt .Errorf ("create secrets client for storage: %v" , err )
222
+ }
187
223
188
- func (c * ownerRefSecretClient ) Update (ctx context.Context , in * corev1.Secret , opts metav1.UpdateOptions ) (* corev1.Secret , error ) {
189
- in .OwnerReferences = append (in .OwnerReferences , c .refs ... )
190
- return c .SecretInterface .Update (ctx , in , opts )
224
+ secretClient := secretsInterface .Secrets (storageNamespace )
225
+ if ! opts .DisableOwnerRefInjection {
226
+ ownerRef := metav1 .NewControllerRef (obj , obj .GetObjectKind ().GroupVersionKind ())
227
+ secretClient = NewOwnerRefSecretClient (secretClient , []metav1.OwnerReference {* ownerRef }, MatchAllSecrets )
228
+ }
229
+ d := driver .NewSecrets (secretClient )
230
+ d .Log = getDebugLogger (ctx )
231
+ return d , nil
232
+ }
191
233
}
0 commit comments