/
storage.go
94 lines (76 loc) · 2.51 KB
/
storage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
Copyright AppsCode Inc. and Contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package rendermenu
import (
"context"
"strings"
"kubeops.dev/ui-server/pkg/menu"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
apirequest "k8s.io/apiserver/pkg/endpoints/request"
"k8s.io/apiserver/pkg/registry/rest"
"k8s.io/client-go/discovery"
rsapi "kmodules.xyz/resource-metadata/apis/meta/v1alpha1"
"sigs.k8s.io/controller-runtime/pkg/client"
)
type Storage struct {
kc client.Client
disco discovery.ServerResourcesInterface
ns string
}
var (
_ rest.GroupVersionKindProvider = &Storage{}
_ rest.Scoper = &Storage{}
_ rest.Storage = &Storage{}
_ rest.Creater = &Storage{}
_ rest.SingularNameProvider = &Storage{}
)
func NewStorage(kc client.Client, disco discovery.ServerResourcesInterface, ns string) *Storage {
return &Storage{
kc: kc,
disco: disco,
ns: ns,
}
}
func (r *Storage) GroupVersionKind(_ schema.GroupVersion) schema.GroupVersionKind {
return rsapi.SchemeGroupVersion.WithKind(rsapi.ResourceKindRenderMenu)
}
func (r *Storage) NamespaceScoped() bool {
return false
}
func (r *Storage) GetSingularName() string {
return strings.ToLower(rsapi.ResourceKindRenderMenu)
}
func (r *Storage) New() runtime.Object {
return &rsapi.RenderMenu{}
}
func (r *Storage) Destroy() {}
func (r *Storage) Create(ctx context.Context, obj runtime.Object, _ rest.ValidateObjectFunc, _ *metav1.CreateOptions) (runtime.Object, error) {
user, ok := apirequest.UserFrom(ctx)
if !ok {
return nil, apierrors.NewBadRequest("missing user info")
}
in := obj.(*rsapi.RenderMenu)
if in.Request == nil {
return nil, apierrors.NewBadRequest("missing apirequest")
}
driver := menu.NewUserMenuDriver(r.kc, r.disco, r.ns, user.GetName())
if resp, err := menu.RenderMenu(driver, in.Request); err != nil {
return nil, err
} else {
in.Response = resp
}
return in, nil
}