Commit 79b91b9e authored by Jan Safranek's avatar Jan Safranek

Refactor persistent volume initialization

There should be only one initialization function, shared by the real controller and unit tests.
parent 7f549511
...@@ -385,6 +385,7 @@ func StartControllers(s *options.CMServer, kubeClient *client.Client, kubeconfig ...@@ -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))
......
...@@ -283,6 +283,9 @@ func (s *CMServer) Run(_ []string) error { ...@@ -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()
......
...@@ -46,15 +46,20 @@ func NewPersistentVolumeController( ...@@ -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,
broadcaster := record.NewBroadcaster() eventRecorder record.EventRecorder,
broadcaster.StartRecordingToSink(&unversioned_core.EventSinkImpl{Interface: kubeClient.Core().Events("")}) ) *PersistentVolumeController {
recorder := broadcaster.NewRecorder(api.EventSource{Component: "persistentvolume-controller"})
if eventRecorder == nil {
broadcaster := record.NewBroadcaster()
broadcaster.StartRecordingToSink(&unversioned_core.EventSinkImpl{Interface: kubeClient.Core().Events("")})
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( ...@@ -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,56 +75,50 @@ func NewPersistentVolumeController( ...@@ -69,56 +75,50 @@ func NewPersistentVolumeController(
} }
} }
volumeSource := &cache.ListWatch{ if volumeSource == nil {
ListFunc: func(options api.ListOptions) (runtime.Object, error) { volumeSource = &cache.ListWatch{
return kubeClient.Core().PersistentVolumes().List(options) ListFunc: func(options api.ListOptions) (runtime.Object, error) {
}, return kubeClient.Core().PersistentVolumes().List(options)
WatchFunc: func(options api.ListOptions) (watch.Interface, error) { },
return kubeClient.Core().PersistentVolumes().Watch(options) WatchFunc: func(options api.ListOptions) (watch.Interface, error) {
}, return kubeClient.Core().PersistentVolumes().Watch(options)
},
}
} }
claimSource := &cache.ListWatch{ if claimSource == nil {
ListFunc: func(options api.ListOptions) (runtime.Object, error) { claimSource = &cache.ListWatch{
return kubeClient.Core().PersistentVolumeClaims(api.NamespaceAll).List(options) ListFunc: func(options api.ListOptions) (runtime.Object, error) {
}, return kubeClient.Core().PersistentVolumeClaims(api.NamespaceAll).List(options)
WatchFunc: func(options api.ListOptions) (watch.Interface, error) { },
return kubeClient.Core().PersistentVolumeClaims(api.NamespaceAll).Watch(options) WatchFunc: func(options api.ListOptions) (watch.Interface, error) {
}, 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
......
...@@ -128,7 +128,7 @@ func TestControllerSync(t *testing.T) { ...@@ -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)
......
...@@ -484,27 +484,27 @@ func newVolumeReactor(client *fake.Clientset, ctrl *PersistentVolumeController, ...@@ -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) { ...@@ -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) { ...@@ -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)
......
...@@ -55,7 +55,7 @@ func TestPersistentVolumeRecycler(t *testing.T) { ...@@ -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()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment