Skip to content

Commit 363a5f4

Browse files
Merge pull request #18100 from deads2k/controller-19-flaggable
Automatic merge from submit-queue. add wiring for running bare kube-controller-manager @smarterclayton @liggitt
2 parents 80aa82c + bd48fc8 commit 363a5f4

File tree

14 files changed

+767
-181
lines changed

14 files changed

+767
-181
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,17 @@
11
package start
22

33
import (
4-
"io/ioutil"
5-
"os"
64
"strconv"
75

86
"github.com/golang/glog"
97
"github.com/spf13/pflag"
108

11-
kapiv1 "k8s.io/api/core/v1"
12-
"k8s.io/apimachinery/pkg/runtime"
13-
"k8s.io/apimachinery/pkg/runtime/schema"
149
kerrors "k8s.io/apimachinery/pkg/util/errors"
15-
kinformers "k8s.io/client-go/informers"
1610
controllerapp "k8s.io/kubernetes/cmd/kube-controller-manager/app"
1711
controlleroptions "k8s.io/kubernetes/cmd/kube-controller-manager/app/options"
18-
"k8s.io/kubernetes/pkg/api/legacyscheme"
19-
"k8s.io/kubernetes/pkg/volume"
2012
_ "k8s.io/kubernetes/plugin/pkg/scheduler/algorithmprovider"
2113

22-
"github.com/openshift/origin/pkg/cmd/server/bootstrappolicy"
2314
cmdflags "github.com/openshift/origin/pkg/cmd/util/flags"
24-
"k8s.io/kubernetes/pkg/apis/componentconfig"
2515
)
2616

2717
func kubeControllerManagerAddFlags(cmserver *controlleroptions.CMServer) func(flags *pflag.FlagSet) {
@@ -30,14 +20,8 @@ func kubeControllerManagerAddFlags(cmserver *controlleroptions.CMServer) func(fl
3020
}
3121
}
3222

