@@ -27,11 +27,13 @@ import (
27
27
"github.com/jaypipes/envutil"
28
28
flag "github.com/spf13/pflag"
29
29
"go.uber.org/zap/zapcore"
30
+ "k8s.io/apimachinery/pkg/runtime/schema"
30
31
ctrlrt "sigs.k8s.io/controller-runtime"
31
32
"sigs.k8s.io/controller-runtime/pkg/log/zap"
32
33
33
34
ackv1alpha1 "github.com/aws-controllers-k8s/runtime/apis/core/v1alpha1"
34
35
acktags "github.com/aws-controllers-k8s/runtime/pkg/tags"
36
+ ackutil "github.com/aws-controllers-k8s/runtime/pkg/util"
35
37
)
36
38
37
39
const (
@@ -211,7 +213,15 @@ func (cfg *Config) SetAWSAccountID() error {
211
213
}
212
214
213
215
// Validate ensures the options are valid
214
- func (cfg * Config ) Validate () error {
216
+ func (cfg * Config ) Validate (options ... Option ) error {
217
+ merged := mergeOptions (options )
218
+ if len (merged .gks ) > 0 {
219
+ err := cfg .validateReconcileConfigResources (merged .gks )
220
+ if err != nil {
221
+ return fmt .Errorf ("invalid value for flag '%s': %v" , flagReconcileResourceResyncSeconds , err )
222
+ }
223
+ }
224
+
215
225
if cfg .Region == "" {
216
226
return errors .New ("unable to start service controller as AWS region is missing. Please pass --aws-region flag or set AWS_REGION environment variable" )
217
227
}
@@ -257,12 +267,6 @@ func (cfg *Config) Validate() error {
257
267
if cfg .ReconcileDefaultResyncSeconds < 0 {
258
268
return fmt .Errorf ("invalid value for flag '%s': resync seconds default must be greater than 0" , flagReconcileDefaultResyncSeconds )
259
269
}
260
-
261
- _ , err := cfg .ParseReconcileResourceResyncSeconds ()
262
- if err != nil {
263
- return fmt .Errorf ("invalid value for flag '%s': %v" , flagReconcileResourceResyncSeconds , err )
264
- }
265
-
266
270
return nil
267
271
}
268
272
@@ -275,6 +279,28 @@ func (cfg *Config) checkUnsafeEndpoint(endpoint *url.URL) error {
275
279
return nil
276
280
}
277
281
282
+ // validateReconcileConfigResources validates the --reconcile-resource-resync-seconds flag
283
+ // by checking the resource names and their corresponding duration.
284
+ func (cfg * Config ) validateReconcileConfigResources (supportedGVKs []* schema.GroupKind ) error {
285
+ validResourceNames := []string {}
286
+ for _ , gvk := range supportedGVKs {
287
+ validResourceNames = append (validResourceNames , gvk .Kind )
288
+ }
289
+ for _ , resourceResyncSecondsFlag := range cfg .ReconcileResourceResyncSeconds {
290
+ resourceName , _ , err := parseReconcileFlagArgument (resourceResyncSecondsFlag )
291
+ if err != nil {
292
+ return fmt .Errorf ("error parsing flag argument '%v': %v. Expected format: resource=seconds" , resourceResyncSecondsFlag , err )
293
+ }
294
+ if ! ackutil .InStrings (resourceName , validResourceNames ) {
295
+ return fmt .Errorf (
296
+ "error parsing flag argument '%v': resource '%v' is not managed by this controller. Expected one of %v" ,
297
+ resourceResyncSecondsFlag , resourceName , strings .Join (validResourceNames , ", " ),
298
+ )
299
+ }
300
+ }
301
+ return nil
302
+ }
303
+
278
304
// ParseReconcileResourceResyncSeconds parses the values of the --reconcile-resource-resync-seconds
279
305
// flag and returns a map that maps resource names to resync periods.
280
306
// The flag arguments are expected to have the format "resource=seconds", where "resource" is the
@@ -284,10 +310,7 @@ func (cfg *Config) ParseReconcileResourceResyncSeconds() (map[string]time.Durati
284
310
resourceResyncPeriods := make (map [string ]time.Duration , len (cfg .ReconcileResourceResyncSeconds ))
285
311
for _ , resourceResyncSecondsFlag := range cfg .ReconcileResourceResyncSeconds {
286
312
// Parse the resource name and resync period from the flag argument
287
- resourceName , resyncSeconds , err := parseReconcileFlagArgument (resourceResyncSecondsFlag )
288
- if err != nil {
289
- return nil , fmt .Errorf ("error parsing flag argument '%v': %v. Expected format: resource=seconds" , resourceResyncSecondsFlag , err )
290
- }
313
+ resourceName , resyncSeconds , _ := parseReconcileFlagArgument (resourceResyncSecondsFlag )
291
314
resourceResyncPeriods [strings .ToLower (resourceName )] = time .Duration (resyncSeconds )
292
315
}
293
316
return resourceResyncPeriods , nil
0 commit comments