Skip to content

Commit

Permalink
Merge pull request #212 from metaicg/hostNetwork
Browse files Browse the repository at this point in the history
Add support for `hostNetwork` and `dnsPolicy`
  • Loading branch information
chusAlvarez authored Dec 17, 2019
2 parents be1e5fc + e134ab7 commit 3f0086b
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 0 deletions.
4 changes: 4 additions & 0 deletions api/redisfailover/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ type RedisSettings struct {
Tolerations []corev1.Toleration `json:"tolerations,omitempty"`
NodeSelector map[string]string `json:"nodeSelector,omitempty"`
PodAnnotations map[string]string `json:"podAnnotations,omitempty"`
HostNetwork bool `json:"hostNetwork,omitempty"`
DNSPolicy corev1.DNSPolicy `json:"dnsPolicy,omitempty"`
}

// SentinelSettings defines the specification of the sentinel cluster
Expand All @@ -57,6 +59,8 @@ type SentinelSettings struct {
NodeSelector map[string]string `json:"nodeSelector,omitempty"`
PodAnnotations map[string]string `json:"podAnnotations,omitempty"`
Exporter SentinelExporter `json:"exporter,omitempty"`
HostNetwork bool `json:"hostNetwork,omitempty"`
DNSPolicy corev1.DNSPolicy `json:"dnsPolicy,omitempty"`
}

// AuthSettings contains settings about auth
Expand Down
11 changes: 11 additions & 0 deletions operator/redisfailover/service/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ func generateRedisStatefulSet(rf *redisfailoverv1.RedisFailover, labels map[stri
Tolerations: rf.Spec.Redis.Tolerations,
NodeSelector: rf.Spec.Redis.NodeSelector,
SecurityContext: getSecurityContext(rf.Spec.Redis.SecurityContext),
HostNetwork: rf.Spec.Redis.HostNetwork,
DNSPolicy: getDnsPolicy(rf.Spec.Redis.DNSPolicy),
ImagePullSecrets: rf.Spec.Redis.ImagePullSecrets,
Containers: []corev1.Container{
{
Expand Down Expand Up @@ -352,6 +354,8 @@ func generateSentinelDeployment(rf *redisfailoverv1.RedisFailover, labels map[st
Tolerations: rf.Spec.Sentinel.Tolerations,
NodeSelector: rf.Spec.Sentinel.NodeSelector,
SecurityContext: getSecurityContext(rf.Spec.Sentinel.SecurityContext),
HostNetwork: rf.Spec.Sentinel.HostNetwork,
DNSPolicy: getDnsPolicy(rf.Spec.Sentinel.DNSPolicy),
ImagePullSecrets: rf.Spec.Sentinel.ImagePullSecrets,
InitContainers: []corev1.Container{
{
Expand Down Expand Up @@ -606,6 +610,13 @@ func getSecurityContext(secctx *corev1.PodSecurityContext) *corev1.PodSecurityCo
}
}

func getDnsPolicy(dnspolicy corev1.DNSPolicy) corev1.DNSPolicy {
if dnspolicy == "" {
return corev1.DNSClusterFirst
}
return dnspolicy
}

func getQuorum(rf *redisfailoverv1.RedisFailover) int32 {
return rf.Spec.Sentinel.Replicas/2 + 1
}
Expand Down
98 changes: 98 additions & 0 deletions operator/redisfailover/service/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,104 @@ func TestSentinelDeploymentPodAnnotations(t *testing.T) {
}
}

func TestRedisHostNetworkAndDnsPolicy(t *testing.T) {
tests := []struct {
name string
hostNetwork bool
expectedHostNetwork bool
dnsPolicy corev1.DNSPolicy
expectedDnsPolicy corev1.DNSPolicy
}{
{
name: "Default",
expectedHostNetwork: false,
expectedDnsPolicy: corev1.DNSClusterFirst,
},
{
name: "Custom",
hostNetwork: true,
expectedHostNetwork: true,
dnsPolicy: corev1.DNSClusterFirstWithHostNet,
expectedDnsPolicy: corev1.DNSClusterFirstWithHostNet,
},
}

for _, test := range tests {
assert := assert.New(t)

rf := generateRF()
rf.Spec.Redis.HostNetwork = test.hostNetwork
rf.Spec.Redis.DNSPolicy = test.dnsPolicy

var actualHostNetwork bool
var actualDnsPolicy corev1.DNSPolicy

ms := &mK8SService.Services{}
ms.On("CreateOrUpdatePodDisruptionBudget", namespace, mock.Anything).Once().Return(nil, nil)
ms.On("CreateOrUpdateStatefulSet", namespace, mock.Anything).Once().Run(func(args mock.Arguments) {
ss := args.Get(1).(*appsv1.StatefulSet)
actualHostNetwork = ss.Spec.Template.Spec.HostNetwork
actualDnsPolicy = ss.Spec.Template.Spec.DNSPolicy
}).Return(nil)

client := rfservice.NewRedisFailoverKubeClient(ms, log.Dummy)
err := client.EnsureRedisStatefulset(rf, nil, []metav1.OwnerReference{})
assert.NoError(err)

assert.Equal(test.expectedHostNetwork, actualHostNetwork)
assert.Equal(test.expectedDnsPolicy, actualDnsPolicy)
}
}

func TestSentinelHostNetworkAndDnsPolicy(t *testing.T) {
tests := []struct {
name string
hostNetwork bool
expectedHostNetwork bool
dnsPolicy corev1.DNSPolicy
expectedDnsPolicy corev1.DNSPolicy
}{
{
name: "Default",
expectedHostNetwork: false,
expectedDnsPolicy: corev1.DNSClusterFirst,
},
{
name: "Custom",
hostNetwork: true,
expectedHostNetwork: true,
dnsPolicy: corev1.DNSClusterFirstWithHostNet,
expectedDnsPolicy: corev1.DNSClusterFirstWithHostNet,
},
}

for _, test := range tests {
assert := assert.New(t)

rf := generateRF()
rf.Spec.Sentinel.HostNetwork = test.hostNetwork
rf.Spec.Sentinel.DNSPolicy = test.dnsPolicy

var actualHostNetwork bool
var actualDnsPolicy corev1.DNSPolicy

ms := &mK8SService.Services{}
ms.On("CreateOrUpdatePodDisruptionBudget", namespace, mock.Anything).Once().Return(nil, nil)
ms.On("CreateOrUpdateDeployment", namespace, mock.Anything).Once().Run(func(args mock.Arguments) {
d := args.Get(1).(*appsv1.Deployment)
actualHostNetwork = d.Spec.Template.Spec.HostNetwork
actualDnsPolicy = d.Spec.Template.Spec.DNSPolicy
}).Return(nil)

client := rfservice.NewRedisFailoverKubeClient(ms, log.Dummy)
err := client.EnsureSentinelDeployment(rf, nil, []metav1.OwnerReference{})
assert.NoError(err)

assert.Equal(test.expectedHostNetwork, actualHostNetwork)
assert.Equal(test.expectedDnsPolicy, actualDnsPolicy)
}
}

func TestRedisImagePullPolicy(t *testing.T) {
tests := []struct {
name string
Expand Down

0 comments on commit 3f0086b

Please sign in to comment.