From 454ee4ad8562c64529d85b7bcd44062efc1dfcfb Mon Sep 17 00:00:00 2001 From: Leon Date: Wed, 3 Apr 2024 12:45:38 +0800 Subject: [PATCH] chore: set DNS policy as ClusterFirstWithHostNet in host-network (#6942) --- .../v1alpha1/componentdefinition_types.go | 2 ++ apis/apps/v1alpha1/type.go | 9 ----- apis/apps/v1alpha1/zz_generated.deepcopy.go | 5 --- ...ps.kubeblocks.io_componentdefinitions.yaml | 11 ++----- .../apps/transformer_component_hostnetwork.go | 5 ++- ...ps.kubeblocks.io_componentdefinitions.yaml | 11 ++----- docs/developer_docs/api-reference/cluster.md | 20 ++--------- .../component_definition_convertor.go | 6 ---- .../component_definition_convertor_test.go | 33 ------------------- 9 files changed, 10 insertions(+), 92 deletions(-) diff --git a/apis/apps/v1alpha1/componentdefinition_types.go b/apis/apps/v1alpha1/componentdefinition_types.go index 14416c1b3b3..30a94894aeb 100644 --- a/apis/apps/v1alpha1/componentdefinition_types.go +++ b/apis/apps/v1alpha1/componentdefinition_types.go @@ -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"` diff --git a/apis/apps/v1alpha1/type.go b/apis/apps/v1alpha1/type.go index aa4564e3ebf..56226ee338b 100644 --- a/apis/apps/v1alpha1/type.go +++ b/apis/apps/v1alpha1/type.go @@ -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 { diff --git a/apis/apps/v1alpha1/zz_generated.deepcopy.go b/apis/apps/v1alpha1/zz_generated.deepcopy.go index b5a52c5764e..23b4209f4e2 100644 --- a/apis/apps/v1alpha1/zz_generated.deepcopy.go +++ b/apis/apps/v1alpha1/zz_generated.deepcopy.go @@ -3480,11 +3480,6 @@ func (in *HostNetwork) DeepCopyInto(out *HostNetwork) { (*in)[i].DeepCopyInto(&(*out)[i]) } } - if in.DNSPolicy != nil { - in, out := &in.DNSPolicy, &out.DNSPolicy - *out = new(v1.DNSPolicy) - **out = **in - } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new HostNetwork. diff --git a/config/crd/bases/apps.kubeblocks.io_componentdefinitions.yaml b/config/crd/bases/apps.kubeblocks.io_componentdefinitions.yaml index 96eb774eab3..2dd295fb642 100644 --- a/config/crd/bases/apps.kubeblocks.io_componentdefinitions.yaml +++ b/config/crd/bases/apps.kubeblocks.io_componentdefinitions.yaml @@ -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 @@ -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: diff --git a/controllers/apps/transformer_component_hostnetwork.go b/controllers/apps/transformer_component_hostnetwork.go index 0d1d3524093..a047813c07d 100644 --- a/controllers/apps/transformer_component_hostnetwork.go +++ b/controllers/apps/transformer_component_hostnetwork.go @@ -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 { diff --git a/deploy/helm/crds/apps.kubeblocks.io_componentdefinitions.yaml b/deploy/helm/crds/apps.kubeblocks.io_componentdefinitions.yaml index 96eb774eab3..2dd295fb642 100644 --- a/deploy/helm/crds/apps.kubeblocks.io_componentdefinitions.yaml +++ b/deploy/helm/crds/apps.kubeblocks.io_componentdefinitions.yaml @@ -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 @@ -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: diff --git a/docs/developer_docs/api-reference/cluster.md b/docs/developer_docs/api-reference/cluster.md index d720c7899ca..95df04800fd 100644 --- a/docs/developer_docs/api-reference/cluster.md +++ b/docs/developer_docs/api-reference/cluster.md @@ -1260,6 +1260,7 @@ HostNetwork (Optional)

Defines the host-network capability and resources.

+

If set, the DNS policy will be automatically set to “ClusterFirstWithHostNet”.

@@ -7093,6 +7094,7 @@ HostNetwork (Optional)

Defines the host-network capability and resources.

+

If set, the DNS policy will be automatically set to “ClusterFirstWithHostNet”.

@@ -11052,24 +11054,6 @@ int32

The list of container ports that are required by the component.

- - -dnsPolicy
- - -Kubernetes core/v1.DNSPolicy - - - - -(Optional) -

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’.

- -

HostNetworkContainerPort diff --git a/pkg/controller/component/component_definition_convertor.go b/pkg/controller/component/component_definition_convertor.go index 211c1ec3e23..e405f559a74 100644 --- a/pkg/controller/component/component_definition_convertor.go +++ b/pkg/controller/component/component_definition_convertor.go @@ -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 } diff --git a/pkg/controller/component/component_definition_convertor_test.go b/pkg/controller/component/component_definition_convertor_test.go index b9842eef683..8de1695f88a 100644 --- a/pkg/controller/component/component_definition_convertor_test.go +++ b/pkg/controller/component/component_definition_convertor_test.go @@ -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() { @@ -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() { @@ -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)) }) })