Skip to content

Commit

Permalink
provision/kubernetes: add dns-config-ndots cluster option for custom …
Browse files Browse the repository at this point in the history
…dns ndots
  • Loading branch information
morpheu authored and wpjunior committed Mar 14, 2022
1 parent a73d7c6 commit b0ec8ec
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
10 changes: 10 additions & 0 deletions provision/kubernetes/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ const (
defaultLogsFromAPIServer = false
versionedServices = "enable-versioned-services"
dockerConfigJSONKey = "docker-config-json"
dnsConfigNdotsKey = "dns-config-ndots"

dialTimeout = 30 * time.Second
tcpKeepAlive = 30 * time.Second
Expand Down Expand Up @@ -105,6 +106,7 @@ var (
versionedServices: "Allow the creation of multiple services for each pair of {process, version} from the app. The default behavior creates versioned services only in a multi versioned deploy scenario.",
dockerConfigJSONKey: "Custom Docker config (~/.docker/config.json) to be mounted on deploy-agent container",
disablePDBKey: "Disable PodDisruptionBudget for entire pool.",
dnsConfigNdotsKey: "Number of dots in the domain name to be used in the search list for DNS lookups. Default to uses kubernetes default value (5).",
}
)

Expand Down Expand Up @@ -493,6 +495,14 @@ func (c *ClusterClient) maxUnavailable(pool string) intstr.IntOrString {
return intstr.Parse(maxUnavailable)
}

func (c *ClusterClient) dnsConfigNdots(pool string) intstr.IntOrString {
DNSConfigNdots := c.configForContext(pool, dnsConfigNdotsKey)
if DNSConfigNdots == "" {
return intstr.FromInt(0)
}
return intstr.Parse(DNSConfigNdots)
}

