Add gitops.kubesphere.io group proxy

This commit is contained in:
johnniang
2022-02-28 11:06:20 +08:00
parent 291d35cf93
commit 0e9d30ffe4
2 changed files with 34 additions and 12 deletions

View File

@@ -17,6 +17,8 @@ limitations under the License.
package devops
import (
"strings"
"github.com/emicklei/go-restful"
"k8s.io/apimachinery/pkg/runtime/schema"
@@ -26,13 +28,17 @@ import (
var devopsGroupVersions = []schema.GroupVersion{
{Group: "devops.kubesphere.io", Version: "v1alpha2"},
{Group: "devops.kubesphere.io", Version: "v1alpha3"},
{Group: "gitops.kubesphere.io", Version: "v1alpha1"},
// TODO Add other group versions here, like cd.devops.kubesphere.io
}
// AddToContainer registers DevOps proxies to the container.
func AddToContainer(container *restful.Container, endpoint string) error {
endpoint = strings.TrimSuffix(endpoint, "/")
for _, groupVersion := range devopsGroupVersions {
proxy, err := generic.NewGenericProxy(endpoint, groupVersion.Group, groupVersion.Version)
// Ensure that we proxy with different group here due to trimming of "/kapis/group_name".
// TODO: We could add a flag to decide to trim "/kapis/group_name" or not when creating a new GenericProxy.
proxy, err := generic.NewGenericProxy(endpoint+"/kapis/"+groupVersion.Group, groupVersion.Group, groupVersion.Version)
if err != nil {
return err
}

View File

@@ -39,44 +39,60 @@ func TestAddToContainer(t *testing.T) {
wantErr bool
wantResponse string
}{{
name: "Should proxy v1alpha1 API properly",
name: "Should proxy devops.kubesphere.io/v1alpha1 API properly",
args: args{
target: "/kapis/devops.kubesphere.io/v1alpha1/resources",
mockAPIPattern: "/v1alpha1/resources",
mockAPIPattern: "/kapis/devops.kubesphere.io/v1alpha1/resources",
mockResponse: fakeResponse,
},
wantResponse: notFoundResponse,
}, {
name: "Should proxy v1alpha2 API properly",
name: "Should proxy devops.kubesphere.io/v1alpha2 API properly",
args: args{
target: "/kapis/devops.kubesphere.io/v1alpha2/resources",
mockAPIPattern: "/v1alpha2/resources",
mockAPIPattern: "/kapis/devops.kubesphere.io/v1alpha2/resources",
mockResponse: fakeResponse,
},
wantResponse: fakeResponse,
}, {
name: "Should proxy v1alpha3 API properly",
name: "Should proxy devops.kubesphere.io/v1alpha3 API properly",
args: args{
target: "/kapis/devops.kubesphere.io/v1alpha3/resources",
mockAPIPattern: "/v1alpha3/resources",
mockAPIPattern: "/kapis/devops.kubesphere.io/v1alpha3/resources",
mockResponse: fakeResponse,
},
wantResponse: fakeResponse,
}, {
name: "Should return 404 if no pattern matches",
name: "Should proxy gitops.kubesphere.io/v1alpha1 API properly",
args: args{
target: "/kapis/gitops.kubesphere.io/v1alpha1/resources",
mockAPIPattern: "/kapis/gitops.kubesphere.io/v1alpha1/resources",
mockResponse: fakeResponse,
},
wantResponse: fakeResponse,
}, {
name: "Should return 404 if versions miss match",
args: args{
target: "/kapis/devops.kubesphere.io/v1alpha3/resources",
mockAPIPattern: "/v1alpha4/resources",
mockAPIPattern: "/kapis/devops.kubesphere.io/v1alpha1/resources",
},
wantResponse: notFoundResponse,
}, {
name: "Should not proxy v1alpha123 API properly event if pattern matched",
name: "Should return 404 if groups miss match",
args: args{
target: "/kapis/devops.kubesphere.io/v1alpha123/resources",
mockAPIPattern: "/v1alpha123/resources",
target: "/kapis/devops.kubesphere.io/v1alpha3/resources",
mockAPIPattern: "/kapis/gitops.kubesphere.io/v1alpha3/resources",
},
wantResponse: notFoundResponse,
},
{
name: "Should not proxy v1alpha123 API properly event if pattern matched",
args: args{
target: "/kapis/devops.kubesphere.io/v1alpha123/resources",
mockAPIPattern: "/kapis/devops.kubesphere.io/v1alpha123/resources",
},
wantResponse: notFoundResponse,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {