From 4ae003657bbbe637fd17a4dcbb915fe25e95fd79 Mon Sep 17 00:00:00 2001 From: yue9944882 <291271447@qq.com> Date: Mon, 6 Sep 2021 17:17:03 +0800 Subject: [PATCH] supporting exposing master loopback client --- pkg/builder/builder_misc.go | 10 ++++++++++ pkg/util/loopback/master.go | 39 +++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 pkg/util/loopback/master.go diff --git a/pkg/builder/builder_misc.go b/pkg/builder/builder_misc.go index 51b0156..00f4f7d 100644 --- a/pkg/builder/builder_misc.go +++ b/pkg/builder/builder_misc.go @@ -2,6 +2,7 @@ package builder import ( "k8s.io/apimachinery/pkg/runtime" + genericapiserver "k8s.io/apiserver/pkg/server" openapicommon "k8s.io/kube-openapi/pkg/common" "sigs.k8s.io/apiserver-runtime/internal/sample-apiserver/pkg/cmd/server" "sigs.k8s.io/apiserver-runtime/pkg/util/loopback" @@ -56,6 +57,15 @@ func (a *Server) ExposeLoopbackAuthorizer() *Server { }) } +// ExposeLoopbackMasterClientConfig exposes loopback client config for accessing the +// configured master cluster's kube-apiserver as an external singleton. +func (a *Server) ExposeLoopbackMasterClientConfig() *Server { + return a.WithConfigFns(func(config *genericapiserver.RecommendedConfig) *genericapiserver.RecommendedConfig { + loopback.SetLoopbackMasterClientConfig(config.ClientConfig) + return config + }) +} + // WithoutEtcd removes etcd related settings from apiserver. func (a *Server) WithoutEtcd() *Server { return a.WithOptionsFns(func(o *ServerOptions) *ServerOptions { diff --git a/pkg/util/loopback/master.go b/pkg/util/loopback/master.go new file mode 100644 index 0000000..0210e40 --- /dev/null +++ b/pkg/util/loopback/master.go @@ -0,0 +1,39 @@ +/* +Copyright 2021 The Kubernetes 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 loopback + +import ( + "sync" + + "k8s.io/client-go/rest" +) + +var setLoopbackMasterClientOnce sync.Once +var loopbackMasterClientConfig *rest.Config + +// SetLoopbackMasterClientConfig provides loopback client config for one time +func SetLoopbackMasterClientConfig(c *rest.Config) { + setLoopbackMasterClientOnce.Do(func() { + loopbackMasterClientConfig = c + }) +} + +// GetLoopbackMasterClientConfig gets loopback client config for the +// master kube-apiserver. +func GetLoopbackMasterClientConfig() *rest.Config { + return loopbackMasterClientConfig +}