mirror of
https://github.com/storytold/irsa-manager.git
synced 2026-10-09 00:09:43 +00:00
manage status
This commit is contained in:
@@ -84,6 +84,16 @@ func (r *IRSASetupReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
|
||||
return ctrl.Result{Requeue: true}, nil
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if err := r.Get(ctx, req.NamespacedName, &irsav1alpha1.IRSASetup{}); err != nil {
|
||||
return
|
||||
}
|
||||
statusHandler := handler.NewStatusHandler(kubeClient)
|
||||
if err := statusHandler.Patch(ctx, obj); err != nil {
|
||||
return
|
||||
}
|
||||
}()
|
||||
|
||||
if !obj.DeletionTimestamp.IsZero() {
|
||||
err = r.reconcileDelete(ctx, obj, kubeClient)
|
||||
if err == nil {
|
||||
|
||||
@@ -15,6 +15,10 @@ type KubernetesClient interface {
|
||||
Delete(ctx context.Context, obj client.Object, opts DeleteOptions) error
|
||||
}
|
||||
|
||||
type StatusClient interface {
|
||||
PatchStatus(ctx context.Context, obj client.Object) error
|
||||
}
|
||||
|
||||
// DeleteOptions contains options for delete requests.
|
||||
type DeleteOptions struct {
|
||||
// DeletionPropagation decides how the garbage collector will handle the propagation.
|
||||
|
||||
@@ -14,9 +14,9 @@ type KubernetesHandler struct {
|
||||
objs []client.Object
|
||||
}
|
||||
|
||||
func NewKubernetesHandler(k KubernetesClient) *KubernetesHandler {
|
||||
func NewKubernetesHandler(c KubernetesClient) *KubernetesHandler {
|
||||
return &KubernetesHandler{
|
||||
client: k,
|
||||
client: c,
|
||||
objs: []client.Object{},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
)
|
||||
|
||||
type StatusHandler struct {
|
||||
client StatusClient
|
||||
}
|
||||
|
||||
func NewStatusHandler(c StatusClient) *StatusHandler {
|
||||
return &StatusHandler{
|
||||
client: c,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *StatusHandler) Patch(ctx context.Context, obj client.Object) error {
|
||||
return s.client.PatchStatus(ctx, obj)
|
||||
}
|
||||
@@ -6,32 +6,18 @@ import (
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
)
|
||||
|
||||
func (h KubernetesClient) Apply(ctx context.Context, obj client.Object) error {
|
||||
func (c KubernetesClient) Apply(ctx context.Context, obj client.Object) error {
|
||||
opts := []client.PatchOption{
|
||||
client.ForceOwnership,
|
||||
client.FieldOwner(h.owner.Field),
|
||||
client.FieldOwner(c.owner.Field),
|
||||
}
|
||||
u, err := h.toUnstructured(obj)
|
||||
u, err := c.toUnstructured(obj)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = h.client.Patch(ctx, u, client.Apply, opts...)
|
||||
err = c.client.Patch(ctx, u, client.Apply, opts...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h KubernetesClient) PatchStatus(ctx context.Context, obj client.Object) error {
|
||||
opts := &client.SubResourcePatchOptions{
|
||||
PatchOptions: client.PatchOptions{
|
||||
FieldManager: h.owner.Field,
|
||||
},
|
||||
}
|
||||
u, err := h.toUnstructured(obj)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return h.client.Status().Patch(ctx, u, client.Apply, opts)
|
||||
}
|
||||
|
||||
@@ -6,15 +6,15 @@ import (
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
)
|
||||
|
||||
func (h KubernetesClient) Create(ctx context.Context, obj client.Object) error {
|
||||
func (c KubernetesClient) Create(ctx context.Context, obj client.Object) error {
|
||||
opts := []client.CreateOption{
|
||||
client.FieldOwner(h.owner.Field),
|
||||
client.FieldOwner(c.owner.Field),
|
||||
}
|
||||
u, err := h.toUnstructured(obj)
|
||||
u, err := c.toUnstructured(obj)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = h.client.Create(ctx, u, opts...)
|
||||
err = c.client.Create(ctx, u, opts...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -14,8 +14,8 @@ import (
|
||||
)
|
||||
|
||||
// Delete deletes the given object (not found errors are ignored).
|
||||
func (h *KubernetesClient) Delete(ctx context.Context, obj client.Object, opts handler.DeleteOptions) error {
|
||||
existingObj, err := h.Get(ctx, obj)
|
||||
func (c *KubernetesClient) Delete(ctx context.Context, obj client.Object, opts handler.DeleteOptions) error {
|
||||
existingObj, err := c.Get(ctx, obj)
|
||||
if err != nil {
|
||||
if !errors.IsNotFound(err) {
|
||||
return fmt.Errorf("failed to delete: %w", err)
|
||||
@@ -32,7 +32,7 @@ func (h *KubernetesClient) Delete(ctx context.Context, obj client.Object, opts h
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := h.client.Delete(ctx, existingObj, client.PropagationPolicy(opts.DeletionPropagation)); err != nil {
|
||||
if err := c.client.Delete(ctx, existingObj, client.PropagationPolicy(opts.DeletionPropagation)); err != nil {
|
||||
return fmt.Errorf("delete failed: %w", err)
|
||||
}
|
||||
|
||||
|
||||
@@ -8,14 +8,14 @@ import (
|
||||
)
|
||||
|
||||
// Get gets the given object.
|
||||
func (h KubernetesClient) Get(ctx context.Context, obj client.Object) (*unstructured.Unstructured, error) {
|
||||
u, err := h.toUnstructured(obj)
|
||||
func (c KubernetesClient) Get(ctx context.Context, obj client.Object) (*unstructured.Unstructured, error) {
|
||||
u, err := c.toUnstructured(obj)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
existingObj := &unstructured.Unstructured{}
|
||||
existingObj.SetGroupVersionKind(u.GroupVersionKind())
|
||||
err = h.client.Get(ctx, client.ObjectKeyFromObject(u), existingObj)
|
||||
err = c.client.Get(ctx, client.ObjectKeyFromObject(u), existingObj)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package kubernetes
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
)
|
||||
|
||||
func (c KubernetesClient) PatchStatus(ctx context.Context, obj client.Object) error {
|
||||
opts := &client.SubResourcePatchOptions{
|
||||
PatchOptions: client.PatchOptions{
|
||||
FieldManager: c.owner.Field,
|
||||
},
|
||||
}
|
||||
u, err := c.toUnstructured(obj)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.client.Status().Patch(ctx, u, client.Apply, opts)
|
||||
}
|
||||
@@ -8,8 +8,8 @@ import (
|
||||
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
|
||||
)
|
||||
|
||||
func (h KubernetesClient) toUnstructured(obj client.Object) (*unstructured.Unstructured, error) {
|
||||
gvk, err := apiutil.GVKForObject(obj, h.client.Scheme())
|
||||
func (c KubernetesClient) toUnstructured(obj client.Object) (*unstructured.Unstructured, error) {
|
||||
gvk, err := apiutil.GVKForObject(obj, c.client.Scheme())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user