Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix nil reference when hub-only controllers are disabled #98

Merged
Merged
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
47 changes: 41 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ func main() {
}

log.Info("Adding controllers to managers")
addControllers(mgrCtx, hubMgr, mgr)
addControllers(mgrCtx, hubCfg, hubMgr, mgr)

log.Info("Starting the controller managers")

Expand Down Expand Up @@ -700,9 +700,40 @@ func getFreeLocalAddr() (string, error) {
}

// addControllers sets up all controllers with their respective managers
func addControllers(ctx context.Context, hubMgr manager.Manager, managedMgr manager.Manager) {
func addControllers(ctx context.Context, hubCfg *rest.Config, hubMgr manager.Manager, managedMgr manager.Manager) {
// Set up all controllers for manager on managed cluster
var kubeClientHub kubernetes.Interface = kubernetes.NewForConfigOrDie(hubMgr.GetConfig())
var hubClient client.Client

if hubMgr == nil {
hubCache, err := cache.New(
hubCfg, cache.Options{Namespaces: []string{tool.Options.ClusterNamespaceOnHub}, Scheme: scheme},
)
if err != nil {
log.Error(err, "Failed to generate a cache to the hub cluster")
os.Exit(1)
}

go func() {
err := hubCache.Start(ctx)
if err != nil {
log.Error(err, "Failed to start the cache to the hub cluster")
os.Exit(1)
}
}()

hubClient, err = client.New(
hubCfg, client.Options{Scheme: scheme, Cache: &client.CacheOptions{Reader: hubCache}},
)

if err != nil {
log.Error(err, "Failed to generate a client to the hub cluster")
os.Exit(1)
}
} else {
hubClient = hubMgr.GetClient()
}

var kubeClientHub kubernetes.Interface = kubernetes.NewForConfigOrDie(hubCfg)

eventBroadcasterHub := record.NewBroadcaster()

Expand All @@ -714,7 +745,7 @@ func addControllers(ctx context.Context, hubMgr manager.Manager, managedMgr mana

if err := (&statussync.PolicyReconciler{
ClusterNamespaceOnHub: tool.Options.ClusterNamespaceOnHub,
HubClient: hubMgr.GetClient(),
HubClient: hubClient,
HubRecorder: hubRecorder,
ManagedClient: managedMgr.GetClient(),
ManagedRecorder: managedMgr.GetEventRecorderFor(statussync.ControllerName),
Expand Down Expand Up @@ -764,6 +795,10 @@ func addControllers(ctx context.Context, hubMgr manager.Manager, managedMgr mana
}

// Set up all controllers for manager on hub cluster
if tool.Options.DisableSpecSync {
return
}

var kubeClient kubernetes.Interface = kubernetes.NewForConfigOrDie(managedMgr.GetConfig())

eventBroadcaster := record.NewBroadcaster()
Expand All @@ -775,7 +810,7 @@ func addControllers(ctx context.Context, hubMgr manager.Manager, managedMgr mana
managedRecorder := eventBroadcaster.NewRecorder(eventsScheme, v1.EventSource{Component: specsync.ControllerName})

if err = (&specsync.PolicyReconciler{
HubClient: hubMgr.GetClient(),
HubClient: hubClient,
ManagedClient: managedMgr.GetClient(),
ManagedRecorder: managedRecorder,
Scheme: hubMgr.GetScheme(),
Expand All @@ -787,7 +822,7 @@ func addControllers(ctx context.Context, hubMgr manager.Manager, managedMgr mana
}

if err = (&secretsync.SecretReconciler{
Client: hubMgr.GetClient(),
Client: hubClient,
ManagedClient: managedMgr.GetClient(),
Scheme: hubMgr.GetScheme(),
TargetNamespace: tool.Options.ClusterNamespace,
Expand Down