Merge pull request #56831 from oracle/for/upstream/master/56830

Automatic merge from submit-queue (batch tested with PRs 54902, 56831, 56702, 56287, 56878). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

Ensure PVL controller is next pending initializer before labeling the PV

**What this PR does / why we need it**:

According the [documentation](https://kubernetes.io/docs/admin/extensible-admission-controllers/#how-are-initializers-triggered), initializer controllers should only initialize the object once its name is at `metadata.initializers.pending[0]`.[Currently](https://github.com/kubernetes/kubernetes/blob/master/pkg/controller/cloud/pvlcontroller.go#L268), the PVL controller just checks if its name is in the list at all and ignores ordering. 

**Which issue this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close that issue when PR gets merged)*: fixes #56830

**Special notes for your reviewer**:

**Release note**:

```release-note
Fix the PersistentVolumeLabel controller from initializing the PV labels when it's not the next pending initializer.
```

/kind bug
/sig storage
/area cloudprovider

/cc @wlan0 @luxas @liggitt
This commit is contained in:
Kubernetes Submit Queue 2017-12-16 09:33:36 -08:00 committed by GitHub
commit 77333e95e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 12 deletions

View File

@ -182,7 +182,7 @@ func (pvlc *PersistentVolumeLabelController) addLabels(key string) error {
func (pvlc *PersistentVolumeLabelController) addLabelsToVolume(vol *v1.PersistentVolume) error {
var volumeLabels map[string]string
// Only add labels if in the list of initializers
// Only add labels if the next pending initializer.
if needsInitialization(vol.Initializers, initializerName) {
if labeler, ok := (pvlc.cloud).(cloudprovider.PVLabeler); ok {
labels, err := labeler.GetLabelsForVolume(vol)
@ -265,16 +265,17 @@ func removeInitializer(initializers *metav1.Initializers, name string) *metav1.I
return &metav1.Initializers{Pending: updated}
}
// needsInitialization checks whether or not the PVL is the next pending initializer.
func needsInitialization(initializers *metav1.Initializers, name string) bool {
hasInitializer := false
if initializers != nil {
for _, pending := range initializers.Pending {
if pending.Name == name {
hasInitializer = true
break
}
}
if initializers == nil {
return false
}
return hasInitializer
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
}

View File

@ -146,11 +146,16 @@ func TestAddLabelsToVolume(t *testing.T) {
initializers: &metav1.Initializers{Pending: []metav1.Initializer{{Name: initializerName}}},
shouldLabel: true,
},
"PV with other initializers": {
"PV with other initializers only": {
vol: pv,
initializers: &metav1.Initializers{Pending: []metav1.Initializer{{Name: "OtherInit"}}},
shouldLabel: false,
},
"PV with other initializers first": {
vol: pv,
initializers: &metav1.Initializers{Pending: []metav1.Initializer{{Name: "OtherInit"}, {Name: initializerName}}},
shouldLabel: false,
},
}
for d, tc := range testCases {