From 0b21296d9532b07647c6df12639a0627ba77b906 Mon Sep 17 00:00:00 2001 From: pingcap-github-bot Date: Wed, 10 Jun 2020 13:50:37 +0800 Subject: [PATCH] Emit an event if failed to sync labels to tikv stores (#2587) (#2675) Signed-off-by: sre-bot Co-authored-by: jipeng --- pkg/controller/tidbcluster/tidb_cluster_controller.go | 1 + pkg/manager/member/failover.go | 1 + pkg/manager/member/tikv_member_manager.go | 10 ++++++++-- pkg/manager/member/tikv_member_manager_test.go | 3 +++ 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/pkg/controller/tidbcluster/tidb_cluster_controller.go b/pkg/controller/tidbcluster/tidb_cluster_controller.go index 3b5d0ec24a..e8d8cce238 100644 --- a/pkg/controller/tidbcluster/tidb_cluster_controller.go +++ b/pkg/controller/tidbcluster/tidb_cluster_controller.go @@ -151,6 +151,7 @@ func NewController( tikvFailover, tikvScaler, tikvUpgrader, + recorder, ), mm.NewTiDBMemberManager( setControl, diff --git a/pkg/manager/member/failover.go b/pkg/manager/member/failover.go index 7a71f1015e..e1e3749a41 100644 --- a/pkg/manager/member/failover.go +++ b/pkg/manager/member/failover.go @@ -18,6 +18,7 @@ import "github.com/pingcap/tidb-operator/pkg/apis/pingcap/v1alpha1" const ( unHealthEventReason = "Unhealthy" unHealthEventMsgPattern = "%s pod[%s] is unhealthy, msg:%s" + FailedSetStoreLabels = "FailedSetStoreLabels" ) // Failover implements the logic for pd/tikv/tidb's failover and recovery. diff --git a/pkg/manager/member/tikv_member_manager.go b/pkg/manager/member/tikv_member_manager.go index a60b16738b..55e99e5717 100644 --- a/pkg/manager/member/tikv_member_manager.go +++ b/pkg/manager/member/tikv_member_manager.go @@ -36,6 +36,7 @@ import ( "k8s.io/apimachinery/pkg/util/uuid" v1 "k8s.io/client-go/listers/apps/v1" corelisters "k8s.io/client-go/listers/core/v1" + "k8s.io/client-go/tools/record" "k8s.io/klog" "k8s.io/utils/pointer" ) @@ -62,6 +63,7 @@ type tikvMemberManager struct { tikvFailover Failover tikvScaler Scaler tikvUpgrader Upgrader + recorder record.EventRecorder tikvStatefulSetIsUpgradingFn func(corelisters.PodLister, pdapi.PDControlInterface, *apps.StatefulSet, *v1alpha1.TidbCluster) (bool, error) } @@ -78,7 +80,8 @@ func NewTiKVMemberManager( autoFailover bool, tikvFailover Failover, tikvScaler Scaler, - tikvUpgrader Upgrader) manager.Manager { + tikvUpgrader Upgrader, + recorder record.EventRecorder) manager.Manager { kvmm := tikvMemberManager{ pdControl: pdControl, podLister: podLister, @@ -92,6 +95,7 @@ func NewTiKVMemberManager( tikvFailover: tikvFailover, tikvScaler: tikvScaler, tikvUpgrader: tikvUpgrader, + recorder: recorder, } kvmm.tikvStatefulSetIsUpgradingFn = tikvStatefulSetIsUpgrading return &kvmm @@ -764,7 +768,9 @@ func (tkmm *tikvMemberManager) setStoreLabelsForTiKV(tc *v1alpha1.TidbCluster) ( if !tkmm.storeLabelsEqualNodeLabels(store.Store.Labels, ls) { set, err := pdCli.SetStoreLabels(store.Store.Id, ls) if err != nil { - klog.Warningf("failed to set pod: [%s/%s]'s store labels: %v", ns, podName, ls) + msg := fmt.Sprintf("failed to set labels %v for store (id: %d, pod: %s/%s): %v ", + ls, store.Store.Id, ns, podName, err) + tkmm.recorder.Event(tc, corev1.EventTypeWarning, FailedSetStoreLabels, msg) continue } if set { diff --git a/pkg/manager/member/tikv_member_manager_test.go b/pkg/manager/member/tikv_member_manager_test.go index 8699b49f01..1ddbaa822b 100644 --- a/pkg/manager/member/tikv_member_manager_test.go +++ b/pkg/manager/member/tikv_member_manager_test.go @@ -39,6 +39,7 @@ import ( kubefake "k8s.io/client-go/kubernetes/fake" corelisters "k8s.io/client-go/listers/core/v1" "k8s.io/client-go/tools/cache" + "k8s.io/client-go/tools/record" "k8s.io/utils/pointer" ) @@ -1454,6 +1455,7 @@ func newFakeTiKVMemberManager(tc *v1alpha1.TidbCluster) ( nodeInformer := kubeinformers.NewSharedInformerFactory(kubeCli, 0).Core().V1().Nodes() tikvScaler := NewFakeTiKVScaler() tikvUpgrader := NewFakeTiKVUpgrader() + recorder := record.NewFakeRecorder(10) genericControl := controller.NewFakeGenericControl() tmm := &tikvMemberManager{ @@ -1467,6 +1469,7 @@ func newFakeTiKVMemberManager(tc *v1alpha1.TidbCluster) ( svcLister: svcInformer.Lister(), tikvScaler: tikvScaler, tikvUpgrader: tikvUpgrader, + recorder: recorder, } tmm.tikvStatefulSetIsUpgradingFn = tikvStatefulSetIsUpgrading return tmm, setControl, svcControl, pdClient, podInformer.Informer().GetIndexer(), nodeInformer.Informer().GetIndexer()