From c13c62748b3c291e78f2c810ebc596c12256f046 Mon Sep 17 00:00:00 2001 From: Burak Sekili Date: Sat, 21 Sep 2024 12:08:50 +0300 Subject: [PATCH 1/2] Enable status subresource in ChaosPod At the moment, even though reconciler works on ChaosPod instance's Status field, the status subresource is not enabled on ChaosPod. This commit adds `// +kubebuilder:subresource:status` marker to allow the generator tool to enable the status subresource on the custom resource definition. --- examples/crd/pkg/resource.go | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/crd/pkg/resource.go b/examples/crd/pkg/resource.go index 80800a23cb..9622d3c753 100644 --- a/examples/crd/pkg/resource.go +++ b/examples/crd/pkg/resource.go @@ -35,6 +35,7 @@ type ChaosPodStatus struct { } // +kubebuilder:object:root=true +// +kubebuilder:subresource:status // ChaosPod is the Schema for the randomjobs API // +kubebuilder:printcolumn:name="next stop",type="string",JSONPath=".spec.nextStop",format="date" From b70639c8bbfc178dac2d9d57948c42666df78502 Mon Sep 17 00:00:00 2001 From: Burak Sekili Date: Sat, 21 Sep 2024 12:50:00 +0300 Subject: [PATCH 2/2] Update scheme initialization in crd example. Add crd and corev1 schemas to the current scheme and generate manager with this new populated scheme Signed-off-by: Burak Sekili --- examples/crd/main.go | 30 ++++++++++++++++++++------- examples/crd/pkg/groupversion_info.go | 1 + 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/examples/crd/main.go b/examples/crd/main.go index 1f6cd5fac2..c6f629233b 100644 --- a/examples/crd/main.go +++ b/examples/crd/main.go @@ -25,6 +25,7 @@ import ( corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/runtime" + utilruntime "k8s.io/apimachinery/pkg/util/runtime" _ "k8s.io/client-go/plugin/pkg/client/auth/gcp" ctrl "sigs.k8s.io/controller-runtime" api "sigs.k8s.io/controller-runtime/examples/crd/pkg" @@ -48,8 +49,11 @@ func (r *reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu var chaospod api.ChaosPod if err := r.Get(ctx, req.NamespacedName, &chaospod); err != nil { - log.Error(err, "unable to get chaosctl") - return ctrl.Result{}, err + if !apierrors.IsNotFound(err) { + log.Error(err, "unable to get chaosctl") + } + + return ctrl.Result{}, client.IgnoreNotFound(err) } var pod corev1.Pod @@ -93,27 +97,37 @@ func (r *reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu } chaospod.Spec.NextStop.Time = time.Now().Add(time.Duration(10*(rand.Int63n(2)+1)) * time.Second) - chaospod.Status.LastRun = pod.CreationTimestamp if err := r.Update(ctx, &chaospod); err != nil { log.Error(err, "unable to update chaosctl status") return ctrl.Result{}, err } + + chaospod.Status.LastRun = pod.CreationTimestamp + err := r.Status().Update(ctx, &chaospod) + if err != nil { + log.Error(err, "unable to update chaosctl status") + return ctrl.Result{}, err + } + return ctrl.Result{}, nil } func main() { ctrl.SetLogger(zap.New()) - mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{}) + scheme := runtime.NewScheme() + utilruntime.Must(corev1.AddToScheme(scheme)) + err := api.AddToScheme(scheme) if err != nil { - setupLog.Error(err, "unable to start manager") + setupLog.Error(err, "unable to add scheme") os.Exit(1) } - // in a real controller, we'd create a new scheme for this - err = api.AddToScheme(mgr.GetScheme()) + mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{ + Scheme: scheme, + }) if err != nil { - setupLog.Error(err, "unable to add scheme") + setupLog.Error(err, "unable to start manager") os.Exit(1) } diff --git a/examples/crd/pkg/groupversion_info.go b/examples/crd/pkg/groupversion_info.go index 04953dd939..24270989c0 100644 --- a/examples/crd/pkg/groupversion_info.go +++ b/examples/crd/pkg/groupversion_info.go @@ -16,6 +16,7 @@ limitations under the License. // +kubebuilder:object:generate=true // +groupName=chaosapps.metamagical.io +// +versionName=v1 package pkg import (