@@ -2,118 +2,81 @@ package options
2
2
3
3
import (
4
4
"fmt"
5
+ "log"
5
6
"net"
6
7
"net/http"
7
8
"strings"
8
9
10
+ "github.com/spf13/pflag"
9
11
utilerrors "k8s.io/apimachinery/pkg/util/errors"
10
- "k8s.io/apiserver/pkg/admission/plugin/namespace/lifecycle "
12
+ "k8s.io/apiserver/pkg/endpoints/openapi "
11
13
genericrequest "k8s.io/apiserver/pkg/endpoints/request"
12
- genericapiserver "k8s.io/apiserver/pkg/server"
14
+ genericserver "k8s.io/apiserver/pkg/server"
13
15
genericoptions "k8s.io/apiserver/pkg/server/options"
14
- "k8s.io/apiserver/pkg/util/feature"
15
- cliflag "k8s.io/component-base/cli/flag"
16
- "k8s.io/component-base/featuregate"
17
- "k8s.io/component-base/logs"
18
- logsapi "k8s.io/component-base/logs/api/v1"
16
+ "k8s.io/client-go/dynamic"
17
+ "k8s.io/klog/v2"
18
+ "sigs.k8s.io/controller-runtime/pkg/client/apiutil"
19
19
20
+ "github.com/kosmos.io/kosmos/pkg/apis/proxy/scheme"
21
+ proxyScheme "github.com/kosmos.io/kosmos/pkg/apis/proxy/scheme"
20
22
"github.com/kosmos.io/kosmos/pkg/clusterlink/proxy"
23
+ proxyctl "github.com/kosmos.io/kosmos/pkg/clusterlink/proxy/controller"
24
+ kosmosclientset "github.com/kosmos.io/kosmos/pkg/generated/clientset/versioned"
25
+ informerfactory "github.com/kosmos.io/kosmos/pkg/generated/informers/externalversions"
26
+ generatedopenapi "github.com/kosmos.io/kosmos/pkg/generated/openapi"
27
+ profileflag "github.com/kosmos.io/kosmos/pkg/sharedcli/profileflag"
21
28
"github.com/kosmos.io/kosmos/pkg/utils"
22
29
)
23
30
24
- // Options contains command line parameters for clusterlink-proxy
25
31
type Options struct {
26
- MaxRequestsInFlight int
27
- MaxMutatingRequestsInFlight int
28
- utils.KubernetesOptions
29
-
30
- Logs * logs.Options
31
- SecureServing * genericoptions.SecureServingOptionsWithLoopback
32
- Authentication * genericoptions.DelegatingAuthenticationOptions
33
- Authorization * genericoptions.DelegatingAuthorizationOptions
34
- Audit * genericoptions.AuditOptions
35
- Features * genericoptions.FeatureOptions
36
- CoreAPI * genericoptions.CoreAPIOptions
37
- FeatureGate featuregate.FeatureGate
38
- Admission * genericoptions.AdmissionOptions
32
+ // RecommendedOptions *genericoptions.RecommendedOptions
33
+ GenericServerRunOptions * genericoptions.ServerRunOptions
34
+ SecureServing * genericoptions.SecureServingOptionsWithLoopback
35
+ Authentication * genericoptions.DelegatingAuthenticationOptions
36
+ Authorization * genericoptions.DelegatingAuthorizationOptions
37
+ Audit * genericoptions.AuditOptions
38
+ Features * genericoptions.FeatureOptions
39
+ CoreAPI * genericoptions.CoreAPIOptions
40
+ ServerRunOptions * genericoptions.ServerRunOptions
41
+
42
+ ProfileOpts profileflag.Options
39
43
}
40
44
41
- // nolint
42
- func NewOptions () * Options {
43
- sso := genericoptions .NewSecureServingOptions ()
44
-
45
- // We are composing recommended options for an aggregated api-server,
46
- // whose client is typically a proxy multiplexing many operations ---
47
- // notably including long-running ones --- into one HTTP/2 connection
48
- // into this server. So allow many concurrent operations.
49
- sso .HTTP2MaxStreamsPerConnection = 1000
50
-
51
- return & Options {
52
- MaxRequestsInFlight : 0 ,
53
- MaxMutatingRequestsInFlight : 0 ,
54
-
55
- Logs : logs .NewOptions (),
56
- SecureServing : sso .WithLoopback (),
57
- Authentication : genericoptions .NewDelegatingAuthenticationOptions (),
58
- Authorization : genericoptions .NewDelegatingAuthorizationOptions (),
59
- Audit : genericoptions .NewAuditOptions (),
60
- Features : genericoptions .NewFeatureOptions (),
61
- CoreAPI : genericoptions .NewCoreAPIOptions (),
62
- FeatureGate : feature .DefaultFeatureGate ,
63
- Admission : genericoptions .NewAdmissionOptions (),
64
- }
45
+ func (o * Options ) AddFlags (flags * pflag.FlagSet ) {
46
+ o .SecureServing .AddFlags (flags )
47
+ o .Authentication .AddFlags (flags )
48
+ o .Authorization .AddFlags (flags )
49
+ o .Audit .AddFlags (flags )
50
+ o .Features .AddFlags (flags )
51
+ o .CoreAPI .AddFlags (flags )
52
+ o .ServerRunOptions .AddUniversalFlags (flags )
53
+ o .ProfileOpts .AddFlags (flags )
65
54
}
66
55
67
56
// nolint
68
- func (o * Options ) Validate () error {
69
- errors := []error {}
70
- errors = append (errors , o .validateGenericOptions ()... )
71
- return utilerrors .NewAggregate (errors )
72
- }
73
-
74
- func (o * Options ) validateGenericOptions () []error {
75
- errors := []error {}
76
- if o .MaxRequestsInFlight < 0 {
77
- errors = append (errors , fmt .Errorf ("--max-requests-inflight can not be negative value" ))
78
- }
79
- if o .MaxMutatingRequestsInFlight < 0 {
80
- errors = append (errors , fmt .Errorf ("--max-mutating-requests-inflight can not be negative value" ))
57
+ func NewOptions () * Options {
58
+ o := & Options {
59
+ GenericServerRunOptions : genericoptions .NewServerRunOptions (),
60
+ SecureServing : genericoptions .NewSecureServingOptions ().WithLoopback (),
61
+ Authentication : genericoptions .NewDelegatingAuthenticationOptions (),
62
+ Authorization : genericoptions .NewDelegatingAuthorizationOptions (),
63
+ Audit : genericoptions .NewAuditOptions (),
64
+ Features : genericoptions .NewFeatureOptions (),
65
+ CoreAPI : genericoptions .NewCoreAPIOptions (),
66
+ ServerRunOptions : genericoptions .NewServerRunOptions (),
81
67
}
82
-
83
- errors = append (errors , o .CoreAPI .Validate ()... )
84
- errors = append (errors , o .SecureServing .Validate ()... )
85
- errors = append (errors , o .Authentication .Validate ()... )
86
- errors = append (errors , o .Authorization .Validate ()... )
87
- errors = append (errors , o .Audit .Validate ()... )
88
- errors = append (errors , o .Features .Validate ()... )
89
- return errors
68
+ return o
90
69
}
91
70
92
71
// nolint
93
- func (o * Options ) Flags () cliflag.NamedFlagSets {
94
- var fss cliflag.NamedFlagSets
95
-
96
- genericfs := fss .FlagSet ("generic" )
97
- genericfs .IntVar (& o .MaxRequestsInFlight , "max-requests-inflight" , o .MaxRequestsInFlight , "" +
98
- "Otherwise, this flag limits the maximum number of non-mutating requests in flight, or a zero value disables the limit completely." )
99
- genericfs .IntVar (& o .MaxMutatingRequestsInFlight , "max-mutating-requests-inflight" , o .MaxMutatingRequestsInFlight , "" +
100
- "this flag limits the maximum number of mutating requests in flight, or a zero value disables the limit completely." )
101
-
102
- globalcfs := fss .FlagSet ("global" )
103
- globalcfs .Float32Var (& o .KubernetesOptions .QPS , "kube-qps" , utils .DefaultKubeQPS , "QPS to use while talking with kube-apiserver." )
104
- globalcfs .IntVar (& o .KubernetesOptions .Burst , "kube-burst" , utils .DefaultKubeBurst , "Burst to use while talking with kube-apiserver." )
105
- o .CoreAPI .AddFlags (globalcfs )
106
- o .SecureServing .AddFlags (fss .FlagSet ("secure serving" ))
107
- o .Authentication .AddFlags (fss .FlagSet ("authentication" ))
108
- o .Authorization .AddFlags (fss .FlagSet ("authorization" ))
109
- o .Audit .AddFlags (fss .FlagSet ("auditing" ))
110
- o .Features .AddFlags (fss .FlagSet ("features" ))
111
- logsapi .AddFlags (o .Logs , fss .FlagSet ("logs" ))
112
-
113
- // o.Admission.AddFlags(fss.FlagSet("admission"))
114
- // o.Traces.AddFlags(fss.FlagSet("traces"))
115
-
116
- return fss
72
+ func (o * Options ) Validate () error {
73
+ errs := []error {}
74
+ errs = append (errs , o .SecureServing .Validate ()... )
75
+ errs = append (errs , o .Authentication .Validate ()... )
76
+ errs = append (errs , o .Authorization .Validate ()... )
77
+ errs = append (errs , o .Audit .Validate ()... )
78
+ errs = append (errs , o .Features .Validate ()... )
79
+ return utilerrors .NewAggregate (errs )
117
80
}
118
81
119
82
// nolint
@@ -126,52 +89,79 @@ func (o *Options) Config() (*proxy.Config, error) {
126
89
return nil , fmt .Errorf ("error create self-signed certificates: %v" , err )
127
90
}
128
91
129
- // remove NamespaceLifecycle admission plugin explicitly
130
- // current admission plugins: mutatingwebhook, validatingwebhook
131
- o .Admission .DisablePlugins = append (o .Admission .DisablePlugins , lifecycle .PluginName )
92
+ // o.Admission.DisablePlugins = append(o.RecommendedOptions.Admission.DisablePlugins, lifecycle.PluginName)
132
93
133
- genericConfig := genericapiserver .NewRecommendedConfig (proxy .Codecs )
134
- // genericConfig.OpenAPIConfig = genericapiserver .DefaultOpenAPIConfig(openapi .GetOpenAPIDefinitions, openapi.NewDefinitionNamer(apiserver .Scheme))
135
- // genericConfig.OpenAPIConfig.Info.Title = openAPITitle
136
- // genericConfig.OpenAPIConfig.Info.Version= openAPIVersion
94
+ genericConfig := genericserver .NewRecommendedConfig (proxyScheme .Codecs )
95
+ genericConfig .OpenAPIConfig = genericserver .DefaultOpenAPIConfig (generatedopenapi .GetOpenAPIDefinitions , openapi .NewDefinitionNamer (scheme .Scheme ))
96
+ genericConfig .OpenAPIConfig .Info .Title = utils . KosmosClusrerLinkRroxyComponentName
97
+ genericConfig .OpenAPIConfig .Info .Version = utils . ClusterLinkOpenAPIVersion
137
98
138
99
// support watch to LongRunningFunc
139
100
genericConfig .LongRunningFunc = func (r * http.Request , requestInfo * genericrequest.RequestInfo ) bool {
140
101
return strings .Contains (r .RequestURI , "watch" )
141
102
}
142
103
143
- if err := o .genericOptionsApplyTo (genericConfig ); err != nil {
104
+ if err := o .ApplyTo (genericConfig ); err != nil {
105
+ return nil , err
106
+ }
107
+
108
+ restMapper , err := apiutil .NewDynamicRESTMapper (genericConfig .ClientConfig )
109
+ if err != nil {
110
+ klog .Errorf ("Failed to create REST mapper: %v" , err )
111
+ return nil , err
112
+ }
113
+ kosmosClient := kosmosclientset .NewForConfigOrDie (genericConfig .ClientConfig )
114
+ kosmosInformerFactory := informerfactory .NewSharedInformerFactory (kosmosClient , 0 )
115
+
116
+ dynamicClient , err := dynamic .NewForConfig (genericConfig .ClientConfig )
117
+ if err != nil {
118
+ log .Fatal (err )
119
+ }
120
+
121
+ proxyCtl , err := proxyctl .NewResourceCacheController (proxyctl.NewControllerOption {
122
+ RestConfig : genericConfig .ClientConfig ,
123
+ RestMapper : restMapper ,
124
+ KosmosFactory : kosmosInformerFactory ,
125
+ DynamicClient : dynamicClient ,
126
+ })
127
+ if err != nil {
144
128
return nil , err
145
129
}
146
130
147
131
return & proxy.Config {
148
132
GenericConfig : genericConfig ,
133
+ ExtraConfig : proxy.ExtraConfig {
134
+ ProxyController : proxyCtl ,
135
+ KosmosInformerFactory : kosmosInformerFactory ,
136
+ },
149
137
}, nil
150
138
}
151
139
152
- func (o * Options ) genericOptionsApplyTo (config * genericapiserver.RecommendedConfig ) error {
153
- config .MaxRequestsInFlight = o .MaxRequestsInFlight
154
- config .MaxMutatingRequestsInFlight = o .MaxMutatingRequestsInFlight
155
-
156
- if err := o .SecureServing .ApplyTo (& config .SecureServing , & config .LoopbackClientConfig ); err != nil {
140
+ func (o * Options ) ApplyTo (config * genericserver.RecommendedConfig ) error {
141
+ if err := o .SecureServing .ApplyTo (& config .Config .SecureServing , & config .Config .LoopbackClientConfig ); err != nil {
157
142
return err
158
143
}
159
- if err := o .Authentication .ApplyTo (& config .Authentication , config .SecureServing , config .OpenAPIConfig ); err != nil {
144
+ if err := o .Authentication .ApplyTo (& config .Config . Authentication , config .SecureServing , config .OpenAPIConfig ); err != nil {
160
145
return err
161
146
}
162
- if err := o .Authorization .ApplyTo (& config .Authorization ); err != nil {
147
+ if err := o .Authorization .ApplyTo (& config .Config . Authorization ); err != nil {
163
148
return err
164
149
}
165
150
if err := o .Audit .ApplyTo (& config .Config ); err != nil {
166
151
return err
167
152
}
153
+
168
154
if err := o .Features .ApplyTo (& config .Config ); err != nil {
169
155
return err
170
156
}
171
157
if err := o .CoreAPI .ApplyTo (config ); err != nil {
172
158
return err
173
159
}
174
-
175
- utils .SetQPSBurst (config .ClientConfig , o .KubernetesOptions )
176
- return o .Admission .ApplyTo (& config .Config , config .SharedInformerFactory , config .ClientConfig , o .FeatureGate )
160
+ if err := o .ServerRunOptions .ApplyTo (& config .Config ); err != nil {
161
+ return err
162
+ }
163
+ if err := o .Features .ApplyTo (& config .Config ); err != nil {
164
+ return err
165
+ }
166
+ return nil
177
167
}
0 commit comments