|
| 1 | +// |
| 2 | +// Copyright (c) 2019-2025 Red Hat, Inc. |
| 3 | +// This program and the accompanying materials are made |
| 4 | +// available under the terms of the Eclipse Public License 2.0 |
| 5 | +// which is available at https://www.eclipse.org/legal/epl-2.0/ |
| 6 | +// |
| 7 | +// SPDX-License-Identifier: EPL-2.0 |
| 8 | +// |
| 9 | +// Contributors: |
| 10 | +// Red Hat, Inc. - initial API and implementation |
| 11 | +// |
| 12 | + |
| 13 | +package utils |
| 14 | + |
| 15 | +import ( |
| 16 | + "context" |
| 17 | + "fmt" |
| 18 | + "reflect" |
| 19 | + |
| 20 | + ctrl "sigs.k8s.io/controller-runtime" |
| 21 | + |
| 22 | + "k8s.io/apimachinery/pkg/api/errors" |
| 23 | + "k8s.io/apimachinery/pkg/runtime" |
| 24 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 25 | +) |
| 26 | + |
| 27 | +var ( |
| 28 | + syncLog = ctrl.Log.WithName("sync") |
| 29 | +) |
| 30 | + |
| 31 | +type Syncer interface { |
| 32 | + // Get gets object. |
| 33 | + // Returns true if object exists otherwise returns false. |
| 34 | + // Returns error if object cannot be retrieved otherwise returns nil. |
| 35 | + Get(key client.ObjectKey, objectMeta client.Object) (bool, error) |
| 36 | + // Delete does delete object by key. |
| 37 | + // Returns true if object deleted or not found otherwise returns false. |
| 38 | + // Returns error if object cannot be deleted otherwise returns nil. |
| 39 | + Delete(context context.Context, key client.ObjectKey, objectMeta client.Object) (bool, error) |
| 40 | +} |
| 41 | + |
| 42 | +type ObjSyncer struct { |
| 43 | + syncer Syncer |
| 44 | + cli client.Client |
| 45 | +} |
| 46 | + |
| 47 | +func (s ObjSyncer) Get(context context.Context, key client.ObjectKey, objectMeta client.Object) (bool, error) { |
| 48 | + return s.doGetIgnoreNotFound(context, key, objectMeta) |
| 49 | +} |
| 50 | + |
| 51 | +func (s ObjSyncer) Delete(context context.Context, key client.ObjectKey, objectMeta client.Object) (bool, error) { |
| 52 | + return s.deleteByKeyIgnoreNotFound(context, key, objectMeta) |
| 53 | +} |
| 54 | + |
| 55 | +// deleteByKeyIgnoreNotFound deletes object by key. |
| 56 | +// Returns true if object deleted or not found otherwise returns false. |
| 57 | +// Returns error if object cannot be deleted otherwise returns nil. |
| 58 | +func (s ObjSyncer) deleteByKeyIgnoreNotFound(context context.Context, key client.ObjectKey, objectMeta client.Object) (bool, error) { |
| 59 | + runtimeObject, ok := objectMeta.(runtime.Object) |
| 60 | + if !ok { |
| 61 | + return false, fmt.Errorf("object %T is not a runtime.Object", runtimeObject) |
| 62 | + } |
| 63 | + |
| 64 | + actual := runtimeObject.DeepCopyObject().(client.Object) |
| 65 | + if exists, err := s.doGetIgnoreNotFound(context, key, actual); !exists { |
| 66 | + return true, nil |
| 67 | + } else if err != nil { |
| 68 | + return false, err |
| 69 | + } |
| 70 | + |
| 71 | + return s.doDeleteIgnoreIfNotFound(context, actual) |
| 72 | +} |
| 73 | + |
| 74 | +// doDeleteIgnoreIfNotFound deletes object. |
| 75 | +// Returns true if object deleted or not found otherwise returns false. |
| 76 | +// Returns error if object cannot be deleted otherwise returns nil. |
| 77 | +func (s ObjSyncer) doDeleteIgnoreIfNotFound( |
| 78 | + context context.Context, |
| 79 | + object client.Object, |
| 80 | +) (bool, error) { |
| 81 | + if err := s.cli.Delete(context, object); err == nil { |
| 82 | + if errors.IsNotFound(err) { |
| 83 | + syncLog.Info("Object not found", "namespace", object.GetNamespace(), "kind", GetObjectType(object), "name", object.GetName()) |
| 84 | + } else { |
| 85 | + syncLog.Info("Object deleted", "namespace", object.GetNamespace(), "kind", GetObjectType(object), "name", object.GetName()) |
| 86 | + } |
| 87 | + return true, nil |
| 88 | + } else { |
| 89 | + return false, err |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +// doGet gets object. |
| 94 | +// Returns true if object exists otherwise returns false. |
| 95 | +// Returns error if object cannot be retrieved otherwise returns nil. |
| 96 | +func (s ObjSyncer) doGetIgnoreNotFound( |
| 97 | + context context.Context, |
| 98 | + key client.ObjectKey, |
| 99 | + object client.Object, |
| 100 | +) (bool, error) { |
| 101 | + if err := s.cli.Get(context, key, object); err == nil { |
| 102 | + return true, nil |
| 103 | + } else if errors.IsNotFound(err) { |
| 104 | + return false, nil |
| 105 | + } else { |
| 106 | + return false, err |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +func GetObjectType(obj interface{}) string { |
| 111 | + objType := reflect.TypeOf(obj).String() |
| 112 | + if reflect.TypeOf(obj).Kind().String() == "ptr" { |
| 113 | + objType = objType[1:] |
| 114 | + } |
| 115 | + |
| 116 | + return objType |
| 117 | +} |
0 commit comments