Skip to content

Commit

Permalink
manager/docker: add liveness/readiness probes
Browse files Browse the repository at this point in the history
  • Loading branch information
cpanato committed Mar 2, 2020
1 parent 308f931 commit 71a4eb5
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 17 deletions.
12 changes: 12 additions & 0 deletions test/infrastructure/docker/config/manager/manager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ spec:
- --enable-leader-election
image: controller:latest
name: manager
ports:
- containerPort: 9440
name: healthz
protocol: TCP
readinessProbe:
httpGet:
path: /readyz
port: healthz
livenessProbe:
httpGet:
path: /healthz
port: healthz
volumeMounts:
- mountPath: /var/run/docker.sock
name: dockersock
Expand Down
57 changes: 40 additions & 17 deletions test/infrastructure/docker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,20 @@ import (
"sigs.k8s.io/cluster-api/test/infrastructure/docker/controllers"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/healthz"
// +kubebuilder:scaffold:imports
)

var (
myscheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")

//flags
metricsAddr string
enableLeaderElection bool
syncPeriod time.Duration
concurrency int
healthAddr string
)

func init() {
Expand All @@ -48,32 +56,54 @@ func init() {

func main() {
klog.InitFlags(nil)
var metricsAddr string
var enableLeaderElection bool
var syncPeriod time.Duration
var concurrency int
flag.StringVar(&metricsAddr, "metrics-addr", ":8080", "The address the metric endpoint binds to.")
flag.IntVar(&concurrency, "concurrency", 10, "The number of docker machines to process simultaneously")
flag.BoolVar(&enableLeaderElection, "enable-leader-election", false,
"Enable leader election for controller manager. Enabling this will ensure there is only one active controller manager.")
flag.DurationVar(&syncPeriod, "sync-period", 10*time.Minute,
"The minimum interval at which watched resources are reconciled (e.g. 15m)")
flag.StringVar(&healthAddr, "health-addr", ":9440", "The address the health endpoint binds to.")
flag.Parse()

ctrl.SetLogger(klogr.New())

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: myscheme,
MetricsBindAddress: metricsAddr,
LeaderElection: enableLeaderElection,
LeaderElectionID: "controller-leader-election-capd",
SyncPeriod: &syncPeriod,
Scheme: myscheme,
MetricsBindAddress: metricsAddr,
LeaderElection: enableLeaderElection,
LeaderElectionID: "controller-leader-election-capd",
SyncPeriod: &syncPeriod,
HealthProbeBindAddress: healthAddr,
})
if err != nil {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
}

setupChecks(mgr)
setupReconcilers(mgr)

// +kubebuilder:scaffold:builder
setupLog.Info("starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "problem running manager")
os.Exit(1)
}
}

func setupChecks(mgr ctrl.Manager) {
if err := mgr.AddReadyzCheck("ping", healthz.Ping); err != nil {
setupLog.Error(err, "unable to create ready check")
os.Exit(1)
}

if err := mgr.AddHealthzCheck("ping", healthz.Ping); err != nil {
setupLog.Error(err, "unable to create health check")
os.Exit(1)
}
}

func setupReconcilers(mgr ctrl.Manager) {
if err := (&controllers.DockerMachineReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("DockerMachine"),
Expand All @@ -84,18 +114,11 @@ func main() {
os.Exit(1)
}

if err = (&controllers.DockerClusterReconciler{
if err := (&controllers.DockerClusterReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("DockerCluster"),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "DockerCluster")
os.Exit(1)
}
// +kubebuilder:scaffold:builder

setupLog.Info("starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "problem running manager")
os.Exit(1)
}
}

0 comments on commit 71a4eb5

Please sign in to comment.