Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KO-348: Skip setting hostPort in Aerospike container for podOnly network and multiPodPerHost: false #316

Merged
merged 4 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion internal/controller/cluster/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ func (r *SingleClusterReconciler) Reconcile() (result ctrl.Result, recErr error)
if r.IsReclusterNeeded() {
if err = deployment.InfoRecluster(
r.Log,
r.getClientPolicy(), allHostConns,
policy, allHostConns,
); err != nil {
r.Log.Error(err, "Failed to do recluster")
return reconcile.Result{}, err
Expand Down Expand Up @@ -1041,6 +1041,8 @@ func (r *SingleClusterReconciler) IsReclusterNeeded() bool {
return false
}

// Check for any active-rack addition/update across all the namespaces.
// If there is any active-rack change, recluster is required.
for specIdx := range r.aeroCluster.Spec.RackConfig.Racks {
for statusIdx := range r.aeroCluster.Status.RackConfig.Racks {
if r.aeroCluster.Spec.RackConfig.Racks[specIdx].ID == r.aeroCluster.Status.RackConfig.Racks[statusIdx].ID &&
Expand Down
21 changes: 18 additions & 3 deletions internal/controller/cluster/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func (r *SingleClusterReconciler) createSTS(
ports := getSTSContainerPort(
r.aeroCluster.Spec.PodSpec.MultiPodPerHost,
r.aeroCluster.Spec.AerospikeConfig,
&r.aeroCluster.Spec.AerospikeNetworkPolicy,
)

operatorDefinedLabels := utils.LabelsForAerospikeClusterRack(
Expand Down Expand Up @@ -608,6 +609,7 @@ func (r *SingleClusterReconciler) updateSTSPorts(
ports := getSTSContainerPort(
r.aeroCluster.Spec.PodSpec.MultiPodPerHost,
r.aeroCluster.Spec.AerospikeConfig,
&r.aeroCluster.Spec.AerospikeNetworkPolicy,
)

st.Spec.Template.Spec.Containers[0].Ports = ports
Expand Down Expand Up @@ -1539,12 +1541,24 @@ func addVolumeDeviceInContainer(
}

func getSTSContainerPort(
multiPodPerHost *bool, aeroConf *asdbv1.AerospikeConfigSpec,
multiPodPerHost *bool, aeroConf *asdbv1.AerospikeConfigSpec, aeroNetworkPolicy *asdbv1.AerospikeNetworkPolicy,
) []corev1.ContainerPort {
ports := make([]corev1.ContainerPort, 0, len(defaultContainerPorts))
portNames := make([]string, 0, len(defaultContainerPorts))
podOnlyNetwork := true

// Sorting defaultContainerPorts to fetch map in ordered manner.
// Check for podOnlyNetwork for all TLS and nonTLS fields.
if svcPort := asdbv1.GetServicePort(aeroConf); svcPort != nil {
podOnlyNetwork = aeroNetworkPolicy.AccessType == asdbv1.AerospikeNetworkTypePod &&
aeroNetworkPolicy.AlternateAccessType == asdbv1.AerospikeNetworkTypePod
}

if _, tlsSvcPort := asdbv1.GetServiceTLSNameAndPort(aeroConf); tlsSvcPort != nil {
podOnlyNetwork = podOnlyNetwork && aeroNetworkPolicy.TLSAccessType == asdbv1.AerospikeNetworkTypePod &&
aeroNetworkPolicy.TLSAlternateAccessType == asdbv1.AerospikeNetworkTypePod
}

// Sorting defaultContainerPorts to fetch map in an ordered manner.
// Helps reduce unnecessary sts object updates.
for portName := range defaultContainerPorts {
portNames = append(portNames, portName)
Expand All @@ -1567,11 +1581,12 @@ func getSTSContainerPort(
ContainerPort: int32(*configPort),
}
// Single pod per host. Enable hostPort setting
// when pod only network is not defined.
// The hostPort setting applies to the Kubernetes containers.
// The container port will be exposed to the external network at <hostIP>:<hostPort>,
// where the hostIP is the IP address of the Kubernetes node where
// the container is running and the hostPort is the port requested by the user
if !asdbv1.GetBool(multiPodPerHost) && portInfo.exposedOnHost {
if !asdbv1.GetBool(multiPodPerHost) && portInfo.exposedOnHost && !podOnlyNetwork {
containerPort.HostPort = containerPort.ContainerPort
}

Expand Down
35 changes: 34 additions & 1 deletion test/cluster/network_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -708,9 +708,42 @@ func doTestNetworkPolicy(
},
)

It("PodOnlyNetwork: Should not set the hostPort", func() {
clusterNamespacedName := getNamespacedName(
"pod-network-cluster", test.MultiClusterNs1)

networkPolicy := asdbv1.AerospikeNetworkPolicy{
AccessType: asdbv1.AerospikeNetworkTypePod,
AlternateAccessType: asdbv1.AerospikeNetworkTypePod,
TLSAccessType: asdbv1.AerospikeNetworkTypePod,
TLSAlternateAccessType: asdbv1.AerospikeNetworkTypePod,
}

aeroCluster = getAerospikeClusterSpecWithNetworkPolicy(
clusterNamespacedName, &networkPolicy, multiPodPerHost,
enableTLS,
)

err := aerospikeClusterCreateUpdate(k8sClient, aeroCluster, ctx)
Expect(err).ToNot(HaveOccurred())

podList, err := getPodList(aeroCluster, k8sClient)
Expect(err).ToNot(HaveOccurred())
Expect(len(podList.Items)).ToNot(BeZero())

for idx := range podList.Items {
pod := &podList.Items[idx]
container := pod.Spec.Containers[0]
Expect(container.Ports).ToNot(BeNil())

for _, port := range container.Ports {
Expect(port.HostPort).To(BeZero())
}
}
})
// test-case valid only for multiPodPerHost true
if multiPodPerHost {
It("OnlyPodNetwork: should create cluster without nodePort service", func() {
It("PodOnlyNetwork: should create cluster without nodePort service", func() {
clusterNamespacedName := getNamespacedName(
"pod-network-cluster", test.MultiClusterNs1)

Expand Down