Skip to content

Commit 18e47bc

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 99b9f99 commit 18e47bc

File tree

4 files changed

+87
-1
lines changed

4 files changed

+87
-1
lines changed

Diff for: cmd/kube-controller-manager/app/config/patch.go

+9
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
}

Diff for: cmd/kube-controller-manager/app/controllermanager.go

+19-1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ import (
8383
serviceaccountcontroller "k8s.io/kubernetes/pkg/controller/serviceaccount"
8484
kubefeatures "k8s.io/kubernetes/pkg/features"
8585
"k8s.io/kubernetes/pkg/serviceaccount"
86+
87+
libgorestclient "github.com/openshift/library-go/pkg/config/client"
8688
)
8789

8890
func init() {
@@ -134,6 +136,11 @@ controller, and serviceaccounts controller.`,
134136
return err
135137
}
136138
cliflag.PrintFlags(cmd.Flags())
139+
140+
if err := SetUpPreferredHostForOpenShift(s); err != nil {
141+
fmt.Fprintf(os.Stderr, "%v\n", err)
142+
os.Exit(1)
143+
}
137144

138145
c, err := s.Config(KnownControllers(), ControllersDisabledByDefault(), ControllerAliases())
139146
if err != nil {
@@ -205,6 +212,17 @@ func Run(ctx context.Context, c *config.CompletedConfig) error {
205212
logger.Error(err, "Unable to register configz")
206213
}
207214

215+
// start the localhost health monitor early so that it can be used by the LE client
216+
if c.OpenShiftContext.PreferredHostHealthMonitor != nil {
217+
hmCtx, cancel := context.WithCancel(context.Background())
218+
defer cancel()
219+
go func() {
220+
<-stopCh
221+
cancel()
222+
}()
223+
go c.OpenShiftContext.PreferredHostHealthMonitor.Run(hmCtx)
224+
}
225+
208226
// Setup any healthz checks we will want to use.
209227
var checks []healthz.HealthChecker
210228
var electionChecker *leaderelection.HealthzAdaptor
@@ -872,7 +890,7 @@ func createClientBuilders(c *config.CompletedConfig) (clientBuilder clientbuilde
872890
if c.ComponentConfig.KubeCloudShared.UseServiceAccountCredentials {
873891

874892
clientBuilder = clientbuilder.NewDynamicClientBuilder(
875-
restclient.AnonymousClientConfig(c.Kubeconfig),
893+
libgorestclient.AnonymousClientConfigWithWrapTransport(c.Kubeconfig),
876894
c.Client.CoreV1(),
877895
metav1.NamespaceSystem)
878896
} else {

Diff for: cmd/kube-controller-manager/app/options/options.go

+8
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ import (
5858

5959
// add the kubernetes feature gates
6060
_ "k8s.io/kubernetes/pkg/features"
61+
62+
libgorestclient "github.com/openshift/library-go/pkg/config/client"
6163
)
6264

6365
const (
@@ -311,6 +313,7 @@ func (s *KubeControllerManagerOptions) Flags(allControllers []string, disabledBy
311313
s.ComponentGlobalsRegistry.AddFlags(fss.FlagSet("generic"))
312314
fs.StringVar(&s.OpenShiftContext.OpenShiftConfig, "openshift-config", s.OpenShiftContext.OpenShiftConfig, "indicates that this process should be compatible with openshift start master")
313315
fs.MarkHidden("openshift-config")
316+
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")
314317

315318
return fss
316319
}
@@ -501,6 +504,11 @@ func (s KubeControllerManagerOptions) Config(allControllers []string, disabledBy
501504
kubeconfig.QPS = s.Generic.ClientConnection.QPS
502505
kubeconfig.Burst = int(s.Generic.ClientConnection.Burst)
503506

507+
if s.OpenShiftContext.PreferredHostRoundTripperWrapperFn != nil {
508+
libgorestclient.DefaultServerName(kubeconfig)
509+
kubeconfig.Wrap(s.OpenShiftContext.PreferredHostRoundTripperWrapperFn)
510+
}
511+
504512
client, err := clientset.NewForConfig(restclient.AddUserAgent(kubeconfig, KubeControllerManagerUserAgent))
505513
if err != nil {
506514
return nil, err

Diff for: 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)