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

🌱 Small improvements to the inmemory api server #9935

Merged
Show file tree
Hide file tree
Changes from all 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
54 changes: 47 additions & 7 deletions test/infrastructure/inmemory/internal/server/api/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,46 @@ var (
corev1APIResourceList = &metav1.APIResourceList{
GroupVersion: "v1",
APIResources: []metav1.APIResource{
{
Name: "configmaps",
SingularName: "",
Namespaced: true,
Kind: "ConfigMap",
Verbs: []string{
"create",
"delete",
"deletecollection",
"get",
"list",
"patch",
"update",
"watch",
},
ShortNames: []string{
"cm",
},
StorageVersionHash: "",
},
{
Name: "endpoints",
SingularName: "",
Namespaced: true,
Kind: "Endpoints",
Verbs: []string{
"create",
"delete",
"deletecollection",
"get",
"list",
"patch",
"update",
"watch",
},
ShortNames: []string{
"ep",
},
StorageVersionHash: "",
},
{
Name: "nodes",
SingularName: "",
Expand Down Expand Up @@ -66,10 +106,10 @@ var (
StorageVersionHash: "",
},
{
Name: "configmaps",
Name: "secrets",
SingularName: "",
Namespaced: true,
Kind: "ConfigMap",
Kind: "Secret",
Verbs: []string{
"create",
"delete",
Expand All @@ -80,16 +120,13 @@ var (
"update",
"watch",
},
ShortNames: []string{
"cm",
},
StorageVersionHash: "",
},
{
Name: "secrets",
Name: "services",
SingularName: "",
Namespaced: true,
Kind: "Secret",
Kind: "Service",
Verbs: []string{
"create",
"delete",
Expand All @@ -100,6 +137,9 @@ var (
"update",
"watch",
},
ShortNames: []string{
"svc",
},
StorageVersionHash: "",
},
},
Expand Down
38 changes: 29 additions & 9 deletions test/infrastructure/inmemory/internal/server/api/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package api

import (
"context"
"errors"
"fmt"
"io"
"net"
Expand All @@ -29,7 +30,6 @@ import (

"github.com/emicklei/go-restful/v3"
"github.com/go-logr/logr"
"github.com/pkg/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down Expand Up @@ -274,7 +274,11 @@ func (h *apiServerHandler) apiV1Create(req *restful.Request, resp *restful.Respo
// TODO: consider check vs enforce for namespace on the object - namespace on the request path
obj.SetNamespace(req.PathParameter("namespace"))
if err := cloudClient.Create(ctx, obj); err != nil {
_ = resp.WriteErrorString(http.StatusInternalServerError, err.Error())
if status, ok := err.(apierrors.APIStatus); ok || errors.As(err, &status) {
_ = resp.WriteHeaderAndEntity(int(status.Status().Code), status)
sbueringer marked this conversation as resolved.
Show resolved Hide resolved
return
}
_ = resp.WriteHeaderAndEntity(http.StatusInternalServerError, err.Error())
return
}
if err := resp.WriteEntity(obj); err != nil {
Expand Down Expand Up @@ -324,7 +328,11 @@ func (h *apiServerHandler) apiV1List(req *restful.Request, resp *restful.Respons
}

if err := cloudClient.List(ctx, list, listOpts...); err != nil {
_ = resp.WriteErrorString(http.StatusInternalServerError, err.Error())
if status, ok := err.(apierrors.APIStatus); ok || errors.As(err, &status) {
_ = resp.WriteHeaderAndEntity(int(status.Status().Code), status)
return
}
_ = resp.WriteHeaderAndEntity(http.StatusInternalServerError, err.Error())
return
}
if err := resp.WriteEntity(list); err != nil {
Expand Down Expand Up @@ -439,7 +447,11 @@ func (h *apiServerHandler) apiV1Update(req *restful.Request, resp *restful.Respo
// TODO: consider check vs enforce for namespace on the object - namespace on the request path
obj.SetNamespace(req.PathParameter("namespace"))
if err := cloudClient.Update(ctx, obj); err != nil {
_ = resp.WriteErrorString(http.StatusInternalServerError, err.Error())
if status, ok := err.(apierrors.APIStatus); ok || errors.As(err, &status) {
_ = resp.WriteHeaderAndEntity(int(status.Status().Code), status)
return
}
_ = resp.WriteHeaderAndEntity(http.StatusInternalServerError, err.Error())
return
}
if err := resp.WriteEntity(obj); err != nil {
Expand Down Expand Up @@ -488,7 +500,11 @@ func (h *apiServerHandler) apiV1Patch(req *restful.Request, resp *restful.Respon
return
}
if err := cloudClient.Patch(ctx, obj, patch); err != nil {
_ = resp.WriteErrorString(http.StatusInternalServerError, err.Error())
if status, ok := err.(apierrors.APIStatus); ok || errors.As(err, &status) {
sbueringer marked this conversation as resolved.
Show resolved Hide resolved
_ = resp.WriteHeaderAndEntity(int(status.Status().Code), status)
return
}
_ = resp.WriteHeaderAndEntity(http.StatusInternalServerError, err.Error())
return
}
if err := resp.WriteEntity(obj); err != nil {
Expand Down Expand Up @@ -525,7 +541,11 @@ func (h *apiServerHandler) apiV1Delete(req *restful.Request, resp *restful.Respo
obj.SetNamespace(req.PathParameter("namespace"))

if err := cloudClient.Delete(ctx, obj); err != nil {
_ = resp.WriteErrorString(http.StatusInternalServerError, err.Error())
if status, ok := err.(apierrors.APIStatus); ok || errors.As(err, &status) {
_ = resp.WriteHeaderAndEntity(int(status.Status().Code), status)
return
}
_ = resp.WriteHeaderAndEntity(http.StatusInternalServerError, err.Error())
return
}
}
Expand Down Expand Up @@ -613,11 +633,11 @@ func (h *apiServerHandler) healthz(_ *restful.Request, resp *restful.Response) {
func requestToGVK(req *restful.Request) (*schema.GroupVersionKind, error) {
resourceList := getAPIResourceList(req)
if resourceList == nil {
return nil, errors.Errorf("no APIResourceList defined for %s", req.PathParameters())
return nil, fmt.Errorf("no APIResourceList defined for %s", req.PathParameters())
}
gv, err := schema.ParseGroupVersion(resourceList.GroupVersion)
if err != nil {
return nil, errors.Errorf("invalid group version in APIResourceList: %s", resourceList.GroupVersion)
return nil, fmt.Errorf("invalid group version in APIResourceList: %s", resourceList.GroupVersion)
}

resource := req.PathParameter("resource")
Expand All @@ -627,7 +647,7 @@ func requestToGVK(req *restful.Request) (*schema.GroupVersionKind, error) {
return &gvk, nil
}
}
return nil, errors.Errorf("Resource %s is not defined in the APIResourceList for %s", resource, req.PathParameters())
return nil, fmt.Errorf("resource %s is not defined in the APIResourceList for %s", resource, req.PathParameters())
}

func getAPIResourceList(req *restful.Request) *metav1.APIResourceList {
Expand Down
Loading