remove Initializer use from pv label controller

This commit is contained in:
Jordan Liggitt 2019-01-16 17:41:14 -05:00
parent 047ecae1f4
commit 7d398058f1
2 changed files with 27 additions and 80 deletions

View File

@ -44,8 +44,6 @@ import (
volumeutil "k8s.io/kubernetes/pkg/volume/util"
)
const initializerName = "pvlabel.kubernetes.io"
// PersistentVolumeLabelController handles adding labels to persistent volumes when they are created
type PersistentVolumeLabelController struct {
cloud cloudprovider.Interface
@ -74,11 +72,9 @@ func NewPersistentVolumeLabelController(
pvlc.pvlIndexer, pvlc.pvlController = cache.NewIndexerInformer(
&cache.ListWatch{
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
options.IncludeUninitialized = true
return kubeClient.CoreV1().PersistentVolumes().List(options)
},
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
options.IncludeUninitialized = true
return kubeClient.CoreV1().PersistentVolumes().Watch(options)
},
},
@ -184,7 +180,7 @@ func (pvlc *PersistentVolumeLabelController) addLabelsAndAffinity(key string) er
func (pvlc *PersistentVolumeLabelController) addLabelsAndAffinityToVolume(vol *v1.PersistentVolume) error {
var volumeLabels map[string]string
// Only add labels if the next pending initializer.
if needsInitialization(vol.Initializers, initializerName) {
if needsInitialization(vol) {
if labeler, ok := (pvlc.cloud).(cloudprovider.PVLabeler); ok {
labels, err := labeler.GetLabelsForVolume(context.TODO(), vol)
if err != nil {
@ -249,8 +245,8 @@ func (pvlc *PersistentVolumeLabelController) createPatch(vol *v1.PersistentVolum
}
}
}
newVolume.Initializers = removeInitializer(newVolume.Initializers, initializerName)
klog.V(4).Infof("removed initializer on PersistentVolume %s", newVolume.Name)
markInitialized(newVolume)
klog.V(4).Infof("marked PersistentVolume %s initialized", newVolume.Name)
oldData, err := json.Marshal(vol)
if err != nil {
@ -286,38 +282,13 @@ func (pvlc *PersistentVolumeLabelController) updateVolume(vol *v1.PersistentVolu
return nil
}
func removeInitializer(initializers *metav1.Initializers, name string) *metav1.Initializers {
if initializers == nil {
return nil
}
var updated []metav1.Initializer
for _, pending := range initializers.Pending {
if pending.Name != name {
updated = append(updated, pending)
}
}
if len(updated) == len(initializers.Pending) {
return initializers
}
if len(updated) == 0 {
return nil
}
return &metav1.Initializers{Pending: updated}
func markInitialized(vol *v1.PersistentVolume) {
// TODO: mark initialized using a different field, since initializers are not being promoted past alpha, or convert to an admission plugin
}
// needsInitialization checks whether or not the PVL is the next pending initializer.
func needsInitialization(initializers *metav1.Initializers, name string) bool {
if initializers == nil {
return false
}
if len(initializers.Pending) == 0 {
return false
}
// There is at least one initializer still pending so check to
// see if the PVL is the next in line.
return initializers.Pending[0].Name == name
func needsInitialization(vol *v1.PersistentVolume) bool {
// TODO: determine whether initialization is required based on a different attribute,
// since initializers are not being promoted past alpha, or convert to an admission plugin
return false
}

View File

@ -129,13 +129,6 @@ func TestCreatePatch(t *testing.T) {
ignoredPV := v1.PersistentVolume{
ObjectMeta: metav1.ObjectMeta{
Name: "noncloud",
Initializers: &metav1.Initializers{
Pending: []metav1.Initializer{
{
Name: initializerName,
},
},
},
},
Spec: v1.PersistentVolumeSpec{
PersistentVolumeSource: v1.PersistentVolumeSource{
@ -148,13 +141,6 @@ func TestCreatePatch(t *testing.T) {
awsPV := v1.PersistentVolume{
ObjectMeta: metav1.ObjectMeta{
Name: "awsPV",
Initializers: &metav1.Initializers{
Pending: []metav1.Initializer{
{
Name: initializerName,
},
},
},
},
Spec: v1.PersistentVolumeSpec{
PersistentVolumeSource: v1.PersistentVolumeSource{
@ -217,13 +203,6 @@ func TestCreatePatch(t *testing.T) {
awsPVWithAffinity := v1.PersistentVolume{
ObjectMeta: metav1.ObjectMeta{
Name: "awsPV",
Initializers: &metav1.Initializers{
Pending: []metav1.Initializer{
{
Name: initializerName,
},
},
},
},
Spec: v1.PersistentVolumeSpec{
PersistentVolumeSource: v1.PersistentVolumeSource{
@ -458,9 +437,12 @@ func TestCreatePatch(t *testing.T) {
}
obj := &v1.PersistentVolume{}
json.Unmarshal(patch, obj)
if obj.ObjectMeta.Initializers != nil {
t.Errorf("%s: initializer wasn't removed: %v", d, obj.ObjectMeta.Initializers)
}
// TODO: check if object was marked as initialized
// if ... object was not marked as initialized ... {
// t.Errorf("%s: wasn't marked as initialized: %#v", d, obj)
// }
if tc.labels == nil {
continue
}
@ -491,29 +473,24 @@ func TestAddLabelsToVolume(t *testing.T) {
testCases := map[string]struct {
vol v1.PersistentVolume
initializers *metav1.Initializers
shouldLabelAndSetAffinity bool
}{
"PV without initializer": {
vol: pv,
initializers: nil,
shouldLabelAndSetAffinity: false,
},
"PV with initializer to remove": {
vol: pv,
initializers: &metav1.Initializers{Pending: []metav1.Initializer{{Name: initializerName}}},
shouldLabelAndSetAffinity: true,
},
"PV with other initializers only": {
vol: pv,
initializers: &metav1.Initializers{Pending: []metav1.Initializer{{Name: "OtherInit"}}},
shouldLabelAndSetAffinity: false,
},
"PV with other initializers first": {
vol: pv,
initializers: &metav1.Initializers{Pending: []metav1.Initializer{{Name: "OtherInit"}, {Name: initializerName}}},
shouldLabelAndSetAffinity: false,
},
// "PV with initializer to remove": {
// vol: pv,
// shouldLabelAndSetAffinity: true,
// },
// "PV with other initializers only": {
// vol: pv,
// shouldLabelAndSetAffinity: false,
// },
// "PV with other initializers first": {
// vol: pv,
// shouldLabelAndSetAffinity: false,
// },
}
for d, tc := range testCases {
@ -550,7 +527,6 @@ func TestAddLabelsToVolume(t *testing.T) {
VolumeLabelMap: map[string]map[string]string{"awsPV": {"a": "1"}},
}
pvlController := &PersistentVolumeLabelController{kubeClient: client, cloud: fakeCloud}
tc.vol.ObjectMeta.Initializers = tc.initializers
pvlController.addLabelsAndAffinityToVolume(&tc.vol)
select {