Skip to content

Commit 4cd8919

Browse files
authored
Merge pull request #136 from tangcong/0.2
cherry-pick feature and bug fixed pr to release-0.2
2 parents ee33870 + 0c79853 commit 4cd8919

File tree

14 files changed

+828
-45
lines changed

14 files changed

+828
-45
lines changed

charts/charts/grafana/dashboards/0.json

Lines changed: 339 additions & 23 deletions
Large diffs are not rendered by default.

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.16
55
require (
66
github.com/aws/aws-sdk-go v1.13.8
77
github.com/codegangsta/inject v0.0.0-20150114235600-33e0aa1cb7c0 // indirect
8+
github.com/coreos/etcd v3.3.13+incompatible
89
github.com/coreos/etcd-operator v0.9.4
910
github.com/gin-gonic/gin v1.7.2
1011
github.com/go-martini/martini v0.0.0-20170121215854-22fa46961aab

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ github.com/codegangsta/inject v0.0.0-20150114235600-33e0aa1cb7c0 h1:sDMmm+q/3+Bu
111111
github.com/codegangsta/inject v0.0.0-20150114235600-33e0aa1cb7c0/go.mod h1:4Zcjuz89kmFXt9morQgcfYZAYZ5n8WHjt81YYWIwtTM=
112112
github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
113113
github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
114+
github.com/coreos/etcd v3.3.13+incompatible h1:8F3hqu9fGYLBifCmRCJsicFqDx/D68Rt3q1JMazcgBQ=
114115
github.com/coreos/etcd v3.3.13+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
115116
github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk=
116117
github.com/coreos/go-oidc v2.1.0+incompatible/go.mod h1:CgnwVTmzoESiwO9qyAFEMiHoZ1nMCKZlZ9V6mm3/LKc=

pkg/apis/kstone/v1alpha2/types.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ const (
8484
EtcdClusterImported EtcdClusterType = "imported"
8585
)
8686

87+
type EtcdStorageBackend string
88+
89+
const (
90+
EtcdStorageV2 EtcdStorageBackend = "v2"
91+
EtcdStorageV3 EtcdStorageBackend = "v3"
92+
)
93+
8794
// EtcdClusterSpec defines the desired state of EtcdCluster
8895
type EtcdClusterSpec struct {
8996
Name string `json:"name" protobuf:"bytes,1,opt,name=name"` // etcd cluster name,uniqueKey
@@ -95,11 +102,12 @@ type EtcdClusterSpec struct {
95102
DiskSize uint `json:"diskSize" protobuf:"varint,5,opt,name=diskSize"` // single node's disk size, unit: GB
96103
Size uint `json:"size" protobuf:"varint,6,opt,name=size"` // etcd cluster member count: support 1, 3, 5, 7
97104

98-
Affinity corev1.Affinity `json:"affinity,omitempty" protobuf:"bytes,7,opt,name=affinity"`
99-
Args []string `json:"args,omitempty" protobuf:"bytes,8,rep,name=args"`
100-
Env []corev1.EnvVar `json:"env,omitempty" protobuf:"bytes,9,rep,name=env"` // etcd environment variables
101-
Version string `json:"version" protobuf:"bytes,10,opt,name=version"` // etcd version
102-
Repository string `json:"repository,omitempty" protobuf:"bytes,11,opt,name=repository"` // etcd image
105+
Affinity corev1.Affinity `json:"affinity,omitempty" protobuf:"bytes,7,opt,name=affinity"`
106+
Args []string `json:"args,omitempty" protobuf:"bytes,8,rep,name=args"`
107+
Env []corev1.EnvVar `json:"env,omitempty" protobuf:"bytes,9,rep,name=env"` // etcd environment variables
108+
StorageBackend string `json:"storageBackend" protobuf:"bytes,7,opt,name=storageBackend"`
109+
Version string `json:"version" protobuf:"bytes,10,opt,name=version"` // etcd version
110+
Repository string `json:"repository,omitempty" protobuf:"bytes,11,opt,name=repository"` // etcd image
103111

104112
ClusterType EtcdClusterType `json:"clusterType" protobuf:"bytes,12,opt,name=clusterType,casttype=EtcdClusterType"` // ClusterType specifies the etcd cluster provider.
105113

pkg/clusterprovider/helper.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@ package clusterprovider
2020

2121
import (
2222
"fmt"
23-
"strconv"
2423
"strings"
2524

2625
"k8s.io/klog/v2"
2726

2827
kstonev1alpha2 "tkestack.io/kstone/pkg/apis/kstone/v1alpha2"
2928
"tkestack.io/kstone/pkg/etcd"
29+
versionClient "tkestack.io/kstone/pkg/etcd/client"
30+
_ "tkestack.io/kstone/pkg/etcd/client/versions" // import etcd client including v2 and v3
3031
)
3132

3233
type EtcdAlarm struct {
@@ -68,22 +69,23 @@ func populateExtensionCientURLMap(extensionClientURLs string) (map[string]string
6869

6970
// GetRuntimeEtcdMembers get members of etcd
7071
func GetRuntimeEtcdMembers(
72+
storageBackend string,
7173
endpoints []string,
7274
extensionClientURLs string,
7375
config *etcd.ClientConfig) ([]kstonev1alpha2.MemberStatus, error) {
7476
etcdMembers := make([]kstonev1alpha2.MemberStatus, 0)
7577

7678
config.Endpoints = endpoints
77-
78-
// GetMemberList
79-
client, err := etcd.NewClientv3(config)
79+
versioned, err := versionClient.GetEtcdClientProvider(kstonev1alpha2.EtcdStorageBackend(storageBackend),
80+
&versionClient.VersionContext{Config: config})
8081
if err != nil {
81-
klog.Errorf("failed to get new etcd clientv3,err is %v ", err)
82+
klog.Errorf("failed get etcd version, err is %v", err)
8283
return etcdMembers, err
8384
}
84-
defer client.Close()
8585

86-
memberRsp, err := etcd.MemberList(client)
86+
defer versioned.Close()
87+
88+
memberRsp, err := versioned.MemberList()
8789
if err != nil {
8890
klog.Errorf("failed to get member list, endpoints is %s,err is %v", endpoints, err)
8991
return etcdMembers, err
@@ -95,7 +97,7 @@ func GetRuntimeEtcdMembers(
9597
return etcdMembers, err
9698
}
9799

98-
for _, m := range memberRsp.Members {
100+
for _, m := range memberRsp {
99101
// parse url
100102
if m.ClientURLs == nil {
101103
continue
@@ -122,7 +124,7 @@ func GetRuntimeEtcdMembers(
122124
// default info
123125
memberVersion, memberStatus, memberRole := "", kstonev1alpha2.MemberPhaseUnStarted, kstonev1alpha2.EtcdMemberUnKnown
124126
var errors []string
125-
statusRsp, err := etcd.Status(extensionClientURL, client)
127+
statusRsp, err := versioned.Status(extensionClientURL)
126128
if err == nil && statusRsp != nil {
127129
memberStatus = kstonev1alpha2.MemberPhaseRunning
128130
memberVersion = statusRsp.Version
@@ -133,15 +135,14 @@ func GetRuntimeEtcdMembers(
133135
} else {
134136
memberRole = kstonev1alpha2.EtcdMemberFollower
135137
}
136-
errors = statusRsp.Errors
137138
} else {
138139
klog.Errorf("failed to get member %s status,err is %v", extensionClientURL, err)
139140
errors = append(errors, err.Error())
140141
}
141142

142143
etcdMembers = append(etcdMembers, kstonev1alpha2.MemberStatus{
143144
Name: m.Name,
144-
MemberId: strconv.FormatUint(m.ID, 10),
145+
MemberId: m.ID,
145146
ClientUrl: m.ClientURLs[0],
146147
ExtensionClientUrl: extensionClientURL,
147148
Role: memberRole,

pkg/clusterprovider/providers/imported/cluster.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ func (c *EtcdClusterImported) Status(config *etcd.ClientConfig, cluster *kstonev
127127
}
128128

129129
members, err := clusterprovider.GetRuntimeEtcdMembers(
130+
cluster.Spec.StorageBackend,
130131
endpoints,
131132
cluster.Annotations[util.ClusterExtensionClientURL],
132133
config,

pkg/clusterprovider/providers/kstone/kstone.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ func (c *EtcdClusterKstone) Status(config *etcd.ClientConfig, cluster *kstonev1a
289289
}
290290

291291
members, err := clusterprovider.GetRuntimeEtcdMembers(
292+
cluster.Spec.StorageBackend,
292293
endpoints,
293294
cluster.Annotations[util.ClusterExtensionClientURL],
294295
config,
@@ -422,13 +423,19 @@ func (c *EtcdClusterKstone) generateEtcdSpec(cluster *kstonev1alpha2.EtcdCluster
422423
spec["repository"] = cluster.Annotations["repository"]
423424
}
424425

425-
affinity := &cluster.Spec.Affinity
426+
// TODO: Use struct to replace
427+
affinity := make(map[string]interface{})
428+
affinityBytes, _ := json.Marshal(cluster.Spec.Affinity)
429+
_ = json.Unmarshal(affinityBytes, &affinity)
426430
if affinity != nil {
427431
spec["template"].(map[string]interface{})["affinity"] = affinity
428432
}
429433

430434
if cluster.Spec.Tolerations != nil && len(cluster.Spec.Tolerations) > 0 {
431-
spec["template"].(map[string]interface{})["tolerations"] = cluster.Spec.Tolerations
435+
tolerations := make([]interface{}, 0)
436+
tolerationsBytes, _ := json.Marshal(cluster.Spec.Tolerations)
437+
_ = json.Unmarshal(tolerationsBytes, &tolerations)
438+
spec["template"].(map[string]interface{})["tolerations"] = tolerations
432439
}
433440

434441
return spec

pkg/etcd/client/cleint.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Tencent is pleased to support the open source community by making TKEStack
3+
* available.
4+
*
5+
* Copyright (C) 2012-2023 Tencent. All Rights Reserved.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use
8+
* this file except in compliance with the License. You may obtain a copy of the
9+
* License at
10+
*
11+
* https://opensource.org/licenses/Apache-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
* WARRANTIES OF ANY KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations under the License.
17+
*/
18+
19+
package client
20+
21+
import (
22+
"tkestack.io/kstone/pkg/etcd"
23+
)
24+
25+
// Member contains member info including v2 and v3
26+
type Member struct {
27+
ID string
28+
Name string
29+
PeerURLs []string
30+
ClientURLs []string
31+
Version string
32+
IsLearner bool
33+
Leader string
34+
}
35+
36+
type VersionClient interface {
37+
MemberList() ([]Member, error)
38+
Status(endpoint string) (*Member, error)
39+
Close()
40+
}
41+
42+
type VersionContext struct {
43+
Config *etcd.ClientConfig
44+
}

pkg/etcd/client/version.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Tencent is pleased to support the open source community by making TKEStack
3+
* available.
4+
*
5+
* Copyright (C) 2012-2023 Tencent. All Rights Reserved.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use
8+
* this file except in compliance with the License. You may obtain a copy of the
9+
* License at
10+
*
11+
* https://opensource.org/licenses/Apache-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
* WARRANTIES OF ANY KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations under the License.
17+
*/
18+
19+
package client
20+
21+
import (
22+
"errors"
23+
"sync"
24+
25+
"k8s.io/klog/v2"
26+
kstonev1alpha2 "tkestack.io/kstone/pkg/apis/kstone/v1alpha2"
27+
)
28+
29+
type Factory func(cluster *VersionContext) (VersionClient, error)
30+
31+
var (
32+
mutex sync.Mutex
33+
providers = make(map[kstonev1alpha2.EtcdStorageBackend]Factory)
34+
)
35+
36+
// RegisterEtcdClientFactory registers the specified etcd client
37+
func RegisterEtcdClientFactory(name kstonev1alpha2.EtcdStorageBackend, factory Factory) {
38+
mutex.Lock()
39+
defer mutex.Unlock()
40+
41+
if _, found := providers[name]; found {
42+
klog.V(2).Infof("etcdcluster provider %s was registered twice", name)
43+
}
44+
45+
klog.V(2).Infof("register etcdCluster provider %s", name)
46+
providers[name] = factory
47+
}
48+
49+
// GetEtcdClientProvider gets the specified etcd client
50+
func GetEtcdClientProvider(
51+
name kstonev1alpha2.EtcdStorageBackend,
52+
ctx *VersionContext,
53+
) (VersionClient, error) {
54+
mutex.Lock()
55+
defer mutex.Unlock()
56+
57+
// compatible with existing clusters
58+
if name == "" {
59+
name = kstonev1alpha2.EtcdStorageV3
60+
}
61+
f, found := providers[name]
62+
63+
klog.V(1).Infof("get provider name %s,status:%t", name, found)
64+
if !found {
65+
return nil, errors.New("fatal error,etcd cluster provider not found")
66+
}
67+
return f(ctx)
68+
}

pkg/etcd/client/versions/providers.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Tencent is pleased to support the open source community by making TKEStack
3+
* available.
4+
*
5+
* Copyright (C) 2012-2023 Tencent. All Rights Reserved.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use
8+
* this file except in compliance with the License. You may obtain a copy of the
9+
* License at
10+
*
11+
* https://opensource.org/licenses/Apache-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
* WARRANTIES OF ANY KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations under the License.
17+
*/
18+
19+
package versions
20+
21+
import (
22+
_ "tkestack.io/kstone/pkg/etcd/client/versions/v2" // import etcd client of v2
23+
_ "tkestack.io/kstone/pkg/etcd/client/versions/v3" // import etcd client of v3
24+
)

0 commit comments

Comments
 (0)