Skip to content
This repository has been archived by the owner on Oct 22, 2021. It is now read-only.

Commit

Permalink
Configurable DNS server in agent settings per IG
Browse files Browse the repository at this point in the history
  • Loading branch information
Mario Manno committed Nov 6, 2020
1 parent 7ff61b4 commit 9456b2a
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 20 deletions.
36 changes: 26 additions & 10 deletions pkg/bosh/bpmconverter/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,16 @@ func (kc *BPMConverter) serviceToQuarksStatefulSet(

spec := &extSts.Spec.Template.Spec.Template.Spec

spec.DNSPolicy, spec.DNSConfig, err = boshdns.DNSSetting(manifest, serviceIP, namespace)
if err != nil {
return qstsv1a1.QuarksStatefulSet{}, err
if instanceGroup.Env.AgentEnvBoshConfig.Agent.Settings.DNS != "" {
spec.DNSPolicy, spec.DNSConfig = boshdns.CustomDNSSetting(
instanceGroup.Env.AgentEnvBoshConfig.Agent.Settings.DNS,
namespace,
)
} else {
spec.DNSPolicy, spec.DNSConfig, err = boshdns.DNSSetting(manifest, serviceIP, namespace)
if err != nil {
return qstsv1a1.QuarksStatefulSet{}, err
}
}

if len(instanceGroup.Env.AgentEnvBoshConfig.Agent.Settings.Tolerations) > 0 {
Expand Down Expand Up @@ -391,25 +398,34 @@ func (kc *BPMConverter) errandToQuarksJob(
},
}

qJob.Spec.Template.Spec.Template.Spec.DNSPolicy, qJob.Spec.Template.Spec.Template.Spec.DNSConfig, err = boshdns.DNSSetting(manifest, serviceIP, namespace)
if err != nil {
return qjv1a1.QuarksJob{}, err
spec := &qJob.Spec.Template.Spec.Template.Spec

if instanceGroup.Env.AgentEnvBoshConfig.Agent.Settings.DNS != "" {
spec.DNSPolicy, spec.DNSConfig = boshdns.CustomDNSSetting(
instanceGroup.Env.AgentEnvBoshConfig.Agent.Settings.DNS,
namespace,
)
} else {
spec.DNSPolicy, spec.DNSConfig, err = boshdns.DNSSetting(manifest, serviceIP, namespace)
if err != nil {
return qjv1a1.QuarksJob{}, err
}
}

if instanceGroup.Env.AgentEnvBoshConfig.Agent.Settings.Affinity != nil {
qJob.Spec.Template.Spec.Template.Spec.Affinity = instanceGroup.Env.AgentEnvBoshConfig.Agent.Settings.Affinity
spec.Affinity = instanceGroup.Env.AgentEnvBoshConfig.Agent.Settings.Affinity
}

if len(instanceGroup.Env.AgentEnvBoshConfig.Agent.Settings.Tolerations) > 0 {
qJob.Spec.Template.Spec.Template.Spec.Tolerations = instanceGroup.Env.AgentEnvBoshConfig.Agent.Settings.Tolerations
spec.Tolerations = instanceGroup.Env.AgentEnvBoshConfig.Agent.Settings.Tolerations
}

if instanceGroup.Env.AgentEnvBoshConfig.Agent.Settings.ServiceAccountName != "" {
qJob.Spec.Template.Spec.Template.Spec.ServiceAccountName = instanceGroup.Env.AgentEnvBoshConfig.Agent.Settings.ServiceAccountName
spec.ServiceAccountName = instanceGroup.Env.AgentEnvBoshConfig.Agent.Settings.ServiceAccountName
}

if instanceGroup.Env.AgentEnvBoshConfig.Agent.Settings.AutomountServiceAccountToken != nil {
qJob.Spec.Template.Spec.Template.Spec.AutomountServiceAccountToken = instanceGroup.Env.AgentEnvBoshConfig.Agent.Settings.AutomountServiceAccountToken
spec.AutomountServiceAccountToken = instanceGroup.Env.AgentEnvBoshConfig.Agent.Settings.AutomountServiceAccountToken
}

return qJob, nil
Expand Down
1 change: 1 addition & 0 deletions pkg/bosh/manifest/instance_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ type AgentSettings struct {
JobBackoffLimit *int32 `json:"jobBackoffLimit,omitempty"`
PreRenderOps *PreRenderOps `json:"preRenderOps,omitempty"`
InjectReplicasEnv *bool `json:"injectReplicasEnv,omitempty"`
DNS string `json:"dns,omitempty"`
}

// Set overrides labels and annotations with operator-owned metadata.
Expand Down
26 changes: 16 additions & 10 deletions pkg/kube/util/boshdns/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,29 @@ func Validate(m bdm.Manifest) error {
return err
}

// CustomDNSSetting sets the pod dns policy.
func CustomDNSSetting(serviceIP, namespace string) (corev1.DNSPolicy, *corev1.PodDNSConfig) {
ndots := "5"
return corev1.DNSNone, &corev1.PodDNSConfig{
Nameservers: []string{serviceIP},
Searches: []string{
fmt.Sprintf("%s.svc.%s", namespace, clusterDomain),
fmt.Sprintf("svc.%s", clusterDomain),
clusterDomain,
},
Options: []corev1.PodDNSConfigOption{{Name: "ndots", Value: &ndots}},
}
}

// DNSSetting sets the pod dns policy.
func DNSSetting(m bdm.Manifest, serviceIP, namespace string) (corev1.DNSPolicy, *corev1.PodDNSConfig, error) {
index := HasBoshDNSAddOn(m)
if index != -1 {
if serviceIP == "" {
return corev1.DNSNone, nil, errors.New("BoshDomainNameService: DNSSetting called before Apply")
}
ndots := "5"
return corev1.DNSNone, &corev1.PodDNSConfig{
Nameservers: []string{serviceIP},
Searches: []string{
fmt.Sprintf("%s.svc.%s", namespace, clusterDomain),
fmt.Sprintf("svc.%s", clusterDomain),
clusterDomain,
},
Options: []corev1.PodDNSConfigOption{{Name: "ndots", Value: &ndots}},
}, nil
p, c := CustomDNSSetting(serviceIP, namespace)
return p, c, nil
}

return corev1.DNSClusterFirst, nil, nil
Expand Down
10 changes: 10 additions & 0 deletions pkg/kube/util/boshdns/interface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,14 @@ var _ = Describe("Interface", func() {
Expect(config).NotTo(BeNil())
})
})

Context("CustomDNSSetting", func() {
It("returns custom dns", func() {
policy, config := boshdns.CustomDNSSetting("1.2.3.5", "default")
Expect(policy).To(Equal(corev1.DNSNone))
Expect(config).NotTo(BeNil())
Expect(config.Nameservers).To(Equal([]string{"1.2.3.5"}))
Expect(config.Searches).To(ContainElements("default.svc.", "svc.", ""))
})
})
})

0 comments on commit 9456b2a

Please sign in to comment.