Skip to content

Commit 25bc6f9

Browse files
committed
Fix deprecated code with the k8s signal context
Signed-off-by: Jorge Turrado <jorge_turrado@hotmail.es>
1 parent a05aaf6 commit 25bc6f9

File tree

5 files changed

+100
-4
lines changed

5 files changed

+100
-4
lines changed

pkg/cmd/builder.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package cmd
1818

1919
import (
20+
"context"
2021
"fmt"
2122
"sync"
2223
"time"
@@ -360,11 +361,11 @@ func (b *AdapterBase) Informers() (informers.SharedInformerFactory, error) {
360361
}
361362

362363
// Run runs this custom metrics adapter until the given stop channel is closed.
363-
func (b *AdapterBase) Run(stopCh <-chan struct{}) error {
364+
func (b *AdapterBase) Run(ctx context.Context) error {
364365
server, err := b.Server()
365366
if err != nil {
366367
return err
367368
}
368369

369-
return server.GenericAPIServer.PrepareRun().Run(stopCh)
370+
return server.GenericAPIServer.PrepareRun().RunWithContext(ctx)
370371
}

test-adapter/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"time"
2323

2424
"github.com/emicklei/go-restful/v3"
25-
"k8s.io/apimachinery/pkg/util/wait"
2625
"k8s.io/component-base/logs"
2726
"k8s.io/component-base/metrics/legacyregistry"
2827
"k8s.io/klog/v2"
@@ -31,6 +30,7 @@ import (
3130
basecmd "sigs.k8s.io/custom-metrics-apiserver/pkg/cmd"
3231
"sigs.k8s.io/custom-metrics-apiserver/pkg/provider"
3332
fakeprov "sigs.k8s.io/custom-metrics-apiserver/test-adapter/provider"
33+
"sigs.k8s.io/custom-metrics-apiserver/test-adapter/signals"
3434
)
3535

3636
type SampleAdapter struct {
@@ -86,7 +86,7 @@ func main() {
8686
}
8787
klog.Fatal(server.ListenAndServe())
8888
}()
89-
if err := cmd.Run(wait.NeverStop); err != nil {
89+
if err := cmd.Run(signals.SetupSignalHandler()); err != nil {
9090
klog.Fatalf("unable to run custom metrics adapter: %v", err)
9191
}
9292
}

test-adapter/signals/signal.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
Copyright 2017 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package signals
18+
19+
import (
20+
"context"
21+
"os"
22+
"os/signal"
23+
)
24+
25+
var onlyOneSignalHandler = make(chan struct{})
26+
27+
// SetupSignalHandler registers for SIGTERM and SIGINT. A context is returned
28+
// which is canceled on one of these signals. If a second signal is caught, the program
29+
// is terminated with exit code 1.
30+
func SetupSignalHandler() context.Context {
31+
close(onlyOneSignalHandler) // panics when called twice
32+
33+
ctx, cancel := context.WithCancel(context.Background())
34+
35+
c := make(chan os.Signal, 2)
36+
signal.Notify(c, shutdownSignals...)
37+
go func() {
38+
<-c
39+
cancel()
40+
<-c
41+
os.Exit(1) // second signal. Exit directly.
42+
}()
43+
44+
return ctx
45+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//go:build !windows
2+
// +build !windows
3+
4+
/*
5+
Copyright 2017 The Kubernetes Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
*/
19+
20+
package signals
21+
22+
import (
23+
"os"
24+
"syscall"
25+
)
26+
27+
var shutdownSignals = []os.Signal{os.Interrupt, syscall.SIGTERM}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
Copyright 2017 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package signals
18+
19+
import (
20+
"os"
21+
)
22+
23+
var shutdownSignals = []os.Signal{os.Interrupt}

0 commit comments

Comments
 (0)