mirror of
https://github.com/rancher/steve.git
synced 2025-09-19 01:29:48 +00:00
Shuffle around code and use rancher/apiserver
This commit is contained in:
104
pkg/resources/common/dynamiccolumns.go
Normal file
104
pkg/resources/common/dynamiccolumns.go
Normal file
@@ -0,0 +1,104 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/rancher/apiserver/pkg/types"
|
||||
"github.com/rancher/steve/pkg/attributes"
|
||||
"github.com/rancher/wrangler/pkg/ratelimit"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/internalversion"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
metav1beta1 "k8s.io/apimachinery/pkg/apis/meta/v1beta1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/apimachinery/pkg/runtime/serializer"
|
||||
"k8s.io/client-go/rest"
|
||||
)
|
||||
|
||||
type DynamicColumns struct {
|
||||
client *rest.RESTClient
|
||||
}
|
||||
|
||||
type ColumnDefinition struct {
|
||||
metav1.TableColumnDefinition `json:",inline"`
|
||||
Field string `json:"field,omitempty"`
|
||||
}
|
||||
|
||||
func NewDynamicColumns(config *rest.Config) (*DynamicColumns, error) {
|
||||
c, err := newClient(config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &DynamicColumns{
|
||||
client: c,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (d *DynamicColumns) SetColumns(ctx context.Context, schema *types.APISchema) error {
|
||||
if attributes.Columns(schema) != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
gvr := attributes.GVR(schema)
|
||||
if gvr.Resource == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
r := d.client.Get()
|
||||
if gvr.Group == "" {
|
||||
r.Prefix("api")
|
||||
} else {
|
||||
r.Prefix("apis", gvr.Group)
|
||||
}
|
||||
r.Prefix(gvr.Version)
|
||||
r.Prefix(gvr.Resource)
|
||||
r.VersionedParams(&metav1.ListOptions{
|
||||
Limit: 1,
|
||||
}, metav1.ParameterCodec)
|
||||
|
||||
obj, err := r.Do(ctx).Get()
|
||||
if err != nil {
|
||||
attributes.SetTable(schema, false)
|
||||
return nil
|
||||
}
|
||||
t, ok := obj.(*metav1.Table)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
if len(t.ColumnDefinitions) > 0 {
|
||||
var cols []ColumnDefinition
|
||||
for i, colDef := range t.ColumnDefinitions {
|
||||
cols = append(cols, ColumnDefinition{
|
||||
TableColumnDefinition: colDef,
|
||||
Field: fmt.Sprintf("$.metadata.fields[%d]", i),
|
||||
})
|
||||
}
|
||||
attributes.SetColumns(schema, cols)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func newClient(config *rest.Config) (*rest.RESTClient, error) {
|
||||
scheme := runtime.NewScheme()
|
||||
if err := internalversion.AddToScheme(scheme); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := metav1.AddMetaToScheme(scheme); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := metav1beta1.AddMetaToScheme(scheme); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
config = rest.CopyConfig(config)
|
||||
config.RateLimiter = ratelimit.None
|
||||
config.UserAgent = rest.DefaultKubernetesUserAgent()
|
||||
config.AcceptContentTypes = "application/json;as=Table;v=v1;g=meta.k8s.io"
|
||||
config.GroupVersion = &schema.GroupVersion{}
|
||||
config.NegotiatedSerializer = serializer.NewCodecFactory(scheme)
|
||||
config.APIPath = "/"
|
||||
return rest.RESTClientFor(config)
|
||||
}
|
54
pkg/resources/common/formatter.go
Normal file
54
pkg/resources/common/formatter.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/rancher/apiserver/pkg/types"
|
||||
"github.com/rancher/steve/pkg/accesscontrol"
|
||||
"github.com/rancher/steve/pkg/schema"
|
||||
"github.com/rancher/steve/pkg/stores/proxy"
|
||||
"github.com/rancher/wrangler/pkg/data"
|
||||
"github.com/rancher/wrangler/pkg/summary"
|
||||
"k8s.io/apimachinery/pkg/api/meta"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
)
|
||||
|
||||
func DefaultTemplate(clientGetter proxy.ClientGetter, asl accesscontrol.AccessSetLookup) schema.Template {
|
||||
return schema.Template{
|
||||
Store: proxy.NewProxyStore(clientGetter, asl),
|
||||
Formatter: Formatter,
|
||||
}
|
||||
}
|
||||
|
||||
func DefaultFormatter(next types.Formatter) types.Formatter {
|
||||
return types.FormatterChain(Formatter, next)
|
||||
}
|
||||
|
||||
func Formatter(request *types.APIRequest, resource *types.RawResource) {
|
||||
meta, err := meta.Accessor(resource.APIObject.Object)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
selfLink := meta.GetSelfLink()
|
||||
if selfLink == "" {
|
||||
return
|
||||
}
|
||||
|
||||
u := request.URLBuilder.RelativeToRoot(selfLink)
|
||||
resource.Links["view"] = u
|
||||
|
||||
if _, ok := resource.Links["update"]; !ok {
|
||||
resource.Links["update"] = u
|
||||
}
|
||||
|
||||
if unstr, ok := resource.APIObject.Object.(*unstructured.Unstructured); ok {
|
||||
summary := summary.Summarize(unstr)
|
||||
data.PutValue(unstr.Object, map[string]interface{}{
|
||||
"name": summary.State,
|
||||
"error": summary.Error,
|
||||
"transitioning": summary.Transitioning,
|
||||
"message": strings.Join(summary.Message, ":"),
|
||||
}, "metadata", "state")
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user