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

OCPBUGS-11888: handle daemonSet pods restart #347

Merged
merged 1 commit into from
May 18, 2023
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
6 changes: 3 additions & 3 deletions controllers/ingressnodefirewallnodestate_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ func (r *IngressNodeFirewallNodeStateReconciler) Reconcile(ctx context.Context,
// Request object not found, could have been deleted after reconcile request.
// Owned objects are automatically garbage collected. For additional cleanup logic use finalizers.
// Return and don't requeue
return r.reconcileResource(ctx, req, nodeState, true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: The code changes in ingressnofirewallnodestate_controller.go and in ebpfsyncer.go are not related, correct? So perhaps break this into 2 commits?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah that was just unused arg

return r.reconcileResource(ctx, nodeState, true)
}
// Error reading the object - requeue the request.
r.Log.Error(err, "Failed to get IngressNodeFirewallNodeState")
return ctrl.Result{}, err
}

r.Log.Info("Reconciling resource and programming bpf", "name", nodeState.Name, "namespace", nodeState.Namespace)
return r.reconcileResource(ctx, req, nodeState, false)
return r.reconcileResource(ctx, nodeState, false)
}

// SetupWithManager sets up the controller with the Manager.
Expand All @@ -90,7 +90,7 @@ var mock ebpfsyncer.EbpfSyncer = nil
// reconcileResource reconciles the resource by getting the EbpfDaemon singleton's SyncInterfaceIngressRules method.
// For mock tests, var mock can be overwritten.
func (r *IngressNodeFirewallNodeStateReconciler) reconcileResource(
ctx context.Context, req ctrl.Request, instance *infv1alpha1.IngressNodeFirewallNodeState, isDelete bool) (ctrl.Result, error) {
ctx context.Context, instance *infv1alpha1.IngressNodeFirewallNodeState, isDelete bool) (ctrl.Result, error) {
if err := ebpfsyncer.GetEbpfSyncer(ctx, r.Log, r.Stats, mock).SyncInterfaceIngressRules(instance.Spec.InterfaceIngressRules, isDelete); err != nil {
return ctrl.Result{}, errors.Wrapf(err, "FailedToSyncIngressNodeFirewallResources")
}
Expand Down
14 changes: 14 additions & 0 deletions pkg/ebpfsyncer/ebpfsyncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package ebpfsyncer
import (
"context"
"fmt"
"os"
"os/signal"
"strings"
"sync"
"syscall"

"github.com/openshift/ingress-node-firewall/api/v1alpha1"
infv1alpha1 "github.com/openshift/ingress-node-firewall/api/v1alpha1"
Expand Down Expand Up @@ -72,6 +75,8 @@ func (e *ebpfSingleton) SyncInterfaceIngressRules(
logger := e.log.WithName("syncIngressNodeFirewallResources")
logger.Info("Running sync operation", "ifaceIngressRules", ifaceIngressRules, "isDelete", isDelete)

sigc := make(chan os.Signal, 1)

// Stop the poller for the time of this operation and start it again afterwards.
if e.stats != nil {
e.stats.StopPoll()
Expand All @@ -82,6 +87,15 @@ func (e *ebpfSingleton) SyncInterfaceIngressRules(
}()
}

signal.Notify(sigc, os.Interrupt, syscall.SIGTERM)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you test if you can still sig TERM the process (if this is needed)?
In a standalone test process, I can't ctrl-c / sig TERM the process any more after doing this, but it might behave differently with the Operator SDK


I tested this quickly and kill -9 obviously works still, but the TERM signal doesn't shut down the process any more:

$ cat main.go
package main

import (
	"fmt"
	"os"
	"os/signal"
	"syscall"
	"time"
)

type eStruct struct {
	c interface{}
}

func (e eStruct) resetAll() {
	fmt.Println("Reset: ", e.c)
}

func foo(e eStruct) {
	fmt.Println("foo", e)

	sigc := make(chan os.Signal, 1)
	signal.Notify(sigc, os.Interrupt, syscall.SIGTERM)
	go func(c chan os.Signal) {
		// Wait for a SIGTERM
		<-c
		if e.c != nil {
			e.resetAll()
		}
	}(sigc)
}

func main() {
	e := eStruct{
		c: "test",
	}
	foo(e)
	time.Sleep(15 * time.Second)
	fmt.Println("15 seconds are over, normal shutdown")
}
$ ./m & pid=$! ; sleep 2; kill $pid ; wait $pid
[1] 81616
foo {test}
Reset:  test
15 seconds are over, normal shutdown
[1]+  Done                    ./m

When I add os.Exit(0), then the program will shut down upon receiving the signal:

 19 func foo(e eStruct) {
   20     fmt.Println("foo", e)
   21 
   22     sigc := make(chan os.Signal, 1)
   23     signal.Notify(sigc, os.Interrupt, syscall.SIGTERM)
   24     go func(c chan os.Signal) {
   25         // Wait for a SIGTERM
   26         <-c
   27         if e.c != nil {
   28             e.resetAll()
   29         }
   30         os.Exit(0)
   31     }(sigc)
   32 }
   33 
[akaris@linux test-signal]$ ./m & pid=$! ; sleep 2; kill $pid ; wait $pid
[1] 82337
foo {test}
Reset:  test
[1]+  Done 

This is not a suggestion to add os.Exit, it's merely a question from my side

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when u delete ds or delete the pods with something like
oc delete pod -l=app=ingress-node-firewall-daemon -n openshift-ingress-node-firewall SIGTERM will be sent and the controller runtime will be bring down all processes so I don't need to exit the process when the signal is detected I just need to cleanup and left the normal bring down flow continue did this answer ur question ? or I missunderstood ur question ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah my question is for the opposite case: when you run kill $pid, will the application be torn down and will the pod restart. Or if you test the process standalone, can you still CTRL-C and it will shut down

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will try it out and see if I can do much in separate PR thanks for the review!!!

go func(c chan os.Signal) {
// Wait for a SIGTERM
<-c
if e.c != nil {
e.resetAll()
}
}(sigc)

// Create a new manager if none exists.
if err := e.createNewManager(); err != nil {
return err
Expand Down