MCPcopy Create free account
hub / github.com/devfile/devworkspace-operator / SyncObjectWithCluster

Function SyncObjectWithCluster

pkg/provision/sync/sync.go:47–82  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

45// NotInSyncError if an update is required, UnrecoverableSyncError if object provided is invalid, or generic error
46// if an unexpected error is encountered
47func 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

Callers 15

syncServicesMethod · 0.92
syncIngressesMethod · 0.92
syncRoutesMethod · 0.92
ensureJobRunnerRBACMethod · 0.92
SyncServiceAccountFunction · 0.92
ProvisionSshAskPassFunction · 0.92
SyncDeploymentToClusterFunction · 0.92
SyncRoutingToClusterFunction · 0.92

Calls 8

createObjectGenericFunction · 0.85
isMutableObjectFunction · 0.85
diffFuncFuncType · 0.85
printDiffFunction · 0.85
NewNotInSyncFunction · 0.85
updateObjectGenericFunction · 0.85
StringMethod · 0.80
GetMethod · 0.65

Tested by

no test coverage detected