Skip to content

Instantly share code, notes, and snippets.

@sttts
Created September 19, 2022 09:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sttts/bafd277eb6cb5ce626f3f188ac55085d to your computer and use it in GitHub Desktop.
Save sttts/bafd277eb6cb5ce626f3f188ac55085d to your computer and use it in GitHub Desktop.
commit 4175145b779c0f7709a010d763dc194fe926532c
Author: Dr. Stefan Schimanski <stefan.schimanski@gmail.com>
Date: Mon Sep 19 11:01:37 2022 +0200
WIP
diff --git a/cmd/cache-server/main.go b/cmd/cache-server/main.go
index abf99223..3baa2b94 100644
--- a/cmd/cache-server/main.go
+++ b/cmd/cache-server/main.go
@@ -26,7 +26,7 @@ import (
"k8s.io/component-base/cli"
cacheserver "github.com/kcp-dev/kcp/pkg/cache/server"
- "github.com/kcp-dev/kcp/pkg/cache/server/options"
+ "github.com/kcp-dev/kcp/cmd/cache-server/options"
"github.com/kcp-dev/kcp/pkg/cmd/help"
"github.com/kcp-dev/kcp/pkg/embeddedetcd"
)
diff --git a/cmd/cache-server/options/options.go b/cmd/cache-server/options/options.go
new file mode 100644
index 00000000..575d1f84
--- /dev/null
+++ b/cmd/cache-server/options/options.go
@@ -0,0 +1,81 @@
+/*
+Copyright 2022 The KCP 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 options
+
+import (
+ "github.com/spf13/pflag"
+
+ genericoptions "k8s.io/apiserver/pkg/server/options"
+
+ cacheoptions "github.com/kcp-dev/kcp/pkg/cache/server/options"
+)
+
+type Options struct {
+ SecureServing *genericoptions.SecureServingOptionsWithLoopback
+
+ CacheServer *cacheoptions.Options
+}
+
+type completedOptions struct {
+ SecureServing *genericoptions.SecureServingOptionsWithLoopback
+
+ Cache *cacheoptions.CompletedOptions
+}
+
+type CompletedOptions struct {
+ *completedOptions
+}
+
+func (o *CompletedOptions) Validate() []error {
+ errors := []error{}
+ errors = append(errors, o.SecureServing.Validate()...)
+ errors = append(errors, o.Cache.Validate()...)
+ return errors
+}
+
+// NewOptions creates a new Options with default parameters.
+func NewOptions(rootDir string) *Options {
+ o := &Options{
+ SecureServing: genericoptions.NewSecureServingOptions().WithLoopback(),
+ CacheServer: cacheoptions.NewOptions(rootDir),
+ }
+
+ o.SecureServing.ServerCert.CertDirectory = rootDir
+ o.SecureServing.BindPort = 6443
+ return o
+}
+
+func (o *Options) Complete() (*CompletedOptions, error) {
+ if err := o.SecureServing.MaybeDefaultWithSelfSignedCerts("localhost", nil, nil); err != nil {
+ return nil, err
+ }
+
+ completed, err := o.CacheServer.Complete()
+ if err != nil {
+ return nil, err
+ }
+
+ return &CompletedOptions{&completedOptions{
+ SecureServing: o.SecureServing,
+ Cache: completed,
+ }}, nil
+}
+
+func (o *Options) AddFlags(fs *pflag.FlagSet) {
+ o.SecureServing.AddFlags(fs)
+ o.CacheServer.AddFlags(fs)
+}
diff --git a/cmd/cache-server/server/config.go b/cmd/cache-server/server/config.go
new file mode 100644
index 00000000..da48e4c4
--- /dev/null
+++ b/cmd/cache-server/server/config.go
@@ -0,0 +1,87 @@
+/*
+Copyright 2022 The KCP 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 server
+
+import (
+ "time"
+
+ "k8s.io/apiserver/pkg/server"
+ "k8s.io/client-go/rest"
+
+ cacheserveroptions "github.com/kcp-dev/kcp/cmd/cache-server/options"
+ cacheserver "github.com/kcp-dev/kcp/pkg/cache/server"
+)
+
+const resyncPeriod = 10 * time.Hour
+
+type Config struct {
+ Options *cacheserveroptions.CompletedOptions
+ SecureServing *server.SecureServingInfo
+ Cache *cacheserver.Config
+
+ ExtraConfig
+}
+
+type completedConfig struct {
+ Options *cacheserveroptions.CompletedOptions
+ Cache *cacheserver.CompletedConfig
+
+ ExtraConfig
+}
+
+type ExtraConfig struct {
+}
+
+type CompletedConfig struct {
+ // embed a private pointer that cannot be instantiated outside this package.
+ *completedConfig
+}
+
+// Complete fills in any fields not set that are required to have valid data. It's mutating the receiver.
+func (c *Config) Complete() (CompletedConfig, error) {
+ completed, err := c.Cache.Complete()
+ if err != nil {
+ return CompletedConfig{}, err
+ }
+
+ return CompletedConfig{&completedConfig{
+ Options: c.Options,
+ Cache: &completed,
+ ExtraConfig: c.ExtraConfig,
+ }}, nil
+}
+
+// NewConfig returns a new Config for the given options and optional rest.Config that point to the local server.
+// Pass it only when you combine this server with a different one.
+func NewConfig(opts *cacheserveroptions.CompletedOptions) (*Config, error) {
+ c := &Config{
+ Options: opts,
+ }
+
+ var loopback *rest.Config
+ if err := opts.SecureServing.ApplyTo(&c.SecureServing, &loopback); err != nil {
+ return nil, err
+ }
+
+ var err error
+ c.Cache, err = cacheserver.NewConfig(opts.Cache, loopback)
+ if err != nil {
+ return nil, err
+ }
+
+ return c, nil
+}
diff --git a/pkg/cache/server/config.go b/pkg/cache/server/config.go
index 522b57a0..bd6b68f9 100644
--- a/pkg/cache/server/config.go
+++ b/pkg/cache/server/config.go
@@ -119,14 +119,7 @@ func NewConfig(opts *cacheserveroptions.CompletedOptions, optionalLocalShardRest
if err := opts.Etcd.ApplyTo(&serverConfig.Config); err != nil {
return nil, err
}
- if optionalLocalShardRestConfig == nil {
- if err := opts.SecureServing.ApplyTo(&serverConfig.Config.SecureServing, &serverConfig.Config.LoopbackClientConfig); err != nil {
- return nil, err
- }
- } else {
- if err := opts.SecureServing.ApplyTo(&serverConfig.Config.SecureServing, nil); err != nil {
- return nil, err
- }
+ if optionalLocalShardRestConfig != nil {
serverConfig.LoopbackClientConfig = rest.CopyConfig(optionalLocalShardRestConfig)
}
if err := opts.Authentication.ApplyTo(&serverConfig.Config.Authentication, serverConfig.SecureServing, serverConfig.OpenAPIConfig); err != nil {
diff --git a/pkg/cache/server/options/options.go b/pkg/cache/server/options/options.go
index 7fa34a30..8ecb7bdc 100644
--- a/pkg/cache/server/options/options.go
+++ b/pkg/cache/server/options/options.go
@@ -29,7 +29,6 @@ import (
type Options struct {
ServerRunOptions *genericoptions.ServerRunOptions
Etcd *genericoptions.EtcdOptions
- SecureServing *genericoptions.SecureServingOptionsWithLoopback
Authentication *genericoptions.DelegatingAuthenticationOptions
Authorization *genericoptions.DelegatingAuthorizationOptions
APIEnablement *genericoptions.APIEnablementOptions
@@ -39,7 +38,6 @@ type Options struct {
type completedOptions struct {
ServerRunOptions *genericoptions.ServerRunOptions
Etcd *genericoptions.EtcdOptions
- SecureServing *genericoptions.SecureServingOptionsWithLoopback
Authentication *genericoptions.DelegatingAuthenticationOptions
Authorization *genericoptions.DelegatingAuthorizationOptions
APIEnablement *genericoptions.APIEnablementOptions
@@ -54,7 +52,6 @@ func (o *CompletedOptions) Validate() []error {
errors := []error{}
errors = append(errors, o.ServerRunOptions.Validate()...)
errors = append(errors, o.Etcd.Validate()...)
- errors = append(errors, o.SecureServing.Validate()...)
errors = append(errors, o.Authentication.Validate()...)
errors = append(errors, o.Authorization.Validate()...)
errors = append(errors, o.APIEnablement.Validate()...)
@@ -67,7 +64,6 @@ func NewOptions(rootDir string) *Options {
o := &Options{
ServerRunOptions: genericoptions.NewServerRunOptions(),
Etcd: genericoptions.NewEtcdOptions(storagebackend.NewDefaultConfig(kubeoptions.DefaultEtcdPathPrefix, nil)),
- SecureServing: genericoptions.NewSecureServingOptions().WithLoopback(),
Authentication: genericoptions.NewDelegatingAuthenticationOptions(),
Authorization: genericoptions.NewDelegatingAuthorizationOptions(),
APIEnablement: genericoptions.NewAPIEnablementOptions(),
@@ -75,8 +71,6 @@ func NewOptions(rootDir string) *Options {
}
o.ServerRunOptions.EnablePriorityAndFairness = false
- o.SecureServing.ServerCert.CertDirectory = rootDir
- o.SecureServing.BindPort = 6443
o.Etcd.StorageConfig.Transport.ServerList = []string{"embedded"}
// TODO: enable the watch cache, it was disabled because
// - we need to pass a shard name so that the watch cache can calculate the key
@@ -95,14 +89,9 @@ func (o *Options) Complete() (*CompletedOptions, error) {
o.Authentication = nil
o.Authorization = nil
- if err := o.SecureServing.MaybeDefaultWithSelfSignedCerts("localhost", nil, nil); err != nil {
- return nil, err
- }
-
return &CompletedOptions{&completedOptions{
ServerRunOptions: o.ServerRunOptions,
Etcd: o.Etcd,
- SecureServing: o.SecureServing,
Authentication: o.Authentication,
Authorization: o.Authorization,
APIEnablement: o.APIEnablement,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment