Skip to content

Commit 5da1477

Browse files
p0lyn0mialbertinatto
authored andcommitted
UPSTREAM: <carry>: allows for switching KCM to talk to Kube API over localhost
to force KCM to use localhost set the following flag in kubecontrollermanager (oc edit kubecontrollermanager cluster) unsupportedConfigOverrides: extendedArguments: unsupported-kube-api-over-localhost: - "true" OpenShift-Rebase-Source: 036b11c UPSTREAM: <carry>: allows for switching KCM to talk to Kube API over localhost
1 parent 96dea4a commit 5da1477

File tree

4 files changed

+87
-1
lines changed

4 files changed

+87
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
package config
22

3+
import (
4+
"k8s.io/client-go/transport"
5+
6+
"github.com/openshift/library-go/pkg/monitor/health"
7+
)
8+
39
// OpenShiftContext is additional context that we need to launch the kube-controller-manager for openshift.
410
// Basically, this holds our additional config information.
511
type OpenShiftContext struct {
612
OpenShiftConfig string
713
OpenShiftDefaultProjectNodeSelector string
814
KubeDefaultProjectNodeSelector string
15+
UnsupportedKubeAPIOverPreferredHost bool
16+
PreferredHostRoundTripperWrapperFn transport.WrapperFunc
17+
PreferredHostHealthMonitor *health.Prober
918
}

cmd/kube-controller-manager/app/controllermanager.go

+19-1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ import (
7979
serviceaccountcontroller "k8s.io/kubernetes/pkg/controller/serviceaccount"
8080
kubefeatures "k8s.io/kubernetes/pkg/features"
8181
"k8s.io/kubernetes/pkg/serviceaccount"
82+
83+
libgorestclient "github.com/openshift/library-go/pkg/config/client"
8284
)
8385

