Refactor persistent volume initialization

There should be only one initialization function, shared by the real
controller and unit tests.
This commit is contained in:
Jan Safranek 2016-05-17 14:55:34 +02:00
parent 7f549511e2
commit 79b91b9ee0
6 changed files with 61 additions and 57 deletions

View File

@ -385,6 +385,7 @@ func StartControllers(s *options.CMServer, kubeClient *client.Client, kubeconfig
ProbeRecyclableVolumePlugins(s.VolumeConfiguration), ProbeRecyclableVolumePlugins(s.VolumeConfiguration),
cloud, cloud,
s.ClusterName, s.ClusterName,
nil, nil, nil,
) )
volumeController.Run() volumeController.Run()
time.Sleep(wait.Jitter(s.ControllerStartInterval.Duration, ControllerStartJitter)) time.Sleep(wait.Jitter(s.ControllerStartInterval.Duration, ControllerStartJitter))

View File

@ -283,6 +283,9 @@ func (s *CMServer) Run(_ []string) error {
kubecontrollermanager.ProbeRecyclableVolumePlugins(s.VolumeConfiguration), kubecontrollermanager.ProbeRecyclableVolumePlugins(s.VolumeConfiguration),
cloud, cloud,
s.ClusterName, s.ClusterName,
nil,
nil,
nil,
) )
volumeController.Run() volumeController.Run()

View File