func (c *ClusterClient) SinglePool() (bool, error) {
if c.CustomData == nil {
return false, nil
Expand Down
20 changes: 19 additions & 1 deletion provision/kubernetes/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ func createAppDeployment(ctx context.Context, client *ClusterClient, depName str
}
maxSurge := client.maxSurge(a.GetPool())
maxUnavailable := client.maxUnavailable(a.GetPool())

dnsConfig := dnsConfigNdots(client, a)
nodeSelector, affinity, err := defineSelectorAndAffinity(ctx, a, client)
if err != nil {
return nil, nil, err
Expand Down Expand Up @@ -782,6 +782,7 @@ func createAppDeployment(ctx context.Context, client *ClusterClient, depName str
Volumes: volumes,
Subdomain: headlessServiceName(a, process),
ReadinessGates: readinessGates,
DNSConfig: dnsConfig,
Containers: []apiv1.Container{
{
Name: depName,
Expand Down Expand Up @@ -1875,6 +1876,7 @@ type deployAgentConfig struct {
}

func newDeployAgentPod(ctx context.Context, params createPodParams, conf deployAgentConfig) (apiv1.Pod, error) {
dnsConfig := dnsConfigNdots(params.client, params.app)
if len(conf.destinationImages) == 0 {
return apiv1.Pod{}, errors.Errorf("no destination images provided")
}
Expand Down Expand Up @@ -1947,6 +1949,7 @@ func newDeployAgentPod(ctx context.Context, params createPodParams, conf deployA
ServiceAccountName: params.client.buildServiceAccount(params.app),
NodeSelector: nodeSelector,
Affinity: affinity,
DNSConfig: dnsConfig,
Volumes: append(deployAgentEngineVolumes(pullSecrets), append([]apiv1.Volume{
{
Name: "intercontainer",
Expand All @@ -1964,6 +1967,21 @@ func newDeployAgentPod(ctx context.Context, params createPodParams, conf deployA
}, nil
}

func dnsConfigNdots(client *ClusterClient, app provision.App) *apiv1.PodDNSConfig {
dnsConfigNdots := client.dnsConfigNdots(app.GetPool())
var dnsConfig *apiv1.PodDNSConfig
if dnsConfigNdots.IntVal > 0 {
ndots := dnsConfigNdots.String()
dnsConfig = &apiv1.PodDNSConfig{
Options: []apiv1.PodDNSConfigOption{{
Name: "ndots",
Value: &ndots,
}},
}
}
return dnsConfig
}

func newDeployAgentImageBuildPod(ctx context.Context, client *ClusterClient, sourceImage string, podName string, conf deployAgentConfig) (apiv1.Pod, error) {
if len(conf.destinationImages) == 0 {
return apiv1.Pod{}, errors.Errorf("no destination images provided")
Expand Down
35 changes: 34 additions & 1 deletion provision/kubernetes/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2536,6 +2536,36 @@ func (s *S) TestServiceManagerDeploySinglePoolEnable(c *check.C) {
c.Assert(dep.Spec.Template.Spec.NodeSelector, check.DeepEquals, map[string]string(nil))
}

func (s *S) TestServiceManagerDeployDnsConfigNdotsEnable(c *check.C) {
waitDep := s.mock.DeploymentReactions(c)
defer waitDep()
s.clusterClient.CustomData[dnsConfigNdotsKey] = "1"
m := serviceManager{client: s.clusterClient}
a := &app.App{Name: "myapp", TeamOwner: s.team.Name}
err := app.CreateApp(context.TODO(), a, s.user)
c.Assert(err, check.IsNil)
version := newCommittedVersion(c, a, map[string]interface{}{
"processes": map[string]interface{}{
"p1": "cm1",
},
})
c.Assert(err, check.IsNil)
err = servicecommon.RunServicePipeline(context.TODO(), &m, 0, provision.DeployArgs{
App: a,
Version: version,
}, servicecommon.ProcessSpec{
"p1": servicecommon.ProcessState{Start: true},
})
c.Assert(err, check.IsNil)
waitDep()
ns, err := s.client.AppNamespace(context.TODO(), a)
c.Assert(err, check.IsNil)
dep, err := s.client.Clientset.AppsV1().Deployments(ns).Get(context.TODO(), "myapp-p1", metav1.GetOptions{})
c.Assert(err, check.IsNil)
dnsConfigNdotsValue := "1"
c.Assert(dep.Spec.Template.Spec.DNSConfig, check.DeepEquals, &apiv1.PodDNSConfig{Options: []apiv1.PodDNSConfigOption{{Name: "ndots", Value: &dnsConfigNdotsValue}}})
}

func (s *S) TestServiceManagerDeployServiceWithPreserveVersions(c *check.C) {
waitDep := s.mock.DeploymentReactions(c)
defer waitDep()
Expand Down Expand Up @@ -3460,7 +3490,7 @@ func (s *S) TestCreatePodContainersWithPoolBuildPlan(c *check.C) {
})
}

func (s *S) TestCreatePodContainersWithPoolBuildPlanAndSidecarBuildPlan(c *check.C) {
func (s *S) TestCreatePodContainersWithPoolBuildPlanAndSidecarBuildPlanAndDnsConfigNdots(c *check.C) {
s.mockService.Plan.OnFindByName = func(plan string) (*appTypes.Plan, error) {
if plan == "c0.5m1" {
return &appTypes.Plan{
Expand All @@ -3475,6 +3505,7 @@ func (s *S) TestCreatePodContainersWithPoolBuildPlanAndSidecarBuildPlan(c *check
Memory: int64(2147483648),
}, nil
}
s.clusterClient.CustomData[dnsConfigNdotsKey] = "1"
a, _, rollback := s.mock.DefaultReactions(c)
defer rollback()
err := s.p.Provision(context.TODO(), a)
Expand All @@ -3498,6 +3529,8 @@ func (s *S) TestCreatePodContainersWithPoolBuildPlanAndSidecarBuildPlan(c *check
c.Assert(pods.Items, check.HasLen, 1)
containers := pods.Items[0].Spec.Containers
c.Assert(containers, check.HasLen, 2)
dnsConfigNdotsValue := "1"
c.Assert(pods.Items[0].Spec.DNSConfig, check.DeepEquals, &apiv1.PodDNSConfig{Options: []apiv1.PodDNSConfigOption{{Name: "ndots", Value: &dnsConfigNdotsValue}}})
sort.Slice(containers, func(i, j int) bool { return containers[i].Name < containers[j].Name })
cpuQuotaBuild, _ := resource.ParseQuantity("1000m") // 1vCPU
memoryQuotaBuild, _ := resource.ParseQuantity("2147483648") // 2Gi
Expand Down

0 comments on commit b0ec8ec

Please sign in to comment.