8486
func init() {
@@ -131,6 +133,11 @@ controller, and serviceaccounts controller.`,
131133
return err
132134
}
133135
cliflag.PrintFlags(cmd.Flags())
136+
137+
if err := SetUpPreferredHostForOpenShift(s); err != nil {
138+
fmt.Fprintf(os.Stderr, "%v\n", err)
139+
os.Exit(1)
140+
}
134141

135142
c, err := s.Config(KnownControllers(), ControllersDisabledByDefault(), ControllerAliases())
136143
if err != nil {
@@ -202,6 +209,17 @@ func Run(ctx context.Context, c *config.CompletedConfig) error {
202209
logger.Error(err, "Unable to register configz")
203210
}
204211

212+
// start the localhost health monitor early so that it can be used by the LE client
213+
if c.OpenShiftContext.PreferredHostHealthMonitor != nil {
214+
hmCtx, cancel := context.WithCancel(context.Background())
215+
defer cancel()
216+
go func() {
217+
<-stopCh
218+
cancel()
219+
}()
220+
go c.OpenShiftContext.PreferredHostHealthMonitor.Run(hmCtx)
221+
}
222+
205223
// Setup any healthz checks we will want to use.
206224
var checks []healthz.HealthChecker
207225
var electionChecker *leaderelection.HealthzAdaptor
@@ -858,7 +876,7 @@ func createClientBuilders(c *config.CompletedConfig) (clientBuilder clientbuilde
858876
if c.ComponentConfig.KubeCloudShared.UseServiceAccountCredentials {
859877

860878
clientBuilder = clientbuilder.NewDynamicClientBuilder(
861-
restclient.AnonymousClientConfig(c.Kubeconfig),
879+
libgorestclient.AnonymousClientConfigWithWrapTransport(c.Kubeconfig),
862880
c.Client.CoreV1(),
863881
metav1.NamespaceSystem)
864882
} else {

cmd/kube-controller-manager/app/options/options.go

+8
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ import (
5656

5757
// add the kubernetes feature gates
5858
_ "k8s.io/kubernetes/pkg/features"
59+
60+
libgorestclient "github.com/openshift/library-go/pkg/config/client"
5961
)
6062

6163
const (
@@ -304,6 +306,7 @@ func (s *KubeControllerManagerOptions) Flags(allControllers []string, disabledBy
304306
s.ComponentGlobalsRegistry.AddFlags(fss.FlagSet("generic"))
305307
fs.StringVar(&s.OpenShiftContext.OpenShiftConfig, "openshift-config", s.OpenShiftContext.OpenShiftConfig, "indicates that this process should be compatible with openshift start master")
306308
fs.MarkHidden("openshift-config")
309+
fs.BoolVar(&s.OpenShiftContext.UnsupportedKubeAPIOverPreferredHost, "unsupported-kube-api-over-localhost", false, "when set makes KCM prefer talking to localhost kube-apiserver (when available) instead of LB")
307310

308311
return fss
309312
}
@@ -495,6 +498,11 @@ func (s KubeControllerManagerOptions) Config(allControllers []string, disabledBy
495498
kubeconfig.QPS = s.Generic.ClientConnection.QPS
496499
kubeconfig.Burst = int(s.Generic.ClientConnection.Burst)
497500

501+
if s.OpenShiftContext.PreferredHostRoundTripperWrapperFn != nil {
502+
libgorestclient.DefaultServerName(kubeconfig)
503+
kubeconfig.Wrap(s.OpenShiftContext.PreferredHostRoundTripperWrapperFn)
504+
}
505+
498506
client, err := clientset.NewForConfig(restclient.AddUserAgent(kubeconfig, KubeControllerManagerUserAgent))
499507
if err != nil {
500508
return nil, err

cmd/kube-controller-manager/app/patch.go

+51
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,62 @@
11
package app
22

33
import (
4+
"fmt"
45
"io/ioutil"
56
"path"
7+
"time"
68

79
"k8s.io/apimachinery/pkg/util/json"
810
kyaml "k8s.io/apimachinery/pkg/util/yaml"
911
"k8s.io/client-go/informers"
12+
"k8s.io/client-go/rest"
13+
"k8s.io/client-go/tools/clientcmd"
14+
"k8s.io/component-base/metrics/legacyregistry"
1015
"k8s.io/kubernetes/cmd/kube-controller-manager/app/config"
1116
"k8s.io/kubernetes/cmd/kube-controller-manager/app/options"
17+
18+
libgorestclient "github.com/openshift/library-go/pkg/config/client"
19+
"github.com/openshift/library-go/pkg/monitor/health"
1220
)
1321

1422
var InformerFactoryOverride informers.SharedInformerFactory
1523

24+
func SetUpPreferredHostForOpenShift(controllerManagerOptions *options.KubeControllerManagerOptions) error {
25+
if !controllerManagerOptions.OpenShiftContext.UnsupportedKubeAPIOverPreferredHost {
26+
return nil
27+
}
28+
29+
config, err := clientcmd.BuildConfigFromFlags(controllerManagerOptions.Master, controllerManagerOptions.Generic.ClientConnection.Kubeconfig)
30+
if err != nil {
31+
return err
32+
}
33+
libgorestclient.DefaultServerName(config)
34+
35+
targetProvider := health.StaticTargetProvider{"localhost:6443"}
36+
controllerManagerOptions.OpenShiftContext.PreferredHostHealthMonitor, err = health.New(targetProvider, createRestConfigForHealthMonitor(config))
37+
if err != nil {
38+
return err
39+
}
40+
controllerManagerOptions.OpenShiftContext.PreferredHostHealthMonitor.
41+
WithHealthyProbesThreshold(3).
42+
WithUnHealthyProbesThreshold(5).
43+
WithProbeInterval(5 * time.Second).
44+
WithProbeResponseTimeout(2 * time.Second).
45+
WithMetrics(health.Register(legacyregistry.MustRegister))
46+
47+
controllerManagerOptions.OpenShiftContext.PreferredHostRoundTripperWrapperFn = libgorestclient.NewPreferredHostRoundTripper(func() string {
48+
healthyTargets, _ := controllerManagerOptions.OpenShiftContext.PreferredHostHealthMonitor.Targets()
49+
if len(healthyTargets) == 1 {
50+
return healthyTargets[0]
51+
}
52+
return ""
53+
})
54+
55+
controllerManagerOptions.Authentication.WithCustomRoundTripper(controllerManagerOptions.OpenShiftContext.PreferredHostRoundTripperWrapperFn)
56+
controllerManagerOptions.Authorization.WithCustomRoundTripper(controllerManagerOptions.OpenShiftContext.PreferredHostRoundTripperWrapperFn)
57+
return nil
58+
}
59+
1660
func ShimForOpenShift(controllerManagerOptions *options.KubeControllerManagerOptions, controllerManager *config.Config) error {
1761
if len(controllerManager.OpenShiftContext.OpenShiftConfig) == 0 {
1862
return nil
@@ -82,3 +126,10 @@ func applyOpenShiftConfigDefaultProjectSelector(controllerManagerOptions *option
82126

83127
return nil
84128
}
129+
130+
func createRestConfigForHealthMonitor(restConfig *rest.Config) *rest.Config {
131+
restConfigCopy := *restConfig
132+
rest.AddUserAgent(&restConfigCopy, fmt.Sprintf("%s-health-monitor", options.KubeControllerManagerUserAgent))
133+
134+
return &restConfigCopy
135+
}

0 commit comments

Comments
 (0)