Skip to content

Commit

Permalink
chore: set DNS policy as ClusterFirstWithHostNet in host-network (#6942)
Browse files Browse the repository at this point in the history
  • Loading branch information
leon-inf committed Apr 3, 2024
1 parent e4dca0b commit 454ee4a
Show file tree
Hide file tree
Showing 9 changed files with 10 additions and 92 deletions.
2 changes: 2 additions & 0 deletions apis/apps/v1alpha1/componentdefinition_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ type ComponentDefinitionSpec struct {

// Defines the host-network capability and resources.
//
// If set, the DNS policy will be automatically set to "ClusterFirstWithHostNet".
//
// +optional
HostNetwork *HostNetwork `json:"hostNetwork,omitempty"`

Expand Down
9 changes: 0 additions & 9 deletions apis/apps/v1alpha1/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -739,15 +739,6 @@ type HostNetwork struct {
//
// +optional
ContainerPorts []HostNetworkContainerPort `json:"containerPorts,omitempty"`

// Set DNS policy for the component.
// Defaults to "ClusterFirst".
// Valid values are 'ClusterFirstWithHostNet', 'ClusterFirst', 'Default' or 'None'.
// DNS parameters given in DNSConfig will be merged with the policy selected with DNSPolicy.
// To have DNS options set along with hostNetwork, you have to specify DNS policy explicitly to 'ClusterFirstWithHostNet'.
//
// +optional
DNSPolicy *corev1.DNSPolicy `json:"dnsPolicy,omitempty"`
}

type HostNetworkContainerPort struct {
Expand Down
5 changes: 0 additions & 5 deletions apis/apps/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 2 additions & 9 deletions config/crd/bases/apps.kubeblocks.io_componentdefinitions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ spec:
maxLength: 256
type: string
hostNetwork:
description: Defines the host-network capability and resources.
description: "Defines the host-network capability and resources. \n
If set, the DNS policy will be automatically set to \"ClusterFirstWithHostNet\"."
properties:
containerPorts:
description: The list of container ports that are required by
Expand All @@ -212,14 +213,6 @@ spec:
- ports
type: object
type: array
dnsPolicy:
description: Set DNS policy for the component. Defaults to "ClusterFirst".
Valid values are 'ClusterFirstWithHostNet', 'ClusterFirst',
'Default' or 'None'. DNS parameters given in DNSConfig will
be merged with the policy selected with DNSPolicy. To have DNS
options set along with hostNetwork, you have to specify DNS
policy explicitly to 'ClusterFirstWithHostNet'.
type: string
type: object
labels:
additionalProperties:
Expand Down
5 changes: 2 additions & 3 deletions controllers/apps/transformer_component_hostnetwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,8 @@ func allocateHostPortsWithFunc(pm *intctrlutil.PortManager, synthesizedComp *com

func updateObjectsWithAllocatedPorts(synthesizedComp *component.SynthesizedComponent, ports map[string]map[string]int32) error {
synthesizedComp.PodSpec.HostNetwork = true
if synthesizedComp.HostNetwork != nil && synthesizedComp.HostNetwork.DNSPolicy != nil {
synthesizedComp.PodSpec.DNSPolicy = *synthesizedComp.HostNetwork.DNSPolicy
}
synthesizedComp.PodSpec.DNSPolicy = corev1.DNSClusterFirstWithHostNet

for i, c := range synthesizedComp.PodSpec.Containers {
containerPorts, ok := ports[c.Name]
if ok {
Expand Down
11 changes: 2 additions & 9 deletions deploy/helm/crds/apps.kubeblocks.io_componentdefinitions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ spec:
maxLength: 256
type: string
hostNetwork:
description: Defines the host-network capability and resources.
description: "Defines the host-network capability and resources. \n
If set, the DNS policy will be automatically set to \"ClusterFirstWithHostNet\"."
properties:
containerPorts:
description: The list of container ports that are required by
Expand All @@ -212,14 +213,6 @@ spec:
- ports
type: object
type: array
dnsPolicy:
description: Set DNS policy for the component. Defaults to "ClusterFirst".
Valid values are 'ClusterFirstWithHostNet', 'ClusterFirst',
'Default' or 'None'. DNS parameters given in DNSConfig will
be merged with the policy selected with DNSPolicy. To have DNS
options set along with hostNetwork, you have to specify DNS
policy explicitly to 'ClusterFirstWithHostNet'.
type: string
type: object
labels:
additionalProperties:
Expand Down
20 changes: 2 additions & 18 deletions docs/developer_docs/api-reference/cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -1260,6 +1260,7 @@ HostNetwork
<td>
<em>(Optional)</em>
<p>Defines the host-network capability and resources.</p>
<p>If set, the DNS policy will be automatically set to &ldquo;ClusterFirstWithHostNet&rdquo;.</p>
</td>
</tr>
<tr>
Expand Down Expand Up @@ -7093,6 +7094,7 @@ HostNetwork
<td>
<em>(Optional)</em>
<p>Defines the host-network capability and resources.</p>
<p>If set, the DNS policy will be automatically set to &ldquo;ClusterFirstWithHostNet&rdquo;.</p>
</td>
</tr>
<tr>
Expand Down Expand Up @@ -11052,24 +11054,6 @@ int32
<p>The list of container ports that are required by the component.</p>
</td>
</tr>
<tr>
<td>
<code>dnsPolicy</code><br/>
<em>
<a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.25/#dnspolicy-v1-core">
Kubernetes core/v1.DNSPolicy
</a>
</em>
</td>
<td>
<em>(Optional)</em>
<p>Set DNS policy for the component.
Defaults to &ldquo;ClusterFirst&rdquo;.
Valid values are &lsquo;ClusterFirstWithHostNet&rsquo;, &lsquo;ClusterFirst&rsquo;, &lsquo;Default&rsquo; or &lsquo;None&rsquo;.
DNS parameters given in DNSConfig will be merged with the policy selected with DNSPolicy.
To have DNS options set along with hostNetwork, you have to specify DNS policy explicitly to &lsquo;ClusterFirstWithHostNet&rsquo;.</p>
</td>
</tr>
</tbody>
</table>
<h3 id="apps.kubeblocks.io/v1alpha1.HostNetworkContainerPort">HostNetworkContainerPort
Expand Down
6 changes: 0 additions & 6 deletions pkg/controller/component/component_definition_convertor.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,12 +230,6 @@ func convertHostNetwork(clusterCompDef *appsv1alpha1.ClusterComponentDefinition)
hostNetwork.ContainerPorts = append(hostNetwork.ContainerPorts, cp)
}
}
if len(clusterCompDef.PodSpec.DNSPolicy) > 0 {
hostNetwork.DNSPolicy = func() *corev1.DNSPolicy {
policy := clusterCompDef.PodSpec.DNSPolicy
return &policy
}()
}
return hostNetwork, nil
}

Expand Down
33 changes: 0 additions & 33 deletions pkg/controller/component/component_definition_convertor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,6 @@ var _ = Describe("Component Definition Convertor", func() {
hostNetwork, ok := res.(*appsv1alpha1.HostNetwork)
Expect(ok).Should(BeTrue())
Expect(hostNetwork.ContainerPorts).Should(HaveLen(0))
Expect(hostNetwork.DNSPolicy).Should(BeNil())
})

It("no dynamic ports", func() {
Expand All @@ -504,7 +503,6 @@ var _ = Describe("Component Definition Convertor", func() {
hostNetwork, ok := res.(*appsv1alpha1.HostNetwork)
Expect(ok).Should(BeTrue())
Expect(hostNetwork.ContainerPorts).Should(HaveLen(0))
Expect(hostNetwork.DNSPolicy).Should(BeNil())
})

It("part dynamic ports", func() {
Expand All @@ -524,37 +522,6 @@ var _ = Describe("Component Definition Convertor", func() {
Expect(hostNetwork.ContainerPorts[0].Container).Should(Equal(container.Name))
Expect(hostNetwork.ContainerPorts[0].Ports).Should(HaveLen(1))
Expect(hostNetwork.ContainerPorts[0].Ports[0]).Should(Equal(container.Ports[0].Name))
Expect(hostNetwork.DNSPolicy).Should(BeNil())
})

It("w/ dns policy ClusterFirst", func() {
clusterCompDef.PodSpec.HostNetwork = true
clusterCompDef.PodSpec.DNSPolicy = corev1.DNSClusterFirst

convertor := &compDefHostNetworkConvertor{}
res, err := convertor.convert(clusterCompDef)
Expect(err).Should(Succeed())
Expect(res).ShouldNot(BeNil())

hostNetwork, ok := res.(*appsv1alpha1.HostNetwork)
Expect(ok).Should(BeTrue())
Expect(hostNetwork.DNSPolicy).ShouldNot(BeNil())
Expect(*hostNetwork.DNSPolicy).Should(Equal(corev1.DNSClusterFirst))
})

It("w/ dns policy non-ClusterFirst", func() {
clusterCompDef.PodSpec.HostNetwork = true
clusterCompDef.PodSpec.DNSPolicy = corev1.DNSClusterFirstWithHostNet

convertor := &compDefHostNetworkConvertor{}
res, err := convertor.convert(clusterCompDef)
Expect(err).Should(Succeed())
Expect(res).ShouldNot(BeNil())

hostNetwork, ok := res.(*appsv1alpha1.HostNetwork)
Expect(ok).Should(BeTrue())
Expect(hostNetwork.DNSPolicy).ShouldNot(BeNil())
Expect(*hostNetwork.DNSPolicy).Should(Equal(corev1.DNSClusterFirstWithHostNet))
})
})

Expand Down

0 comments on commit 454ee4a

Please sign in to comment.