Skip to content

Commit

Permalink
Add health and readyness probes
Browse files Browse the repository at this point in the history
and use the new ctrl.NewManager, which is an alias of manager.Manager to
create the manager.

Various options are defined at
https://godocs.io/sigs.k8s.io/controller-runtime/pkg/manager#Options.

Health and readyness logic is not implemented yet but are added for
future usage.

Part of epic: submariner-io/enhancements#81

Signed-off-by: Janki Chhatbar <[email protected]>
  • Loading branch information
Jaanki committed Sep 7, 2022
1 parent 831d21f commit ad2d164
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ import (
"k8s.io/client-go/dynamic"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/controller-runtime/pkg/healthz"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
)

Expand All @@ -73,7 +74,15 @@ func init() {
flag.BoolVar(&help, "help", help, "Print usage options")
}

// nolint:gocyclo // No further refactors necessary
func main() {
var enableLeaderElection bool
var probeAddr string
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")

kzerolog.AddFlags(nil)
flag.Parse()

Expand Down Expand Up @@ -139,11 +148,14 @@ func main() {
// +kubebuilder:scaffold:scheme

// Create a new Cmd to provide shared dependencies and start components
mgr, err := manager.New(cfg, manager.Options{
Namespace: namespace,
MapperProvider: apiutil.NewDiscoveryRESTMapper,
MetricsBindAddress: fmt.Sprintf("%s:%d", metricsHost, metricsPort),
Scheme: scheme,
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: fmt.Sprintf("%s:%d", metricsHost, metricsPort),
HealthProbeBindAddress: probeAddr,
LeaderElection: enableLeaderElection,
LeaderElectionID: "2a1e5b0d.submariner.io",
Namespace: namespace, // namespaced-scope when the value is not an empty string
MapperProvider: apiutil.NewDiscoveryRESTMapper,
})
if err != nil {
log.Error(err, "unable to start manager")
Expand Down Expand Up @@ -203,6 +215,16 @@ func main() {

// +kubebuilder:scaffold:builder

if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
log.Error(err, "unable to set up health check")
os.Exit(1) // We might not want to exit here if healthchecks are not setup.
}

if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
log.Error(err, "unable to set up ready check")
os.Exit(1) // We might not want to exit here if ready checks are not setup.
}

// Start the Cmd
log.Info("Starting the Cmd.")

Expand Down

0 comments on commit ad2d164

Please sign in to comment.