This repository has been archived by the owner on Nov 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
configmaps.go
131 lines (108 loc) · 3.82 KB
/
configmaps.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package controllers
import (
"context"
corev1 "k8s.io/api/core/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/yaml"
"github.com/milvus-io/milvus-operator/apis/milvus.io/v1beta1"
"github.com/milvus-io/milvus-operator/pkg/config"
"github.com/milvus-io/milvus-operator/pkg/util"
)
func (r *MilvusReconciler) getMinioAccessInfo(ctx context.Context, mc v1beta1.Milvus) (string, string) {
if mc.Spec.Dep.Storage.SecretRef == "" {
return "", ""
}
secret := &corev1.Secret{}
key := types.NamespacedName{Namespace: mc.Namespace, Name: mc.Spec.Dep.Storage.SecretRef}
if err := r.Get(ctx, key, secret); err != nil {
// TODO @shaoyue: handle error, or not get if no secret set
r.logger.Error(err, "get minio secret error")
return "", ""
}
return string(secret.Data[AccessKey]), string(secret.Data[SecretKey])
}
func (r *MilvusReconciler) updateConfigMap(ctx context.Context, mc v1beta1.Milvus, configmap *corev1.ConfigMap) error {
confYaml, err := util.GetTemplatedValues(config.GetMilvusConfigTemplate(), mc)
if err != nil {
return err
}
conf := map[string]interface{}{}
if err := yaml.Unmarshal(confYaml, &conf); err != nil {
r.logger.Error(err, "yaml Unmarshal conf error")
return err
}
key, secret := r.getMinioAccessInfo(ctx, mc)
util.SetValue(conf, key, "minio", "accessKeyID")
util.SetValue(conf, secret, "minio", "secretAccessKey")
util.MergeValues(conf, mc.Spec.Conf.Data)
util.SetStringSlice(conf, mc.Spec.Dep.Etcd.Endpoints, "etcd", "endpoints")
host, port := util.GetHostPort(mc.Spec.Dep.Storage.Endpoint)
util.SetValue(conf, host, "minio", "address")
util.SetValue(conf, int64(port), "minio", "port")
switch mc.Spec.Dep.MsgStreamType {
case v1beta1.MsgStreamTypeKafka:
util.SetStringSlice(conf, mc.Spec.Dep.Kafka.BrokerList, "kafka", "brokerList")
// delete other mq config to make milvus use kafka
delete(conf, "pulsar")
delete(conf, "rocksmq")
case v1beta1.MsgStreamTypePulsar:
host, port = util.GetHostPort(mc.Spec.Dep.Pulsar.Endpoint)
util.SetValue(conf, host, "pulsar", "address")
util.SetValue(conf, int64(port), "pulsar", "port")
// delete other mq config to make milvus use kafka
delete(conf, "kafka")
delete(conf, "rocksmq")
case v1beta1.MsgStreamTypeRocksMQ:
// adhoc: to let the merger know we're using rocksmq config
conf["rocksmq"] = map[string]interface{}{}
// delete other mq config to make milvus use rocksmq
delete(conf, "pulsar")
delete(conf, "kafka")
}
conf[util.MqTypeConfigKey] = mc.Spec.Dep.MsgStreamType
milvusYaml, err := yaml.Marshal(conf)
if err != nil {
r.logger.Error(err, "yaml Marshal conf error")
return err
}
configmap.Labels = MergeLabels(configmap.Labels, NewAppLabels(mc.Name))
if err := SetControllerReference(&mc, configmap, r.Scheme); err != nil {
return err
}
if configmap.Data == nil {
configmap.Data = make(map[string]string)
}
configmap.Data[MilvusUserConfigMountSubPath] = string(milvusYaml)
return nil
}
func (r *MilvusReconciler) ReconcileConfigMaps(ctx context.Context, mc v1beta1.Milvus) error {
namespacedName := NamespacedName(mc.Namespace, mc.Name)
old := &corev1.ConfigMap{}
err := r.Get(ctx, namespacedName, old)
if kerrors.IsNotFound(err) {
new := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: mc.Name,
Namespace: mc.Namespace,
},
}
if err = r.updateConfigMap(ctx, mc, new); err != nil {
return err
}
r.logger.Info("Create Configmap", "name", new.Name, "namespace", new.Namespace)
return r.Create(ctx, new)
} else if err != nil {
return err
}
cur := old.DeepCopy()
if err := r.updateConfigMap(ctx, mc, cur); err != nil {
return err
}
if IsEqual(old, cur) {
return nil
}
r.logger.Info("Update Configmap", "name", cur.Name, "namespace", cur.Namespace)
return r.Update(ctx, cur)
}