@ -46,15 +46,20 @@ func NewPersistentVolumeController(
provisioner vol.ProvisionableVolumePlugin, provisioner vol.ProvisionableVolumePlugin,
recyclers []vol.VolumePlugin, recyclers []vol.VolumePlugin,
cloud cloudprovider.Interface, cloud cloudprovider.Interface,
clusterName string) *PersistentVolumeController { clusterName string,
volumeSource, claimSource cache.ListerWatcher,
eventRecorder record.EventRecorder,
) *PersistentVolumeController {
if eventRecorder == nil {
broadcaster := record.NewBroadcaster() broadcaster := record.NewBroadcaster()
broadcaster.StartRecordingToSink(&unversioned_core.EventSinkImpl{Interface: kubeClient.Core().Events("")}) broadcaster.StartRecordingToSink(&unversioned_core.EventSinkImpl{Interface: kubeClient.Core().Events("")})
recorder := broadcaster.NewRecorder(api.EventSource{Component: "persistentvolume-controller"}) eventRecorder = broadcaster.NewRecorder(api.EventSource{Component: "persistentvolume-controller"})
}
controller := &PersistentVolumeController{ controller := &PersistentVolumeController{
kubeClient: kubeClient, kubeClient: kubeClient,
eventRecorder: recorder, eventRecorder: eventRecorder,
runningOperations: make(map[string]bool), runningOperations: make(map[string]bool),
cloud: cloud, cloud: cloud,
provisioner: provisioner, provisioner: provisioner,
@ -62,6 +67,7 @@ func NewPersistentVolumeController(
createProvisionedPVRetryCount: createProvisionedPVRetryCount, createProvisionedPVRetryCount: createProvisionedPVRetryCount,
createProvisionedPVInterval: createProvisionedPVInterval, createProvisionedPVInterval: createProvisionedPVInterval,
} }
controller.recyclePluginMgr.InitPlugins(recyclers, controller) controller.recyclePluginMgr.InitPlugins(recyclers, controller)
if controller.provisioner != nil { if controller.provisioner != nil {
if err := controller.provisioner.Init(controller); err != nil { if err := controller.provisioner.Init(controller); err != nil {
@ -69,7 +75,8 @@ func NewPersistentVolumeController(
} }
} }
volumeSource := &cache.ListWatch{ if volumeSource == nil {
volumeSource = &cache.ListWatch{
ListFunc: func(options api.ListOptions) (runtime.Object, error) { ListFunc: func(options api.ListOptions) (runtime.Object, error) {
return kubeClient.Core().PersistentVolumes().List(options) return kubeClient.Core().PersistentVolumes().List(options)
}, },
@ -77,8 +84,10 @@ func NewPersistentVolumeController(
return kubeClient.Core().PersistentVolumes().Watch(options) return kubeClient.Core().PersistentVolumes().Watch(options)
}, },
} }
}
claimSource := &cache.ListWatch{ if claimSource == nil {
claimSource = &cache.ListWatch{
ListFunc: func(options api.ListOptions) (runtime.Object, error) { ListFunc: func(options api.ListOptions) (runtime.Object, error) {
return kubeClient.Core().PersistentVolumeClaims(api.NamespaceAll).List(options) return kubeClient.Core().PersistentVolumeClaims(api.NamespaceAll).List(options)
}, },
@ -86,39 +95,30 @@ func NewPersistentVolumeController(
return kubeClient.Core().PersistentVolumeClaims(api.NamespaceAll).Watch(options) return kubeClient.Core().PersistentVolumeClaims(api.NamespaceAll).Watch(options)
}, },
} }
}
controller.initializeController(syncPeriod, volumeSource, claimSource) controller.volumes.store, controller.volumeController = framework.NewIndexerInformer(
return controller
}
// initializeController prepares watching for PersistentVolume and
// PersistentVolumeClaim events from given sources. This should be used to
// initialize the controller for real operation (with real event sources) and
// also during testing (with fake ones).
func (ctrl *PersistentVolumeController) initializeController(syncPeriod time.Duration, volumeSource, claimSource cache.ListerWatcher) {
glog.V(4).Infof("initializing PersistentVolumeController, sync every %s", syncPeriod.String())
ctrl.volumes.store, ctrl.volumeController = framework.NewIndexerInformer(
volumeSource, volumeSource,
&api.PersistentVolume{}, &api.PersistentVolume{},
syncPeriod, syncPeriod,
framework.ResourceEventHandlerFuncs{ framework.ResourceEventHandlerFuncs{
AddFunc: ctrl.addVolume, AddFunc: controller.addVolume,
UpdateFunc: ctrl.updateVolume, UpdateFunc: controller.updateVolume,
DeleteFunc: ctrl.deleteVolume, DeleteFunc: controller.deleteVolume,
}, },
cache.Indexers{"accessmodes": accessModesIndexFunc}, cache.Indexers{"accessmodes": accessModesIndexFunc},
) )
ctrl.claims, ctrl.claimController = framework.NewInformer( controller.claims, controller.claimController = framework.NewInformer(
claimSource, claimSource,
&api.PersistentVolumeClaim{}, &api.PersistentVolumeClaim{},
syncPeriod, syncPeriod,
framework.ResourceEventHandlerFuncs{ framework.ResourceEventHandlerFuncs{
AddFunc: ctrl.addClaim, AddFunc: controller.addClaim,
UpdateFunc: ctrl.updateClaim, UpdateFunc: controller.updateClaim,
DeleteFunc: ctrl.deleteClaim, DeleteFunc: controller.deleteClaim,
}, },
) )
return controller
} }
// addVolume is callback from framework.Controller watching PersistentVolume // addVolume is callback from framework.Controller watching PersistentVolume

View File

