mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-04 09:49:50 +00:00
Ensure PVL controller is next pending initializer before labeling the PV
This commit is contained in:
parent
2175199646
commit
1f840944a6
@ -182,7 +182,7 @@ func (pvlc *PersistentVolumeLabelController) addLabels(key string) error {
|
|||||||
|
|
||||||
func (pvlc *PersistentVolumeLabelController) addLabelsToVolume(vol *v1.PersistentVolume) error {
|
func (pvlc *PersistentVolumeLabelController) addLabelsToVolume(vol *v1.PersistentVolume) error {
|
||||||
var volumeLabels map[string]string
|
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 needsInitialization(vol.Initializers, initializerName) {
|
||||||
if labeler, ok := (pvlc.cloud).(cloudprovider.PVLabeler); ok {
|
if labeler, ok := (pvlc.cloud).(cloudprovider.PVLabeler); ok {
|
||||||
labels, err := labeler.GetLabelsForVolume(vol)
|
labels, err := labeler.GetLabelsForVolume(vol)
|
||||||
@ -265,16 +265,17 @@ func removeInitializer(initializers *metav1.Initializers, name string) *metav1.I
|
|||||||
return &metav1.Initializers{Pending: updated}
|
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 {
|
func needsInitialization(initializers *metav1.Initializers, name string) bool {
|
||||||
hasInitializer := false
|
if initializers == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
if initializers != nil {
|
if len(initializers.Pending) == 0 {
|
||||||
for _, pending := range initializers.Pending {
|
return false
|
||||||
if pending.Name == name {
|
|
||||||
hasInitializer = true
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
// There is at least one initializer still pending so check to
|
||||||
return hasInitializer
|
// see if the PVL is the next in line.
|
||||||
|
return initializers.Pending[0].Name == name
|
||||||
}
|
}
|
||||||
|
@ -146,11 +146,16 @@ func TestAddLabelsToVolume(t *testing.T) {
|
|||||||
initializers: &metav1.Initializers{Pending: []metav1.Initializer{{Name: initializerName}}},
|
initializers: &metav1.Initializers{Pending: []metav1.Initializer{{Name: initializerName}}},
|
||||||
shouldLabel: true,
|
shouldLabel: true,
|
||||||
},
|
},
|
||||||
"PV with other initializers": {
|
"PV with other initializers only": {
|
||||||
vol: pv,
|
vol: pv,
|
||||||
initializers: &metav1.Initializers{Pending: []metav1.Initializer{{Name: "OtherInit"}}},
|
initializers: &metav1.Initializers{Pending: []metav1.Initializer{{Name: "OtherInit"}}},
|
||||||
shouldLabel: false,
|
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 {
|
for d, tc := range testCases {
|
||||||
|
Loading…
Reference in New Issue
Block a user