-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathoptions.go
105 lines (87 loc) · 3.3 KB
/
options.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
/*
Copyright 2023 The KusionStack Authors.
Copyright 2021 The Kruise Authors.
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 common
import (
"fmt"
"net/http"
"strings"
"k8s.io/apimachinery/pkg/util/sets"
utilwaitgroup "k8s.io/apimachinery/pkg/util/waitgroup"
apirequest "k8s.io/apiserver/pkg/endpoints/request"
"k8s.io/apiserver/pkg/server"
genericfilters "k8s.io/apiserver/pkg/server/filters"
"k8s.io/apiserver/pkg/server/options"
"k8s.io/client-go/rest"
protomanager "github.com/KusionStack/controller-mesh/pkg/proxy/proto"
)
const (
// DefaultLegacyAPIPrefix is where the legacy APIs will be located.
DefaultLegacyAPIPrefix = "/api"
// APIGroupPrefix is where non-legacy API group will be located.
APIGroupPrefix = "/apis"
)
// Options contains everything necessary to create and run proxy.
type Options struct {
Config *rest.Config
SecureServingOptions *options.SecureServingOptions
RequestInfoResolver apirequest.RequestInfoResolver
LegacyAPIGroupPrefixes sets.Set[string]
LongRunningFunc apirequest.LongRunningRequestCheck
HandlerChainWaitGroup *utilwaitgroup.SafeWaitGroup
LeaderElectionName string
SpecManager *protomanager.SpecManager
BreakerWrapperFunc func(http.Handler) http.Handler
FaultInjectionWrapperFunc func(http.Handler) http.Handler
}
func NewOptions() *Options {
o := &Options{
Config: new(rest.Config),
SecureServingOptions: options.NewSecureServingOptions(),
LegacyAPIGroupPrefixes: sets.New[string](DefaultLegacyAPIPrefix),
LongRunningFunc: genericfilters.BasicLongRunningRequestCheck(
sets.NewString("watch", "proxy"),
sets.NewString("attach", "exec", "proxy", "log", "portforward"),
), // BasicLongRunningRequestCheck(sets.NewString("watch"), sets.NewString()),
HandlerChainWaitGroup: new(utilwaitgroup.SafeWaitGroup),
}
o.RequestInfoResolver = NewRequestInfoResolver(o)
return o
}
func (o *Options) ApplyTo(apiserver **server.SecureServingInfo) error {
if o == nil {
return fmt.Errorf("SecureServingInfo is empty")
}
err := o.SecureServingOptions.ApplyTo(apiserver)
if err != nil {
return err
}
return err
}
func (o *Options) Validate() []error {
var errors []error
errors = append(errors, o.SecureServingOptions.Validate()...)
return errors
}
func NewRequestInfoResolver(o *Options) *apirequest.RequestInfoFactory {
apiPrefixes := sets.NewString(strings.Trim(APIGroupPrefix, "/")) // all possible API prefixes
legacyAPIPrefixes := sets.String{} // APIPrefixes that won't have groups (legacy)
for legacyAPIPrefix := range o.LegacyAPIGroupPrefixes {
apiPrefixes.Insert(strings.Trim(legacyAPIPrefix, "/"))
legacyAPIPrefixes.Insert(strings.Trim(legacyAPIPrefix, "/"))
}
return &apirequest.RequestInfoFactory{
APIPrefixes: apiPrefixes,
GrouplessAPIPrefixes: legacyAPIPrefixes,
}
}