|
| 1 | +package app |
| 2 | + |
| 3 | +import ( |
| 4 | + "time" |
| 5 | + |
| 6 | + "k8s.io/client-go/rest" |
| 7 | + "k8s.io/client-go/tools/clientcmd" |
| 8 | + "k8s.io/component-base/metrics/legacyregistry" |
| 9 | + "k8s.io/kubernetes/cmd/kube-scheduler/app/options" |
| 10 | + |
| 11 | + libgorestclient "github.com/openshift/library-go/pkg/config/client" |
| 12 | + "github.com/openshift/library-go/pkg/monitor/health" |
| 13 | +) |
| 14 | + |
| 15 | +func setUpPreferredHostForOpenShift(kubeSchedulerOptions *options.Options) error { |
| 16 | + if !kubeSchedulerOptions.OpenShiftContext.UnsupportedKubeAPIOverPreferredHost { |
| 17 | + return nil |
| 18 | + } |
| 19 | + |
| 20 | + master, kubeConfig := kubeSchedulerOptions.Master, kubeSchedulerOptions.ComponentConfig.ClientConnection.Kubeconfig |
| 21 | + |
| 22 | + // this makes our patch small |
| 23 | + // if there was no kubeconfig specified we won't be able to get cluster info. |
| 24 | + // in that case try to load the configuration and read kubeconfig directly from it if it was provided. |
| 25 | + if len(kubeConfig) == 0 && len(kubeSchedulerOptions.ConfigFile) > 0 { |
| 26 | + cfg, err := options.LoadKubeSchedulerConfiguration(kubeSchedulerOptions.ConfigFile) |
| 27 | + if err != nil { |
| 28 | + return err |
| 29 | + } |
| 30 | + kubeConfig = cfg.ClientConnection.Kubeconfig |
| 31 | + } |
| 32 | + |
| 33 | + config, err := clientcmd.BuildConfigFromFlags(master, kubeConfig) |
| 34 | + if err != nil { |
| 35 | + return err |
| 36 | + } |
| 37 | + libgorestclient.DefaultServerName(config) |
| 38 | + |
| 39 | + targetProvider := health.StaticTargetProvider{"localhost:6443"} |
| 40 | + kubeSchedulerOptions.OpenShiftContext.PreferredHostHealthMonitor, err = health.New(targetProvider, createRestConfigForHealthMonitor(config)) |
| 41 | + if err != nil { |
| 42 | + return err |
| 43 | + } |
| 44 | + kubeSchedulerOptions.OpenShiftContext.PreferredHostHealthMonitor. |
| 45 | + WithHealthyProbesThreshold(3). |
| 46 | + WithUnHealthyProbesThreshold(5). |
| 47 | + WithProbeInterval(5 * time.Second). |
| 48 | + WithProbeResponseTimeout(2 * time.Second). |
| 49 | + WithMetrics(health.Register(legacyregistry.MustRegister)) |
| 50 | + |
| 51 | + kubeSchedulerOptions.OpenShiftContext.PreferredHostRoundTripperWrapperFn = libgorestclient.NewPreferredHostRoundTripper(func() string { |
| 52 | + healthyTargets, _ := kubeSchedulerOptions.OpenShiftContext.PreferredHostHealthMonitor.Targets() |
| 53 | + if len(healthyTargets) == 1 { |
| 54 | + return healthyTargets[0] |
| 55 | + } |
| 56 | + return "" |
| 57 | + }) |
| 58 | + |
| 59 | + kubeSchedulerOptions.Authentication.WithCustomRoundTripper(kubeSchedulerOptions.OpenShiftContext.PreferredHostRoundTripperWrapperFn) |
| 60 | + kubeSchedulerOptions.Authorization.WithCustomRoundTripper(kubeSchedulerOptions.OpenShiftContext.PreferredHostRoundTripperWrapperFn) |
| 61 | + return nil |
| 62 | +} |
| 63 | + |
| 64 | +func createRestConfigForHealthMonitor(restConfig *rest.Config) *rest.Config { |
| 65 | + restConfigCopy := *restConfig |
| 66 | + rest.AddUserAgent(&restConfigCopy, "kube-scheduler-health-monitor") |
| 67 | + |
| 68 | + return &restConfigCopy |
| 69 | +} |
0 commit comments