From cd5ef393bf5d7b3d81674b88e308d808632b09e4 Mon Sep 17 00:00:00 2001 From: Song Gao <2695690803@qq.com> Date: Mon, 15 Jun 2020 14:37:18 +0800 Subject: [PATCH 1/2] fix labelselector error --- pkg/controller/pv_control.go | 12 ++++++++-- pkg/manager/meta/reclaim_policy_manager.go | 28 +++++++++++++++++----- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/pkg/controller/pv_control.go b/pkg/controller/pv_control.go index c76562560f..1eaf27e227 100644 --- a/pkg/controller/pv_control.go +++ b/pkg/controller/pv_control.go @@ -69,9 +69,17 @@ func (rpc *realPVControl) PatchPVReclaimPolicy(obj runtime.Object, pv *corev1.Pe name := metaObj.GetName() pvName := pv.GetName() - patchBytes := []byte(fmt.Sprintf(`{"spec":{"persistentVolumeReclaimPolicy":"%s"}}`, reclaimPolicy)) - err := retry.RetryOnConflict(retry.DefaultBackoff, func() error { + latestPV, err := rpc.kubeCli.CoreV1().PersistentVolumes().Get(pvName, metav1.GetOptions{}) + if err != nil { + return err + } + if string(latestPV.Spec.PersistentVolumeReclaimPolicy) == string(reclaimPolicy) { + return nil + } + patchBytes := []byte(fmt.Sprintf(`{"spec":{"persistentVolumeReclaimPolicy":"%s"}}`, reclaimPolicy)) + + err = retry.RetryOnConflict(retry.DefaultBackoff, func() error { _, err := rpc.kubeCli.CoreV1().PersistentVolumes().Patch(pvName, types.StrategicMergePatchType, patchBytes) return err }) diff --git a/pkg/manager/meta/reclaim_policy_manager.go b/pkg/manager/meta/reclaim_policy_manager.go index 9fba256589..fe0625215f 100644 --- a/pkg/manager/meta/reclaim_policy_manager.go +++ b/pkg/manager/meta/reclaim_policy_manager.go @@ -14,12 +14,15 @@ package meta import ( + "fmt" + "github.com/pingcap/tidb-operator/pkg/apis/pingcap/v1alpha1" "github.com/pingcap/tidb-operator/pkg/controller" "github.com/pingcap/tidb-operator/pkg/label" "github.com/pingcap/tidb-operator/pkg/manager" "github.com/pingcap/tidb-operator/pkg/monitor" corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/runtime" corelisters "k8s.io/client-go/listers/core/v1" ) @@ -53,18 +56,31 @@ func NewReclaimPolicyMonitorManager(pvcLister corelisters.PersistentVolumeClaimL } func (rpm *reclaimPolicyManager) Sync(tc *v1alpha1.TidbCluster) error { - return rpm.sync(tc.GetNamespace(), tc.GetInstanceName(), tc.IsPVReclaimEnabled(), tc.Spec.PVReclaimPolicy, tc) + return rpm.sync(v1alpha1.TiDBClusterKind, tc.GetNamespace(), tc.GetInstanceName(), tc.IsPVReclaimEnabled(), tc.Spec.PVReclaimPolicy, tc) } func (rpm *reclaimPolicyManager) SyncMonitor(tm *v1alpha1.TidbMonitor) error { - return rpm.sync(tm.GetNamespace(), tm.GetName(), false, tm.Spec.PVReclaimPolicy, tm) + return rpm.sync(v1alpha1.TiDBMonitorKind, tm.GetNamespace(), tm.GetName(), false, tm.Spec.PVReclaimPolicy, tm) } -func (rpm *reclaimPolicyManager) sync(ns, instanceName string, isPVReclaimEnabled bool, policy corev1.PersistentVolumeReclaimPolicy, obj runtime.Object) error { - l, err := label.New().Instance(instanceName).Selector() - if err != nil { - return err +func (rpm *reclaimPolicyManager) sync(kind, ns, instanceName string, isPVReclaimEnabled bool, policy corev1.PersistentVolumeReclaimPolicy, obj runtime.Object) error { + var l labels.Selector + var err error + switch kind { + case v1alpha1.TiDBMonitorKind: + l, err = label.NewMonitor().Instance(instanceName).Monitor().Selector() + if err != nil { + return err + } + case v1alpha1.TiDBClusterKind: + l, err = label.New().Instance(instanceName).Selector() + if err != nil { + return err + } + default: + return fmt.Errorf("unknown kind = %s", kind) } + pvcs, err := rpm.pvcLister.PersistentVolumeClaims(ns).List(l) if err != nil { return err From 9c0d9fb0a972ccb8830e917c3627c6e9eaad7d42 Mon Sep 17 00:00:00 2001 From: Song Gao <2695690803@qq.com> Date: Mon, 15 Jun 2020 14:46:00 +0800 Subject: [PATCH 2/2] remove unnecessary check --- pkg/controller/pv_control.go | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/pkg/controller/pv_control.go b/pkg/controller/pv_control.go index 1eaf27e227..c76562560f 100644 --- a/pkg/controller/pv_control.go +++ b/pkg/controller/pv_control.go @@ -69,17 +69,9 @@ func (rpc *realPVControl) PatchPVReclaimPolicy(obj runtime.Object, pv *corev1.Pe name := metaObj.GetName() pvName := pv.GetName() - - latestPV, err := rpc.kubeCli.CoreV1().PersistentVolumes().Get(pvName, metav1.GetOptions{}) - if err != nil { - return err - } - if string(latestPV.Spec.PersistentVolumeReclaimPolicy) == string(reclaimPolicy) { - return nil - } patchBytes := []byte(fmt.Sprintf(`{"spec":{"persistentVolumeReclaimPolicy":"%s"}}`, reclaimPolicy)) - - err = retry.RetryOnConflict(retry.DefaultBackoff, func() error { + + err := retry.RetryOnConflict(retry.DefaultBackoff, func() error { _, err := rpc.kubeCli.CoreV1().PersistentVolumes().Patch(pvName, types.StrategicMergePatchType, patchBytes) return err })