Skip to content

Instantly share code, notes, and snippets.

@sttts
Created September 19, 2022 09:01
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/d6a4a00383496a82ab2fb44c081e479b to your computer and use it in GitHub Desktop.
Save sttts/d6a4a00383496a82ab2fb44c081e479b to your computer and use it in GitHub Desktop.
commit ea73ce4085fa7f05993a075daa9b9e59eaf50933
Author: Dr. Stefan Schimanski <stefan.schimanski@gmail.com>
Date: Mon Sep 19 11:01:37 2022 +0200
WIP
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
+}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment