Skip to content

Commit

Permalink
correction in the format, imports and rbac marker for events
Browse files Browse the repository at this point in the history
  • Loading branch information
NikhilSharmaWe committed Jul 6, 2022
1 parent 5454995 commit 748178a
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,15 @@ package {{ if and .MultiGroup .Resource.Group }}{{ .Resource.PackageName }}{{ el
import (
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"errors"
"context"
"time"
"fmt"
"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
Expand Down Expand Up @@ -101,7 +103,7 @@ type {{ .Resource.Kind }}Reconciler struct {
//+kubebuilder:rbac:groups={{ .Resource.QualifiedGroup }},resources={{ .Resource.Plural }},verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups={{ .Resource.QualifiedGroup }},resources={{ .Resource.Plural }}/status,verbs=get;update;patch
//+kubebuilder:rbac:groups={{ .Resource.QualifiedGroup }},resources={{ .Resource.Plural }}/finalizers,verbs=update
//+kubebuilder:rbac:groups={{ .Resource.QualifiedGroup }},resources=events,verbs=create;patch
//+kubebuilder:rbac:groups=core,resources=events,verbs=create;patch
//+kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=core,resources=pods,verbs=get;list;watch
Expand All @@ -126,7 +128,7 @@ func (r *{{ .Resource.Kind }}Reconciler) Reconcile(ctx context.Context, req ctrl
{{ lower .Resource.Kind }} := &{{ .Resource.ImportAlias }}.{{ .Resource.Kind }}{}
err := r.Get(ctx, req.NamespacedName, {{ lower .Resource.Kind }})
if err != nil {
if errors.IsNotFound(err) {
if apierrors.IsNotFound(err) {
// Request object not found, could have been deleted after reconcile request.
// Owned objects are automatically garbage collected. For additional cleanup logic use finalizers.
// Return and don't requeue
Expand All @@ -152,21 +154,26 @@ func (r *{{ .Resource.Kind }}Reconciler) Reconcile(ctx context.Context, req ctrl
}
}
// Check if the Busybox instance is marked to be deleted, which is
// Check if the {{ .Resource.Kind }} instance is marked to be deleted, which is
// indicated by the deletion timestamp being set.
isBusyboxMarkedToBeDeleted := busybox.GetDeletionTimestamp() != nil
if isBusyboxMarkedToBeDeleted {
is{{ .Resource.Kind }}MarkedToBeDeleted := {{ lower .Resource.Kind }}.GetDeletionTimestamp() != nil
if is{{ .Resource.Kind }}MarkedToBeDeleted {
if controllerutil.ContainsFinalizer({{ lower .Resource.Kind }}, {{ lower .Resource.Kind }}Finalizer) {
// Run finalization logic for memcachedFinalizer. If the
// finalization logic fails, don't remove the finalizer so
// that we can retry during the next reconciliation.
if err := r.{{ lower .Resource.Kind }}Busybox(log, {{ lower .Resource.Kind }}); err != nil {
if err := r.finalize{{ .Resource.Kind }}(log, {{ lower .Resource.Kind }}); err != nil {
return ctrl.Result{}, err
}
// Remove memcachedFinalizer. Once all finalizers have been
// removed, the object will be deleted.
controllerutil.RemoveFinalizer(busybox, {{ lower .Resource.Kind }}Finalizer)
if ok:= controllerutil.RemoveFinalizer({{ lower .Resource.Kind }}, {{ lower .Resource.Kind }}Finalizer); !ok{
if err != nil {
log.Error(errors.New("Unable to remove the finalizer"), "CR.Namespace", {{ lower .Resource.Kind }}.Namespace, "CR.Name", {{ lower .Resource.Kind }}.Name)
return ctrl.Result{}, err
}
}
err := r.Update(ctx, {{ lower .Resource.Kind }})
if err != nil {
return ctrl.Result{}, err
Expand All @@ -178,7 +185,7 @@ func (r *{{ .Resource.Kind }}Reconciler) Reconcile(ctx context.Context, req ctrl
// Check if the deployment already exists, if not create a new one
found := &appsv1.Deployment{}
err = r.Get(ctx, types.NamespacedName{Name: {{ lower .Resource.Kind }}.Name, Namespace: {{ lower .Resource.Kind }}.Namespace}, found)
if err != nil && errors.IsNotFound(err) {
if err != nil && apierrors.IsNotFound(err) {
// Define a new deployment
dep := r.deploymentFor{{ .Resource.Kind }}({{ lower .Resource.Kind }})
log.Info("Creating a new Deployment", "Deployment.Namespace", dep.Namespace, "Deployment.Name", dep.Name)
Expand Down
14 changes: 7 additions & 7 deletions testdata/project-v3-with-deploy-image/config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ rules:
- patch
- update
- watch
- apiGroups:
- ""
resources:
- events
verbs:
- create
- patch
- apiGroups:
- ""
resources:
Expand Down Expand Up @@ -51,13 +58,6 @@ rules:
- get
- patch
- update
- apiGroups:
- example.com.testproject.org
resources:
- events
verbs:
- create
- patch
- apiGroups:
- example.com.testproject.org
resources:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,17 @@ package controllers
import (
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"

"context"
"errors"
"fmt"
"time"

"github.com/go-logr/logr"

"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -53,7 +56,7 @@ type BusyboxReconciler struct {
//+kubebuilder:rbac:groups=example.com.testproject.org,resources=busyboxes,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=example.com.testproject.org,resources=busyboxes/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=example.com.testproject.org,resources=busyboxes/finalizers,verbs=update
//+kubebuilder:rbac:groups=example.com.testproject.org,resources=events,verbs=create;patch
//+kubebuilder:rbac:groups=core,resources=events,verbs=create;patch
//+kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=core,resources=pods,verbs=get;list;watch

Expand All @@ -78,7 +81,7 @@ func (r *BusyboxReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
busybox := &examplecomv1alpha1.Busybox{}
err := r.Get(ctx, req.NamespacedName, busybox)
if err != nil {
if errors.IsNotFound(err) {
if apierrors.IsNotFound(err) {
// Request object not found, could have been deleted after reconcile request.
// Owned objects are automatically garbage collected. For additional cleanup logic use finalizers.
// Return and don't requeue
Expand Down Expand Up @@ -112,13 +115,18 @@ func (r *BusyboxReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
// Run finalization logic for memcachedFinalizer. If the
// finalization logic fails, don't remove the finalizer so
// that we can retry during the next reconciliation.
if err := r.busyboxBusybox(log, busybox); err != nil {
if err := r.finalizeBusybox(log, busybox); err != nil {
return ctrl.Result{}, err
}

// Remove memcachedFinalizer. Once all finalizers have been
// removed, the object will be deleted.
controllerutil.RemoveFinalizer(busybox, busyboxFinalizer)
if ok := controllerutil.RemoveFinalizer(busybox, busyboxFinalizer); !ok {
if err != nil {
log.Error(errors.New("Unable to remove the finalizer"), "CR.Namespace", busybox.Namespace, "CR.Name", busybox.Name)
return ctrl.Result{}, err
}
}
err := r.Update(ctx, busybox)
if err != nil {
return ctrl.Result{}, err
Expand All @@ -130,7 +138,7 @@ func (r *BusyboxReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
// Check if the deployment already exists, if not create a new one
found := &appsv1.Deployment{}
err = r.Get(ctx, types.NamespacedName{Name: busybox.Name, Namespace: busybox.Namespace}, found)
if err != nil && errors.IsNotFound(err) {
if err != nil && apierrors.IsNotFound(err) {
// Define a new deployment
dep := r.deploymentForBusybox(busybox)
log.Info("Creating a new Deployment", "Deployment.Namespace", dep.Namespace, "Deployment.Name", dep.Name)
Expand Down Expand Up @@ -175,7 +183,7 @@ func (r *BusyboxReconciler) finalizeBusybox(log logr.Logger, cr *examplecomv1alp
// of finalizers include performing backups and deleting
// resources that are not owned by this CR, like a PVC.
// The following implementation will raise an event
r.recorder.Event(cr, "Normal", "Deleting",
r.recorder.Event(cr, "Warning", "Deleting",
fmt.Sprintf("Custom Resource %s is being deleted from the namespace %s",
cr.Name,
cr.Namespace))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,17 @@ package controllers
import (
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"

"context"
"errors"
"fmt"
"time"

"github.com/go-logr/logr"

"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -53,7 +56,7 @@ type MemcachedReconciler struct {
//+kubebuilder:rbac:groups=example.com.testproject.org,resources=memcacheds,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=example.com.testproject.org,resources=memcacheds/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=example.com.testproject.org,resources=memcacheds/finalizers,verbs=update
//+kubebuilder:rbac:groups=example.com.testproject.org,resources=events,verbs=create;patch
//+kubebuilder:rbac:groups=core,resources=events,verbs=create;patch
//+kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=core,resources=pods,verbs=get;list;watch

Expand All @@ -78,7 +81,7 @@ func (r *MemcachedReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
memcached := &examplecomv1alpha1.Memcached{}
err := r.Get(ctx, req.NamespacedName, memcached)
if err != nil {
if errors.IsNotFound(err) {
if apierrors.IsNotFound(err) {
// Request object not found, could have been deleted after reconcile request.
// Owned objects are automatically garbage collected. For additional cleanup logic use finalizers.
// Return and don't requeue
Expand All @@ -104,21 +107,26 @@ func (r *MemcachedReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
}
}

// Check if the Busybox instance is marked to be deleted, which is
// Check if the Memcached instance is marked to be deleted, which is
// indicated by the deletion timestamp being set.
isBusyboxMarkedToBeDeleted := busybox.GetDeletionTimestamp() != nil
if isBusyboxMarkedToBeDeleted {
isMemcachedMarkedToBeDeleted := memcached.GetDeletionTimestamp() != nil
if isMemcachedMarkedToBeDeleted {
if controllerutil.ContainsFinalizer(memcached, memcachedFinalizer) {
// Run finalization logic for memcachedFinalizer. If the
// finalization logic fails, don't remove the finalizer so
// that we can retry during the next reconciliation.
if err := r.memcachedBusybox(log, memcached); err != nil {
if err := r.finalizeMemcached(log, memcached); err != nil {
return ctrl.Result{}, err
}

// Remove memcachedFinalizer. Once all finalizers have been
// removed, the object will be deleted.
controllerutil.RemoveFinalizer(busybox, memcachedFinalizer)
if ok := controllerutil.RemoveFinalizer(memcached, memcachedFinalizer); !ok {
if err != nil {
log.Error(errors.New("Unable to remove the finalizer"), "CR.Namespace", memcached.Namespace, "CR.Name", memcached.Name)
return ctrl.Result{}, err
}
}
err := r.Update(ctx, memcached)
if err != nil {
return ctrl.Result{}, err
Expand All @@ -130,7 +138,7 @@ func (r *MemcachedReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
// Check if the deployment already exists, if not create a new one
found := &appsv1.Deployment{}
err = r.Get(ctx, types.NamespacedName{Name: memcached.Name, Namespace: memcached.Namespace}, found)
if err != nil && errors.IsNotFound(err) {
if err != nil && apierrors.IsNotFound(err) {
// Define a new deployment
dep := r.deploymentForMemcached(memcached)
log.Info("Creating a new Deployment", "Deployment.Namespace", dep.Namespace, "Deployment.Name", dep.Name)
Expand Down Expand Up @@ -175,7 +183,7 @@ func (r *MemcachedReconciler) finalizeMemcached(log logr.Logger, cr *examplecomv
// of finalizers include performing backups and deleting
// resources that are not owned by this CR, like a PVC.
// The following implementation will raise an event
r.recorder.Event(cr, "Normal", "Deleting",
r.recorder.Event(cr, "Warning", "Deleting",
fmt.Sprintf("Custom Resource %s is being deleted from the namespace %s",
cr.Name,
cr.Namespace))
Expand Down
2 changes: 1 addition & 1 deletion testdata/project-v3-with-deploy-image/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module sigs.k8s.io/kubebuilder/testdata/project-v3-with-deploy-image
go 1.18

require (
github.com/go-logr/logr v1.2.0
github.com/onsi/ginkgo v1.16.5
github.com/onsi/gomega v1.18.1
k8s.io/api v0.24.2
Expand All @@ -28,7 +29,6 @@ require (
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
github.com/form3tech-oss/jwt-go v3.2.3+incompatible // indirect
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/go-logr/logr v1.2.0 // indirect
github.com/go-logr/zapr v1.2.0 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.19.5 // indirect
Expand Down

0 comments on commit 748178a

Please sign in to comment.