Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix TidbMonitor several error #1962

Merged
merged 12 commits into from
Mar 19, 2020
18 changes: 18 additions & 0 deletions pkg/controller/generic_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,26 @@ func (w *typedWrapper) CreateOrUpdateService(controller runtime.Object, svc *cor
}
existingSvc.Annotations[LastAppliedConfigAnnotation] = string(b)
clusterIp := existingSvc.Spec.ClusterIP
ports := existingSvc.Spec.Ports
serviceType := existingSvc.Spec.Type

existingSvc.Spec = desiredSvc.Spec
existingSvc.Spec.ClusterIP = clusterIp

// If the existed service and the desired service is both NodePort, we shall guaranteed the nodePort unchanged
if serviceType == corev1.ServiceTypeNodePort && desiredSvc.Spec.Type == corev1.ServiceTypeNodePort {
for i, dport := range existingSvc.Spec.Ports {
for _, eport := range ports {
// Because the portName could be edited,
// we use Port number to link the desired Service Port and the existed Service Port in the nested loop
if dport.Port == eport.Port {
Yisaer marked this conversation as resolved.
Show resolved Hide resolved
dport.NodePort = eport.NodePort
existingSvc.Spec.Ports[i] = dport
DanielZhangQD marked this conversation as resolved.
Show resolved Hide resolved
break
}
}
}
}
}
return nil
})
Expand Down
6 changes: 4 additions & 2 deletions pkg/monitor/monitor/monitor_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
type MonitorManager struct {
typedControl controller.TypedControlInterface
deploymentLister appslisters.DeploymentLister
svcLister corelisters.ServiceLister
Yisaer marked this conversation as resolved.
Show resolved Hide resolved
tcLister v1alpha1listers.TidbClusterLister
pvLister corelisters.PersistentVolumeLister
pvControl controller.PVControlInterface
Expand All @@ -54,6 +55,7 @@ func NewMonitorManager(
return &MonitorManager{
typedControl: typedControl,
deploymentLister: kubeInformerFactory.Apps().V1().Deployments().Lister(),
svcLister: kubeInformerFactory.Core().V1().Services().Lister(),
tcLister: informerFactory.Pingcap().V1alpha1().TidbClusters().Lister(),
pvControl: controller.NewRealPVControl(kubeCli, pvcLister, pvLister, recorder),
pvLister: pvLister,
Expand Down Expand Up @@ -105,8 +107,8 @@ func (mm *MonitorManager) Sync(monitor *v1alpha1.TidbMonitor) error {
}

func (mm *MonitorManager) syncTidbMonitorService(monitor *v1alpha1.TidbMonitor) error {
service := getMonitorService(monitor)
for _, svc := range service {
services := getMonitorService(monitor)
for _, svc := range services {
_, err := mm.typedControl.CreateOrUpdateService(monitor, svc)
if err != nil {
klog.Errorf("tm[%s/%s]'s service[%s] failed to sync,err: %v", monitor.Namespace, monitor.Name, svc.Name, err)
Expand Down
49 changes: 47 additions & 2 deletions pkg/monitor/monitor/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"encoding/json"
"fmt"
"github.com/pingcap/tidb-operator/pkg/util"
"sort"
"strconv"

"github.com/pingcap/tidb-operator/pkg/apis/pingcap/v1alpha1"
Expand Down Expand Up @@ -513,6 +514,7 @@ func getMonitorGrafanaContainer(secret *core.Secret, monitor *v1alpha1.TidbMonit
if monitor.Spec.Grafana.ImagePullPolicy != nil {
c.ImagePullPolicy = *monitor.Spec.Grafana.ImagePullPolicy
}
c.Env = sortEnvByName(c.Env)
return c
}

Expand Down Expand Up @@ -668,9 +670,10 @@ func getMonitorService(monitor *v1alpha1.TidbMonitor) []*core.Service {
grafanaPortName = *monitor.BaseGrafanaSpec().PortName()
}

promethuesName := fmt.Sprintf("%s-prometheus", monitor.Name)
prometheusService := &core.Service{
ObjectMeta: meta.ObjectMeta{
Name: fmt.Sprintf("%s-prometheus", monitor.Name),
Name: promethuesName,
Namespace: monitor.Namespace,
Labels: monitorLabel,
OwnerReferences: []meta.OwnerReference{controller.GetTiDBMonitorOwnerRef(monitor)},
Expand All @@ -689,10 +692,15 @@ func getMonitorService(monitor *v1alpha1.TidbMonitor) []*core.Service {
Selector: labels,
},
}
if monitor.BasePrometheusSpec().ServiceType() == core.ServiceTypeLoadBalancer {
if monitor.Spec.Prometheus.Service.LoadBalancerIP != nil {
prometheusService.Spec.LoadBalancerIP = *monitor.Spec.Prometheus.Service.LoadBalancerIP
}
}

reloaderService := &core.Service{
ObjectMeta: meta.ObjectMeta{
Name: fmt.Sprintf("%s-reloader", monitor.Name),
Name: fmt.Sprintf("%s-monitor-reloader", monitor.Name),
Namespace: monitor.Namespace,
Labels: monitorLabel,
OwnerReferences: []meta.OwnerReference{controller.GetTiDBMonitorOwnerRef(monitor)},
Expand All @@ -715,6 +723,12 @@ func getMonitorService(monitor *v1alpha1.TidbMonitor) []*core.Service {
},
}

if monitor.BaseReloaderSpec().ServiceType() == core.ServiceTypeLoadBalancer {
if monitor.Spec.Reloader.Service.LoadBalancerIP != nil {
reloaderService.Spec.LoadBalancerIP = *monitor.Spec.Reloader.Service.LoadBalancerIP
}
}

services = append(services, prometheusService, reloaderService)
if monitor.Spec.Grafana != nil {
grafanaService := &core.Service{
Expand All @@ -741,6 +755,13 @@ func getMonitorService(monitor *v1alpha1.TidbMonitor) []*core.Service {
},
},
}

if monitor.BaseGrafanaSpec().ServiceType() == core.ServiceTypeLoadBalancer {
if monitor.Spec.Grafana.Service.LoadBalancerIP != nil {
grafanaService.Spec.LoadBalancerIP = *monitor.Spec.Grafana.Service.LoadBalancerIP
}
}

services = append(services, grafanaService)
}
return services
Expand Down Expand Up @@ -770,3 +791,27 @@ func getMonitorPVC(monitor *v1alpha1.TidbMonitor) *core.PersistentVolumeClaim {
},
}
}

// sortEnvByName in order to avoid syncing same template into different results
func sortEnvByName(envlist []core.EnvVar) []core.EnvVar {
if envlist == nil || len(envlist) < 1 {
return envlist
}
var wrappers EnvListWrapper
wrappers = envlist
sort.Sort(wrappers)
return wrappers
}

type EnvListWrapper []core.EnvVar

func (e EnvListWrapper) Len() int {
return len(e)
}
func (e EnvListWrapper) Swap(i, j int) {
e[i], e[j] = e[j], e[i]
}

func (e EnvListWrapper) Less(i, j int) bool {
return e[i].Name < e[j].Name
}