@ -128,7 +128,7 @@ func TestControllerSync(t *testing.T) {
client := &fake.Clientset{} client := &fake.Clientset{}
volumeSource := framework.NewFakeControllerSource() volumeSource := framework.NewFakeControllerSource()
claimSource := framework.NewFakeControllerSource() claimSource := framework.NewFakeControllerSource()
ctrl := newPersistentVolumeController(client, volumeSource, claimSource) ctrl := newTestController(client, volumeSource, claimSource)
reactor := newVolumeReactor(client, ctrl, volumeSource, claimSource, test.errors) reactor := newVolumeReactor(client, ctrl, volumeSource, claimSource, test.errors)
for _, claim := range test.initialClaims { for _, claim := range test.initialClaims {
claimSource.Add(claim) claimSource.Add(claim)

View File

@ -484,27 +484,27 @@ func newVolumeReactor(client *fake.Clientset, ctrl *PersistentVolumeController,
return reactor return reactor
} }
func newPersistentVolumeController(kubeClient clientset.Interface, volumeSource, claimSource cache.ListerWatcher) *PersistentVolumeController { func newTestController(kubeClient clientset.Interface, volumeSource, claimSource cache.ListerWatcher) *PersistentVolumeController {
ctrl := &PersistentVolumeController{
volumes: newPersistentVolumeOrderedIndex(),
claims: cache.NewStore(cache.MetaNamespaceKeyFunc),
kubeClient: kubeClient,
eventRecorder: record.NewFakeRecorder(1000),
runningOperations: make(map[string]bool),
// Speed up the testing
createProvisionedPVRetryCount: createProvisionedPVRetryCount,
createProvisionedPVInterval: 5 * time.Millisecond,
}
// Create dummy volume/claim sources for controller watchers when needed
if volumeSource == nil { if volumeSource == nil {
volumeSource = framework.NewFakeControllerSource() volumeSource = framework.NewFakeControllerSource()
} }
if claimSource == nil { if claimSource == nil {
claimSource = framework.NewFakeControllerSource() claimSource = framework.NewFakeControllerSource()
} }
ctrl.initializeController(5*time.Second, volumeSource, claimSource) ctrl := NewPersistentVolumeController(
kubeClient,
5*time.Second, // sync period
nil, // provisioner
[]vol.VolumePlugin{}, // recyclers
nil, // cloud
"",
volumeSource,
claimSource,
record.NewFakeRecorder(1000), // event recorder
)
// Speed up the test
ctrl.createProvisionedPVInterval = 5 * time.Millisecond
return ctrl return ctrl
} }
@ -732,7 +732,7 @@ func runSyncTests(t *testing.T, tests []controllerTest) {
// Initialize the controller // Initialize the controller
client := &fake.Clientset{} client := &fake.Clientset{}
ctrl := newPersistentVolumeController(client, nil, nil) ctrl := newTestController(client, nil, nil)
reactor := newVolumeReactor(client, ctrl, nil, nil, test.errors) reactor := newVolumeReactor(client, ctrl, nil, nil, test.errors)
for _, claim := range test.initialClaims { for _, claim := range test.initialClaims {
ctrl.claims.Add(claim) ctrl.claims.Add(claim)
@ -776,7 +776,7 @@ func runMultisyncTests(t *testing.T, tests []controllerTest) {
// Initialize the controller // Initialize the controller
client := &fake.Clientset{} client := &fake.Clientset{}
ctrl := newPersistentVolumeController(client, nil, nil) ctrl := newTestController(client, nil, nil)
reactor := newVolumeReactor(client, ctrl, nil, nil, test.errors) reactor := newVolumeReactor(client, ctrl, nil, nil, test.errors)
for _, claim := range test.initialClaims { for _, claim := range test.initialClaims {
ctrl.claims.Add(claim) ctrl.claims.Add(claim)

View File

@ -55,7 +55,7 @@ func TestPersistentVolumeRecycler(t *testing.T) {
plugins := []volume.VolumePlugin{&volumetest.FakeVolumePlugin{"plugin-name", host, volume.VolumeConfig{}, volume.VolumeOptions{}, 0, 0, nil, nil, nil, nil}} plugins := []volume.VolumePlugin{&volumetest.FakeVolumePlugin{"plugin-name", host, volume.VolumeConfig{}, volume.VolumeOptions{}, 0, 0, nil, nil, nil, nil}}
cloud := &fake_cloud.FakeCloud{} cloud := &fake_cloud.FakeCloud{}
ctrl := persistentvolumecontroller.NewPersistentVolumeController(testClient, 10*time.Second, nil, plugins, cloud, "") ctrl := persistentvolumecontroller.NewPersistentVolumeController(testClient, 10*time.Second, nil, plugins, cloud, "", nil, nil, nil)
ctrl.Run() ctrl.Run()
defer ctrl.Stop() defer ctrl.Stop()