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

feat: add event list scenario #1542

Merged
merged 3 commits into from
Aug 24, 2021
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
1 change: 1 addition & 0 deletions apistructs/component_protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ const (
OnCancel OperationKey = "cancel"
OnChangePageNoOperation OperationKey = "changePageNo"
OnChangePageSizeOperation OperationKey = "changePageSize"
OnChangeSortOperation OperationKey = "changeSort"
// Issue
MoveOutOperation OperationKey = "MoveOut"
DragOperation OperationKey = "drag"
Expand Down
57 changes: 3 additions & 54 deletions apistructs/steve.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,13 @@ import (
"encoding/json"
"fmt"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/erda-project/erda/pkg/strutil"
)

const (
SteveErrorType = "error"
)

// SteveCollection is a resource collection returned from steve server.
type SteveCollection struct {
Type string `json:"type,omitempty"`
Links map[string]string `json:"links"`
CreateTypes map[string]string `json:"createTypes,omitempty"`
Actions map[string]string `json:"actions"`
ResourceType string `json:"resourceType"`
Revision string `json:"revision"`
Pagination *Pagination `json:"pagination,omitempty"`
Continue string `json:"continue,omitempty"`
// steve resources
Data []SteveResource `json:"data"`
}

var (
BadRequest = SteveErrorCode{"BadRequest", 400}
Unauthorized = SteveErrorCode{"Unauthorized", 401}
Expand Down Expand Up @@ -81,31 +65,6 @@ func (s SteveError) JSON() []byte {
return data
}

// SteveResource is a steve resource returned from steve server.
type SteveResource struct {
K8SResource
ID string `json:"id,omitempty"`
Type string `json:"type,omitempty"`
Links map[string]string `json:"links"`
}

// K8SResource is a original k8s resource.
type K8SResource struct {
metav1.TypeMeta
Metadata metav1.ObjectMeta `json:"metadata,omitempty"`
Spec interface{} `json:"spec,omitempty"`
Status interface{} `json:"status,omitempty"`
Extra interface{} `json:"extra,omitempty"`
}

// Pagination used to paging query.
type Pagination struct {
Limit int `json:"limit,omitempty"` // maximum number of each page
First string `json:"first,omitempty"` // first page link
Next string `json:"next,omitempty"` // next page link
Partial bool `json:"partial,omitempty"` // whether partial
}

type K8SResType string

const (
Expand All @@ -115,6 +74,9 @@ const (
K8SReplicaSet K8SResType = "apps.replicasets"
K8SDaemonSet K8SResType = "apps.daemonsets"
K8SStatefulSet K8SResType = "apps.statefulsets"
K8SJob K8SResType = "batch.jobs"
K8SCronJob K8SResType = "batch.cronjobs"
K8SNamespace K8SResType = "namespace"
K8SEvent K8SResType = "events"
)

Expand Down Expand Up @@ -153,16 +115,3 @@ func (k *SteveRequest) URLQueryString() map[string][]string {
}
return query
}

func GetValue(obj interface{}, keys ...string) (interface{}, bool) {
data, _ := obj.(map[string]interface{})
for i, key := range keys {
if i == len(keys)-1 {
val, ok := data[key]
return val, ok
}
data, _ = data[key].(map[string]interface{})
}

return nil, false
}
96 changes: 43 additions & 53 deletions bundle/steve.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ import (
"io/ioutil"
"net/http"
"reflect"
"strconv"

"github.com/pkg/errors"
"github.com/rancher/wrangler/pkg/data"

"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/bundle/apierrors"
Expand All @@ -32,7 +34,7 @@ import (

// GetSteveResource gets k8s resource from steve server.
// Required fields: ClusterName, Name, Type.
func (b *Bundle) GetSteveResource(req *apistructs.SteveRequest) (*apistructs.SteveResource, error) {
func (b *Bundle) GetSteveResource(req *apistructs.SteveRequest) (data.Object, error) {
if req.Type == "" || req.ClusterName == "" || req.Name == "" {
return nil, errors.New("clusterName, name and type fields are required")
}
Expand Down Expand Up @@ -61,20 +63,20 @@ func (b *Bundle) GetSteveResource(req *apistructs.SteveRequest) (*apistructs.Ste
return nil, apierrors.ErrInvoke.InternalError(err)
}

if err = isSteveError(data); err != nil {
return nil, err
obj := map[string]interface{}{}
if err = json.Unmarshal(data, &obj); err != nil {
return nil, apierrors.ErrInvoke.InternalError(err)
}

var resource apistructs.SteveResource
if err = json.Unmarshal(data, &resource); err != nil {
return nil, apierrors.ErrInvoke.InternalError(err)
if err = isSteveError(obj); err != nil {
return nil, err
}
return &resource, nil
return obj, nil
}

// ListSteveResource lists k8s resource from steve server.
// Required fields: ClusterName, Type.
func (b *Bundle) ListSteveResource(req *apistructs.SteveRequest) (*apistructs.SteveCollection, error) {
func (b *Bundle) ListSteveResource(req *apistructs.SteveRequest) (data.Object, error) {
if req.Type == "" || req.ClusterName == "" {
return nil, errors.New("clusterName and type fields are required")
}
Expand Down Expand Up @@ -103,20 +105,20 @@ func (b *Bundle) ListSteveResource(req *apistructs.SteveRequest) (*apistructs.St
return nil, apierrors.ErrInvoke.InternalError(err)
}

if err = isSteveError(data); err != nil {
return nil, err
obj := map[string]interface{}{}
if err = json.Unmarshal(data, &obj); err != nil {
return nil, apierrors.ErrInvoke.InternalError(err)
}

var collection apistructs.SteveCollection
if err = json.Unmarshal(data, &collection); err != nil {
return nil, apierrors.ErrInvoke.InternalError(err)
if err = isSteveError(obj); err != nil {
return nil, err
}
return &collection, nil
return obj, nil
}

// UpdateSteveResource update a k8s resource described by req.Obj from steve server.
// Required fields: ClusterName, Type, Name, Obj
func (b *Bundle) UpdateSteveResource(req *apistructs.SteveRequest) (*apistructs.SteveResource, error) {
func (b *Bundle) UpdateSteveResource(req *apistructs.SteveRequest) (data.Object, error) {
if req.Type == "" || req.ClusterName == "" || req.Name == "" {
return nil, errors.New("clusterName, name and type fields are required")
}
Expand Down Expand Up @@ -147,20 +149,20 @@ func (b *Bundle) UpdateSteveResource(req *apistructs.SteveRequest) (*apistructs.
return nil, apierrors.ErrInvoke.InternalError(err)
}

if err = isSteveError(data); err != nil {
return nil, err
obj := map[string]interface{}{}
if err = json.Unmarshal(data, &obj); err != nil {
return nil, apierrors.ErrInvoke.InternalError(err)
}

var resource apistructs.SteveResource
if err = json.Unmarshal(data, &resource); err != nil {
return nil, apierrors.ErrInvoke.InternalError(err)
if err = isSteveError(obj); err != nil {
return nil, err
}
return &resource, nil
return obj, nil
}

// CreateSteveResource creates a k8s resource described by req.Obj from steve server.
// Required fields: ClusterName, Type, Obj
func (b *Bundle) CreateSteveResource(req *apistructs.SteveRequest) (*apistructs.SteveResource, error) {
func (b *Bundle) CreateSteveResource(req *apistructs.SteveRequest) (data.Object, error) {
if req.Type == "" || req.ClusterName == "" {
return nil, errors.New("clusterName and type fields are required")
}
Expand Down Expand Up @@ -191,15 +193,15 @@ func (b *Bundle) CreateSteveResource(req *apistructs.SteveRequest) (*apistructs.
return nil, apierrors.ErrInvoke.InternalError(err)
}

if err = isSteveError(data); err != nil {
return nil, err
obj := map[string]interface{}{}
if err = json.Unmarshal(data, &obj); err != nil {
return nil, apierrors.ErrInvoke.InternalError(err)
}

var resource apistructs.SteveResource
if err = json.Unmarshal(data, &resource); err != nil {
return nil, apierrors.ErrInvoke.InternalError(err)
if err = isSteveError(obj); err != nil {
return nil, err
}
return &resource, nil
return obj, nil
}

// DeleteSteveResource delete a k8s resource from steve server.
Expand Down Expand Up @@ -232,39 +234,27 @@ func (b *Bundle) DeleteSteveResource(req *apistructs.SteveRequest) error {
return apierrors.ErrInvoke.InternalError(err)
}

return isSteveError(data)
obj := map[string]interface{}{}
if err = json.Unmarshal(data, &obj); err != nil {
return apierrors.ErrInvoke.InternalError(err)
}
return isSteveError(obj)
}

func isObjInvalid(obj interface{}) bool {
v := reflect.ValueOf(obj)
return v.Kind() == reflect.Ptr && !v.IsNil()
}

func isSteveError(data []byte) error {
if len(data) == 0 {
return nil
}
var obj map[string]interface{}
err := json.Unmarshal(data, &obj)
if err != nil {
return apierrors.ErrInvoke.InternalError(err)
}

typ, ok := obj["type"].(string)
if !ok {
return apierrors.ErrInvoke.InternalError(errors.New("type field is null"))
}

if typ != apistructs.SteveErrorType {
func isSteveError(obj data.Object) error {
if obj.String("type") != "error" {
return nil
}

var steveErr apistructs.SteveError
if err = json.Unmarshal(data, &steveErr); err != nil {
return apierrors.ErrInvoke.InternalError(err)
}
return toAPIError(steveErr.Status, apistructs.ErrorResponse{
Code: steveErr.Code,
Msg: steveErr.Message,
status, _ := strconv.ParseInt(obj.String("status"), 10, 64)
code := obj.String("code")
message := obj.String("message")
return toAPIError(int(status), apistructs.ErrorResponse{
Code: code,
Msg: message,
})
}
8 changes: 7 additions & 1 deletion bundle/steve_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package bundle

import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
Expand Down Expand Up @@ -57,7 +58,12 @@ func (b *Bundle) PatchNode(req *apistructs.SteveRequest) error {
if err != nil {
return apierrors.ErrInvoke.InternalError(err)
}
return isSteveError(data)

obj := map[string]interface{}{}
if err = json.Unmarshal(data, &obj); err != nil {
return apierrors.ErrInvoke.InternalError(err)
}
return isSteveError(obj)
}

// LabelNode labels a node.
Expand Down
5 changes: 0 additions & 5 deletions modules/cmp/steve/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,6 @@ func (a *Aggregator) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
a.Add(cluster)
if s, ok = a.servers.Load(cluster.Name); !ok {
rw.WriteHeader(http.StatusInternalServerError)
rw.Write(apistructs.SteveError{
SteveErrorCode: apistructs.ServerError,
Type: "error",
Message: "Internal server error",
}.JSON())
rw.Write(apistructs.NewSteveError(apistructs.ServerError, "Internal server error").JSON())
}
}
Expand Down
6 changes: 3 additions & 3 deletions modules/openapi/api/apis/cmp/cmp_k8s_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
package cmp

import (
"github.com/erda-project/erda/apistructs"
"github.com/rancher/apiserver/pkg/types"

"github.com/erda-project/erda/modules/openapi/api/apis"
)

Expand All @@ -29,7 +30,6 @@ var CMP_STEVE_CREATE = apis.ApiSpec{
Audit: nil,
CheckLogin: true,
Doc: "create a k8s resource",
RequestType: apistructs.K8SResource{},
ResponseType: apistructs.SteveCollection{},
ResponseType: types.RawResource{},
IsOpenAPI: true,
}
5 changes: 3 additions & 2 deletions modules/openapi/api/apis/cmp/cmp_k8s_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
package cmp

import (
"github.com/erda-project/erda/apistructs"
"github.com/rancher/apiserver/pkg/types"

"github.com/erda-project/erda/modules/openapi/api/apis"
)

Expand All @@ -29,6 +30,6 @@ var CMP_STEVE_GET = apis.ApiSpec{
Audit: nil,
CheckLogin: true,
Doc: "get a k8s resource",
ResponseType: apistructs.SteveResource{},
ResponseType: types.RawResource{},
IsOpenAPI: true,
}
5 changes: 3 additions & 2 deletions modules/openapi/api/apis/cmp/cmp_k8s_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
package cmp

import (
"github.com/erda-project/erda/apistructs"
"github.com/rancher/apiserver/pkg/types"

"github.com/erda-project/erda/modules/openapi/api/apis"
)

Expand All @@ -29,6 +30,6 @@ var CMP_STEVE_LIST = apis.ApiSpec{
Audit: nil,
CheckLogin: true,
Doc: "list a type of k8s resource",
ResponseType: apistructs.SteveCollection{},
ResponseType: types.GenericCollection{},
IsOpenAPI: true,
}
6 changes: 3 additions & 3 deletions modules/openapi/api/apis/cmp/cmp_k8s_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
package cmp

import (
"github.com/erda-project/erda/apistructs"
"github.com/rancher/apiserver/pkg/types"

"github.com/erda-project/erda/modules/openapi/api/apis"
)

Expand All @@ -29,7 +30,6 @@ var CMP_STEVE_UPDATE = apis.ApiSpec{
Audit: nil,
CheckLogin: true,
Doc: "update a k8s resource",
RequestType: apistructs.K8SResource{},
ResponseType: apistructs.SteveResource{},
ResponseType: types.RawResource{},
IsOpenAPI: true,
}
Loading