@@ -26,6 +26,7 @@ import (
26
26
apierrors "k8s.io/apimachinery/pkg/api/errors"
27
27
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28
28
"k8s.io/apimachinery/pkg/runtime"
29
+ "k8s.io/apimachinery/pkg/types"
29
30
"k8s.io/klog/v2"
30
31
"k8s.io/utils/pointer"
31
32
ctrl "sigs.k8s.io/controller-runtime"
@@ -145,6 +146,41 @@ func (r *EKSConfigReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
145
146
return r .joinWorker (ctx , cluster , config , configOwner )
146
147
}
147
148
149
+ func (r * EKSConfigReconciler ) resolveFiles (ctx context.Context , cfg * eksbootstrapv1.EKSConfig ) ([]eksbootstrapv1.File , error ) {
150
+ collected := make ([]eksbootstrapv1.File , 0 , len (cfg .Spec .Files ))
151
+
152
+ for i := range cfg .Spec .Files {
153
+ in := cfg .Spec .Files [i ]
154
+ if in .ContentFrom != nil {
155
+ data , err := r .resolveSecretFileContent (ctx , cfg .Namespace , in )
156
+ if err != nil {
157
+ return nil , errors .Wrapf (err , "failed to resolve file source" )
158
+ }
159
+ in .ContentFrom = nil
160
+ in .Content = string (data )
161
+ }
162
+ collected = append (collected , in )
163
+ }
164
+
165
+ return collected , nil
166
+ }
167
+
168
+ func (r * EKSConfigReconciler ) resolveSecretFileContent (ctx context.Context , ns string , source eksbootstrapv1.File ) ([]byte , error ) {
169
+ secret := & corev1.Secret {}
170
+ key := types.NamespacedName {Namespace : ns , Name : source .ContentFrom .Secret .Name }
171
+ if err := r .Client .Get (ctx , key , secret ); err != nil {
172
+ if apierrors .IsNotFound (err ) {
173
+ return nil , errors .Wrapf (err , "secret not found: %s" , key )
174
+ }
175
+ return nil , errors .Wrapf (err , "failed to retrieve Secret %q" , key )
176
+ }
177
+ data , ok := secret .Data [source .ContentFrom .Secret .Key ]
178
+ if ! ok {
179
+ return nil , errors .Errorf ("secret references non-existent secret key: %q" , source .ContentFrom .Secret .Key )
180
+ }
181
+ return data , nil
182
+ }
183
+
148
184
func (r * EKSConfigReconciler ) joinWorker (ctx context.Context , cluster * clusterv1.Cluster , config * eksbootstrapv1.EKSConfig , configOwner * bsutil.ConfigOwner ) (ctrl.Result , error ) {
149
185
log := logger .FromContext (ctx )
150
186
@@ -191,6 +227,12 @@ func (r *EKSConfigReconciler) joinWorker(ctx context.Context, cluster *clusterv1
191
227
}
192
228
193
229
log .Info ("Generating userdata" )
230
+ files , err := r .resolveFiles (ctx , config )
231
+ if err != nil {
232
+ log .Info ("Failed to resolve files for user data" )
233
+ conditions .MarkFalse (config , eksbootstrapv1 .DataSecretAvailableCondition , eksbootstrapv1 .DataSecretGenerationFailedReason , clusterv1 .ConditionSeverityWarning , err .Error ())
234
+ return ctrl.Result {}, err
235
+ }
194
236
195
237
nodeInput := & userdata.NodeInput {
196
238
// AWSManagedControlPlane webhooks default and validate EKSClusterName
@@ -208,7 +250,7 @@ func (r *EKSConfigReconciler) joinWorker(ctx context.Context, cluster *clusterv1
208
250
Users : config .Spec .Users ,
209
251
DiskSetup : config .Spec .DiskSetup ,
210
252
Mounts : config .Spec .Mounts ,
211
- Files : config . Spec . Files ,
253
+ Files : files ,
212
254
}
213
255
if config .Spec .PauseContainer != nil {
214
256
nodeInput .PauseContainerAccount = & config .Spec .PauseContainer .AccountNumber
0 commit comments