-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.go
140 lines (113 loc) · 4.29 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// SPDX-FileCopyrightText: 2024 Axel Christ and Spheric contributors
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and IronCore contributors
// SPDX-License-Identifier: Apache-2.0
package storage
import (
"context"
"fmt"
"net/http"
"net/url"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/proxy"
"k8s.io/apiserver/pkg/registry/generic"
genericregistry "k8s.io/apiserver/pkg/registry/generic/registry"
"k8s.io/apiserver/pkg/registry/rest"
"k8s.io/apiserver/pkg/storage"
"sigs.k8s.io/structured-merge-diff/v4/fieldpath"
"spheric.cloud/spheric/internal/apis/compute"
"spheric.cloud/spheric/internal/machinepoollet/client"
"spheric.cloud/spheric/internal/registry/compute/machine"
)
type MachineStorage struct {
Machine *REST
Status *StatusREST
Exec *ExecREST
}
type REST struct {
*genericregistry.Store
}
func NewStorage(optsGetter generic.RESTOptionsGetter, k client.ConnectionInfoGetter) (MachineStorage, error) {
store := &genericregistry.Store{
NewFunc: func() runtime.Object {
return &compute.Machine{}
},
NewListFunc: func() runtime.Object {
return &compute.MachineList{}
},
PredicateFunc: machine.MatchMachine,
DefaultQualifiedResource: compute.Resource("machines"),
SingularQualifiedResource: compute.Resource("machine"),
CreateStrategy: machine.Strategy,
UpdateStrategy: machine.Strategy,
DeleteStrategy: machine.Strategy,
TableConvertor: newTableConvertor(),
}
options := &generic.StoreOptions{
RESTOptions: optsGetter,
AttrFunc: machine.GetAttrs,
TriggerFunc: map[string]storage.IndexerFunc{
compute.MachineMachinePoolRefNameField: machine.MachinePoolRefNameTriggerFunc,
},
Indexers: machine.Indexers(),
}
if err := store.CompleteWithOptions(options); err != nil {
return MachineStorage{}, err
}
statusStore := *store
statusStore.UpdateStrategy = machine.StatusStrategy
statusStore.ResetFieldsStrategy = machine.StatusStrategy
return MachineStorage{
Machine: &REST{store},
Status: &StatusREST{&statusStore},
Exec: &ExecREST{store, k},
}, nil
}
type StatusREST struct {
store *genericregistry.Store
}
func (r *StatusREST) New() runtime.Object {
return &compute.Machine{}
}
func (r *StatusREST) Get(ctx context.Context, name string, options *metav1.GetOptions) (runtime.Object, error) {
return r.store.Get(ctx, name, options)
}
func (r *StatusREST) Update(ctx context.Context, name string, objInfo rest.UpdatedObjectInfo, createValidation rest.ValidateObjectFunc, updateValidation rest.ValidateObjectUpdateFunc, forceAllowCreate bool, options *metav1.UpdateOptions) (runtime.Object, bool, error) {
return r.store.Update(ctx, name, objInfo, createValidation, updateValidation, false, options)
}
func (r *StatusREST) GetResetFields() map[fieldpath.APIVersion]*fieldpath.Set {
return r.store.GetResetFields()
}
func (r *StatusREST) Destroy() {}
// Support both GET and POST methods. We must support GET for browsers that want to use WebSockets.
var upgradeableMethods = []string{"GET", "POST"}
type ExecREST struct {
Store *genericregistry.Store
MachineConn client.ConnectionInfoGetter
}
func (r *ExecREST) New() runtime.Object {
return &compute.MachineExecOptions{}
}
func (r *ExecREST) Connect(ctx context.Context, name string, opts runtime.Object, responder rest.Responder) (http.Handler, error) {
execOpts, ok := opts.(*compute.MachineExecOptions)
if !ok {
return nil, fmt.Errorf("invalid options objects: %#v", opts)
}
location, transport, err := machine.ExecLocation(ctx, r.Store, r.MachineConn, name, execOpts)
if err != nil {
return nil, err
}
return newThrottledUpgradeAwareProxyHandler(location, transport, false, true, responder), nil
}
func newThrottledUpgradeAwareProxyHandler(location *url.URL, transport http.RoundTripper, wrapTransport, upgradeRequired bool, responder rest.Responder) *proxy.UpgradeAwareHandler {
handler := proxy.NewUpgradeAwareHandler(location, transport, wrapTransport, upgradeRequired, proxy.NewErrorResponder(responder))
return handler
}
func (r *ExecREST) NewConnectOptions() (runtime.Object, bool, string) {
return &compute.MachineExecOptions{}, false, ""
}
func (r *ExecREST) ConnectMethods() []string {
return upgradeableMethods
}
func (r *ExecREST) Destroy() {}