Skip to content

Commit

Permalink
Show file tree
Hide file tree
Showing 18 changed files with 97 additions and 5 deletions.
6 changes: 6 additions & 0 deletions modules/certmanager/certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,14 @@ func (c *Certificate) CreateOrPatch(
h.GetLogger().Info(fmt.Sprintf("Certificate %s not found, reconcile in %s", cert.Name, c.timeout))
return ctrl.Result{RequeueAfter: c.timeout}, nil
}
h.GetRecorder().Event(cert, k8s_corev1.EventTypeWarning, "Error", fmt.Sprintf("error create/updating certificate: %s", c.certificate.Name))
return ctrl.Result{}, err
}

if op == controllerutil.OperationResultCreated {
h.GetRecorder().Event(cert, k8s_corev1.EventTypeNormal, "Created", fmt.Sprintf("certificate %s created", c.certificate.Name))
}

if op != controllerutil.OperationResultNone {
h.GetLogger().Info(fmt.Sprintf("Route %s - %s", cert.Name, op))
}
Expand Down
7 changes: 7 additions & 0 deletions modules/certmanager/issuer.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/openstack-k8s-operators/lib-common/modules/common/helper"
"github.com/openstack-k8s-operators/lib-common/modules/common/util"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
k8s_errors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -133,8 +134,14 @@ func (i *Issuer) CreateOrPatch(
h.GetLogger().Info(fmt.Sprintf("Issuer %s not found, reconcile in %s", issuer.Name, i.timeout))
return ctrl.Result{RequeueAfter: i.timeout}, nil
}
h.GetRecorder().Event(issuer, corev1.EventTypeWarning, "Error", fmt.Sprintf("error create/updating issuer: %s", i.issuer.Name))
return ctrl.Result{}, err
}

if op == controllerutil.OperationResultCreated {
h.GetRecorder().Event(issuer, corev1.EventTypeNormal, "Created", fmt.Sprintf("issuer %s created", i.issuer.Name))
}

if op != controllerutil.OperationResultNone {
h.GetLogger().Info(fmt.Sprintf("Issuer %s - %s", issuer.Name, op))
}
Expand Down
5 changes: 4 additions & 1 deletion modules/certmanager/test/functional/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,10 @@ var _ = BeforeEach(func() {
// Note(mschuppert) using a Secret as a Namespace object does not have
// metadata with namespace and some functions use the BeforeObject.GetNamespace()
genericObject := th.CreateSecret(types.NamespacedName{Name: "generic", Namespace: namespace}, map[string][]byte{})
h, err = helper.NewHelper(genericObject, k8sClient, kclient, testEnv.Scheme, ctrl.Log)
manager, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{})
Expect(err).NotTo(HaveOccurred())
recorder := manager.GetEventRecorderFor("test")
h, err = helper.NewHelper(genericObject, k8sClient, kclient, testEnv.Scheme, ctrl.Log, recorder)
Expect(err).NotTo(HaveOccurred())
Expect(h).NotTo(BeNil())

Expand Down
4 changes: 4 additions & 0 deletions modules/common/configmap/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,12 @@ func createOrPatchConfigMap(
return nil
})
if err != nil {
h.GetRecorder().Event(obj, corev1.EventTypeWarning, "Error", fmt.Sprintf("error create/updating configmap: %s", cm.Name))
return "", op, fmt.Errorf("error create/updating configmap: %w", err)
}
if op == controllerutil.OperationResultCreated {
h.GetRecorder().Event(obj, corev1.EventTypeNormal, "Created", fmt.Sprintf("configmap %s created", cm.Name))
}

configMapHash, err := Hash(configMap)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions modules/common/cronjob/cronjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"time"

batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
k8s_errors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"

