Skip to content

Commit 54881d2

Browse files
authored
add flag based fix to avoid default endpoint (#7691)
1 parent f0a5755 commit 54881d2

File tree

4 files changed

+114
-1
lines changed

4 files changed

+114
-1
lines changed

cmd/nginx-ingress/main.go

+2
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ func main() {
323323
NICVersion: version,
324324
DynamicWeightChangesReload: *enableDynamicWeightChangesReload,
325325
InstallationFlags: parsedFlags,
326+
ShuttingDown: false,
326327
}
327328

328329
lbc := k8s.NewLoadBalancerController(lbcInput)
@@ -816,6 +817,7 @@ func handleTermination(lbc *k8s.LoadBalancerController, nginxManager nginx.Manag
816817
nl.Fatalf(lbc.Logger, "AppProtectDosAgent command exited unexpectedly with status: %v", err)
817818
case <-signalChan:
818819
nl.Info(lbc.Logger, "Received SIGTERM, shutting down")
820+
lbc.ShuttingDown = true
819821
lbc.Stop()
820822
nginxManager.Quit()
821823
<-cpcfg.nginxDone

internal/k8s/configmap.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ func (lbc *LoadBalancerController) syncConfigMap(task task) {
8282
key := task.Key
8383
nl.Debugf(lbc.Logger, "Syncing configmap %v", key)
8484

85+
if key == lbc.mgmtConfigMapName && lbc.isPodMarkedForDeletion() {
86+
nl.Debugf(lbc.Logger, "Pod is shutting down, skipping management ConfigMap sync")
87+
return
88+
}
89+
8590
switch key {
8691
case lbc.nginxConfigMapName:
8792
obj, configExists, err := lbc.configMapLister.GetByKey(key)
@@ -120,6 +125,9 @@ func (lbc *LoadBalancerController) syncConfigMap(task task) {
120125
nl.Debugf(lbc.Logger, "Skipping ConfigMap update because batch sync is on")
121126
return
122127
}
123-
128+
if err := lbc.ctx.Err(); err != nil {
129+
nl.Debugf(lbc.Logger, "Context canceled, skipping ConfigMap sync for %v: %v", task.Key, err)
130+
return
131+
}
124132
lbc.updateAllConfigs()
125133
}

internal/k8s/controller.go

+20
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ type LoadBalancerController struct {
185185
weightChangesDynamicReload bool
186186
nginxConfigMapName string
187187
mgmtConfigMapName string
188+
ShuttingDown bool
188189
}
189190

190191
var keyFunc = cache.DeletionHandlingMetaNamespaceKeyFunc
@@ -241,6 +242,7 @@ type NewLoadBalancerControllerInput struct {
241242
NICVersion string
242243
DynamicWeightChangesReload bool
243244
InstallationFlags []string
245+
ShuttingDown bool
244246
}
245247

246248
// NewLoadBalancerController creates a controller
@@ -286,6 +288,7 @@ func NewLoadBalancerController(input NewLoadBalancerControllerInput) *LoadBalanc
286288
weightChangesDynamicReload: input.DynamicWeightChangesReload,
287289
nginxConfigMapName: input.ConfigMaps,
288290
mgmtConfigMapName: input.MGMTConfigMap,
291+
ShuttingDown: input.ShuttingDown,
289292
}
290293

291294
lbc.syncQueue = newTaskQueue(lbc.Logger, lbc.sync)
@@ -3649,3 +3652,20 @@ func (lbc *LoadBalancerController) createCombinedDeploymentHeadlessServiceName()
36493652
combinedDeployment := fmt.Sprintf("%s-%s", name, strings.ToLower(owner.Kind))
36503653
return combinedDeployment
36513654
}
3655+
3656+
func (lbc *LoadBalancerController) isPodMarkedForDeletion() bool {
3657+
// Check if the controller is shutting down
3658+
if lbc.ShuttingDown {
3659+
nl.Debugf(lbc.Logger, "SIGTERM already received, controller is shutting down")
3660+
return true
3661+
}
3662+
podName := lbc.metadata.pod.Name
3663+
podNamespace := lbc.metadata.pod.Namespace
3664+
pod, err := lbc.client.CoreV1().Pods(podNamespace).Get(context.Background(), podName, meta_v1.GetOptions{})
3665+
if err == nil && pod.DeletionTimestamp != nil {
3666+
nl.Debugf(lbc.Logger, "Pod %s/%s is marked for deletion", podNamespace, podName)
3667+
return true
3668+
}
3669+
3670+
return false
3671+
}

internal/k8s/controller_test.go

+83
Original file line numberDiff line numberDiff line change
@@ -3639,3 +3639,86 @@ func TestCreateIngressExWithZoneSync(t *testing.T) {
36393639
}
36403640
}
36413641
}
3642+
3643+
func TestIsPodMarkedForDeletion(t *testing.T) {
3644+
t.Parallel()
3645+
3646+
logger := nl.LoggerFromContext(context.Background())
3647+
3648+
tests := []struct {
3649+
name string
3650+
shutdownFlag bool
3651+
envPodName string
3652+
envPodNamespace string
3653+
podExists bool
3654+
podHasTimestamp bool
3655+
expectedResult bool
3656+
}{
3657+
{
3658+
name: "controller is shutting down",
3659+
shutdownFlag: true,
3660+
expectedResult: true,
3661+
},
3662+
{
3663+
name: "pod exists with deletion timestamp",
3664+
envPodName: "test-pod",
3665+
envPodNamespace: "test-namespace",
3666+
podExists: true,
3667+
podHasTimestamp: true,
3668+
expectedResult: true,
3669+
},
3670+
{
3671+
name: "pod exists without deletion timestamp",
3672+
envPodName: "test-pod",
3673+
envPodNamespace: "test-namespace",
3674+
podExists: true,
3675+
podHasTimestamp: false,
3676+
expectedResult: false,
3677+
},
3678+
}
3679+
3680+
for _, test := range tests {
3681+
test := test
3682+
t.Run(test.name, func(t *testing.T) {
3683+
client := fake.NewSimpleClientset()
3684+
if test.podExists {
3685+
pod := &api_v1.Pod{
3686+
ObjectMeta: meta_v1.ObjectMeta{
3687+
Name: test.envPodName,
3688+
Namespace: test.envPodNamespace,
3689+
},
3690+
}
3691+
3692+
if test.podHasTimestamp {
3693+
now := meta_v1.Now()
3694+
pod.DeletionTimestamp = &now
3695+
}
3696+
3697+
_, err := client.CoreV1().Pods(test.envPodNamespace).Create(context.Background(), pod, meta_v1.CreateOptions{})
3698+
if err != nil {
3699+
t.Fatalf("Error creating pod: %v", err)
3700+
}
3701+
}
3702+
3703+
lbc := &LoadBalancerController{
3704+
client: client,
3705+
metadata: controllerMetadata{
3706+
pod: &api_v1.Pod{
3707+
ObjectMeta: meta_v1.ObjectMeta{
3708+
Name: test.envPodName,
3709+
Namespace: test.envPodNamespace,
3710+
},
3711+
},
3712+
},
3713+
ShuttingDown: test.shutdownFlag,
3714+
Logger: logger,
3715+
}
3716+
3717+
// Call the function and verify result
3718+
result := lbc.isPodMarkedForDeletion()
3719+
if result != test.expectedResult {
3720+
t.Errorf("Returned %v but expected %v", result, test.expectedResult)
3721+
}
3722+
})
3723+
}
3724+
}

0 commit comments

Comments
 (0)