mirror of
https://github.com/niusmallnan/steve.git
synced 2025-06-27 06:56:55 +00:00
Remove userpreferences from steve
This commit is contained in:
parent
d496dd31d1
commit
e106b9e16b
@ -3,8 +3,6 @@ package resources
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"github.com/rancher/steve/pkg/summarycache"
|
|
||||||
|
|
||||||
"github.com/rancher/apiserver/pkg/store/apiroot"
|
"github.com/rancher/apiserver/pkg/store/apiroot"
|
||||||
"github.com/rancher/apiserver/pkg/subscribe"
|
"github.com/rancher/apiserver/pkg/subscribe"
|
||||||
"github.com/rancher/apiserver/pkg/types"
|
"github.com/rancher/apiserver/pkg/types"
|
||||||
@ -15,9 +13,9 @@ import (
|
|||||||
"github.com/rancher/steve/pkg/resources/common"
|
"github.com/rancher/steve/pkg/resources/common"
|
||||||
"github.com/rancher/steve/pkg/resources/counts"
|
"github.com/rancher/steve/pkg/resources/counts"
|
||||||
"github.com/rancher/steve/pkg/resources/formatters"
|
"github.com/rancher/steve/pkg/resources/formatters"
|
||||||
"github.com/rancher/steve/pkg/resources/userpreferences"
|
|
||||||
"github.com/rancher/steve/pkg/schema"
|
"github.com/rancher/steve/pkg/schema"
|
||||||
"github.com/rancher/steve/pkg/stores/proxy"
|
"github.com/rancher/steve/pkg/stores/proxy"
|
||||||
|
"github.com/rancher/steve/pkg/summarycache"
|
||||||
"k8s.io/client-go/discovery"
|
"k8s.io/client-go/discovery"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -25,7 +23,6 @@ func DefaultSchemas(ctx context.Context, baseSchema *types.APISchemas, ccache cl
|
|||||||
counts.Register(baseSchema, ccache)
|
counts.Register(baseSchema, ccache)
|
||||||
subscribe.Register(baseSchema)
|
subscribe.Register(baseSchema)
|
||||||
apiroot.Register(baseSchema, []string{"v1"}, "proxy:/apis")
|
apiroot.Register(baseSchema, []string{"v1"}, "proxy:/apis")
|
||||||
userpreferences.Register(baseSchema, cg)
|
|
||||||
return baseSchema, nil
|
return baseSchema, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,111 +0,0 @@
|
|||||||
package userpreferences
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/rancher/apiserver/pkg/store/empty"
|
|
||||||
"github.com/rancher/apiserver/pkg/types"
|
|
||||||
"github.com/rancher/norman/types/convert"
|
|
||||||
"github.com/rancher/steve/pkg/stores/proxy"
|
|
||||||
"github.com/rancher/wrangler/pkg/schemas/validation"
|
|
||||||
corev1 "k8s.io/api/core/v1"
|
|
||||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
||||||
"k8s.io/apiserver/pkg/authentication/user"
|
|
||||||
v1 "k8s.io/client-go/kubernetes/typed/core/v1"
|
|
||||||
)
|
|
||||||
|
|
||||||
type configMapStore struct {
|
|
||||||
empty.Store
|
|
||||||
cg proxy.ClientGetter
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *configMapStore) getClient(apiOp *types.APIRequest) (v1.ConfigMapInterface, error) {
|
|
||||||
c, err := e.cg.AdminK8sInterface()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return c.CoreV1().ConfigMaps("kube-system"), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func newPref(u user.Info) (types.APIObject, *UserPreference) {
|
|
||||||
pref := &UserPreference{
|
|
||||||
Data: map[string]string{},
|
|
||||||
}
|
|
||||||
return types.APIObject{
|
|
||||||
Type: "userpreference",
|
|
||||||
ID: u.GetName(),
|
|
||||||
Object: pref,
|
|
||||||
}, pref
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *configMapStore) ByID(apiOp *types.APIRequest, schema *types.APISchema, id string) (types.APIObject, error) {
|
|
||||||
u := getUser(apiOp)
|
|
||||||
result, pref := newPref(u)
|
|
||||||
|
|
||||||
client, err := e.getClient(apiOp)
|
|
||||||
if err == validation.NotFound {
|
|
||||||
return result, nil
|
|
||||||
} else if err != nil {
|
|
||||||
return types.APIObject{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
obj, err := client.Get(apiOp.Context(), prefName(u), metav1.GetOptions{})
|
|
||||||
if apierrors.IsNotFound(err) {
|
|
||||||
return result, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
pref.Data = obj.Data
|
|
||||||
return result, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *configMapStore) List(apiOp *types.APIRequest, schema *types.APISchema) (types.APIObjectList, error) {
|
|
||||||
obj, err := e.ByID(apiOp, schema, "")
|
|
||||||
if err != nil {
|
|
||||||
return types.APIObjectList{}, err
|
|
||||||
}
|
|
||||||
return types.APIObjectList{
|
|
||||||
Objects: []types.APIObject{
|
|
||||||
obj,
|
|
||||||
},
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *configMapStore) Update(apiOp *types.APIRequest, schema *types.APISchema, data types.APIObject, id string) (types.APIObject, error) {
|
|
||||||
u := getUser(apiOp)
|
|
||||||
client, err := e.getClient(apiOp)
|
|
||||||
if err != nil {
|
|
||||||
return types.APIObject{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
values := map[string]string{}
|
|
||||||
for k, v := range data.Data().Map("data") {
|
|
||||||
values[k] = convert.ToString(v)
|
|
||||||
}
|
|
||||||
|
|
||||||
obj, err := client.Get(apiOp.Context(), prefName(u), metav1.GetOptions{})
|
|
||||||
if apierrors.IsNotFound(err) {
|
|
||||||
_, err = client.Create(apiOp.Context(), &corev1.ConfigMap{
|
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
|
||||||
Name: prefName(u),
|
|
||||||
},
|
|
||||||
Data: values,
|
|
||||||
}, metav1.CreateOptions{})
|
|
||||||
} else if err == nil {
|
|
||||||
obj.Data = values
|
|
||||||
_, err = client.Update(apiOp.Context(), obj, metav1.UpdateOptions{})
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
return types.APIObject{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return e.ByID(apiOp, schema, "")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *configMapStore) Delete(apiOp *types.APIRequest, schema *types.APISchema, id string) (types.APIObject, error) {
|
|
||||||
u := getUser(apiOp)
|
|
||||||
client, err := e.getClient(apiOp)
|
|
||||||
if err != nil {
|
|
||||||
return types.APIObject{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return types.APIObject{}, client.Delete(apiOp.Context(), prefName(u), metav1.DeleteOptions{})
|
|
||||||
}
|
|
@ -1,164 +0,0 @@
|
|||||||
package userpreferences
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/rancher/apiserver/pkg/store/empty"
|
|
||||||
"github.com/rancher/apiserver/pkg/types"
|
|
||||||
"github.com/rancher/steve/pkg/attributes"
|
|
||||||
"github.com/rancher/steve/pkg/stores/proxy"
|
|
||||||
"github.com/rancher/wrangler/pkg/data/convert"
|
|
||||||
"github.com/rancher/wrangler/pkg/schemas/validation"
|
|
||||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
||||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
||||||
"k8s.io/client-go/dynamic"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
rancherSchema = "management.cattle.io.preference"
|
|
||||||
)
|
|
||||||
|
|
||||||
type rancherPrefStore struct {
|
|
||||||
empty.Store
|
|
||||||
cg proxy.ClientGetter
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *rancherPrefStore) getClient(apiOp *types.APIRequest) (dynamic.ResourceInterface, error) {
|
|
||||||
u := getUser(apiOp).GetName()
|
|
||||||
cmSchema := apiOp.Schemas.LookupSchema(rancherSchema)
|
|
||||||
if cmSchema == nil {
|
|
||||||
return nil, validation.NotFound
|
|
||||||
}
|
|
||||||
|
|
||||||
return e.cg.AdminClient(apiOp, cmSchema, u)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *rancherPrefStore) ByID(apiOp *types.APIRequest, schema *types.APISchema, id string) (types.APIObject, error) {
|
|
||||||
u := getUser(apiOp)
|
|
||||||
client, err := e.getClient(apiOp)
|
|
||||||
if err != nil {
|
|
||||||
return types.APIObject{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
pref := &UserPreference{
|
|
||||||
Data: map[string]string{},
|
|
||||||
}
|
|
||||||
result := types.APIObject{
|
|
||||||
Type: "userpreference",
|
|
||||||
ID: u.GetName(),
|
|
||||||
Object: pref,
|
|
||||||
}
|
|
||||||
|
|
||||||
objs, err := client.List(apiOp.Context(), metav1.ListOptions{})
|
|
||||||
if err != nil {
|
|
||||||
return result, err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, obj := range objs.Items {
|
|
||||||
pref.Data[obj.GetName()] = convert.ToString(obj.Object["value"])
|
|
||||||
}
|
|
||||||
|
|
||||||
return result, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *rancherPrefStore) List(apiOp *types.APIRequest, schema *types.APISchema) (types.APIObjectList, error) {
|
|
||||||
obj, err := e.ByID(apiOp, schema, "")
|
|
||||||
if err != nil {
|
|
||||||
return types.APIObjectList{}, err
|
|
||||||
}
|
|
||||||
return types.APIObjectList{
|
|
||||||
Objects: []types.APIObject{
|
|
||||||
obj,
|
|
||||||
},
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *rancherPrefStore) createNamespace(apiOp *types.APIRequest, ns string) error {
|
|
||||||
client, err := e.cg.AdminClient(apiOp, apiOp.Schemas.LookupSchema("namespace"), "")
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
_, err = client.Get(apiOp.Context(), ns, metav1.GetOptions{})
|
|
||||||
if !apierrors.IsNotFound(err) {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
_, err = client.Create(apiOp.Context(), &unstructured.Unstructured{
|
|
||||||
Object: map[string]interface{}{
|
|
||||||
"metadata": map[string]interface{}{
|
|
||||||
"name": ns,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}, metav1.CreateOptions{})
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *rancherPrefStore) Update(apiOp *types.APIRequest, schema *types.APISchema, data types.APIObject, id string) (types.APIObject, error) {
|
|
||||||
client, err := e.getClient(apiOp)
|
|
||||||
if err != nil {
|
|
||||||
return types.APIObject{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
gvk := attributes.GVK(apiOp.Schemas.LookupSchema(rancherSchema))
|
|
||||||
|
|
||||||
newValues := map[string]string{}
|
|
||||||
for k, v := range data.Data().Map("data") {
|
|
||||||
newValues[k] = convert.ToString(v)
|
|
||||||
}
|
|
||||||
|
|
||||||
prefs, err := client.List(apiOp.Context(), metav1.ListOptions{})
|
|
||||||
if err != nil {
|
|
||||||
return types.APIObject{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, pref := range prefs.Items {
|
|
||||||
key := pref.GetName()
|
|
||||||
newValue, ok := newValues[key]
|
|
||||||
delete(newValues, key)
|
|
||||||
if ok && newValue != pref.Object["value"] {
|
|
||||||
pref.Object["value"] = newValue
|
|
||||||
_, err := client.Update(apiOp.Context(), &pref, metav1.UpdateOptions{})
|
|
||||||
if err != nil {
|
|
||||||
return types.APIObject{}, err
|
|
||||||
}
|
|
||||||
} else if !ok {
|
|
||||||
err := client.Delete(apiOp.Context(), key, metav1.DeleteOptions{})
|
|
||||||
if err != nil {
|
|
||||||
return types.APIObject{}, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
nsExists := false
|
|
||||||
for k, v := range newValues {
|
|
||||||
if !nsExists {
|
|
||||||
if err := e.createNamespace(apiOp, getUser(apiOp).GetName()); err != nil {
|
|
||||||
return types.APIObject{}, err
|
|
||||||
}
|
|
||||||
nsExists = true
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = client.Create(apiOp.Context(), &unstructured.Unstructured{
|
|
||||||
Object: map[string]interface{}{
|
|
||||||
"apiVersion": gvk.GroupVersion().String(),
|
|
||||||
"kind": gvk.Kind,
|
|
||||||
"metadata": map[string]interface{}{
|
|
||||||
"name": k,
|
|
||||||
},
|
|
||||||
"value": v,
|
|
||||||
},
|
|
||||||
}, metav1.CreateOptions{})
|
|
||||||
if err != nil {
|
|
||||||
return types.APIObject{}, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return e.ByID(apiOp, schema, "")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *rancherPrefStore) Delete(apiOp *types.APIRequest, schema *types.APISchema, id string) (types.APIObject, error) {
|
|
||||||
client, err := e.getClient(apiOp)
|
|
||||||
if err != nil {
|
|
||||||
return types.APIObject{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return types.APIObject{}, client.DeleteCollection(apiOp.Context(), metav1.DeleteOptions{}, metav1.ListOptions{})
|
|
||||||
}
|
|
@ -1,88 +0,0 @@
|
|||||||
package userpreferences
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"github.com/rancher/apiserver/pkg/store/empty"
|
|
||||||
"github.com/rancher/apiserver/pkg/types"
|
|
||||||
"github.com/rancher/steve/pkg/stores/proxy"
|
|
||||||
"github.com/rancher/wrangler/pkg/name"
|
|
||||||
"k8s.io/apiserver/pkg/authentication/user"
|
|
||||||
"k8s.io/apiserver/pkg/endpoints/request"
|
|
||||||
)
|
|
||||||
|
|
||||||
type UserPreference struct {
|
|
||||||
Data map[string]string `json:"data"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func Register(schemas *types.APISchemas, cg proxy.ClientGetter) {
|
|
||||||
schemas.InternalSchemas.TypeName("userpreference", UserPreference{})
|
|
||||||
schemas.MustImportAndCustomize(UserPreference{}, func(schema *types.APISchema) {
|
|
||||||
schema.CollectionMethods = []string{http.MethodGet}
|
|
||||||
schema.ResourceMethods = []string{http.MethodGet, http.MethodPut, http.MethodDelete}
|
|
||||||
schema.Store = New(cg)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func New(cg proxy.ClientGetter) types.Store {
|
|
||||||
return &Store{
|
|
||||||
rancher: &rancherPrefStore{
|
|
||||||
cg: cg,
|
|
||||||
},
|
|
||||||
configMapStore: &configMapStore{
|
|
||||||
cg: cg,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type Store struct {
|
|
||||||
empty.Store
|
|
||||||
rancher *rancherPrefStore
|
|
||||||
configMapStore *configMapStore
|
|
||||||
}
|
|
||||||
|
|
||||||
func isRancher(apiOp *types.APIRequest) bool {
|
|
||||||
return apiOp.Schemas.LookupSchema(rancherSchema) != nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *Store) ByID(apiOp *types.APIRequest, schema *types.APISchema, id string) (types.APIObject, error) {
|
|
||||||
if isRancher(apiOp) {
|
|
||||||
return e.rancher.ByID(apiOp, schema, id)
|
|
||||||
}
|
|
||||||
return e.configMapStore.ByID(apiOp, schema, id)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *Store) List(apiOp *types.APIRequest, schema *types.APISchema) (types.APIObjectList, error) {
|
|
||||||
if isRancher(apiOp) {
|
|
||||||
return e.rancher.List(apiOp, schema)
|
|
||||||
}
|
|
||||||
return e.configMapStore.List(apiOp, schema)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *Store) Update(apiOp *types.APIRequest, schema *types.APISchema, data types.APIObject, id string) (types.APIObject, error) {
|
|
||||||
if isRancher(apiOp) {
|
|
||||||
return e.rancher.Update(apiOp, schema, data, id)
|
|
||||||
}
|
|
||||||
return e.configMapStore.Update(apiOp, schema, data, id)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *Store) Delete(apiOp *types.APIRequest, schema *types.APISchema, id string) (types.APIObject, error) {
|
|
||||||
if isRancher(apiOp) {
|
|
||||||
return e.rancher.Delete(apiOp, schema, id)
|
|
||||||
}
|
|
||||||
return e.configMapStore.Delete(apiOp, schema, id)
|
|
||||||
}
|
|
||||||
|
|
||||||
func prefName(u user.Info) string {
|
|
||||||
return name.SafeConcatName("pref", u.GetName())
|
|
||||||
}
|
|
||||||
|
|
||||||
func getUser(apiOp *types.APIRequest) user.Info {
|
|
||||||
u, ok := request.UserFrom(apiOp.Context())
|
|
||||||
if !ok {
|
|
||||||
u = &user.DefaultInfo{
|
|
||||||
Name: "dashboard-user",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return u
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user