Skip to content

Commit a532f2f

Browse files
committed
add reconcile reources flag
1 parent 07dcd40 commit a532f2f

File tree

2 files changed

+97
-1
lines changed

2 files changed

+97
-1
lines changed

pkg/config/config.go

+71
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ const (
6464
flagReconcileDefaultMaxConcurrency = "reconcile-default-max-concurrent-syncs"
6565
flagReconcileResourceMaxConcurrency = "reconcile-resource-max-concurrent-syncs"
6666
flagFeatureGates = "feature-gates"
67+
flagReconcileResources = "reconcile-resources"
6768
envVarAWSRegion = "AWS_REGION"
6869
)
6970

@@ -104,6 +105,7 @@ type Config struct {
104105
ReconcileResourceResyncSeconds []string
105106
ReconcileDefaultMaxConcurrency int
106107
ReconcileResourceMaxConcurrency []string
108+
ReconcileResources string
107109
// TODO(a-hilaly): migrate to k8s.io/component-base and implement a proper parser for feature gates.
108110
FeatureGates featuregate.FeatureGates
109111
featureGatesRaw string
@@ -250,6 +252,11 @@ func (cfg *Config) BindFlags() {
250252
"Valid keys are feature names and valid values are 'true' or 'false'."+
251253
"Available features: "+strings.Join(featuregate.GetDefaultFeatureGates().GetFeatureNames(), ", "),
252254
)
255+
flag.StringVar(
256+
&cfg.ReconcileResources, flagReconcileResources,
257+
"",
258+
"A comma-separated list of resource kinds to reconcile. If unspecified, all resources will be reconciled.",
259+
)
253260
}
254261

255262
// SetupLogger initializes the logger used in the service controller
@@ -389,6 +396,12 @@ func (cfg *Config) validateReconcileConfigResources(supportedGVKs []schema.Group
389396
return fmt.Errorf("invalid value for flag '%s': %v", flagReconcileResourceMaxConcurrency, err)
390397
}
391398
}
399+
400+
// Also validate the resource filter settings
401+
if err := cfg.validateReconcileResources(supportedGVKs); err != nil {
402+
return err
403+
}
404+
392405
return nil
393406
}
394407

@@ -564,3 +577,61 @@ func parseFeatureGates(featureGatesRaw string) (map[string]bool, error) {
564577

565578
return featureGatesMap, nil
566579
}
580+
581+
// GetReconcileResources returns a slice of resource kinds that should be reconciled.
582+
func (cfg *Config) GetReconcileResources() ([]string, error) {
583+
return parseReconcileResourcesString(cfg.ReconcileResources)
584+
}
585+
586+
// parseReconcileResourcesString parses the reconcileResources flag and returns a slice
587+
// of resource kinds to reconcile.
588+
func parseReconcileResourcesString(resources string) ([]string, error) {
589+
resources = strings.TrimSpace(resources)
590+
if resources == "" {
591+
return nil, nil
592+
}
593+
594+
visited := make(map[string]bool)
595+
resourceKinds := []string{}
596+
597+
for _, kind := range strings.Split(resources, ",") {
598+
kind = strings.TrimSpace(kind)
599+
if kind == "" {
600+
return nil, fmt.Errorf("invalid resource kind: empty kind")
601+
}
602+
if _, ok := visited[kind]; ok {
603+
return nil, fmt.Errorf("duplicate resource kind '%s'", kind)
604+
}
605+
visited[kind] = true
606+
resourceKinds = append(resourceKinds, kind)
607+
}
608+
return resourceKinds, nil
609+
}
610+
611+
// validateReconcileResources validates that the specified resource kinds are supported by the controller.
612+
func (cfg *Config) validateReconcileResources(supportedGVKs []schema.GroupVersionKind) error {
613+
resources, err := cfg.GetReconcileResources()
614+
if err != nil {
615+
return fmt.Errorf("invalid value for flag '%s': %v", flagReconcileResources, err)
616+
}
617+
if len(resources) == 0 {
618+
return nil
619+
}
620+
621+
validResourceKinds := make([]string, 0, len(supportedGVKs))
622+
for _, gvk := range supportedGVKs {
623+
validResourceKinds = append(validResourceKinds, gvk.Kind)
624+
}
625+
626+
for _, resource := range resources {
627+
if !ackutil.InStrings(resource, validResourceKinds) {
628+
return fmt.Errorf(
629+
"invalid value for flag '%s': resource kind '%s' is not supported by this controller. Valid resource kinds are: %s",
630+
flagReconcileResources,
631+
resource,
632+
strings.Join(validResourceKinds, ", "),
633+
)
634+
}
635+
}
636+
return nil
637+
}

pkg/runtime/service_controller.go

+26-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
ackmetrics "github.com/aws-controllers-k8s/runtime/pkg/metrics"
3535
ackrtcache "github.com/aws-controllers-k8s/runtime/pkg/runtime/cache"
3636
acktypes "github.com/aws-controllers-k8s/runtime/pkg/types"
37+
ackutil "github.com/aws-controllers-k8s/runtime/pkg/util"
3738
)
3839

3940
const (
@@ -271,7 +272,31 @@ func (c *serviceController) BindControllerManager(mgr ctrlrt.Manager, cfg ackcfg
271272
c.fieldExportReconciler = rec
272273
}
273274

274-
for _, rmf := range c.rmFactories {
275+
// Get the list of resources to reconcile from the config
276+
reconcileResources, err := cfg.GetReconcileResources()
277+
if err != nil {
278+
return fmt.Errorf("error parsing reconcile resources: %v", err)
279+
}
280+
281+
// Filter the resource manager factories
282+
filteredRMFs := c.rmFactories
283+
if len(reconcileResources) > 0 {
284+
285+
filteredRMFs = make(map[string]acktypes.AWSResourceManagerFactory)
286+
for key, rmf := range c.rmFactories {
287+
rd := rmf.ResourceDescriptor()
288+
resourceKind := rd.GroupVersionKind().Kind
289+
290+
if ackutil.InStrings(resourceKind, reconcileResources) {
291+
filteredRMFs[key] = rmf
292+
c.log.Info("including reconciler for resource kind", "kind", resourceKind)
293+
} else {
294+
c.log.Info("excluding reconciler for resource kind", "kind", resourceKind, "reason", "not in reconcile-resources flag")
295+
}
296+
}
297+
}
298+
299+
for _, rmf := range filteredRMFs {
275300
rec := NewReconciler(c, rmf, c.log, cfg, c.metrics, cache)
276301
if err := rec.BindControllerManager(mgr); err != nil {
277302
return err

0 commit comments

Comments
 (0)