Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP]Feat version convert proxy #172

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,7 @@ github.com/morikuni/aec v1.0.0 h1:nP9CBfwrvYnBRgY6qfDQkygYDmYwOilePFkwzv4dU8A=
github.com/morikuni/aec v1.0.0/go.mod h1:BbKIizmSmc5MMPqRYbxO4ZU0S0+P200+tUnFx7PXmsc=
github.com/mrunalp/fileutils v0.0.0-20200520151820-abd8a0e76976/go.mod h1:x8F1gnqOkIEiO4rqoeEEEqQbo7HjGMTvyoq3gej4iT0=
github.com/munnerz/goautoneg v0.0.0-20120707110453-a547fc61f48d/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/mvdan/xurls v1.1.0/go.mod h1:tQlNn3BED8bE/15hnSL2HLkDeLWpNPAwtw7wkEq44oU=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
Expand Down
24 changes: 12 additions & 12 deletions pkg/conversion/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type VersionConverter struct {
// clusterInfo hold the version info of target cluster
clusterInfo *version.Info

// RestMapper is response to map gvk and gvr
// RestMapper is response to map gvk and rawGvr
RestMapper meta.RESTMapper
}

Expand Down Expand Up @@ -147,7 +147,7 @@ func (c *VersionConverter) ObjectGreeting(obj runtime.Object) (greetBack GreetBa
return c.GvkGreeting(&gvk)
}