Expand Down Expand Up @@ -63,8 +64,12 @@ func (cj *CronJob) CreateOrPatch(
h.GetLogger().Info(fmt.Sprintf("CronJob %s not found, reconcile in %s", cj.cronjob.Name, cj.timeout))
return ctrl.Result{RequeueAfter: cj.timeout}, nil
}
h.GetRecorder().Event(cronjob, corev1.EventTypeWarning, "Error", fmt.Sprintf("error create/updating cronjob: %s", cj.cronjob.Name))
return ctrl.Result{}, err
}
if op == controllerutil.OperationResultCreated {
h.GetRecorder().Event(cronjob, corev1.EventTypeNormal, "Created", fmt.Sprintf("cronjob %s created", cj.cronjob.Name))
}
if op != controllerutil.OperationResultNone {
h.GetLogger().Info(fmt.Sprintf("CronJob %s - %s", cj.cronjob.Name, op))
}
Expand Down
5 changes: 5 additions & 0 deletions modules/common/daemonset/daemonset.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/openstack-k8s-operators/lib-common/modules/common/helper"
"github.com/openstack-k8s-operators/lib-common/modules/common/util"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
k8s_errors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -76,8 +77,12 @@ func (d *DaemonSet) CreateOrPatch(
util.LogForObject(h, fmt.Sprintf("DaemonSet not found, reconcile in %s", d.timeout), daemonset)
return ctrl.Result{RequeueAfter: d.timeout}, nil
}
h.GetRecorder().Event(daemonset, corev1.EventTypeWarning, "Error", fmt.Sprintf("error create/updating daemonset: %s", d.daemonset.Name))
return ctrl.Result{}, err
}
if op == controllerutil.OperationResultCreated {
h.GetRecorder().Event(daemonset, corev1.EventTypeNormal, "Created", fmt.Sprintf("daemonset %s created", d.daemonset.Name))
}
if op != controllerutil.OperationResultNone {
util.LogForObject(h, fmt.Sprintf("DaemonSet: %s", op), daemonset)
}
Expand Down
5 changes: 5 additions & 0 deletions modules/common/deployment/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/openstack-k8s-operators/lib-common/modules/common/helper"
"github.com/openstack-k8s-operators/lib-common/modules/common/util"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
k8s_errors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -78,8 +79,12 @@ func (d *Deployment) CreateOrPatch(
h.GetLogger().Info(fmt.Sprintf("Deployment %s not found, reconcile in %s", deployment.Name, d.timeout))
return ctrl.Result{RequeueAfter: d.timeout}, nil
}
h.GetRecorder().Event(deployment, corev1.EventTypeWarning, "Error", fmt.Sprintf("error create/updating deployment: %s", d.deployment.Name))
return ctrl.Result{}, err
}
if op == controllerutil.OperationResultCreated {
h.GetRecorder().Event(deployment, corev1.EventTypeNormal, "Created", fmt.Sprintf("deployment %s created", d.deployment.Name))
}
if op != controllerutil.OperationResultNone {
h.GetLogger().Info(fmt.Sprintf("Deployment %s - %s", deployment.Name, op))
}
Expand Down
10 changes: 9 additions & 1 deletion modules/common/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/record"

