Skip to content

Commit 2398445

Browse files
authored
Merge pull request #2021 from HoustonPutman/book-empty-examples-v3
📖 Update static book examples to Kubebuilder v3.
2 parents b46beea + 5afa97e commit 2398445

File tree

4 files changed

+68
-19
lines changed

4 files changed

+68
-19
lines changed

docs/book/src/cronjob-tutorial/testdata/emptyapi.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,9 @@ a Kind. Then, the `object` generator generates an implementation of the
6868
interface that all types representing Kinds must implement.
6969
*/
7070

71-
// +kubebuilder:object:root=true
72-
// +kubebuilder:subresource:status
71+
//+kubebuilder:object:root=true
72+
//+kubebuilder:subresource:status
73+
7374
// CronJob is the Schema for the cronjobs API
7475
type CronJob struct {
7576
metav1.TypeMeta `json:",inline"`
@@ -79,7 +80,7 @@ type CronJob struct {
7980
Status CronJobStatus `json:"status,omitempty"`
8081
}
8182

82-
// +kubebuilder:object:root=true
83+
//+kubebuilder:object:root=true
8384

8485
// CronJobList contains a list of CronJob
8586
type CronJobList struct {

docs/book/src/cronjob-tutorial/testdata/emptycontroller.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ logging works by attaching key-value pairs to a static message. We can pre-assi
7777
some pairs at the top of our reconcile method to have those attached to all log
7878
lines in this reconciler.
7979
*/
80-
func (r *CronJobReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
81-
_ = context.Background()
80+
func (r *CronJobReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
8281
_ = r.Log.WithValues("cronjob", req.NamespacedName)
8382

8483
// your logic here

docs/book/src/cronjob-tutorial/testdata/emptymain.go

+52-10
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,17 @@ import (
3030
"fmt"
3131
"os"
3232

33+
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
34+
// to ensure that exec-entrypoint and run can make use of them.
35+
_ "k8s.io/client-go/plugin/pkg/client/auth"
36+
3337
"k8s.io/apimachinery/pkg/runtime"
38+
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
39+
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
3440
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
3541
ctrl "sigs.k8s.io/controller-runtime"
3642
"sigs.k8s.io/controller-runtime/pkg/cache"
43+
"sigs.k8s.io/controller-runtime/pkg/healthz"
3744
"sigs.k8s.io/controller-runtime/pkg/log/zap"
3845
// +kubebuilder:scaffold:imports
3946
)
@@ -51,8 +58,9 @@ var (
5158
)
5259

5360
func init() {
61+
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
5462

55-
// +kubebuilder:scaffold:scheme
63+
//+kubebuilder:scaffold:scheme
5664
}
5765

5866
/*
@@ -79,12 +87,29 @@ soon.
7987

8088
func main() {
8189
var metricsAddr string
82-
flag.StringVar(&metricsAddr, "metrics-addr", ":8080", "The address the metric endpoint binds to.")
90+
var enableLeaderElection bool
91+
var probeAddr string
92+
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
93+
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
94+
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
95+
"Enable leader election for controller manager. "+
96+
"Enabling this will ensure there is only one active controller manager.")
97+
opts := zap.Options{
98+
Development: true,
99+
}
100+
opts.BindFlags(flag.CommandLine)
83101
flag.Parse()
84102

85-
ctrl.SetLogger(zap.New(zap.UseDevMode(true)))
103+
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
86104

87-
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{Scheme: scheme, MetricsBindAddress: metricsAddr})
105+
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
106+
Scheme: scheme,
107+
MetricsBindAddress: metricsAddr,
108+
Port: 9443,
109+
HealthProbeBindAddress: probeAddr,
110+
LeaderElection: enableLeaderElection,
111+
LeaderElectionID: "80807133.tutorial.kubebuilder.io",
112+
})
88113
if err != nil {
89114
setupLog.Error(err, "unable to start manager")
90115
os.Exit(1)
@@ -95,9 +120,13 @@ func main() {
95120
*/
96121

97122
mgr, err = ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
98-
Scheme: scheme,
99-
Namespace: namespace,
100-
MetricsBindAddress: metricsAddr,
123+
Scheme: scheme,
124+
Namespace: namespace,
125+
MetricsBindAddress: metricsAddr,
126+
Port: 9443,
127+
HealthProbeBindAddress: probeAddr,
128+
LeaderElection: enableLeaderElection,
129+
LeaderElectionID: "80807133.tutorial.kubebuilder.io",
101130
})
102131

103132
/*
@@ -112,9 +141,13 @@ func main() {
112141
var namespaces []string // List of Namespaces
113142

114143
mgr, err = ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
115-
Scheme: scheme,
116-
NewCache: cache.MultiNamespacedCacheBuilder(namespaces),
117-
MetricsBindAddress: fmt.Sprintf("%s:%d", metricsHost, metricsPort),
144+
Scheme: scheme,
145+
NewCache: cache.MultiNamespacedCacheBuilder(namespaces),
146+
MetricsBindAddress: fmt.Sprintf("%s:%d", metricsHost, metricsPort),
147+
Port: 9443,
148+
HealthProbeBindAddress: probeAddr,
149+
LeaderElection: enableLeaderElection,
150+
LeaderElectionID: "80807133.tutorial.kubebuilder.io",
118151
})
119152

120153
/*
@@ -123,6 +156,15 @@ func main() {
123156

124157
// +kubebuilder:scaffold:builder
125158

159+
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
160+
setupLog.Error(err, "unable to set up health check")
161+
os.Exit(1)
162+
}
163+
if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
164+
setupLog.Error(err, "unable to set up ready check")
165+
os.Exit(1)
166+
}
167+
126168
setupLog.Info("starting manager")
127169
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
128170
setupLog.Error(err, "problem running manager")

docs/book/src/cronjob-tutorial/testdata/finalizer_example.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,19 @@ import (
3333

3434
// +kubebuilder:docs-gen:collapse=Imports
3535

36+
/*
37+
By default, kubebuilder will include the RBAC rules necessary to update finalizers for CronJobs.
38+
*/
39+
40+
//+kubebuilder:rbac:groups=batch.tutorial.kubebuilder.io,resources=cronjobs,verbs=get;list;watch;create;update;patch;delete
41+
//+kubebuilder:rbac:groups=batch.tutorial.kubebuilder.io,resources=cronjobs/status,verbs=get;update;patch
42+
//+kubebuilder:rbac:groups=batch.tutorial.kubebuilder.io,resources=cronjobs/finalizers,verbs=update
43+
3644
/*
3745
The code snippet below shows skeleton code for implementing a finalizer.
3846
*/
3947

40-
func (r *CronJobReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
41-
ctx := context.Background()
48+
func (r *CronJobReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
4249
log := r.Log.WithValues("cronjob", req.NamespacedName)
4350

4451
var cronJob *batchv1.CronJob
@@ -60,7 +67,7 @@ func (r *CronJobReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
6067
// registering our finalizer.
6168
if !containsString(cronJob.GetFinalizers(), myFinalizerName) {
6269
cronJob.SetFinalizers(append(cronJob.GetFinalizers(), myFinalizerName))
63-
if err := r.Update(context.Background(), cronJob); err != nil {
70+
if err := r.Update(ctx, cronJob); err != nil {
6471
return ctrl.Result{}, err
6572
}
6673
}
@@ -76,7 +83,7 @@ func (r *CronJobReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
7683

7784
// remove our finalizer from the list and update it.
7885
cronJob.SetFinalizers(removeString(cronJob.GetFinalizers(), myFinalizerName))
79-
if err := r.Update(context.Background(), cronJob); err != nil {
86+
if err := r.Update(ctx, cronJob); err != nil {
8087
return ctrl.Result{}, err
8188
}
8289
}

0 commit comments

Comments
 (0)