|
1 | 1 | package start
|
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/golang/glog" |
| 5 | + |
| 6 | + kerrors "k8s.io/apimachinery/pkg/util/errors" |
| 7 | + controllerapp "k8s.io/kubernetes/cmd/kube-controller-manager/app" |
| 8 | + controlleroptions "k8s.io/kubernetes/cmd/kube-controller-manager/app/options" |
| 9 | + _ "k8s.io/kubernetes/plugin/pkg/scheduler/algorithmprovider" |
| 10 | + |
| 11 | + "github.com/openshift/origin/pkg/cmd/server/bootstrappolicy" |
| 12 | + "k8s.io/kubernetes/pkg/api/v1" |
| 13 | + kapiv1 "k8s.io/kubernetes/pkg/api/v1" |
| 14 | + "k8s.io/kubernetes/pkg/volume" |
| 15 | + |
| 16 | + cmdflags "github.com/openshift/origin/pkg/cmd/util/flags" |
| 17 | + "github.com/spf13/pflag" |
| 18 | +) |
| 19 | + |
| 20 | +// newPersistentVolumeRecyclerPodTemplate provides a function which makes our recycler pod template for use in the kube-controller-manager |
| 21 | +// this is a stop-gap until the kube-controller-manager take a pod manifest |
| 22 | +func newPersistentVolumeRecyclerPodTemplate(recyclerImageName string) func() *v1.Pod { |
| 23 | + oldTemplateFunc := volume.NewPersistentVolumeRecyclerPodTemplate |
| 24 | + return func() *v1.Pod { |
| 25 | + uid := int64(0) |
| 26 | + defaultScrubPod := oldTemplateFunc() |
| 27 | + // TODO: Move the recycler pods to dedicated namespace instead of polluting openshift-infra. |
| 28 | + defaultScrubPod.Namespace = "openshift-infra" |
| 29 | + defaultScrubPod.Spec.ServiceAccountName = bootstrappolicy.InfraPersistentVolumeRecyclerControllerServiceAccountName |
| 30 | + defaultScrubPod.Spec.Containers[0].Image = recyclerImageName |
| 31 | + defaultScrubPod.Spec.Containers[0].Command = []string{"/usr/bin/openshift-recycle"} |
| 32 | + defaultScrubPod.Spec.Containers[0].Args = []string{"/scrub"} |
| 33 | + defaultScrubPod.Spec.Containers[0].SecurityContext = &kapiv1.SecurityContext{RunAsUser: &uid} |
| 34 | + defaultScrubPod.Spec.Containers[0].ImagePullPolicy = kapiv1.PullIfNotPresent |
| 35 | + |
| 36 | + return defaultScrubPod |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +func kubeControllerManagerAddFlags(cmserver *controlleroptions.CMServer) func(flags *pflag.FlagSet) { |
| 41 | + return func(flags *pflag.FlagSet) { |
| 42 | + cmserver.AddFlags(flags, controllerapp.KnownControllers(), controllerapp.ControllersDisabledByDefault.List()) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func newKubeControllerManager(kubeconfigFile, saPrivateKeyFile, saRootCAFile string, cmdLineArgs map[string][]string) (*controlleroptions.CMServer, error) { |
| 47 | + if cmdLineArgs == nil { |
| 48 | + cmdLineArgs = map[string][]string{} |
| 49 | + } |
| 50 | + |
| 51 | + if len(cmdLineArgs["controllers"]) == 0 { |
| 52 | + cmdLineArgs["controllers"] = []string{} |
| 53 | + } |
| 54 | + // these two are ones we disable in addition to others |
| 55 | + cmdLineArgs["controllers"] = []string{ |
| 56 | + // we don't appear to use this |
| 57 | + "-ttl", |
| 58 | + // we have to configure this separately until it is generic |
| 59 | + "-horizontalpodautoscaler", |
| 60 | + // we carry patches on this. For now.... |
| 61 | + "-serviceaccount-token", |
| 62 | + } |
| 63 | + if len(cmdLineArgs["use-service-account-credentials"]) == 0 { |
| 64 | + cmdLineArgs["use-service-account-credentials"] = []string{"true"} |
| 65 | + } |
| 66 | + if len(cmdLineArgs["service-account-private-key-file"]) == 0 { |
| 67 | + cmdLineArgs["service-account-private-key-file"] = []string{saPrivateKeyFile} |
| 68 | + } |
| 69 | + if len(cmdLineArgs["root-ca-file"]) == 0 { |
| 70 | + cmdLineArgs["root-ca-file"] = []string{saRootCAFile} |
| 71 | + } |
| 72 | + if len(cmdLineArgs["kubeconfig"]) == 0 { |
| 73 | + cmdLineArgs["kubeconfig"] = []string{kubeconfigFile} |
| 74 | + } |
| 75 | + |
| 76 | + // disable serving http since we didn't used to expose it |
| 77 | + if len(cmdLineArgs["port"]) == 0 { |
| 78 | + cmdLineArgs["port"] = []string{"-1"} |
| 79 | + } |
| 80 | + |
| 81 | + // resolve arguments |
| 82 | + controllerManager := controlleroptions.NewCMServer() |
| 83 | + if err := cmdflags.Resolve(cmdLineArgs, kubeControllerManagerAddFlags(controllerManager)); len(err) > 0 { |
| 84 | + return nil, kerrors.NewAggregate(err) |
| 85 | + } |
| 86 | + |
| 87 | + return controllerManager, nil |
| 88 | +} |
| 89 | + |
| 90 | +func runEmbeddedKubeControllerManager(kubeconfigFile, saPrivateKeyFile, saRootCAFile string, cmdLineArgs map[string][]string, recyclerImage string) { |
| 91 | + volume.NewPersistentVolumeRecyclerPodTemplate = newPersistentVolumeRecyclerPodTemplate(recyclerImage) |
| 92 | + |
| 93 | + // TODO we need a real identity for this. Right now it's just using the loopback connection like it used to. |
| 94 | + controllerManager, err := newKubeControllerManager(kubeconfigFile, saPrivateKeyFile, saRootCAFile, cmdLineArgs) |
| 95 | + if err != nil { |
| 96 | + glog.Fatal(err) |
| 97 | + } |
| 98 | + // this does a second leader election, but doing the second leader election will allow us to move out process in |
| 99 | + // 3.8 if we so choose. |
| 100 | + if err := controllerapp.Run(controllerManager); err != nil { |
| 101 | + glog.Fatal(err) |
| 102 | + } |
| 103 | +} |
0 commit comments