"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
Expand All @@ -40,6 +41,7 @@ type Helper struct {
kclient kubernetes.Interface
gvk schema.GroupVersionKind
scheme *runtime.Scheme
recorder record.EventRecorder
beforeObject client.Object
before *unstructured.Unstructured
after *unstructured.Unstructured
Expand All @@ -50,7 +52,7 @@ type Helper struct {
}

// NewHelper returns an initialized Helper.
func NewHelper(obj client.Object, crClient client.Client, kclient kubernetes.Interface, scheme *runtime.Scheme, log logr.Logger) (*Helper, error) {
func NewHelper(obj client.Object, crClient client.Client, kclient kubernetes.Interface, scheme *runtime.Scheme, log logr.Logger, recorder record.EventRecorder) (*Helper, error) {
// Get the GroupVersionKind of the object,
// used to validate against later on.
gvk, err := apiutil.GVKForObject(obj, crClient.Scheme())
Expand All @@ -69,6 +71,7 @@ func NewHelper(obj client.Object, crClient client.Client, kclient kubernetes.Int
kclient: kclient,
gvk: gvk,
scheme: scheme,
recorder: recorder,
before: unstructuredObj,
beforeObject: obj.DeepCopyObject().(client.Object),
logger: log,
Expand Down Expand Up @@ -96,6 +99,11 @@ func (h *Helper) GetScheme() *runtime.Scheme {
return h.scheme
}

// GetRecorder - returns the event recorder of the object
func (h *Helper) GetRecorder() record.EventRecorder {
return h.recorder
}

// GetAfter - returns unstructured object after modification
func (h *Helper) GetAfter() *unstructured.Unstructured {
return h.after
Expand Down
5 changes: 5 additions & 0 deletions modules/common/job/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"time"

batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
k8s_errors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -79,9 +80,13 @@ func (j *Job) createJob(
h.GetLogger().Info(fmt.Sprintf("Job %s not found, reconcile in %s", job.Name, j.timeout))
return ctrl.Result{RequeueAfter: j.timeout}, nil
}
h.GetRecorder().Event(job, corev1.EventTypeWarning, "Error", fmt.Sprintf("error create/updating job: %s", j.expectedJob.Name))
h.GetLogger().Error(err, "Job CreateOrPatch failed", "job", job.Name)
return ctrl.Result{}, err
}
if op == controllerutil.OperationResultCreated {
h.GetRecorder().Event(job, corev1.EventTypeNormal, "Created", fmt.Sprintf("job %s created", j.expectedJob.Name))
}
j.actualJob = job
if op != controllerutil.OperationResultNone {
h.GetLogger().Info(fmt.Sprintf("Job %s %s - %s", j.jobType, job.Name, op))
Expand Down
5 changes: 4 additions & 1 deletion modules/common/pvc/pvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,12 @@ func (p *Pvc) CreateOrPatch(
h.GetLogger().Info(fmt.Sprintf("Pvc %s not found, reconcile in %s", pvc.Name, p.timeout))
return ctrl.Result{RequeueAfter: p.timeout}, nil
}
h.GetRecorder().Event(pvc, corev1.EventTypeWarning, "Error", fmt.Sprintf("error create/updating pvc: %s", p.pvc.Name))
return ctrl.Result{}, err
}

if op == controllerutil.OperationResultCreated {
h.GetRecorder().Event(pvc, corev1.EventTypeNormal, "Created", fmt.Sprintf("pvc %s created", p.pvc.Name))
}
if op != controllerutil.OperationResultNone {
h.GetLogger().Info(fmt.Sprintf("Pvc %s - %s", pvc.Name, op))
}
Expand Down
5 changes: 5 additions & 0 deletions modules/common/role/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
rbacv1 "k8s.io/api/rbac/v1"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

corev1 "k8s.io/api/core/v1"
k8s_errors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -70,12 +71,16 @@ func (r *Role) CreateOrPatch(
h.GetLogger().Info(fmt.Sprintf("Role %s not found, reconcile in %s", role.Name, r.timeout))
return ctrl.Result{RequeueAfter: r.timeout}, nil
}
h.GetRecorder().Event(r.role, corev1.EventTypeWarning, "Error", fmt.Sprintf("error create/updating role: %s", r.role.Name))
return ctrl.Result{}, util.WrapErrorForObject(
fmt.Sprintf("Error creating role %s", role.Name),
role,
err,
)
}
if op == controllerutil.OperationResultCreated {
h.GetRecorder().Event(r.role, corev1.EventTypeNormal, "Created", fmt.Sprintf("role %s created", r.role.Name))
}
if op != controllerutil.OperationResultNone {
h.GetLogger().Info(fmt.Sprintf("Role %s - %s", role.Name, op))
}
Expand Down
7 changes: 6 additions & 1 deletion modules/common/rolebinding/rolebinding.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
rbacv1 "k8s.io/api/rbac/v1"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

corev1 "k8s.io/api/core/v1"
k8s_errors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -73,12 +74,16 @@ func (r *RoleBinding) CreateOrPatch(
h.GetLogger().Info(fmt.Sprintf("RoleBinding %s not found, reconcile in %s", rb.Name, r.timeout))
return ctrl.Result{RequeueAfter: r.timeout}, nil
}
h.GetRecorder().Event(rb, corev1.EventTypeWarning, "Error", fmt.Sprintf("error create/updating rolebinding: %s", r.roleBinding.Name))
return ctrl.Result{}, util.WrapErrorForObject(
fmt.Sprintf("Error creating rol binding %s", rb.Name),
fmt.Sprintf("Error creating role binding %s", rb.Name),
rb,
err,
)
}
if op == controllerutil.OperationResultCreated {
h.GetRecorder().Event(rb, corev1.EventTypeNormal, "Created", fmt.Sprintf("rolebinding %s created", r.roleBinding.Name))
}
if op != controllerutil.OperationResultNone {
h.GetLogger().Info(fmt.Sprintf("RoleBinding %s - %s", rb.Name, op))
}
Expand Down
5 changes: 5 additions & 0 deletions modules/common/route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/openstack-k8s-operators/lib-common/modules/common/util"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

corev1 "k8s.io/api/core/v1"
k8s_errors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
Expand Down Expand Up @@ -164,8 +165,12 @@ func (r *Route) CreateOrPatch(
h.GetLogger().Info(fmt.Sprintf("Route %s not found, reconcile in %s", route.Name, r.timeout))
return ctrl.Result{RequeueAfter: r.timeout}, nil
}
h.GetRecorder().Event(route, corev1.EventTypeWarning, "Error", fmt.Sprintf("error create/updating route: %s", r.route.Name))
return ctrl.Result{}, err
}
if op == controllerutil.OperationResultCreated {
h.GetRecorder().Event(route, corev1.EventTypeNormal, "Created", fmt.Sprintf("route %s created", r.route.Name))
}
if op != controllerutil.OperationResultNone {
h.GetLogger().Info(fmt.Sprintf("Route %s - %s", route.Name, op))
}
Expand Down
10 changes: 10 additions & 0 deletions modules/common/secret/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,14 @@ func CreateOrPatchSecret(
return nil
})
if err != nil {
h.GetRecorder().Event(obj, corev1.EventTypeWarning, "Error", fmt.Sprintf("error create/updating secret: %s", s.Name))
return "", op, fmt.Errorf("error create/updating secret: %w", err)
}

