Skip to content

Commit

Permalink
Fix hub resource filename parsing
Browse files Browse the repository at this point in the history
Signed-off-by: Tamal Saha <[email protected]>
  • Loading branch information
tamalsaha committed Oct 9, 2023
1 parent 2f5d3ac commit e972e69
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 18 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
48 changes: 46 additions & 2 deletions vendor/kmodules.xyz/resource-metadata/hub/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
}
15 changes: 3 additions & 12 deletions vendor/kmodules.xyz/resource-metadata/pkg/layouts/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package layouts

import (
"fmt"
"strings"

kmapi "kmodules.xyz/client-go/api/v1"
meta_util "kmodules.xyz/client-go/meta"
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e972e69

Please sign in to comment.