Skip to content

Commit

Permalink
Merge pull request #85 from warjiang/feature/opt-unstructured-api
Browse files Browse the repository at this point in the history
feat: update err-check cond, add method for create unstructured resou…
  • Loading branch information
karmada-bot authored Aug 19, 2024
2 parents 8777d59 + 68723c4 commit a2850b4
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
35 changes: 33 additions & 2 deletions cmd/api/app/routes/unstructured/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ func handleDeleteResource(c *gin.Context) {
_, getErr := verber.Get(kind, namespace, name)
return getErr
})
if err != nil {
klog.ErrorS(err, "Wait for verber delete resourcefailed")
if !errors.IsNotFound(err) {
klog.ErrorS(err, "Wait for verber delete resource failed")
common.Fail(c, err)
return
}
Expand Down Expand Up @@ -97,14 +97,45 @@ func handlePutResource(c *gin.Context) {
common.Success(c, "ok")
}

func handleCreateResource(c *gin.Context) {
// todo double-check existence of target resources, if exist return directly.
verber, err := client.VerberClient(c.Request)
if err != nil {
klog.ErrorS(err, "Failed to init VerberClient")
common.Fail(c, err)
return
}

raw := &unstructured.Unstructured{}
bytes, err := io.ReadAll(c.Request.Body)
if err != nil {
klog.ErrorS(err, "Failed to read request body")
common.Fail(c, err)
return
}
err = raw.UnmarshalJSON(bytes)
if err != nil {
klog.ErrorS(err, "Failed to unmarshal request body")
common.Fail(c, err)
}
if _, err = verber.Create(raw); err != nil {
klog.ErrorS(err, "Failed to create resource")
common.Fail(c, err)
return
}
common.Success(c, "ok")
}

func init() {
r := router.V1()
r.DELETE("/_raw/:kind/namespace/:namespace/name/:name", handleDeleteResource)
r.GET("/_raw/:kind/namespace/:namespace/name/:name", handleGetResource)
r.PUT("/_raw/:kind/namespace/:namespace/name/:name", handlePutResource)
r.POST("/_raw/:kind/namespace/:namespace/name/:name", handleCreateResource)

// Verber (non-namespaced)
r.DELETE("/_raw/:kind/name/:name", handleDeleteResource)
r.GET("/_raw/:kind/name/:name", handleGetResource)
r.PUT("/_raw/:kind/name/:name", handlePutResource)
r.POST("/_raw/:kind/name/:name", handleCreateResource)
}
1 change: 1 addition & 0 deletions pkg/client/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ type ResourceVerber interface {
Update(object *unstructured.Unstructured) error
Get(kind string, namespace string, name string) (runtime.Object, error)
Delete(kind string, namespace string, name string, deleteNow bool) error
Create(object *unstructured.Unstructured) (*unstructured.Unstructured, error)
}
8 changes: 8 additions & 0 deletions pkg/client/verber.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,14 @@ func (v *resourceVerber) Get(kind string, namespace string, name string) (runtim
return v.client.Resource(gvr).Namespace(namespace).Get(context.TODO(), name, metav1.GetOptions{})
}

// Create creates the resource of the given kind in the given namespace with the given name.
func (v *resourceVerber) Create(object *unstructured.Unstructured) (*unstructured.Unstructured, error) {
namespace := object.GetNamespace()
gvr := v.groupVersionResourceFromUnstructured(object)

return v.client.Resource(gvr).Namespace(namespace).Create(context.TODO(), object, metav1.CreateOptions{})
}

func VerberClient(request *http.Request) (ResourceVerber, error) {
// todo currently ignore rest.config from http.Request
restConfig, _, err := GetKarmadaConfig()
Expand Down
10 changes: 10 additions & 0 deletions ui/apps/dashboard/src/services/unstructured.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,13 @@ function generateUrlForUnstructuredParams(params: UnstructuredParams) {
return `/_raw/${kind}/name/${name}`;
}
}

export async function CreateResource(
params: UnstructuredParams & {
content: Record<string, any>;
},
) {
const url = generateUrlForUnstructuredParams(params);
const resp = await karmadaClient.post<IResponse<any>>(url, params.content);
return resp.data;
}

0 comments on commit a2850b4

Please sign in to comment.