33-
func newKubeControllerManager(kubeconfigFile, saPrivateKeyFile, saRootCAFile, podEvictionTimeout, recyclerImage string, dynamicProvisioningEnabled bool, controllerArgs map[string][]string) (*controlleroptions.CMServer, []func(), error) {
23+
func newKubeControllerManager(kubeconfigFile, saPrivateKeyFile, saRootCAFile, podEvictionTimeout, openshiftConfigFile string, dynamicProvisioningEnabled bool) (*controlleroptions.CMServer, error) {
3424
cmdLineArgs := map[string][]string{}
35-
// deep-copy the input args to avoid mutation conflict.
36-
for k, v := range controllerArgs {
37-
cmdLineArgs[k] = append([]string{}, v...)
38-
}
39-
cleanupFunctions := []func(){}
40-
4125
if _, ok := cmdLineArgs["controllers"]; !ok {
4226
cmdLineArgs["controllers"] = []string{
4327
"*", // start everything but the exceptions}
@@ -91,158 +75,24 @@ func newKubeControllerManager(kubeconfigFile, saPrivateKeyFile, saRootCAFile, po
9175
if _, ok := cmdLineArgs["leader-elect-resource-lock"]; !ok {
9276
cmdLineArgs["leader-elect-resource-lock"] = []string{"configmaps"}
9377
}
94-
95-
_, hostPathTemplateSet := cmdLineArgs["pv-recycler-pod-template-filepath-hostpath"]
96-
_, nfsTemplateSet := cmdLineArgs["pv-recycler-pod-template-filepath-nfs"]
97-
if !hostPathTemplateSet || !nfsTemplateSet {
98-
// OpenShift uses a different default volume recycler template than
99-
// Kubernetes. This default template is hardcoded in Kubernetes and it
100-
// isn't possible to pass it via ControllerContext. Crate a temporary
101-
// file with OpenShift's template and let's pretend it was set by user
102-
// as --recycler-pod-template-filepath-hostpath and
103-
// --pv-recycler-pod-template-filepath-nfs arguments.
104-
// This template then needs to be deleted by caller!
105-
templateFilename, err := createRecylerTemplate(recyclerImage)
106-
if err != nil {
107-
return nil, nil, err
108-
}
109-
110-
cleanupFunctions = append(cleanupFunctions, func() {
111-
// Remove the template when it's not needed. This is called aftet
112-
// controller is initialized
113-
glog.V(4).Infof("Removing temporary file %s", templateFilename)
114-
err := os.Remove(templateFilename)
115-
if err != nil {
116-
glog.Warningf("Failed to remove %s: %v", templateFilename, err)
117-
}
118-
})
119-
120-
if !hostPathTemplateSet {
121-
cmdLineArgs["pv-recycler-pod-template-filepath-hostpath"] = []string{templateFilename}
122-
}
123-
if !nfsTemplateSet {
124-
cmdLineArgs["pv-recycler-pod-template-filepath-nfs"] = []string{templateFilename}
125-
}
126-
}
78+
cmdLineArgs["openshift-config"] = []string{openshiftConfigFile}
12779

12880
// resolve arguments
12981
controllerManager := controlleroptions.NewCMServer()
13082
if err := cmdflags.Resolve(cmdLineArgs, kubeControllerManagerAddFlags(controllerManager)); len(err) > 0 {
131-
return nil, cleanupFunctions, kerrors.NewAggregate(err)
83+
return nil, kerrors.NewAggregate(err)
13284
}
13385

134-
// TODO make this configurable or discoverable. This is going to prevent us from running the stock GC controller
135-
// IF YOU ADD ANYTHING TO THIS LIST, MAKE SURE THAT YOU UPDATE THEIR STRATEGIES TO PREVENT GC FINALIZERS
136-
controllerManager.GCIgnoredResources = append(controllerManager.GCIgnoredResources,
137-
// explicitly disabled from GC for now - not enough value to track them
138-
componentconfig.GroupResource{Group: "authorization.openshift.io", Resource: "rolebindingrestrictions"},
139-
componentconfig.GroupResource{Group: "network.openshift.io", Resource: "clusternetworks"},
140-
componentconfig.GroupResource{Group: "network.openshift.io", Resource: "egressnetworkpolicies"},
141-
componentconfig.GroupResource{Group: "network.openshift.io", Resource: "hostsubnets"},
142-
componentconfig.GroupResource{Group: "network.openshift.io", Resource: "netnamespaces"},
143-
componentconfig.GroupResource{Group: "oauth.openshift.io", Resource: "oauthclientauthorizations"},
144-
componentconfig.GroupResource{Group: "oauth.openshift.io", Resource: "oauthclients"},
145-
componentconfig.GroupResource{Group: "quota.openshift.io", Resource: "clusterresourcequotas"},
146-
componentconfig.GroupResource{Group: "user.openshift.io", Resource: "groups"},
147-
componentconfig.GroupResource{Group: "user.openshift.io", Resource: "identities"},
148-
componentconfig.GroupResource{Group: "user.openshift.io", Resource: "users"},
149-
componentconfig.GroupResource{Group: "image.openshift.io", Resource: "images"},
150-
151-
// virtual resource
152-
componentconfig.GroupResource{Group: "project.openshift.io", Resource: "projects"},
153-
// virtual and unwatchable resource, surfaced via rbac.authorization.k8s.io objects
154-
componentconfig.GroupResource{Group: "authorization.openshift.io", Resource: "clusterroles"},
155-
componentconfig.GroupResource{Group: "authorization.openshift.io", Resource: "clusterrolebindings"},
156-
componentconfig.GroupResource{Group: "authorization.openshift.io", Resource: "roles"},
157-
componentconfig.GroupResource{Group: "authorization.openshift.io", Resource: "rolebindings"},
158-
// these resources contain security information in their names, and we don't need to track them
159-
componentconfig.GroupResource{Group: "oauth.openshift.io", Resource: "oauthaccesstokens"},
160-
componentconfig.GroupResource{Group: "oauth.openshift.io", Resource: "oauthauthorizetokens"},
161-
// exposed already as extensions v1beta1 by other controllers
162-
componentconfig.GroupResource{Group: "apps", Resource: "deployments"},
163-
// exposed as autoscaling v1
164-
componentconfig.GroupResource{Group: "extensions", Resource: "horizontalpodautoscalers"},
165-
// exposed as security.openshift.io v1
166-
componentconfig.GroupResource{Group: "", Resource: "securitycontextconstraints"},
167-
)
168-
169-
return controllerManager, cleanupFunctions, nil
86+
return controllerManager, nil
17087
}
17188

172-
func createRecylerTemplate(recyclerImage string) (string, error) {
173-
uid := int64(0)
174-
template := volume.NewPersistentVolumeRecyclerPodTemplate()
175-
template.Namespace = "openshift-infra"
176-
template.Spec.ServiceAccountName = bootstrappolicy.InfraPersistentVolumeRecyclerControllerServiceAccountName
177-
template.Spec.Containers[0].Image = recyclerImage
178-
template.Spec.Containers[0].Command = []string{"/usr/bin/openshift-recycle"}
179-
template.Spec.Containers[0].Args = []string{"/scrub"}
180-
template.Spec.Containers[0].SecurityContext = &kapiv1.SecurityContext{RunAsUser: &uid}
181-
template.Spec.Containers[0].ImagePullPolicy = kapiv1.PullIfNotPresent
182-
183-
templateBytes, err := runtime.Encode(legacyscheme.Codecs.LegacyCodec(kapiv1.SchemeGroupVersion), template)
184-
if err != nil {
185-
return "", err
186-
}
187-
188-
f, err := ioutil.TempFile("", "openshift-recycler-template-")
189-
if err != nil {
190-
return "", err
191-
}
192-
filename := f.Name()
193-
glog.V(4).Infof("Creating file %s with recycler templates", filename)
194-
195-
_, err = f.Write(templateBytes)
196-
if err != nil {
197-
f.Close()
198-
os.Remove(filename)
199-
return "", err
200-
}
201-
f.Close()
202-
return filename, nil
203-
}
204-
205-
func runEmbeddedKubeControllerManager(kubeconfigFile, saPrivateKeyFile, saRootCAFile, podEvictionTimeout string, dynamicProvisioningEnabled bool, cmdLineArgs map[string][]string,
206-
recyclerImage string, informers *informers) {
207-
208-
// Overwrite the informers, because we have our custom generic informers for quota.
209-
// TODO update quota to create its own informer like garbage collection or if we split this out, actually add our external types to the kube generic informer
210-
controllerapp.InformerFactoryOverride = externalKubeInformersWithExtraGenerics{
211-
SharedInformerFactory: informers.GetExternalKubeInformers(),
212-
genericResourceInformer: informers.ToGenericInformer(),
213-
}
214-
89+
func runEmbeddedKubeControllerManager(kubeconfigFile, saPrivateKeyFile, saRootCAFile, podEvictionTimeout, openshiftConfigFile string, dynamicProvisioningEnabled bool) {
21590
// TODO we need a real identity for this. Right now it's just using the loopback connection like it used to.
216-
controllerManager, cleanupFunctions, err := newKubeControllerManager(kubeconfigFile, saPrivateKeyFile, saRootCAFile, podEvictionTimeout, recyclerImage, dynamicProvisioningEnabled, cmdLineArgs)
217-
defer func() {
218-
// Clean up any temporary files and similar stuff.
219-
// TODO: Make sure this defer is actually called - controllerapp.Run()
220-
// below never returns -> defer is not called.
221-
for _, f := range cleanupFunctions {
222-
f()
223-
}
224-
}()
225-
91+
controllerManager, err := newKubeControllerManager(kubeconfigFile, saPrivateKeyFile, saRootCAFile, podEvictionTimeout, openshiftConfigFile, dynamicProvisioningEnabled)
22692
if err != nil {
22793
glog.Fatal(err)
22894
}
229-
// this does a second leader election, but doing the second leader election will allow us to move out process in
230-
// 3.8 if we so choose.
23195
if err := controllerapp.Run(controllerManager); err != nil {
23296
glog.Fatal(err)
23397
}
23498
}
235-
236-
type externalKubeInformersWithExtraGenerics struct {
237-
kinformers.SharedInformerFactory
238-
genericResourceInformer GenericResourceInformer
239-
}
240-
241-
func (i externalKubeInformersWithExtraGenerics) ForResource(resource schema.GroupVersionResource) (kinformers.GenericInformer, error) {
242-
return i.genericResourceInformer.ForResource(resource)
243-
}
244-
245-
func (i externalKubeInformersWithExtraGenerics) Start(stopCh <-chan struct{}) {
246-
i.SharedInformerFactory.Start(stopCh)
247-
i.genericResourceInformer.Start(stopCh)
248-
}

pkg/cmd/server/start/start_master.go

+26-20
Original file line numberDiff line numberDiff line change
@@ -398,19 +398,12 @@ func (m *Master) Start() error {
398398
return err
399399
}
400400

401-
imageTemplate := variable.NewDefaultImageTemplate()
402-
imageTemplate.Format = m.config.ImageConfig.Format
403-
imageTemplate.Latest = m.config.ImageConfig.Latest
404-
recyclerImage := imageTemplate.ExpandOrDie("recycler")
405-
406401
// you can't double run healthz, so only do this next bit if we aren't starting the API
407402
if !m.api {
408-
409403
glog.Infof("Starting controllers on %s (%s)", m.config.ServingInfo.BindAddress, version.Get().String())
410404
if len(m.config.DisabledFeatures) > 0 {
411405
glog.V(4).Infof("Disabled features: %s", strings.Join(m.config.DisabledFeatures, ", "))
412406
}
413-
glog.Infof("Using images from %q", imageTemplate.ExpandOrDie("<component>"))
414407

415408
if err := origincontrollers.RunControllerServer(m.config.ServingInfo, clientGoKubeExternal); err != nil {
416409
return err
@@ -465,19 +458,32 @@ func (m *Master) Start() error {
465458

466459
go runEmbeddedScheduler(m.config.MasterClients.OpenShiftLoopbackKubeConfig, m.config.KubernetesMasterConfig.SchedulerConfigFile, m.config.KubernetesMasterConfig.SchedulerArguments)
467460

468-
kubeControllerInformers, err := NewInformers(*m.config)
469-
if err != nil {
470-
return err
471-
}
472-
go runEmbeddedKubeControllerManager(
473-
m.config.MasterClients.OpenShiftLoopbackKubeConfig,
474-
m.config.ServiceAccountConfig.PrivateKeyFile,
475-
m.config.ServiceAccountConfig.MasterCA,
476-
m.config.KubernetesMasterConfig.PodEvictionTimeout,
477-
m.config.VolumeConfig.DynamicProvisioningEnabled,
478-
m.config.KubernetesMasterConfig.ControllerArguments,
479-
recyclerImage,
480-
kubeControllerInformers)
461+
go func() {
462+
kubeControllerConfigBytes, err := configapilatest.WriteYAML(m.config)
463+
if err != nil {
464+
glog.Fatal(err)
465+
}
466+
// this creates using 0600
467+
kubeControllerConfigFile, err := ioutil.TempFile("", "openshift-kube-controler-manager-config.yaml")
468+
if err != nil {
469+
glog.Fatal(err)
470+
}
471+
defer func() {
472+
os.Remove(kubeControllerConfigFile.Name())
473+
}()
474+
if err := ioutil.WriteFile(kubeControllerConfigFile.Name(), kubeControllerConfigBytes, 0644); err != nil {
475+
glog.Fatal(err)
476+
}
477+
478+
runEmbeddedKubeControllerManager(
479+
m.config.MasterClients.OpenShiftLoopbackKubeConfig,
480+
m.config.ServiceAccountConfig.PrivateKeyFile,
481+
m.config.ServiceAccountConfig.MasterCA,
482+
m.config.KubernetesMasterConfig.PodEvictionTimeout,
483+
kubeControllerConfigFile.Name(),
484+
m.config.VolumeConfig.DynamicProvisioningEnabled,
485+
)
486+
}()
481487

482488
go func() {
483489
controllerPlug.WaitForStart()

vendor/k8s.io/kubernetes/cmd/cloud-controller-manager/app/BUILD

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/k8s.io/kubernetes/cmd/cloud-controller-manager/app/controllermanager.go

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/k8s.io/kubernetes/cmd/kube-controller-manager/app/BUILD

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/k8s.io/kubernetes/cmd/kube-controller-manager/app/controllermanager.go

+12-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/k8s.io/kubernetes/cmd/kube-controller-manager/app/options/options.go

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)