|
| 1 | +/* |
| 2 | +Copyright 2021 RadonDB. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package syncer |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/json" |
| 21 | + "fmt" |
| 22 | + |
| 23 | + "github.com/presslabs/controller-util/syncer" |
| 24 | + corev1 "k8s.io/api/core/v1" |
| 25 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 26 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 27 | + |
| 28 | + "github.com/radondb/radondb-mysql-kubernetes/mysqlcluster" |
| 29 | + "github.com/radondb/radondb-mysql-kubernetes/utils" |
| 30 | +) |
| 31 | + |
| 32 | +// NewXenonCMSyncer returns xenon configmap syncer. |
| 33 | +func NewXenonCMSyncer(cli client.Client, c *mysqlcluster.MysqlCluster) syncer.Interface { |
| 34 | + cm := &corev1.ConfigMap{ |
| 35 | + TypeMeta: metav1.TypeMeta{ |
| 36 | + APIVersion: "v1", |
| 37 | + Kind: "ConfigMap", |
| 38 | + }, |
| 39 | + ObjectMeta: metav1.ObjectMeta{ |
| 40 | + Name: c.GetNameForResource(utils.XenonMetaData), |
| 41 | + Namespace: c.Namespace, |
| 42 | + Labels: c.GetLabels(), |
| 43 | + }, |
| 44 | + } |
| 45 | + |
| 46 | + return syncer.NewObjectSyncer("ConfigMap", c.Unwrap(), cm, cli, func() error { |
| 47 | + if int(*c.Spec.Replicas) == 0 { |
| 48 | + return nil |
| 49 | + } |
| 50 | + |
| 51 | + metaData, err := buildXenonMetaData(c) |
| 52 | + if err != nil { |
| 53 | + return fmt.Errorf("failed to build xenon metadata: %s", err) |
| 54 | + } |
| 55 | + |
| 56 | + cm.Data = map[string]string{ |
| 57 | + "peers.json": metaData, |
| 58 | + } |
| 59 | + |
| 60 | + return nil |
| 61 | + }) |
| 62 | +} |
| 63 | + |
| 64 | +type XenonMetaData struct { |
| 65 | + Idlepeers []string `json:"idlepeers"` |
| 66 | + Peers []string `json:"peers"` |
| 67 | +} |
| 68 | + |
| 69 | +// buildXenonMetaData build the default metadata of xenon. |
| 70 | +func buildXenonMetaData(c *mysqlcluster.MysqlCluster) (string, error) { |
| 71 | + replicas := c.Spec.Replicas |
| 72 | + xenonMetaData := XenonMetaData{} |
| 73 | + for i := 0; i < int(*replicas); i++ { |
| 74 | + xenonMetaData.Peers = append(xenonMetaData.Peers, |
| 75 | + fmt.Sprintf("%s-%d.%s.%s:%d", |
| 76 | + c.GetNameForResource(utils.StatefulSet), |
| 77 | + i, |
| 78 | + c.GetNameForResource(utils.HeadlessSVC), |
| 79 | + c.Namespace, |
| 80 | + utils.XenonPort, |
| 81 | + )) |
| 82 | + } |
| 83 | + metaJson, err := json.Marshal(xenonMetaData) |
| 84 | + if err != nil { |
| 85 | + return "", err |
| 86 | + } |
| 87 | + return string(metaJson), nil |
| 88 | +} |
0 commit comments