|
1 | 1 | package app
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "fmt" |
4 | 5 | "io/ioutil"
|
5 | 6 | "path"
|
| 7 | + "time" |
6 | 8 |
|
7 | 9 | "k8s.io/apimachinery/pkg/util/json"
|
8 | 10 | kyaml "k8s.io/apimachinery/pkg/util/yaml"
|
9 | 11 | "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" |
10 | 15 | "k8s.io/kubernetes/cmd/kube-controller-manager/app/config"
|
11 | 16 | "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" |
12 | 20 | )
|
13 | 21 |
|
14 | 22 | var InformerFactoryOverride informers.SharedInformerFactory
|
15 | 23 |
|
| 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 | + |
16 | 60 | func ShimForOpenShift(controllerManagerOptions *options.KubeControllerManagerOptions, controllerManager *config.Config) error {
|
17 | 61 | if len(controllerManager.OpenShiftContext.OpenShiftConfig) == 0 {
|
18 | 62 | return nil
|
@@ -82,3 +126,10 @@ func applyOpenShiftConfigDefaultProjectSelector(controllerManagerOptions *option
|
82 | 126 |
|
83 | 127 | return nil
|
84 | 128 | }
|
| 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