@@ -20,6 +20,7 @@ import (
20
20
"context"
21
21
"fmt"
22
22
"os"
23
+ "time"
23
24
24
25
appsv1 "k8s.io/api/apps/v1"
25
26
corev1 "k8s.io/api/core/v1"
@@ -59,6 +60,50 @@ func Example() {
59
60
}
60
61
}
61
62
63
+ // This example creates a simple application Controller that is configured for ReplicaSets and Pods.
64
+ // This application controller will be running leader election with the provided configuration in the manager options.
65
+ // If leader election configuration is not provided, controller runs leader election with default values.
66
+ // Default values taken from: https://github.com/kubernetes/apiserver/blob/master/pkg/apis/config/v1alpha1/defaults.go
67
+ // defaultLeaseDuration = 15 * time.Second
68
+ // defaultRenewDeadline = 10 * time.Second
69
+ // defaultRetryPeriod = 2 * time.Second
70
+ //
71
+ // * Create a new application for ReplicaSets that manages Pods owned by the ReplicaSet and calls into
72
+ // ReplicaSetReconciler.
73
+ //
74
+ // * Start the application.
75
+ // TODO(pwittrock): Update this example when we have better dependency injection support
76
+ func Example_updateLeaderElectionDurations () {
77
+ var log = controllers .Log .WithName ("builder-examples" )
78
+ leaseDuration := 100 * time .Second
79
+ renewDeadline := 80 * time .Second
80
+ retryPeriod := 20 * time .Second
81
+ manager , err := controllers .NewManager (controllers .GetConfigOrDie (), controllers.Options {
82
+ LeaseDuration : & leaseDuration ,
83
+ RenewDeadline : & renewDeadline ,
84
+ RetryPeriod : & retryPeriod ,
85
+ })
86
+ if err != nil {
87
+ log .Error (err , "could not create manager" )
88
+ os .Exit (1 )
89
+ }
90
+
91
+ err = controllers .
92
+ NewControllerManagedBy (manager ). // Create the Controller
93
+ For (& appsv1.ReplicaSet {}). // ReplicaSet is the Application API
94
+ Owns (& corev1.Pod {}). // ReplicaSet owns Pods created by it
95
+ Complete (& ReplicaSetReconciler {Client : manager .GetClient ()})
96
+ if err != nil {
97
+ log .Error (err , "could not create controller" )
98
+ os .Exit (1 )
99
+ }
100
+
101
+ if err := manager .Start (controllers .SetupSignalHandler ()); err != nil {
102
+ log .Error (err , "could not start manager" )
103
+ os .Exit (1 )
104
+ }
105
+ }
106
+
62
107
// ReplicaSetReconciler is a simple Controller example implementation.
63
108
type ReplicaSetReconciler struct {
64
109
client.Client
0 commit comments