add signals based on controller-runtime signals handler

This commit is contained in:
2021-11-21 12:24:11 +01:00
parent 8f40c6fc53
commit 9ae20eab1e
7 changed files with 235 additions and 13 deletions

20
signals/doc.go Normal file
View File

@ -0,0 +1,20 @@
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package signals contains libraries for handling signals to gracefully
// shutdown the manager in combination with Kubernetes pod graceful termination
// policy.
package signals

52
signals/signal.go Normal file
View File

@ -0,0 +1,52 @@
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package signals
import (
"context"
"os"
"os/signal"
)
var onlyOneSignalHandler = make(chan struct{})
// SetupSignalHandler registers for SIGTERM and SIGINT. A context is returned
// which is canceled on one of these signals. If a second signal is caught, the program
// is terminated with exit code 1.
func SetupSignalHandler() context.Context {
return SetupSignalHandlerWithContext(context.Background())
}
// SetupSignalHandlerWithContext registers for SIGTERM and SIGINT. A context using the
// given context as parent is returned which is canceled on one of these signals.
// If a second signal is caught, the program is terminated with exit code 1.
func SetupSignalHandlerWithContext(ctx context.Context) context.Context {
close(onlyOneSignalHandler) // panics when called twice
ctx, cancel := context.WithCancel(ctx)
c := make(chan os.Signal, 2)
signal.Notify(c, shutdownSignals...)
go func() {
<-c
cancel()
<-c
os.Exit(1) // second signal. Exit directly.
}()
return ctx
}

26
signals/signal_posix.go Normal file
View File

@ -0,0 +1,26 @@
// +build !windows
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package signals
import (
"os"
"syscall"
)
var shutdownSignals = []os.Signal{os.Interrupt, syscall.SIGTERM}

75
signals/signal_test.go Normal file
View File

@ -0,0 +1,75 @@
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package signals
import (
"fmt"
"os"
"os/signal"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestSignalsHandler(t *testing.T) {
ctx := SetupSignalHandler()
task := &Task{
ticker: time.NewTicker(time.Second * 2),
}
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
task.wg.Add(1)
go func(c chan os.Signal) {
defer task.wg.Done()
task.Run(c)
}(c)
select {
case sig := <-c:
fmt.Printf("Got %s signal. Aborting...\n", sig)
case _, ok := <-ctx.Done():
assert.False(t, ok)
}
}
type Task struct {
wg sync.WaitGroup
ticker *time.Ticker
}
func (t *Task) Run(c chan os.Signal) {
for {
go sendSignal(c)
handle()
}
}
func handle() {
for i := 0; i < 5; i++ {
fmt.Print("#")
time.Sleep(time.Millisecond * 100)
}
fmt.Println()
}
func sendSignal(stopChan chan os.Signal) {
fmt.Printf("...")
time.Sleep(1 * time.Second)
stopChan <- os.Interrupt
}

23
signals/signal_windows.go Normal file
View File

@ -0,0 +1,23 @@
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package signals
import (
"os"
)
var shutdownSignals = []os.Signal{os.Interrupt}