-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Alper Rifat Ulucinar <ulucinar@users.noreply.github.com>
- Loading branch information
Showing
7 changed files
with
420 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package migration | ||
|
||
import ( | ||
"github.com/crossplane/crossplane-runtime/pkg/fieldpath" | ||
"github.com/pkg/errors" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/apimachinery/pkg/util/json" | ||
) | ||
|
||
const ( | ||
errFromUnstructured = "failed to convert from unstructured.Unstructured to the managed resource type" | ||
errToUnstructured = "failed to convert from the managed resource type to unstructured.Unstructured" | ||
errRawExtensionUnmarshal = "failed to unmarshal runtime.RawExtension" | ||
|
||
errFmtPavedDelete = "failed to delete fieldpath %q from paved" | ||
) | ||
|
||
func CopyInto(source any, target any, targetGVK schema.GroupVersionKind, skipFieldPaths ...string) (any, error) { | ||
u := ToUnstructured(source) | ||
paved := fieldpath.Pave(u.Object) | ||
skipFieldPaths = append(skipFieldPaths, "apiVersion", "kind") | ||
for _, p := range skipFieldPaths { | ||
if err := paved.DeleteField(p); err != nil { | ||
return nil, errors.Wrapf(err, errFmtPavedDelete, p) | ||
} | ||
} | ||
u.SetGroupVersionKind(targetGVK) | ||
return target, errors.Wrap(runtime.DefaultUnstructuredConverter.FromUnstructured(u.Object, target), errFromUnstructured) | ||
} | ||
|
||
func sanitizeResource(m map[string]any) map[string]any { | ||
delete(m, "status") | ||
if _, ok := m["metadata"]; !ok { | ||
return m | ||
} | ||
metadata := m["metadata"].(map[string]any) | ||
|
||
if v := metadata["creationTimestamp"]; v == nil { | ||
delete(metadata, "creationTimestamp") | ||
} | ||
if len(metadata) == 0 { | ||
delete(m, "metadata") | ||
} | ||
return m | ||
} | ||
|
||
func ToUnstructured(mg any) unstructured.Unstructured { | ||
m, err := runtime.DefaultUnstructuredConverter.ToUnstructured(mg) | ||
if err != nil { | ||
panic(errors.Wrap(err, errToUnstructured)) | ||
} | ||
return unstructured.Unstructured{ | ||
Object: sanitizeResource(m), | ||
} | ||
} | ||
|
||
func FromRawExtension(r runtime.RawExtension) (unstructured.Unstructured, error) { | ||
var m map[string]interface{} | ||
if err := json.Unmarshal(r.Raw, &m); err != nil { | ||
return unstructured.Unstructured{}, errors.Wrap(err, errRawExtensionUnmarshal) | ||
} | ||
return unstructured.Unstructured{ | ||
Object: m, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package migration | ||
|
||
type NoopTarget struct{} | ||
|
||
func (_ NoopTarget) Put(_ UnstructuredWithMetadata) error { | ||
return nil | ||
} | ||
|
||
func (_ NoopTarget) Patch(_ UnstructuredWithMetadata) error { | ||
return nil | ||
} | ||
|
||
func (_ NoopTarget) Delete(_ UnstructuredWithMetadata) error { | ||
return nil | ||
} |
Oops, something went wrong.