Skip to content

Commit

Permalink
Moved resource version annotation to a different file
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bharat-m1 committed Aug 8, 2023
1 parent f5d414e commit 925d1f9
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 23 deletions.
4 changes: 2 additions & 2 deletions commands/alpha/rpkg/pull/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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 {
Expand Down
22 changes: 20 additions & 2 deletions commands/alpha/rpkg/pull/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,22 @@ data:
apiVersion: config.kubernetes.io/v1
kind: ResourceList
items:
- apiVersion: kpt.dev/v1
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'
- 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'
Expand Down Expand Up @@ -121,13 +130,22 @@ data:
apiVersion: config.kubernetes.io/v1
kind: ResourceList
items:
- apiVersion: kpt.dev/v1
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'
- 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'
Expand Down
2 changes: 1 addition & 1 deletion commands/alpha/rpkg/push/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (r *runner) runE(cmd *cobra.Command, args []string) error {
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)
}

Expand Down
31 changes: 13 additions & 18 deletions commands/alpha/rpkg/util/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -68,11 +69,11 @@ func GetResourceFileKubeObject(prr *api.PackageRevisionResources, file, kind, na
}

func GetResourceVersionAnnotation(prr *api.PackageRevisionResources) (string, error) {
ko, err := GetResourceFileKubeObject(prr, "Kptfile", "Kptfile", "")

ko, err := GetResourceFileKubeObject(prr, kptfilev1.RevisionMetaDataFileName, kptfilev1.RevisionMetaDataKind, "")
if err != nil {
return "", err
}

annotations := ko.GetAnnotations()
rv, ok := annotations[ResourceVersionAnnotation]
if !ok {
Expand All @@ -81,29 +82,23 @@ func GetResourceVersionAnnotation(prr *api.PackageRevisionResources) (string, er
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)
kptMetaDataKo.SetName(kptfileKo.GetName())
kptMetaDataKo.SetAnnotation(ResourceVersionAnnotation, prr.GetResourceVersion())
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
}
4 changes: 4 additions & 0 deletions pkg/api/kptfile/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ import (
const (
KptFileName = "Kptfile"

RevisionMetaDataFileName = "KptRevisionMetaData"

RevisionMetaDataKind = "KptRevisionMetaData"

// Deprecated: prefer KptFileGVK
KptFileKind = "Kptfile"

Expand Down

0 comments on commit 925d1f9

Please sign in to comment.