Skip to content

Commit

Permalink
Move discovery to DNS names
Browse files Browse the repository at this point in the history
  • Loading branch information
pebrc committed Aug 27, 2020
1 parent 283aa79 commit 59c217f
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 39 deletions.
9 changes: 3 additions & 6 deletions pkg/controller/elasticsearch/certificates/transport/csr.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"net"
"time"

"github.com/elastic/cloud-on-k8s/pkg/controller/elasticsearch/label"
"github.com/elastic/cloud-on-k8s/pkg/controller/elasticsearch/nodespec"
"github.com/elastic/cloud-on-k8s/pkg/controller/elasticsearch/sset"

netutil "github.com/elastic/cloud-on-k8s/pkg/utils/net"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -73,9 +73,6 @@ func buildGeneralNames(
return nil, errors.Errorf("pod currently has no valid IP, found: [%s]", pod.Status.PodIP)
}

ssetName := pod.Labels[label.StatefulSetNameLabelName]
svcName := nodespec.HeadlessServiceName(ssetName)

commonName := buildCertificateCommonName(pod, cluster.Name, cluster.Namespace)

commonNameUTF8OtherName := &certificates.UTF8StringValuedOtherName{
Expand All @@ -96,7 +93,7 @@ func buildGeneralNames(
// the DNS name has to match the seed hosts configured in the remote cluster settings
{DNSName: fmt.Sprintf("%s.%s.svc", esv1.TransportService(cluster.Name), cluster.Namespace)},
// add the resolvable DNS name of the Pod as published by Elasticsearch
{DNSName: fmt.Sprintf("%s.%s", pod.Name, svcName)},
{DNSName: sset.PodDNSName(pod)},
{IPAddress: netutil.IPToRFCForm(podIP)},
{IPAddress: netutil.IPToRFCForm(netutil.LoopbackFor(netutil.ToIPFamily(podIP.String())))},
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/elasticsearch/driver/upscale_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ func TestHandleUpscaleAndSpecChanges(t *testing.T) {
require.Equal(t, pointer.Int32(4), sset2.Spec.Replicas)
comparison.RequireEqual(t, &updatedStatefulSets[1], &sset2)
// headless services should be created for both
require.NoError(t, k8sClient.Get(types.NamespacedName{Namespace: "ns", Name: nodespec.HeadlessServiceName("sset1")}, &corev1.Service{}))
require.NoError(t, k8sClient.Get(types.NamespacedName{Namespace: "ns", Name: nodespec.HeadlessServiceName("sset2")}, &corev1.Service{}))
require.NoError(t, k8sClient.Get(types.NamespacedName{Namespace: "ns", Name: sset.HeadlessServiceName("sset1")}, &corev1.Service{}))
require.NoError(t, k8sClient.Get(types.NamespacedName{Namespace: "ns", Name: sset.HeadlessServiceName("sset2")}, &corev1.Service{}))
// config should be created for both
require.NoError(t, k8sClient.Get(types.NamespacedName{Namespace: "ns", Name: esv1.ConfigSecret("sset1")}, &corev1.Secret{}))
require.NoError(t, k8sClient.Get(types.NamespacedName{Namespace: "ns", Name: esv1.ConfigSecret("sset2")}, &corev1.Secret{}))
Expand Down
4 changes: 3 additions & 1 deletion pkg/controller/elasticsearch/nodespec/podspec.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"crypto/sha256"
"fmt"

"github.com/elastic/cloud-on-k8s/pkg/controller/elasticsearch/sset"

corev1 "k8s.io/api/core/v1"

esv1 "github.com/elastic/cloud-on-k8s/pkg/apis/elasticsearch/v1"
Expand Down Expand Up @@ -58,7 +60,7 @@ func BuildPodTemplateSpec(
WithPorts(defaultContainerPorts).
WithReadinessProbe(*NewReadinessProbe()).
WithAffinity(DefaultAffinity(es.Name)).
WithEnv(DefaultEnvVars(es.Spec.HTTP, HeadlessServiceName(esv1.StatefulSet(es.Name, nodeSet.Name)))...).
WithEnv(DefaultEnvVars(es.Spec.HTTP, sset.HeadlessServiceName(esv1.StatefulSet(es.Name, nodeSet.Name)))...).
WithVolumes(volumes...).
WithVolumeMounts(volumeMounts...).
WithInitContainers(initContainers...).
Expand Down
4 changes: 3 additions & 1 deletion pkg/controller/elasticsearch/nodespec/podspec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"sort"
"testing"

"github.com/elastic/cloud-on-k8s/pkg/controller/elasticsearch/sset"

commonv1 "github.com/elastic/cloud-on-k8s/pkg/apis/common/v1"
esv1 "github.com/elastic/cloud-on-k8s/pkg/apis/elasticsearch/v1"
"github.com/elastic/cloud-on-k8s/pkg/controller/common/defaults"
Expand Down Expand Up @@ -173,7 +175,7 @@ func TestBuildPodTemplateSpec(t *testing.T) {
},
Env: append(
[]corev1.EnvVar{{Name: "my-env", Value: "my-value"}},
DefaultEnvVars(sampleES.Spec.HTTP, HeadlessServiceName(esv1.StatefulSet(sampleES.Name, nodeSet.Name)))...),
DefaultEnvVars(sampleES.Spec.HTTP, sset.HeadlessServiceName(esv1.StatefulSet(sampleES.Name, nodeSet.Name)))...),
Resources: DefaultResources,
VolumeMounts: volumeMounts,
ReadinessProbe: NewReadinessProbe(),
Expand Down
10 changes: 2 additions & 8 deletions pkg/controller/elasticsearch/nodespec/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,14 @@ var (
f = false
)

// HeadlessServiceName returns the name of the headless service for the given StatefulSet.
func HeadlessServiceName(ssetName string) string {
// just use the sset name
return ssetName
}

// HeadlessService returns a headless service for the given StatefulSet
func HeadlessService(es *esv1.Elasticsearch, ssetName string) corev1.Service {
nsn := k8s.ExtractNamespacedName(es)

return corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Namespace: nsn.Namespace,
Name: HeadlessServiceName(ssetName),
Name: sset.HeadlessServiceName(ssetName),
Labels: label.NewStatefulSetLabels(nsn, ssetName),
},
Spec: corev1.ServiceSpec{
Expand Down Expand Up @@ -115,7 +109,7 @@ func BuildStatefulSet(
// use default revision history limit
RevisionHistoryLimit: nil,
// build a headless service per StatefulSet, matching the StatefulSet labels
ServiceName: HeadlessServiceName(statefulSetName),
ServiceName: sset.HeadlessServiceName(statefulSetName),
Selector: &metav1.LabelSelector{
MatchLabels: ssetSelector,
},
Expand Down
6 changes: 2 additions & 4 deletions pkg/controller/elasticsearch/settings/masters.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,16 @@ package settings

import (
"context"
"net"
"reflect"
"sort"
"strconv"
"strings"

esv1 "github.com/elastic/cloud-on-k8s/pkg/apis/elasticsearch/v1"
"github.com/elastic/cloud-on-k8s/pkg/controller/common/annotation"
"github.com/elastic/cloud-on-k8s/pkg/controller/common/reconciler"
"github.com/elastic/cloud-on-k8s/pkg/controller/common/tracing"
"github.com/elastic/cloud-on-k8s/pkg/controller/elasticsearch/label"
"github.com/elastic/cloud-on-k8s/pkg/controller/elasticsearch/network"
"github.com/elastic/cloud-on-k8s/pkg/controller/elasticsearch/sset"
"github.com/elastic/cloud-on-k8s/pkg/controller/elasticsearch/volume"
"github.com/elastic/cloud-on-k8s/pkg/utils/k8s"
"go.elastic.co/apm"
Expand Down Expand Up @@ -59,7 +57,7 @@ func UpdateSeedHostsConfigMap(
if len(master.Status.PodIP) > 0 { // do not add pod with no IPs
seedHosts = append(
seedHosts,
net.JoinHostPort(master.Status.PodIP, strconv.Itoa(network.TransportPort)),
sset.PodDNSName(master),
)
}
}
Expand Down
21 changes: 4 additions & 17 deletions pkg/controller/elasticsearch/settings/masters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func newPodWithIP(name, ip string, master bool) corev1.Pod {
},
}
label.NodeTypesMasterLabelName.Set(master, p.Labels)
p.Labels[label.StatefulSetNameLabelName] = "test-sset"
return p
}

Expand Down Expand Up @@ -97,7 +98,7 @@ func TestUpdateSeedHostsConfigMap(t *testing.T) {
es: es,
},
wantErr: false,
expectedContent: "10.0.3.3:9300\n10.0.9.2:9300",
expectedContent: "master1.test-sset\nmaster3.test-sset",
},
{
name: "All masters have IPs, some nodes don't",
Expand All @@ -113,7 +114,7 @@ func TestUpdateSeedHostsConfigMap(t *testing.T) {
es: es,
},
wantErr: false,
expectedContent: "10.0.3.3:9300\n10.0.6.5:9300\n10.0.9.2:9300",
expectedContent: "master1.test-sset\nmaster2.test-sset\nmaster3.test-sset",
},
{
name: "Ordering of pods should not matter",
Expand All @@ -127,21 +128,7 @@ func TestUpdateSeedHostsConfigMap(t *testing.T) {
es: es,
},
wantErr: false,
expectedContent: "10.0.3.3:9300\n10.0.6.5:9300\n10.0.9.2:9300",
},
{
name: "Can handle IPv6 addresses",
args: args{
pods: []corev1.Pod{ //
newPodWithIP("master2", "fd00:10:244:0:2::3", true),
newPodWithIP("master3", "fd00:10:244:0:2::5", true),
newPodWithIP("master1", "fd00:10:244:0:2::2", true),
},
c: k8s.WrappedFakeClient(),
es: es,
},
wantErr: false,
expectedContent: "[fd00:10:244:0:2::2]:9300\n[fd00:10:244:0:2::3]:9300\n[fd00:10:244:0:2::5]:9300",
expectedContent: "master1.test-sset\nmaster2.test-sset\nmaster3.test-sset",
},
}
for _, tt := range tests {
Expand Down
12 changes: 12 additions & 0 deletions pkg/controller/elasticsearch/sset/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ import (
"github.com/elastic/cloud-on-k8s/pkg/utils/stringsutil"
)

// HeadlessServiceName returns the name of the headless service for the given StatefulSet.
func HeadlessServiceName(ssetName string) string {
// just use the sset name
return ssetName
}

//PodDNSName returns the DNS resolvable name of the given pod resolvable within the namespace.
func PodDNSName(pod corev1.Pod) string {
ssetName := pod.Labels[label.StatefulSetNameLabelName]
return fmt.Sprintf("%s.%s", pod.Name, HeadlessServiceName(ssetName))
}

// PodName returns the name of the pod with the given ordinal for this StatefulSet.
func PodName(ssetName string, ordinal int32) string {
return fmt.Sprintf("%s-%d", ssetName, ordinal)
Expand Down

0 comments on commit 59c217f

Please sign in to comment.