SyncObjectWithCluster synchronises the state of specObj to the cluster, creating or updating the cluster object as required. If specObj is in sync with the cluster, returns the object as it exists on the cluster. Returns a NotInSyncError if an update is required, UnrecoverableSyncError if object pro
(specObj crclient.Object, api ClusterAPI)
| 45 | // NotInSyncError if an update is required, UnrecoverableSyncError if object provided is invalid, or generic error |
| 46 | // if an unexpected error is encountered |
| 47 | func SyncObjectWithCluster(specObj crclient.Object, api ClusterAPI) (crclient.Object, error) { |
| 48 | objType := reflect.TypeOf(specObj).Elem() |
| 49 | clusterObj := reflect.New(objType).Interface().(crclient.Object) |
| 50 | |
| 51 | err := api.Client.Get(api.Ctx, types.NamespacedName{Name: specObj.GetName(), Namespace: specObj.GetNamespace()}, clusterObj) |
| 52 | if err != nil { |
| 53 | if k8sErrors.IsNotFound(err) { |
| 54 | return nil, createObjectGeneric(specObj, api) |
| 55 | } |
| 56 | return nil, err |
| 57 | } |
| 58 | |
| 59 | if !isMutableObject(specObj) { // TODO: we could still update labels here, or treat a need to update as a fatal error |
| 60 | return clusterObj, nil |
| 61 | } |
| 62 | |
| 63 | diffFunc := diffFuncs[objType] |
| 64 | if diffFunc == nil { |
| 65 | return nil, &UnrecoverableSyncError{fmt.Errorf("attempting to sync unrecognized object %s", objType)} |
| 66 | } |
| 67 | shouldDelete, shouldUpdate := diffFunc(specObj, clusterObj) |
| 68 | if shouldDelete { |
| 69 | printDiff(specObj, clusterObj, api.Logger) |
| 70 | err := api.Client.Delete(api.Ctx, specObj) |
| 71 | if err != nil { |
| 72 | return nil, err |
| 73 | } |
| 74 | api.Logger.Info("Deleted object", "kind", objType.String(), "name", specObj.GetName()) |
| 75 | return nil, NewNotInSync(specObj, DeletedObjectReason) |
| 76 | } |
| 77 | if shouldUpdate { |
| 78 | printDiff(specObj, clusterObj, api.Logger) |
| 79 | return nil, updateObjectGeneric(specObj, clusterObj, api) |
| 80 | } |
| 81 | return clusterObj, nil |
| 82 | } |
| 83 | |
| 84 | // SyncUnrecognizedObjectWithCluster allows syncing objects not supported by SyncObjectWithCluster. As there is |
| 85 | // no generic way of deciding if an object needs to be updated, a WarningError is returned if the object exists |
no test coverage detected