// GvrGreeting describes if given gvr is available in target cluster.
// GvrGreeting describes if given rawGvr is available in target cluster.
// a recommend group version kind will return if it cloud not pass through.
func (c *VersionConverter) GvrGreeting(gvr *schema.GroupVersionResource) (greetBack GreetBackType, rawGvk *schema.GroupVersionKind, recommendGvk *schema.GroupVersionKind, err error) {
gvk, err := Gvr2Gvk(c.RestMapper, gvr)
Expand Down Expand Up @@ -239,7 +239,7 @@ func (c *VersionConverter) Decode(data []byte, defaults *schema.GroupVersionKind
return decoder.Decode(data, defaults, into)
}

// Gvr2Gvk convert gvr to gvk by specified cluster
// Gvr2Gvk convert rawGvr to gvk by specified cluster
func Gvr2Gvk(mapper meta.RESTMapper, gvr *schema.GroupVersionResource) (*schema.GroupVersionKind, error) {
kinds, err := mapper.KindsFor(*gvr)
if err != nil {
Expand All @@ -254,7 +254,7 @@ func Gvr2Gvk(mapper meta.RESTMapper, gvr *schema.GroupVersionResource) (*schema.
return &kinds[0], nil
}

// Gvk2Gvr convert gvk to gvr by specified cluster
// Gvk2Gvr convert gvk to rawGvr by specified cluster
func Gvk2Gvr(mapper meta.RESTMapper, gvk *schema.GroupVersionKind) (*schema.GroupVersionResource, error) {
m, err := mapper.RESTMapping(gvk.GroupKind(), gvk.Version)
if err != nil {
Expand All @@ -264,7 +264,7 @@ func Gvk2Gvr(mapper meta.RESTMapper, gvk *schema.GroupVersionKind) (*schema.Grou
return &m.Resource, nil
}

// ConvertURL convert url by given gvr
// ConvertURL convert url by given rawGvr
func ConvertURL(url string, gvr *schema.GroupVersionResource) (convertedUrl string, err error) {
const sep = "/"

Expand Down Expand Up @@ -296,9 +296,9 @@ func ConvertURL(url string, gvr *schema.GroupVersionResource) (convertedUrl stri
return sep + strings.Join(ss, "/"), nil
}

// ParseURL parse k8s api url into gvr
// ParseURL parse k8s api url into rawGvr
func ParseURL(url string) (bool, bool, *schema.GroupVersionResource, error) {
invalidUrlErr := fmt.Errorf("url not k8s format: %s", url)
notExpectedUrlErr := fmt.Errorf("url format not expected: %s", url)

const (
coreApiPrefix = "/api/"
Expand All @@ -319,35 +319,35 @@ func ParseURL(url string) (bool, bool, *schema.GroupVersionResource, error) {
case isCoreApi && isNamespaced:
// like: /api/v1/namespaces/{namespace}/pods
if len(ss) < 5 {
return false, false, nil, invalidUrlErr
return false, false, nil, notExpectedUrlErr
}
gvr.Version = ss[1]
gvr.Resource = ss[4]
case isCoreApi && !isNamespaced:
// like: /api/v1/namespaces/{name}
if len(ss) < 3 {
return false, false, nil, invalidUrlErr
return false, false, nil, notExpectedUrlErr
}
gvr.Version = ss[1]
gvr.Resource = ss[2]
case isNonCoreApi && isNamespaced:
// like: /apis/batch/v1/namespaces/{namespace}/jobs
if len(ss) < 6 {
return false, false, nil, invalidUrlErr
return false, false, nil, notExpectedUrlErr
}
gvr.Group = ss[1]
gvr.Version = ss[2]
gvr.Resource = ss[5]
case isNonCoreApi && !isNamespaced:
// like: /apis/rbac.authorization.k8s.io/v1/clusterroles
if len(ss) < 4 {
return false, false, nil, invalidUrlErr
return false, false, nil, notExpectedUrlErr
}
gvr.Group = ss[1]
gvr.Version = ss[2]
gvr.Resource = ss[3]
default:
return false, false, nil, invalidUrlErr
return false, false, nil, notExpectedUrlErr
}

return isCoreApi, isNamespaced, gvr, nil
Expand Down
32 changes: 30 additions & 2 deletions pkg/conversion/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,27 +181,55 @@ func TestConvertURL(t *testing.T) {
wantConvertedUrl: "/api/test-version/namespaces/default/pods",
wantErr: false,
},
{
name: "core namespaced watch api",
url: "/api/v1/watch/namespaces/default/pods",
gvr: &schema.GroupVersionResource{Version: testVersion, Resource: "pods"},
wantConvertedUrl: "/api/test-version/watch/namespaces/default/pods",
wantErr: false,
},
{
name: "core cluster api",
url: "/api/v1/namespaces/test-ns",
gvr: &schema.GroupVersionResource{Version: testVersion, Resource: "namespaces"},
wantConvertedUrl: "/api/test-version/namespaces/test-ns",
wantErr: false,
},
{
name: "core cluster watch api",
url: "/api/v1/watch/namespaces/test-ns",
gvr: &schema.GroupVersionResource{Version: testVersion, Resource: "namespaces"},
wantConvertedUrl: "/api/test-version/watch/namespaces/test-ns",
wantErr: false,
},
{
name: "no-core namespaced api",
url: "/apis/batch/v1/namespaces/default/jobs",
gvr: &schema.GroupVersionResource{Group: testGroup, Version: testVersion, Resource: "jobs"},
wantConvertedUrl: "/apis/test-group/test-version/namespaces/default/jobs",
wantErr: false,
},
{
name: "no-core namespaced watch api",
url: "/apis/batch/v1/watch/namespaces/default/jobs",
gvr: &schema.GroupVersionResource{Group: testGroup, Version: testVersion, Resource: "jobs"},
wantConvertedUrl: "/apis/test-group/test-version/watch/namespaces/default/jobs",
wantErr: false,
},
{
name: "no-core cluster api",
url: "/apis/rbac.authorization.k8s.io/v1/clusterroles",
gvr: &schema.GroupVersionResource{Group: testGroup, Version: testVersion, Resource: "clusterroles"},
wantConvertedUrl: "/apis/test-group/test-version/clusterroles",
wantErr: false,
},
{
name: "no-core cluster watch api",
url: "/apis/rbac.authorization.k8s.io/v1/watch/clusterroles",
gvr: &schema.GroupVersionResource{Group: testGroup, Version: testVersion, Resource: "clusterroles"},
wantConvertedUrl: "/apis/test-group/test-version/watch/clusterroles",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -274,7 +302,7 @@ func TestGvr2Gvk(t *testing.T) {
wantErr bool
}{
{
name: "normal gvr",
name: "normal rawGvr",
gvr: &schema.GroupVersionResource{Group: "apps", Version: "v1", Resource: "deployments"},
want: &schema.GroupVersionKind{Group: "apps", Version: "v1", Kind: "Deployment"},
wantErr: false,
Expand All @@ -295,7 +323,7 @@ func TestGvr2Gvk(t *testing.T) {
wantErr: true,
},
{
name: "unknown gvr",
name: "unknown rawGvr",
gvr: &schema.GroupVersionResource{Group: "unknown", Version: "unknown", Resource: "unknown"},
wantErr: true,
},
Expand Down
Loading