Skip to content

Commit

Permalink
Make service port-name configurable in monitor (#1521)
Browse files Browse the repository at this point in the history
* 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 <cofyc.jackson@gmail.com>
  • Loading branch information
2 people authored and aylei committed Jan 17, 2020
1 parent d1887f2 commit 97d07a1
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 8 deletions.
6 changes: 3 additions & 3 deletions charts/tidb-cluster/templates/monitor-service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ metadata:
{{- end }}
spec:
ports:
- name: grafana
- name: {{ .Values.monitor.grafana.service.portName }}
port: 3000
protocol: TCP
targetPort: 3000
Expand All @@ -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
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions charts/tidb-cluster/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@ monitor:
imagePullPolicy: IfNotPresent
service:
type: NodePort
portName: tcp-reloader
resources: {}
# limits:
# cpu: 50m
Expand Down Expand Up @@ -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
Expand All @@ -518,6 +520,7 @@ monitor:
# memory: 4Gi
service:
type: NodePort
portName: http-prometheus
reserveDays: 12
# alertmanagerURL: ""
nodeSelector: {}
Expand Down
3 changes: 3 additions & 0 deletions manifests/crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions pkg/apis/pingcap/v1alpha1/openapi_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

76 changes: 76 additions & 0 deletions pkg/apis/pingcap/v1alpha1/tidbmonitor_component.go
Original file line number Diff line number Diff line change
@@ -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,
}
}
6 changes: 4 additions & 2 deletions pkg/apis/pingcap/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`

Expand Down Expand Up @@ -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 Basic<Component>Spec() 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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions pkg/apis/pingcap/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 20 additions & 3 deletions pkg/monitor/monitor/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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),
Expand All @@ -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),
Expand All @@ -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),
Expand All @@ -683,6 +699,7 @@ func getMonitorService(monitor *v1alpha1.TidbMonitor) []*core.Service {
},
},
}

services = append(services, prometheusService, reloaderService)
if monitor.Spec.Grafana != nil {
grafanaService := &core.Service{
Expand All @@ -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),
Expand Down

0 comments on commit 97d07a1

Please sign in to comment.