if op == controllerutil.OperationResultCreated {
h.GetRecorder().Event(obj, corev1.EventTypeNormal, "Created", fmt.Sprintf("secret %s created", s.Name))
}

secretHash, err := Hash(s)
if err != nil {
return "", "", fmt.Errorf("error calculating configuration hash: %w", err)
Expand Down Expand Up @@ -216,9 +221,14 @@ func createOrUpdateSecret(
})

if err != nil {
h.GetRecorder().Event(obj, corev1.EventTypeWarning, "Error", fmt.Sprintf("error create/updating secret: %s", secret.Name))
return "", op, err
}

if op == controllerutil.OperationResultCreated {
h.GetRecorder().Event(obj, corev1.EventTypeNormal, "Created", fmt.Sprintf("secret %s created", secret.Name))
}

secretHash, err := Hash(secret)
if err != nil {
return "", op, fmt.Errorf("error calculating configuration hash: %w", err)
Expand Down
4 changes: 4 additions & 0 deletions modules/common/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,12 @@ func (s *Service) CreateOrPatch(
h.GetLogger().Info(fmt.Sprintf("Service %s not found, reconcile in %s", service.Name, s.timeout))
return ctrl.Result{RequeueAfter: s.timeout}, nil
}
h.GetRecorder().Event(service, corev1.EventTypeWarning, "Error", fmt.Sprintf("error create/updating service: %s", service.Name))
return ctrl.Result{}, err
}
if op == controllerutil.OperationResultCreated {
h.GetRecorder().Event(service, corev1.EventTypeNormal, "Created", fmt.Sprintf("service %s created", service.Name))
}
if op != controllerutil.OperationResultNone {
h.GetLogger().Info(fmt.Sprintf("Service %s - %s", service.Name, op))
}
Expand Down
4 changes: 4 additions & 0 deletions modules/common/serviceaccount/serviceaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,16 @@ func (s *ServiceAccount) CreateOrPatch(
h.GetLogger().Info(fmt.Sprintf("ServiceAccount %s not found, reconcile in %s", sa.Name, s.timeout))
return ctrl.Result{RequeueAfter: s.timeout}, nil
}
h.GetRecorder().Event(sa, corev1.EventTypeWarning, "Error", fmt.Sprintf("error create/updating service account: %s", sa.Name))
return ctrl.Result{}, util.WrapErrorForObject(
fmt.Sprintf("Error creating service account %s", sa.Name),
sa,
err,
)
}
if op == controllerutil.OperationResultCreated {
h.GetRecorder().Event(sa, corev1.EventTypeNormal, "Created", fmt.Sprintf("service account %s created", sa.Name))
}
if op != controllerutil.OperationResultNone {
h.GetLogger().Info(fmt.Sprintf("ServiceAccount %s - %s", sa.Name, op))
}
Expand Down
5 changes: 5 additions & 0 deletions modules/common/statefulset/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/openstack-k8s-operators/lib-common/modules/common/helper"
"github.com/openstack-k8s-operators/lib-common/modules/common/util"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
k8s_errors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -88,8 +89,12 @@ func (s *StatefulSet) CreateOrPatch(
h.GetLogger().Info(fmt.Sprintf("StatefulSet %s not found, reconcile in %s", statefulset.Name, s.timeout))
return ctrl.Result{RequeueAfter: s.timeout}, nil
}
h.GetRecorder().Event(statefulset, corev1.EventTypeWarning, "Error", fmt.Sprintf("error create/updating statefulset: %s", statefulset.Name))
return ctrl.Result{}, err
}
if op == controllerutil.OperationResultCreated {
h.GetRecorder().Event(statefulset, corev1.EventTypeNormal, "Created", fmt.Sprintf("statefulset %s created", statefulset.Name))
}
if op != controllerutil.OperationResultNone {
h.GetLogger().Info(fmt.Sprintf("StatefulSet %s - %s", statefulset.Name, op))
}
Expand Down
5 changes: 4 additions & 1 deletion modules/common/test/functional/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,10 @@ var _ = BeforeSuite(func() {
// provide a valid object. It is used as controller reference for certain
// objects created in the test. So we provide a simple one, a Namespace.
genericObject := th.CreateNamespace("generic-object")
h, err = helper.NewHelper(genericObject, cClient, client, testEnv.Scheme, ctrl.Log)
manager, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{})
Expect(err).NotTo(HaveOccurred())
recorder := manager.GetEventRecorderFor("test")
h, err = helper.NewHelper(genericObject, cClient, client, testEnv.Scheme, ctrl.Log, recorder)
Expect(err).NotTo(HaveOccurred())
Expect(h).NotTo(BeNil())

Expand Down

0 comments on commit 5271d47

Please sign in to comment.