diff --git a/pkg/manager/member/tidb_discovery_manager.go b/pkg/manager/member/tidb_discovery_manager.go index 1d6f056e3c6..f3627240931 100644 --- a/pkg/manager/member/tidb_discovery_manager.go +++ b/pkg/manager/member/tidb_discovery_manager.go @@ -14,6 +14,8 @@ package member import ( + "encoding/json" + "github.com/pingcap/tidb-operator/pkg/apis/pingcap/v1alpha1" "github.com/pingcap/tidb-operator/pkg/controller" "github.com/pingcap/tidb-operator/pkg/label" @@ -81,8 +83,11 @@ func (m *realTidbDiscoveryManager) Reconcile(tc *v1alpha1.TidbCluster) error { if err != nil { return controller.RequeueErrorf("error creating or updating discovery rolebinding: %v", err) } - - deploy, err := m.ctrl.CreateOrUpdateDeployment(tc, getTidbDiscoveryDeployment(tc)) + d, err := getTidbDiscoveryDeployment(tc) + if err != nil { + return controller.RequeueErrorf("error generating discovery deployment: %v", err) + } + deploy, err := m.ctrl.CreateOrUpdateDeployment(tc, d) if err != nil { return controller.RequeueErrorf("error creating or updating discovery service: %v", err) } @@ -111,9 +116,9 @@ func getTidbDiscoveryService(tc *v1alpha1.TidbCluster, deploy *appsv1.Deployment } } -func getTidbDiscoveryDeployment(tc *v1alpha1.TidbCluster) *appsv1.Deployment { +func getTidbDiscoveryDeployment(tc *v1alpha1.TidbCluster) (*appsv1.Deployment, error) { meta, l := getDiscoveryMeta(tc, controller.DiscoveryMemberName) - return &appsv1.Deployment{ + d := &appsv1.Deployment{ ObjectMeta: meta, Spec: appsv1.DeploymentSpec{ Replicas: controller.Int32Ptr(1), @@ -150,6 +155,15 @@ func getTidbDiscoveryDeployment(tc *v1alpha1.TidbCluster) *appsv1.Deployment { }, }, } + b, err := json.Marshal(d.Spec.Template.Spec) + if err != nil { + return nil, err + } + if d.Annotations == nil { + d.Annotations = map[string]string{} + } + d.Annotations[controller.LastAppliedPodTemplate] = string(b) + return d, nil } func getDiscoveryMeta(tc *v1alpha1.TidbCluster, nameFunc func(string) string) (metav1.ObjectMeta, label.Label) { diff --git a/pkg/monitor/monitor/monitor_manager.go b/pkg/monitor/monitor/monitor_manager.go index bfe2a2d58f5..23aa61a04b6 100644 --- a/pkg/monitor/monitor/monitor_manager.go +++ b/pkg/monitor/monitor/monitor_manager.go @@ -169,7 +169,11 @@ func (mm *MonitorManager) syncTidbMonitorDeployment(monitor *v1alpha1.TidbMonito return err } - deployment := getMonitorDeployment(sa, cm, secret, monitor, tc) + deployment, err := getMonitorDeployment(sa, cm, secret, monitor, tc) + if err != nil { + klog.Errorf("tm[%s/%s]'s deployment failed to generate,err: %v", monitor.Namespace, monitor.Name, err) + return err + } _, err = mm.typedControl.CreateOrUpdateDeployment(monitor, deployment) if err != nil { klog.Errorf("tm[%s/%s]'s deployment failed to sync,err: %v", monitor.Namespace, monitor.Name, err) diff --git a/pkg/monitor/monitor/util.go b/pkg/monitor/monitor/util.go index ddfc1b143f0..e319f41df23 100644 --- a/pkg/monitor/monitor/util.go +++ b/pkg/monitor/monitor/util.go @@ -14,6 +14,7 @@ package monitor import ( + "encoding/json" "fmt" "strconv" @@ -206,7 +207,7 @@ func getMonitorRoleBinding(sa *core.ServiceAccount, role *rbac.Role, monitor *v1 } } -func getMonitorDeployment(sa *core.ServiceAccount, config *core.ConfigMap, secret *core.Secret, monitor *v1alpha1.TidbMonitor, tc *v1alpha1.TidbCluster) *apps.Deployment { +func getMonitorDeployment(sa *core.ServiceAccount, config *core.ConfigMap, secret *core.Secret, monitor *v1alpha1.TidbMonitor, tc *v1alpha1.TidbCluster) (*apps.Deployment, error) { deployment := getMonitorDeploymentSkeleton(sa, monitor) initContainer := getMonitorInitContainer(monitor, tc) deployment.Spec.Template.Spec.InitContainers = append(deployment.Spec.Template.Spec.InitContainers, initContainer) @@ -219,7 +220,15 @@ func getMonitorDeployment(sa *core.ServiceAccount, config *core.ConfigMap, secre } volumes := getMonitorVolumes(config, monitor, tc) deployment.Spec.Template.Spec.Volumes = volumes - return deployment + b, err := json.Marshal(deployment.Spec.Template.Spec) + if err != nil { + return nil, err + } + if deployment.Annotations == nil { + deployment.Annotations = map[string]string{} + } + deployment.Annotations[controller.LastAppliedPodTemplate] = string(b) + return deployment, nil } func getMonitorDeploymentSkeleton(sa *core.ServiceAccount, monitor *v1alpha1.TidbMonitor) *apps.Deployment {