Skip to content

Commit

Permalink
Use list for advertise-client-urls as well
Browse files Browse the repository at this point in the history
  • Loading branch information
anveshreddy18 committed Oct 25, 2024
1 parent b0ef023 commit 59c80b9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 40 deletions.
33 changes: 12 additions & 21 deletions internal/component/configmap/configmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ func matchClientTLSRelatedConfiguration(g *WithT, etcd *druidv1alpha1.Etcd, actu
if etcd.Spec.Etcd.ClientUrlTLS != nil {
g.Expect(actualETCDConfig).To(MatchKeys(IgnoreExtras|IgnoreMissing, Keys{
"listen-client-urls": Equal(fmt.Sprintf("https://0.0.0.0:%d", ptr.Deref(etcd.Spec.Etcd.ClientPort, common.DefaultPortEtcdClient))),
"advertise-client-urls": Equal(getAdvertiseInterface(etcd, "https")),
"advertise-client-urls": Equal(getAdvertiseURLsInterface(etcd, commTypeClient, "https")),
"client-transport-security": MatchKeys(IgnoreExtras, Keys{
"cert-file": Equal("/var/etcd/ssl/server/tls.crt"),
"key-file": Equal("/var/etcd/ssl/server/tls.key"),
Expand All @@ -370,26 +370,17 @@ func matchClientTLSRelatedConfiguration(g *WithT, etcd *druidv1alpha1.Etcd, actu
}
}

func getAdvertiseInterface(etcd *druidv1alpha1.Etcd, scheme string) map[string]interface{} {
advertiseClientUrls := getAdvertiseClientUrlMap(etcd, scheme, druidv1alpha1.GetPeerServiceName(etcd.ObjectMeta))
advertiseClientUrlsInterface := make(map[string]interface{}, len(advertiseClientUrls))
for k, v := range advertiseClientUrls {
advertiseClientUrlsInterface[k] = v
}
return advertiseClientUrlsInterface
}

func getAdvertisePeerInterface(etcd *druidv1alpha1.Etcd, scheme string) map[string]interface{} {
advertisePeerUrls := getAdvertisePeerUrlsMap(etcd, scheme, druidv1alpha1.GetPeerServiceName(etcd.ObjectMeta))
advertisePeerUrlsInterface := make(map[string]interface{}, len(advertisePeerUrls))
for k, v := range advertisePeerUrls {
urlsInterface := make([]interface{}, len(v))
for i, url := range v {
urlsInterface[i] = url
func getAdvertiseURLsInterface(etcd *druidv1alpha1.Etcd, commType, scheme string) map[string]interface{} {
advertiseUrlsMap := getAdvertiseUrlsMap(etcd, commType, scheme, druidv1alpha1.GetPeerServiceName(etcd.ObjectMeta))
advertiseUrlsInterface := make(map[string]interface{}, len(advertiseUrlsMap))
for podName, urlList := range advertiseUrlsMap {
urlsListInterface := make([]interface{}, len(urlList))
for i, url := range urlList {
urlsListInterface[i] = url
}
advertisePeerUrlsInterface[k] = urlsInterface
advertiseUrlsInterface[podName] = urlsListInterface
}
return advertisePeerUrlsInterface
return advertiseUrlsInterface
}

func matchPeerTLSRelatedConfiguration(g *WithT, etcd *druidv1alpha1.Etcd, actualETCDConfig map[string]interface{}) {
Expand All @@ -403,12 +394,12 @@ func matchPeerTLSRelatedConfiguration(g *WithT, etcd *druidv1alpha1.Etcd, actual
"auto-tls": Equal(false),
}),
"listen-peer-urls": Equal(fmt.Sprintf("https://0.0.0.0:%d", ptr.Deref(etcd.Spec.Etcd.ServerPort, common.DefaultPortEtcdPeer))),
"initial-advertise-peer-urls": Equal(getAdvertisePeerInterface(etcd, "https")),
"initial-advertise-peer-urls": Equal(getAdvertiseURLsInterface(etcd, commTypePeer, "https")),
}))
} else {
g.Expect(actualETCDConfig).To(MatchKeys(IgnoreExtras|IgnoreMissing, Keys{
"listen-peer-urls": Equal(fmt.Sprintf("http://0.0.0.0:%d", ptr.Deref(etcd.Spec.Etcd.ServerPort, common.DefaultPortEtcdPeer))),
"initial-advertise-peer-urls": Equal(getAdvertisePeerInterface(etcd, "http")),
"initial-advertise-peer-urls": Equal(getAdvertiseURLsInterface(etcd, commTypePeer, "http")),
}))
g.Expect(actualETCDConfig).ToNot(HaveKey("peer-transport-security"))
}
Expand Down
38 changes: 19 additions & 19 deletions internal/component/configmap/etcdconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ const (
defaultInitialClusterState = "new"
// For more information refer to https://etcd.io/docs/v3.4/op-guide/maintenance/#raft-log-retention
defaultSnapshotCount = int64(75000)
commTypePeer = "peer" // communication type
commTypeClient = "client"
)

var (
defaultDataDir = fmt.Sprintf("%s/new.etcd", common.VolumeMountPathEtcdData)
)

type advPeerUrls map[string][]string

type advClientUrls map[string]string
type advertiseURLs map[string][]string

type etcdConfig struct {
Name string `yaml:"name"`
Expand All @@ -47,8 +47,8 @@ type etcdConfig struct {
AutoCompactionRetention string `yaml:"auto-compaction-retention"`
ListenPeerUrls string `yaml:"listen-peer-urls"`
ListenClientUrls string `yaml:"listen-client-urls"`
AdvertisePeerUrls advPeerUrls `yaml:"initial-advertise-peer-urls"`
AdvertiseClientUrls advClientUrls `yaml:"advertise-client-urls"`
AdvertisePeerUrls advertiseURLs `yaml:"initial-advertise-peer-urls"`
AdvertiseClientUrls advertiseURLs `yaml:"advertise-client-urls"`
ClientSecurity securityConfig `yaml:"client-transport-security,omitempty"`
PeerSecurity securityConfig `yaml:"peer-transport-security,omitempty"`
}
Expand Down Expand Up @@ -79,8 +79,8 @@ func createEtcdConfig(etcd *druidv1alpha1.Etcd) *etcdConfig {
AutoCompactionRetention: ptr.Deref(etcd.Spec.Common.AutoCompactionRetention, defaultAutoCompactionRetention),
ListenPeerUrls: fmt.Sprintf("%s://0.0.0.0:%d", peerScheme, ptr.Deref(etcd.Spec.Etcd.ServerPort, common.DefaultPortEtcdPeer)),
ListenClientUrls: fmt.Sprintf("%s://0.0.0.0:%d", clientScheme, ptr.Deref(etcd.Spec.Etcd.ClientPort, common.DefaultPortEtcdClient)),
AdvertisePeerUrls: getAdvertisePeerUrlsMap(etcd, peerScheme, peerSvcName),
AdvertiseClientUrls: getAdvertiseClientUrlMap(etcd, clientScheme, peerSvcName),
AdvertisePeerUrls: getAdvertiseUrlsMap(etcd, commTypePeer, peerScheme, peerSvcName),
AdvertiseClientUrls: getAdvertiseUrlsMap(etcd, commTypeClient, clientScheme, peerSvcName),
}
if peerSecurityConfig != nil {
cfg.PeerSecurity = *peerSecurityConfig
Expand Down Expand Up @@ -133,20 +133,20 @@ func prepareInitialCluster(etcd *druidv1alpha1.Etcd, peerScheme string) string {
return strings.Trim(builder.String(), ",")
}

func getAdvertisePeerUrlsMap(etcd *druidv1alpha1.Etcd, peerScheme string, peerSvcName string) advPeerUrls {
advPeerUrlsMap := make(map[string][]string)
for i := 0; i < int(etcd.Spec.Replicas); i++ {
podName := druidv1alpha1.GetOrdinalPodName(etcd.ObjectMeta, i)
advPeerUrlsMap[podName] = []string{fmt.Sprintf("%s://%s.%s.%s.svc:%d", peerScheme, podName, peerSvcName, etcd.Namespace, ptr.Deref(etcd.Spec.Etcd.ServerPort, common.DefaultPortEtcdPeer))}
func getAdvertiseUrlsMap(etcd *druidv1alpha1.Etcd, commType, scheme, peerSvcName string) advertiseURLs {
var port int32
switch commType {
case commTypePeer:
port = ptr.Deref(etcd.Spec.Etcd.ServerPort, common.DefaultPortEtcdPeer)
case commTypeClient:
port = ptr.Deref(etcd.Spec.Etcd.ClientPort, common.DefaultPortEtcdClient)
default:
return nil
}
return advPeerUrlsMap
}

func getAdvertiseClientUrlMap(etcd *druidv1alpha1.Etcd, clientScheme string, peerSvcName string) advClientUrls {
advClientUrlMap := make(map[string]string)
advUrlsMap := make(map[string][]string)
for i := 0; i < int(etcd.Spec.Replicas); i++ {
podName := druidv1alpha1.GetOrdinalPodName(etcd.ObjectMeta, i)
advClientUrlMap[podName] = fmt.Sprintf("%s://%s.%s.%s.svc:%d", clientScheme, podName, peerSvcName, etcd.Namespace, ptr.Deref(etcd.Spec.Etcd.ClientPort, common.DefaultPortEtcdClient))
advUrlsMap[podName] = []string{fmt.Sprintf("%s://%s.%s.%s.svc:%d", scheme, podName, peerSvcName, etcd.Namespace, port)}
}
return advClientUrlMap
return advUrlsMap
}

0 comments on commit 59c80b9

Please sign in to comment.