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 6, 2022
1 parent c53383d commit 02580ca
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 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 @@ -74,6 +75,13 @@ func init() {
}

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 +147,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 +214,14 @@ 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)
}
if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
log.Error(err, "unable to set up ready check")
}

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

Expand Down

0 comments on commit 02580ca

Please sign in to comment.