From e972e69896b45bb244bbf82ec871eadb860911d2 Mon Sep 17 00:00:00 2001 From: Tamal Saha Date: Mon, 9 Oct 2023 01:01:22 -0700 Subject: [PATCH] Fix hub resource filename parsing Signed-off-by: Tamal Saha --- go.mod | 2 +- go.sum | 4 +- .../resource-metadata/hub/registry.go | 48 ++++++++++++++++++- .../resource-metadata/pkg/layouts/lib.go | 15 ++---- vendor/modules.txt | 2 +- 5 files changed, 53 insertions(+), 18 deletions(-) diff --git a/go.mod b/go.mod index 4d27c203e..575465f5b 100644 --- a/go.mod +++ b/go.mod @@ -46,7 +46,7 @@ require ( kmodules.xyz/custom-resources v0.25.2 kmodules.xyz/go-containerregistry v0.0.11 kmodules.xyz/monitoring-agent-api v0.25.4 - kmodules.xyz/resource-metadata v0.17.25-0.20231008042713-ef9e7bdbcfb9 + kmodules.xyz/resource-metadata v0.17.26-0.20231009075914-70d9638b6217 kmodules.xyz/resource-metrics v0.25.3 kmodules.xyz/sets v0.25.0 kubeops.dev/scanner v0.0.15-0.20230929084557-c991a6514ff9 diff --git a/go.sum b/go.sum index 562351c81..675181c24 100644 --- a/go.sum +++ b/go.sum @@ -2141,8 +2141,8 @@ kmodules.xyz/monitoring-agent-api v0.25.4 h1:OwkvtV23QhUy3f7o9lxPWaAW3jiJMGAMPPR kmodules.xyz/monitoring-agent-api v0.25.4/go.mod h1:3LhrLDGQKQXhxYcjA/WNaO4HPpopYQzOutsEp2i3008= kmodules.xyz/offshoot-api v0.25.4 h1:IjJNvkphcdYUG8XO/pBwXpuP8W+jxAWJZ3yH8vgI/as= kmodules.xyz/offshoot-api v0.25.4/go.mod h1:PUk4EuJFhhyQykCflHj7EgXcljGIqs9vi0IN0RpxtY4= -kmodules.xyz/resource-metadata v0.17.25-0.20231008042713-ef9e7bdbcfb9 h1:CNuwviXfHRSi8m8pgYc5CVLvd3jTghDRKgsixbE1ei8= -kmodules.xyz/resource-metadata v0.17.25-0.20231008042713-ef9e7bdbcfb9/go.mod h1:tyLxzAVkhlL3/jFdcQcX1RZ8i8h9bs+AJur5RcuaW40= +kmodules.xyz/resource-metadata v0.17.26-0.20231009075914-70d9638b6217 h1:CFfhJCxurqlOaBlmzRWZAeCYQ6sOihDmIaSx0FWNcLg= +kmodules.xyz/resource-metadata v0.17.26-0.20231009075914-70d9638b6217/go.mod h1:tyLxzAVkhlL3/jFdcQcX1RZ8i8h9bs+AJur5RcuaW40= kmodules.xyz/resource-metrics v0.25.3 h1:g9EjNfYRrUSnbA4r+bUQefQ5Ban6I6rpKjnB3ER+Yew= kmodules.xyz/resource-metrics v0.25.3/go.mod h1:H7YLdUQJXUSzf5cNI4IYWU4Wsmrua/jpw7gqDnE3BwM= kmodules.xyz/sets v0.25.0 h1:belY/3trp/M/CKc1TEteA40jb2uCIdwKHhjpvrIxG+8= diff --git a/vendor/kmodules.xyz/resource-metadata/hub/registry.go b/vendor/kmodules.xyz/resource-metadata/hub/registry.go index 3722556e2..0ef52c5f8 100644 --- a/vendor/kmodules.xyz/resource-metadata/hub/registry.go +++ b/vendor/kmodules.xyz/resource-metadata/hub/registry.go @@ -446,8 +446,15 @@ func (r *Registry) LoadByGVK(gvk schema.GroupVersionKind) (*v1alpha1.ResourceDes } func (r *Registry) LoadByName(name string) (*v1alpha1.ResourceDescriptor, error) { - filename := strings.Replace(name, "-", "/", 2) + ".yaml" - return r.LoadByFile(filename) + return r.LoadByFile(toFilename(name)) +} + +func toFilename(name string) string { + name = reverse(name) + name = strings.Replace(name, "-", "/", 2) + name = reverse(name) + filename := name + ".yaml" + return filename } func (r *Registry) LoadByFile(filename string) (*v1alpha1.ResourceDescriptor, error) { @@ -473,3 +480,40 @@ func IsUnregisteredErr(err error) bool { _, okp := err.(*UnregisteredErr) return err != nil && (ok || okp) } + +func ParseGVR(name string) (*schema.GroupVersionResource, error) { + name = reverse(name) + parts := strings.SplitN(name, "-", 3) + if len(parts) != 3 { + return nil, fmt.Errorf("%s is not a valid gvr encoded name", name) + } + gvr := schema.GroupVersionResource{ + Group: reverse(parts[2]), + Version: reverse(parts[1]), + Resource: reverse(parts[0]), + } + if gvr.Group == "core" { + gvr.Group = "" + } + return &gvr, nil +} + +// ref: https://groups.google.com/g/golang-nuts/c/oPuBaYJ17t4/m/PCmhdAyrNVkJ +func reverse(input string) string { + // Get Unicode code points. + n := 0 + rune := make([]int32, len(input)) + for _, r := range input { + rune[n] = r + n++ + } + rune = rune[0:n] + + // Reverse + for i := 0; i < n/2; i++ { + rune[i], rune[n-1-i] = rune[n-1-i], rune[i] + } + + // Convert back to UTF-8. + return string(rune) +} diff --git a/vendor/kmodules.xyz/resource-metadata/pkg/layouts/lib.go b/vendor/kmodules.xyz/resource-metadata/pkg/layouts/lib.go index c78930db3..284cccef8 100644 --- a/vendor/kmodules.xyz/resource-metadata/pkg/layouts/lib.go +++ b/vendor/kmodules.xyz/resource-metadata/pkg/layouts/lib.go @@ -18,7 +18,6 @@ package layouts import ( "fmt" - "strings" kmapi "kmodules.xyz/client-go/api/v1" meta_util "kmodules.xyz/client-go/meta" @@ -151,19 +150,11 @@ func generateDefaultLayout(kc client.Client, rid kmapi.ResourceID) (*v1alpha1.Re func LoadResourceLayout(kc client.Client, name string) (*v1alpha1.ResourceLayout, error) { outline, err := resourceoutlines.LoadByName(name) if apierrors.IsNotFound(err) { - parts := strings.SplitN(name, "-", 3) - if len(parts) != 3 { + gvr, e2 := hub.ParseGVR(name) + if e2 != nil { return nil, err } - var group string - if parts[0] != "core" { - group = parts[0] - } - return LoadResourceLayoutForGVR(kc, schema.GroupVersionResource{ - Group: group, - Version: parts[1], - Resource: parts[2], - }) + return LoadResourceLayoutForGVR(kc, *gvr) } else if err != nil { return nil, err } diff --git a/vendor/modules.txt b/vendor/modules.txt index 38af978f7..83e92ff56 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1800,7 +1800,7 @@ kmodules.xyz/monitoring-agent-api/client # kmodules.xyz/offshoot-api v0.25.4 ## explicit; go 1.18 kmodules.xyz/offshoot-api/api/v1 -# kmodules.xyz/resource-metadata v0.17.25-0.20231008042713-ef9e7bdbcfb9 +# kmodules.xyz/resource-metadata v0.17.26-0.20231009075914-70d9638b6217 ## explicit; go 1.18 kmodules.xyz/resource-metadata/apis/core/install kmodules.xyz/resource-metadata/apis/core/v1alpha1