Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,6 @@ func main() {
os.Exit(1)
}

leaderElectionID := fmt.Sprintf("%s-%s", controllerName, "leader-election")

// Configure the manager with the GitOps Toolkit runtime options.
mgrConfig := ctrlruntime.Options{
Scheme: scheme,
Expand All @@ -155,7 +153,6 @@ func main() {
},
HealthProbeBindAddress: healthAddr,
LeaderElection: leaderElectionOptions.Enable,
LeaderElectionID: leaderElectionID,
LeaderElectionReleaseOnCancel: leaderElectionOptions.ReleaseOnCancel,
LeaseDuration: &leaderElectionOptions.LeaseDuration,
RenewDeadline: &leaderElectionOptions.RenewDeadline,
Expand All @@ -174,6 +171,11 @@ func main() {
},
}

if err := applyWatchOptions(&mgrConfig, controllerName, watchOptions); err != nil {
setupLog.Error(err, "unable to configure watch label selector for manager")
os.Exit(1)
}

// Limit the watch scope to the runtime namespace if specified.
if !watchOptions.AllNamespaces {
mgrConfig.Cache.DefaultNamespaces = map[string]ctrlcache.Config{
Expand Down Expand Up @@ -246,6 +248,27 @@ func main() {
}
}

// applyWatchOptions configures the manager cache and leader election for watch selectors.
func applyWatchOptions(mgrConfig *ctrlruntime.Options, controllerName string, watchOptions gotkctrl.WatchOptions) error {
watchSelector, err := gotkctrl.GetWatchSelector(watchOptions)
if err != nil {
return err
}

leaderElectionID := fmt.Sprintf("%s-%s", controllerName, "leader-election")
if watchOptions.LabelSelector != "" {
leaderElectionID = gotkelection.GenerateID(leaderElectionID, watchOptions.LabelSelector)
}
mgrConfig.LeaderElectionID = leaderElectionID

if mgrConfig.Cache.ByObject == nil {
mgrConfig.Cache.ByObject = make(map[ctrlclient.Object]ctrlcache.ByObject)
}
mgrConfig.Cache.ByObject[&swapi.ArtifactGenerator{}] = ctrlcache.ByObject{Label: watchSelector}

return nil
}

func mustSetupEventRecorder(mgr ctrlruntime.Manager, eventsAddr, controllerName string) record.EventRecorder {
eventRecorder, err := gotkevents.NewRecorder(mgr, ctrlruntime.Log, eventsAddr, controllerName)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ with advanced source composition and decomposition patterns.
| `--storage-adv-addr` | string | The advertised address of the static file server. |
| `--storage-path` | string | The local storage path. (default "/data") |
| `--watch-all-namespaces` | boolean | Watch for resources in all namespaces, if set to false it will only watch the runtime namespace. (default true) |
| `--watch-label-selector` | string | Watch for resources with matching labels e.g. 'sharding.fluxcd.io/shard=shard1'. |
| `--feature-gates` | mapStringBool | A comma separated list of key=value pairs defining the state of experimental features. |

### Feature Gates
Expand Down
Loading