mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-26 05:03:09 +00:00
remove Initializer use from pv label controller
This commit is contained in:
parent
047ecae1f4
commit
7d398058f1
@ -44,8 +44,6 @@ import (
|
|||||||
volumeutil "k8s.io/kubernetes/pkg/volume/util"
|
volumeutil "k8s.io/kubernetes/pkg/volume/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
const initializerName = "pvlabel.kubernetes.io"
|
|
||||||
|
|
||||||
// PersistentVolumeLabelController handles adding labels to persistent volumes when they are created
|
// PersistentVolumeLabelController handles adding labels to persistent volumes when they are created
|
||||||
type PersistentVolumeLabelController struct {
|
type PersistentVolumeLabelController struct {
|
||||||
cloud cloudprovider.Interface
|
cloud cloudprovider.Interface
|
||||||
@ -74,11 +72,9 @@ func NewPersistentVolumeLabelController(
|
|||||||
pvlc.pvlIndexer, pvlc.pvlController = cache.NewIndexerInformer(
|
pvlc.pvlIndexer, pvlc.pvlController = cache.NewIndexerInformer(
|
||||||
&cache.ListWatch{
|
&cache.ListWatch{
|
||||||
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
|
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
|
||||||
options.IncludeUninitialized = true
|
|
||||||
return kubeClient.CoreV1().PersistentVolumes().List(options)
|
return kubeClient.CoreV1().PersistentVolumes().List(options)
|
||||||
},
|
},
|
||||||
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
|
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
|
||||||
options.IncludeUninitialized = true
|
|
||||||
return kubeClient.CoreV1().PersistentVolumes().Watch(options)
|
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 {
|
func (pvlc *PersistentVolumeLabelController) addLabelsAndAffinityToVolume(vol *v1.PersistentVolume) error {
|
||||||
var volumeLabels map[string]string
|
var volumeLabels map[string]string
|
||||||
// Only add labels if the next pending initializer.
|
// Only add labels if the next pending initializer.
|
||||||
if needsInitialization(vol.Initializers, initializerName) {
|
if needsInitialization(vol) {
|
||||||
if labeler, ok := (pvlc.cloud).(cloudprovider.PVLabeler); ok {
|
if labeler, ok := (pvlc.cloud).(cloudprovider.PVLabeler); ok {
|
||||||
labels, err := labeler.GetLabelsForVolume(context.TODO(), vol)
|
labels, err := labeler.GetLabelsForVolume(context.TODO(), vol)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -249,8 +245,8 @@ func (pvlc *PersistentVolumeLabelController) createPatch(vol *v1.PersistentVolum
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
newVolume.Initializers = removeInitializer(newVolume.Initializers, initializerName)
|
markInitialized(newVolume)
|
||||||
klog.V(4).Infof("removed initializer on PersistentVolume %s", newVolume.Name)
|
klog.V(4).Infof("marked PersistentVolume %s initialized", newVolume.Name)
|
||||||
|
|
||||||
oldData, err := json.Marshal(vol)
|
oldData, err := json.Marshal(vol)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -286,38 +282,13 @@ func (pvlc *PersistentVolumeLabelController) updateVolume(vol *v1.PersistentVolu
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func removeInitializer(initializers *metav1.Initializers, name string) *metav1.Initializers {
|
func markInitialized(vol *v1.PersistentVolume) {
|
||||||
if initializers == nil {
|
// TODO: mark initialized using a different field, since initializers are not being promoted past alpha, or convert to an admission plugin
|
||||||
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}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// needsInitialization checks whether or not the PVL is the next pending initializer.
|
// needsInitialization checks whether or not the PVL is the next pending initializer.
|
||||||
func needsInitialization(initializers *metav1.Initializers, name string) bool {
|
func needsInitialization(vol *v1.PersistentVolume) bool {
|
||||||
if initializers == nil {
|
// TODO: determine whether initialization is required based on a different attribute,
|
||||||
return false
|
// since initializers are not being promoted past alpha, or convert to an admission plugin
|
||||||
}
|
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
|
|
||||||
}
|
}
|
||||||
|
@ -129,13 +129,6 @@ func TestCreatePatch(t *testing.T) {
|
|||||||
ignoredPV := v1.PersistentVolume{
|
ignoredPV := v1.PersistentVolume{
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
Name: "noncloud",
|
Name: "noncloud",
|
||||||
Initializers: &metav1.Initializers{
|
|
||||||
Pending: []metav1.Initializer{
|
|
||||||
{
|
|
||||||
Name: initializerName,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
Spec: v1.PersistentVolumeSpec{
|
Spec: v1.PersistentVolumeSpec{
|
||||||
PersistentVolumeSource: v1.PersistentVolumeSource{
|
PersistentVolumeSource: v1.PersistentVolumeSource{
|
||||||
@ -148,13 +141,6 @@ func TestCreatePatch(t *testing.T) {
|
|||||||
awsPV := v1.PersistentVolume{
|
awsPV := v1.PersistentVolume{
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
Name: "awsPV",
|
Name: "awsPV",
|
||||||
Initializers: &metav1.Initializers{
|
|
||||||
Pending: []metav1.Initializer{
|
|
||||||
{
|
|
||||||
Name: initializerName,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
Spec: v1.PersistentVolumeSpec{
|
Spec: v1.PersistentVolumeSpec{
|
||||||
PersistentVolumeSource: v1.PersistentVolumeSource{
|
PersistentVolumeSource: v1.PersistentVolumeSource{
|
||||||
@ -217,13 +203,6 @@ func TestCreatePatch(t *testing.T) {
|
|||||||
awsPVWithAffinity := v1.PersistentVolume{
|
awsPVWithAffinity := v1.PersistentVolume{
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
Name: "awsPV",
|
Name: "awsPV",
|
||||||
Initializers: &metav1.Initializers{
|
|
||||||
Pending: []metav1.Initializer{
|
|
||||||
{
|
|
||||||
Name: initializerName,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
Spec: v1.PersistentVolumeSpec{
|
Spec: v1.PersistentVolumeSpec{
|
||||||
PersistentVolumeSource: v1.PersistentVolumeSource{
|
PersistentVolumeSource: v1.PersistentVolumeSource{
|
||||||
@ -458,9 +437,12 @@ func TestCreatePatch(t *testing.T) {
|
|||||||
}
|
}
|
||||||
obj := &v1.PersistentVolume{}
|
obj := &v1.PersistentVolume{}
|
||||||
json.Unmarshal(patch, obj)
|
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 {
|
if tc.labels == nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -491,29 +473,24 @@ func TestAddLabelsToVolume(t *testing.T) {
|
|||||||
|
|
||||||
testCases := map[string]struct {
|
testCases := map[string]struct {
|
||||||
vol v1.PersistentVolume
|
vol v1.PersistentVolume
|
||||||
initializers *metav1.Initializers
|
|
||||||
shouldLabelAndSetAffinity bool
|
shouldLabelAndSetAffinity bool
|
||||||
}{
|
}{
|
||||||
"PV without initializer": {
|
"PV without initializer": {
|
||||||
vol: pv,
|
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,
|
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 {
|
for d, tc := range testCases {
|
||||||
@ -550,7 +527,6 @@ func TestAddLabelsToVolume(t *testing.T) {
|
|||||||
VolumeLabelMap: map[string]map[string]string{"awsPV": {"a": "1"}},
|
VolumeLabelMap: map[string]map[string]string{"awsPV": {"a": "1"}},
|
||||||
}
|
}
|
||||||
pvlController := &PersistentVolumeLabelController{kubeClient: client, cloud: fakeCloud}
|
pvlController := &PersistentVolumeLabelController{kubeClient: client, cloud: fakeCloud}
|
||||||
tc.vol.ObjectMeta.Initializers = tc.initializers
|
|
||||||
pvlController.addLabelsAndAffinityToVolume(&tc.vol)
|
pvlController.addLabelsAndAffinityToVolume(&tc.vol)
|
||||||
|
|
||||||
select {
|
select {
|
||||||
|
Loading…
Reference in New Issue
Block a user