From 3295ee559fbfdb90772540048056c2aa2a8e4e46 Mon Sep 17 00:00:00 2001 From: Bharat Mudragada Date: Wed, 9 Aug 2023 09:24:43 +0000 Subject: [PATCH] Move resource version annotation to a different file The newly added file can be used for storing all the metadata going forward It is added and removed from the client side and will not be present in porch Please refer this for more info: #4017 --- commands/alpha/rpkg/pull/command.go | 4 +- commands/alpha/rpkg/pull/command_test.go | 50 ++++++++++++++++++++++- commands/alpha/rpkg/push/command.go | 4 +- commands/alpha/rpkg/util/common.go | 45 ++++++++++++-------- e2e/testdata/porch/rpkg-clone/config.yaml | 22 ++++++++++ pkg/api/kptfile/v1/types.go | 10 +++++ 6 files changed, 111 insertions(+), 24 deletions(-) diff --git a/commands/alpha/rpkg/pull/command.go b/commands/alpha/rpkg/pull/command.go index e022f80ee7..279a9bf0e5 100644 --- a/commands/alpha/rpkg/pull/command.go +++ b/commands/alpha/rpkg/pull/command.go @@ -113,7 +113,7 @@ func (r *runner) runE(_ *cobra.Command, args []string) error { return errors.E(op, err) } - if err := util.AddResourceVersionAnnotation(&resources); err != nil { + if err := util.AddRevisionMetaData(&resources); err != nil { return errors.E(op, err) } @@ -200,7 +200,7 @@ func createScheme() (*runtime.Scheme, error) { return scheme, nil } -var matchResourceContents = append(kio.MatchAll, kptfilev1.KptFileName) +var matchResourceContents = append(kio.MatchAll, kptfilev1.KptFileName, kptfilev1.RevisionMetaDataFileName) func includeFile(path string) bool { for _, m := range matchResourceContents { diff --git a/commands/alpha/rpkg/pull/command_test.go b/commands/alpha/rpkg/pull/command_test.go index 2adc9b0b1d..9dcd6c0ea8 100644 --- a/commands/alpha/rpkg/pull/command_test.go +++ b/commands/alpha/rpkg/pull/command_test.go @@ -68,13 +68,36 @@ data: apiVersion: config.kubernetes.io/v1 kind: ResourceList items: +- apiVersion: kpt.dev/v1 +<<<<<<< HEAD + kind: KptRevisionMetaData + metadata: + name: bar + annotations: + internal.kpt.dev/resource-version: "999" + config.kubernetes.io/index: '0' + internal.config.kubernetes.io/index: '0' + internal.config.kubernetes.io/path: 'KptRevisionMetaData' + config.kubernetes.io/path: 'KptRevisionMetaData' +======= + kind: KptRevisionMetadata + metadata: + name: repo-fjdos9u2nfe2f32 + namespace: ns + creationTimestamp: null + resourceVersion: "999" + annotations: + config.kubernetes.io/index: '0' + internal.config.kubernetes.io/index: '0' + internal.config.kubernetes.io/path: '.KptRevisionMetadata' + config.kubernetes.io/path: '.KptRevisionMetadata' +>>>>>>> 90760422 (Moved resource version annotation to a different file) - apiVersion: kpt.dev/v1 kind: Kptfile metadata: name: bar annotations: config.kubernetes.io/local-config: "true" - internal.kpt.dev/resource-version: "999" config.kubernetes.io/index: '0' internal.config.kubernetes.io/index: '0' internal.config.kubernetes.io/path: 'Kptfile' @@ -121,13 +144,36 @@ data: apiVersion: config.kubernetes.io/v1 kind: ResourceList items: +- apiVersion: kpt.dev/v1 +<<<<<<< HEAD + kind: KptRevisionMetaData + metadata: + name: bar + annotations: + internal.kpt.dev/resource-version: "999" + config.kubernetes.io/index: '0' + internal.config.kubernetes.io/index: '0' + internal.config.kubernetes.io/path: 'KptRevisionMetaData' + config.kubernetes.io/path: 'KptRevisionMetaData' +======= + kind: KptRevisionMetadata + metadata: + name: repo-fjdos9u2nfe2f32 + namespace: ns + creationTimestamp: null + resourceVersion: "999" + annotations: + config.kubernetes.io/index: '0' + internal.config.kubernetes.io/index: '0' + internal.config.kubernetes.io/path: '.KptRevisionMetadata' + config.kubernetes.io/path: '.KptRevisionMetadata' +>>>>>>> 90760422 (Moved resource version annotation to a different file) - apiVersion: kpt.dev/v1 kind: Kptfile metadata: name: bar annotations: config.kubernetes.io/local-config: "true" - internal.kpt.dev/resource-version: "999" config.kubernetes.io/index: '0' internal.config.kubernetes.io/index: '0' internal.config.kubernetes.io/path: 'Kptfile' diff --git a/commands/alpha/rpkg/push/command.go b/commands/alpha/rpkg/push/command.go index e96f713292..92e07127d4 100644 --- a/commands/alpha/rpkg/push/command.go +++ b/commands/alpha/rpkg/push/command.go @@ -134,12 +134,12 @@ func (r *runner) runE(cmd *cobra.Command, args []string) error { }, } - rv, err := util.GetResourceVersionAnnotation(&pkgResources) + rv, err := util.GetResourceVersion(&pkgResources) if err != nil { return errors.E(op, err) } pkgResources.ResourceVersion = rv - if err = util.RemoveResourceVersionAnnotation(&pkgResources); err != nil { + if err = util.RemoveRevisionMetaData(&pkgResources); err != nil { return errors.E(op, err) } diff --git a/commands/alpha/rpkg/util/common.go b/commands/alpha/rpkg/util/common.go index 7dafe9795d..d621d7450a 100644 --- a/commands/alpha/rpkg/util/common.go +++ b/commands/alpha/rpkg/util/common.go @@ -19,6 +19,7 @@ import ( "fmt" fnsdk "github.com/GoogleContainerTools/kpt-functions-sdk/go/fn" + kptfilev1 "github.com/GoogleContainerTools/kpt/pkg/api/kptfile/v1" api "github.com/GoogleContainerTools/kpt/porch/api/porch/v1alpha1" "sigs.k8s.io/controller-runtime/pkg/client" ) @@ -67,43 +68,51 @@ func GetResourceFileKubeObject(prr *api.PackageRevisionResources, file, kind, na return ko, nil } +<<<<<<< HEAD func GetResourceVersionAnnotation(prr *api.PackageRevisionResources) (string, error) { - ko, err := GetResourceFileKubeObject(prr, "Kptfile", "Kptfile", "") - +======= +func GetResourceVersion(prr *api.PackageRevisionResources) (string, error) { +>>>>>>> 90760422 (Moved resource version annotation to a different file) + ko, err := GetResourceFileKubeObject(prr, kptfilev1.RevisionMetaDataFileName, kptfilev1.RevisionMetaDataKind, "") if err != nil { return "", err } + +<<<<<<< HEAD annotations := ko.GetAnnotations() rv, ok := annotations[ResourceVersionAnnotation] if !ok { rv = "" } +======= + rv, _, _ := ko.NestedString("metadata", "resourceVersion") +>>>>>>> 90760422 (Moved resource version annotation to a different file) return rv, nil } -func AddResourceVersionAnnotation(prr *api.PackageRevisionResources) error { - ko, err := GetResourceFileKubeObject(prr, "Kptfile", "Kptfile", "") +func AddRevisionMetaData(prr *api.PackageRevisionResources) error { + kptfileKo, err := GetResourceFileKubeObject(prr, "Kptfile", "Kptfile", "") if err != nil { return err } - ko.SetAnnotation(ResourceVersionAnnotation, prr.GetResourceVersion()) - prr.Spec.Resources["Kptfile"] = ko.String() + kptMetaDataKo := fnsdk.NewEmptyKubeObject() + kptMetaDataKo.SetAPIVersion(kptfileKo.GetAPIVersion()) + kptMetaDataKo.SetKind(kptfilev1.RevisionMetaDataKind) +<<<<<<< HEAD + kptMetaDataKo.SetName(kptfileKo.GetName()) + kptMetaDataKo.SetAnnotation(ResourceVersionAnnotation, prr.GetResourceVersion()) +======= + if err := kptMetaDataKo.SetNestedField(prr.GetObjectMeta(), "metadata"); err != nil { + return fmt.Errorf("cannot set metadata: %v", err) + } +>>>>>>> 90760422 (Moved resource version annotation to a different file) + prr.Spec.Resources[kptfilev1.RevisionMetaDataFileName] = kptMetaDataKo.String() return nil } -func RemoveResourceVersionAnnotation(prr *api.PackageRevisionResources) error { - ko, err := GetResourceFileKubeObject(prr, "Kptfile", "Kptfile", "") - if err != nil { - return err - } - - _, err = ko.RemoveNestedField("metadata", "annotations", ResourceVersionAnnotation) - if err != nil { - return err - } - prr.Spec.Resources["Kptfile"] = ko.String() - +func RemoveRevisionMetaData(prr *api.PackageRevisionResources) error { + delete(prr.Spec.Resources, kptfilev1.RevisionMetaDataFileName) return nil } diff --git a/e2e/testdata/porch/rpkg-clone/config.yaml b/e2e/testdata/porch/rpkg-clone/config.yaml index 05ba6bf2e7..cbf9483e23 100644 --- a/e2e/testdata/porch/rpkg-clone/config.yaml +++ b/e2e/testdata/porch/rpkg-clone/config.yaml @@ -61,6 +61,17 @@ commands: stdout: | apiVersion: config.kubernetes.io/v1 items: + - apiVersion: kpt.dev/v1 + kind: KptRevisionMetadata + metadata: + name: git-3465eed5831e5c372243d048631c8ef1666b47d6 + namespace: rpkg-clone + creationTimestamp: null + annotations: + config.kubernetes.io/index: '0' + internal.config.kubernetes.io/index: '0' + internal.config.kubernetes.io/path: '.KptRevisionMetadata' + config.kubernetes.io/path: '.KptRevisionMetadata' - apiVersion: kpt.dev/v1 info: description: sample description @@ -107,6 +118,17 @@ commands: stdout: | apiVersion: config.kubernetes.io/v1 items: + - apiVersion: kpt.dev/v1 + kind: KptRevisionMetadata + metadata: + name: git-b67f9ce14d378317ba83c9504eab9cc024932dd3 + namespace: rpkg-clone + creationTimestamp: null + annotations: + config.kubernetes.io/index: '0' + internal.config.kubernetes.io/index: '0' + internal.config.kubernetes.io/path: '.KptRevisionMetadata' + config.kubernetes.io/path: '.KptRevisionMetadata' - apiVersion: kpt.dev/v1 info: description: Empty Blueprint diff --git a/pkg/api/kptfile/v1/types.go b/pkg/api/kptfile/v1/types.go index eed630e845..0d6a7dd6f2 100644 --- a/pkg/api/kptfile/v1/types.go +++ b/pkg/api/kptfile/v1/types.go @@ -29,6 +29,16 @@ import ( const ( KptFileName = "Kptfile" +<<<<<<< HEAD + RevisionMetaDataFileName = "KptRevisionMetaData" + + RevisionMetaDataKind = "KptRevisionMetaData" +======= + RevisionMetaDataFileName = ".KptRevisionMetadata" + + RevisionMetaDataKind = "KptRevisionMetadata" +>>>>>>> 90760422 (Moved resource version annotation to a different file) + // Deprecated: prefer KptFileGVK KptFileKind = "Kptfile"