(ctx context.Context, config *config.Config)
| 88 | } |
| 89 | |
| 90 | func Run(ctx context.Context, config *config.Config) error { |
| 91 | // To help debugging, immediately log version |
| 92 | klog.Infof("cloudshell-controller-manager version: %s", version.Get()) |
| 93 | klog.InfoS("Golang settings", "GOGC", os.Getenv("GOGC"), "GOMAXPROCS", os.Getenv("GOMAXPROCS"), "GOTRACEBACK", os.Getenv("GOTRACEBACK")) |
| 94 | |
| 95 | if !config.LeaderElection.LeaderElect { |
| 96 | return StartControllers(config, ctx.Done()) |
| 97 | } |
| 98 | |
| 99 | id, err := os.Hostname() |
| 100 | if err != nil { |
| 101 | return err |
| 102 | } |
| 103 | |
| 104 | // add an uniquifier so that two processes on the same host don't accidentally both become active |
| 105 | id += "_" + string(uuid.NewUUID()) |
| 106 | |
| 107 | rl, err := resourcelock.NewFromKubeconfig( |
| 108 | config.LeaderElection.ResourceLock, |
| 109 | config.LeaderElection.ResourceNamespace, |
| 110 | config.LeaderElection.ResourceName, |
| 111 | resourcelock.ResourceLockConfig{ |
| 112 | Identity: id, |
| 113 | EventRecorder: config.EventRecorder, |
| 114 | }, |
| 115 | config.Kubeconfig, |
| 116 | config.LeaderElection.RenewDeadline.Duration, |
| 117 | ) |
| 118 | if err != nil { |
| 119 | return fmt.Errorf("failed to create resource lock: %w", err) |
| 120 | } |
| 121 | |
| 122 | leaderelection.RunOrDie(context.TODO(), leaderelection.LeaderElectionConfig{ |
| 123 | Name: config.LeaderElection.ResourceName, |
| 124 | Lock: rl, |
| 125 | LeaseDuration: config.LeaderElection.LeaseDuration.Duration, |
| 126 | RenewDeadline: config.LeaderElection.RenewDeadline.Duration, |
| 127 | RetryPeriod: config.LeaderElection.RetryPeriod.Duration, |
| 128 | |
| 129 | Callbacks: leaderelection.LeaderCallbacks{ |
| 130 | OnStartedLeading: func(_ context.Context) { |
| 131 | _ = StartControllers(config, ctx.Done()) |
| 132 | }, |
| 133 | OnStoppedLeading: func() { |
| 134 | klog.InfoS("leaderelection lost") |
| 135 | }, |
| 136 | }, |
| 137 | }) |
| 138 | |
| 139 | return nil |
| 140 | } |
| 141 | |
| 142 | func StartControllers(c *config.Config, stopCh <-chan struct{}) error { |
| 143 | ownerExist, err := labels.NewRequirement(constants.WorkerOwnerLabelKey, selection.Exists, nil) |
no test coverage detected