diff --git a/api/v1/mysql_types.go b/api/v1/mysql_types.go index bcfe74b..0959ab4 100644 --- a/api/v1/mysql_types.go +++ b/api/v1/mysql_types.go @@ -232,6 +232,14 @@ func (r *Mysql) BuildName(suffix string) string { return r.Name + "-" + suffix } +func (r *Mysql) MergeLabels(svc *corev1.Service) { + for key, value := range r.Spec.Labels { + if _, exist := svc.Labels[key]; !exist { + svc.Labels[key] = value + } + } +} + func (spec *MysqlSpec) Size() int { return spec.Primaries + *spec.Replicas } diff --git a/controllers/mutate.go b/controllers/mutate.go index 8d5e803..e213ec6 100644 --- a/controllers/mutate.go +++ b/controllers/mutate.go @@ -18,6 +18,11 @@ var ( PersistentVolumeFilesystem = corev1.PersistentVolumeFilesystem ) +const ( + LabelMonitorErdaCloudEnabled = "monitor.erda.cloud/enabled" + LabelMonitorErdaCloudExporter = "monitor.erda.cloud/exporter" +) + func MutateSts(mysql *databasev1.Mysql, sts *appsv1.StatefulSet) { labels := mysql.NewLabels() podLables := make(map[string]string, len(labels)+len(mysql.Spec.Labels)) @@ -254,6 +259,7 @@ func NewEnv(a ...corev1.EnvVar) []corev1.EnvVar { func MutateSvc(mysql *databasev1.Mysql, svc *corev1.Service, x string) { svc.Labels = mysql.NewLabels() + mysql.MergeLabels(svc) svc.Spec = corev1.ServiceSpec{ Selector: mysql.NewLabels(), Ports: []corev1.ServicePort{ @@ -284,9 +290,15 @@ func MutateSvc(mysql *databasev1.Mysql, svc *corev1.Service, x string) { switch x { case databasev1.HeadlessSuffix: svc.Spec.ClusterIP = corev1.ClusterIPNone + enableMonitor(svc) case "write": svc.Spec.Selector["statefulset.kubernetes.io/pod-name"] = mysql.SoloName(*mysql.Status.WriteId) case "read": svc.Spec.Selector["statefulset.kubernetes.io/pod-name"] = mysql.SoloName(*mysql.Status.ReadId) } } + +func enableMonitor(svc *corev1.Service) { + svc.Labels[LabelMonitorErdaCloudEnabled] = "true" + svc.Labels[LabelMonitorErdaCloudExporter] = "true" +}