From 97d07a1caa75799b649632e5709eb4d123251dcc Mon Sep 17 00:00:00 2001 From: Song Gao Date: Fri, 17 Jan 2020 15:16:31 +0800 Subject: [PATCH] Make service port-name configurable in monitor (#1521) * add port name * Update zz_generated.deepcopy.go * fix * Update values.yaml * Update util.go * add Monitor Portname access * exponse public access for tibdmonitor Co-authored-by: Yecheng Fu --- .../templates/monitor-service.yaml | 6 +- charts/tidb-cluster/values.yaml | 3 + manifests/crd.yaml | 3 + .../pingcap/v1alpha1/openapi_generated.go | 7 ++ .../pingcap/v1alpha1/tidbmonitor_component.go | 76 +++++++++++++++++++ pkg/apis/pingcap/v1alpha1/types.go | 6 +- .../pingcap/v1alpha1/zz_generated.deepcopy.go | 5 ++ pkg/monitor/monitor/util.go | 23 +++++- 8 files changed, 121 insertions(+), 8 deletions(-) create mode 100644 pkg/apis/pingcap/v1alpha1/tidbmonitor_component.go diff --git a/charts/tidb-cluster/templates/monitor-service.yaml b/charts/tidb-cluster/templates/monitor-service.yaml index 72f099d714..8ebd88b84e 100644 --- a/charts/tidb-cluster/templates/monitor-service.yaml +++ b/charts/tidb-cluster/templates/monitor-service.yaml @@ -16,7 +16,7 @@ metadata: {{- end }} spec: ports: - - name: grafana + - name: {{ .Values.monitor.grafana.service.portName }} port: 3000 protocol: TCP targetPort: 3000 @@ -40,7 +40,7 @@ metadata: helm.sh/chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }} spec: ports: - - name: reloader + - name: {{ .Values.monitor.reloader.service.portName }} port: 9089 protocol: TCP targetPort: 9089 @@ -63,7 +63,7 @@ metadata: helm.sh/chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }} spec: ports: - - name: prometheus + - name: {{ .Values.monitor.prometheus.service.portName }} port: 9090 protocol: TCP targetPort: 9090 diff --git a/charts/tidb-cluster/values.yaml b/charts/tidb-cluster/values.yaml index 8bb72d24b4..80dd0bd2c4 100644 --- a/charts/tidb-cluster/values.yaml +++ b/charts/tidb-cluster/values.yaml @@ -473,6 +473,7 @@ monitor: imagePullPolicy: IfNotPresent service: type: NodePort + portName: tcp-reloader resources: {} # limits: # cpu: 50m @@ -505,6 +506,7 @@ monitor: # GF_SERVER_ROOT_URL: "%(protocol)s://%(domain)s/grafana/" service: type: NodePort + portName: http-grafana prometheus: image: prom/prometheus:v2.11.1 imagePullPolicy: IfNotPresent @@ -518,6 +520,7 @@ monitor: # memory: 4Gi service: type: NodePort + portName: http-prometheus reserveDays: 12 # alertmanagerURL: "" nodeSelector: {} diff --git a/manifests/crd.yaml b/manifests/crd.yaml index 5752940e2f..ea1b930873 100644 --- a/manifests/crd.yaml +++ b/manifests/crd.yaml @@ -1845,6 +1845,9 @@ spec: description: 'LoadBalancerIP is the loadBalancerIP of service Optional: Defaults to omitted' type: string + portName: + description: PortName is the name of service port + type: string type: description: Type of the real kubernetes service type: string diff --git a/pkg/apis/pingcap/v1alpha1/openapi_generated.go b/pkg/apis/pingcap/v1alpha1/openapi_generated.go index bc2150d298..92a6748926 100644 --- a/pkg/apis/pingcap/v1alpha1/openapi_generated.go +++ b/pkg/apis/pingcap/v1alpha1/openapi_generated.go @@ -2868,6 +2868,13 @@ func schema_pkg_apis_pingcap_v1alpha1_ServiceSpec(ref common.ReferenceCallback) Format: "", }, }, + "portName": { + SchemaProps: spec.SchemaProps{ + Description: "PortName is the name of service port", + Type: []string{"string"}, + Format: "", + }, + }, }, }, }, diff --git a/pkg/apis/pingcap/v1alpha1/tidbmonitor_component.go b/pkg/apis/pingcap/v1alpha1/tidbmonitor_component.go new file mode 100644 index 0000000000..0c2e6fb8a0 --- /dev/null +++ b/pkg/apis/pingcap/v1alpha1/tidbmonitor_component.go @@ -0,0 +1,76 @@ +// Copyright 2019 PingCAP, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// See the License for the specific language governing permissions and +// limitations under the License. + +package v1alpha1 + +import corev1 "k8s.io/api/core/v1" + +type MonitorComponentAccessor interface { + PortName() *string + ServiceType() corev1.ServiceType + ImagePullPolicy() *corev1.PullPolicy +} + +type monitorComponentAccessorImpl struct { + // Monitor is the TidbMonitor Spec + MonitorSpec *TidbMonitorSpec + + // Container is the Component Spec + MonitorComponentSpec *MonitorContainer + + // Service is Component Service Spec + MonitorServiceSpec *ServiceSpec +} + +func (m *monitorComponentAccessorImpl) PortName() *string { + return m.MonitorServiceSpec.PortName +} + +func (m *monitorComponentAccessorImpl) ServiceType() corev1.ServiceType { + return m.MonitorServiceSpec.Type +} + +func (m *monitorComponentAccessorImpl) ImagePullPolicy() *corev1.PullPolicy { + if m.MonitorComponentSpec.ImagePullPolicy == nil { + return &m.MonitorSpec.ImagePullPolicy + } + return m.MonitorComponentSpec.ImagePullPolicy +} + +// BasePrometheusSpec return the base spec of Prometheus service +func (tm *TidbMonitor) BasePrometheusSpec() MonitorComponentAccessor { + return &monitorComponentAccessorImpl{ + MonitorSpec: &tm.Spec, + MonitorComponentSpec: &tm.Spec.Prometheus.MonitorContainer, + MonitorServiceSpec: &tm.Spec.Prometheus.Service, + } +} + +func (tm *TidbMonitor) BaseGrafanaSpec() MonitorComponentAccessor { + if tm.Spec.Grafana != nil { + return &monitorComponentAccessorImpl{ + MonitorSpec: &tm.Spec, + MonitorComponentSpec: &tm.Spec.Grafana.MonitorContainer, + MonitorServiceSpec: &tm.Spec.Grafana.Service, + } + } + return nil +} + +func (tm *TidbMonitor) BaseReloaderSpec() MonitorComponentAccessor { + return &monitorComponentAccessorImpl{ + MonitorSpec: &tm.Spec, + MonitorComponentSpec: &tm.Spec.Reloader.MonitorContainer, + MonitorServiceSpec: &tm.Spec.Reloader.Service, + } +} diff --git a/pkg/apis/pingcap/v1alpha1/types.go b/pkg/apis/pingcap/v1alpha1/types.go index d24ef59b91..ccb1644f3d 100644 --- a/pkg/apis/pingcap/v1alpha1/types.go +++ b/pkg/apis/pingcap/v1alpha1/types.go @@ -102,7 +102,6 @@ type TidbClusterList struct { // +k8s:openapi-gen=true // TidbClusterSpec describes the attributes that a user creates on a tidb cluster type TidbClusterSpec struct { - // PD cluster spec PD PDSpec `json:"pd"` @@ -393,7 +392,6 @@ type TiDBSlowLogTailerSpec struct { // +k8s:openapi-gen=true // ComponentSpec is the base spec of each component, the fields should always accessed by the BasicSpec() method to respect the cluster-level properties type ComponentSpec struct { - // Image of the component, override baseImage and version if present // Deprecated // +k8s:openapi-gen=false @@ -471,6 +469,10 @@ type ServiceSpec struct { // ClusterIP is the clusterIP of service // +optional ClusterIP *string `json:"clusterIP,omitempty"` + + // PortName is the name of service port + // +optional + PortName *string `json:"portName,omitempty"` } // +k8s:openapi-gen=true diff --git a/pkg/apis/pingcap/v1alpha1/zz_generated.deepcopy.go b/pkg/apis/pingcap/v1alpha1/zz_generated.deepcopy.go index 71833be5c7..a44a1a0826 100644 --- a/pkg/apis/pingcap/v1alpha1/zz_generated.deepcopy.go +++ b/pkg/apis/pingcap/v1alpha1/zz_generated.deepcopy.go @@ -1973,6 +1973,11 @@ func (in *ServiceSpec) DeepCopyInto(out *ServiceSpec) { *out = new(string) **out = **in } + if in.PortName != nil { + in, out := &in.PortName, &out.PortName + *out = new(string) + **out = **in + } return } diff --git a/pkg/monitor/monitor/util.go b/pkg/monitor/monitor/util.go index d7cf7b4211..cf660c35ce 100644 --- a/pkg/monitor/monitor/util.go +++ b/pkg/monitor/monitor/util.go @@ -638,6 +638,21 @@ func getMonitorService(monitor *v1alpha1.TidbMonitor) []*core.Service { var services []*core.Service monitorLabel := label.New().Instance(monitor.Name).Monitor() labels := label.NewMonitor().Instance(monitor.Name).Monitor() + + reloaderPortName := "tcp-reloader" + prometheusPortName := "http-prometheus" + grafanaPortName := "http-grafana" + + if monitor.BaseReloaderSpec().PortName() != nil { + reloaderPortName = *monitor.BaseReloaderSpec().PortName() + } + if monitor.BasePrometheusSpec().PortName() != nil { + prometheusPortName = *monitor.BasePrometheusSpec().PortName() + } + if monitor.BaseGrafanaSpec() != nil && monitor.BaseGrafanaSpec().PortName() != nil { + grafanaPortName = *monitor.BaseGrafanaSpec().PortName() + } + prometheusService := &core.Service{ ObjectMeta: meta.ObjectMeta{ Name: fmt.Sprintf("%s-prometheus", monitor.Name), @@ -649,7 +664,7 @@ func getMonitorService(monitor *v1alpha1.TidbMonitor) []*core.Service { Spec: core.ServiceSpec{ Ports: []core.ServicePort{ { - Name: "prometheus", + Name: prometheusPortName, Port: 9090, Protocol: core.ProtocolTCP, TargetPort: intstr.FromInt(9090), @@ -659,6 +674,7 @@ func getMonitorService(monitor *v1alpha1.TidbMonitor) []*core.Service { Selector: labels, }, } + reloaderService := &core.Service{ ObjectMeta: meta.ObjectMeta{ Name: fmt.Sprintf("%s-reloader", monitor.Name), @@ -670,7 +686,7 @@ func getMonitorService(monitor *v1alpha1.TidbMonitor) []*core.Service { Spec: core.ServiceSpec{ Ports: []core.ServicePort{ { - Name: "reloader", + Name: reloaderPortName, Port: 9089, Protocol: core.ProtocolTCP, TargetPort: intstr.FromInt(9089), @@ -683,6 +699,7 @@ func getMonitorService(monitor *v1alpha1.TidbMonitor) []*core.Service { }, }, } + services = append(services, prometheusService, reloaderService) if monitor.Spec.Grafana != nil { grafanaService := &core.Service{ @@ -696,7 +713,7 @@ func getMonitorService(monitor *v1alpha1.TidbMonitor) []*core.Service { Spec: core.ServiceSpec{ Ports: []core.ServicePort{ { - Name: "grafana", + Name: grafanaPortName, Port: 3000, Protocol: core.ProtocolTCP, TargetPort: intstr.FromInt(3000),