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

Update client-go to support Kubernetes 1.18.0 #1998

Merged
merged 5 commits into from
Feb 25, 2021
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
10 changes: 5 additions & 5 deletions cmd/allocator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,18 +241,18 @@ func newServiceHandler(kubeClient kubernetes.Interface, agonesClient versioned.I
remoteAllocationTimeout,
totalRemoteAllocationTimeout)

stop := signals.NewStopChannel()
ctx := signals.NewSigKillContext()
h := serviceHandler{
allocationCallback: func(gsa *allocationv1.GameServerAllocation) (k8sruntime.Object, error) {
return allocator.Allocate(gsa, stop)
return allocator.Allocate(ctx, gsa)
},
mTLSDisabled: mTLSDisabled,
tlsDisabled: tlsDisabled,
}

kubeInformerFactory.Start(stop)
agonesInformerFactory.Start(stop)
if err := allocator.Start(stop); err != nil {
kubeInformerFactory.Start(ctx.Done())
agonesInformerFactory.Start(ctx.Done())
if err := allocator.Start(ctx); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

ctx.Done?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Because the allocator has to go all the way down to client-go to allocate a GameServer, we have to pass the ctx all the way through.

And it also means the API for all systems is the same all the way through.

It's also possible that allocation.Start(...) should be changed to allocation.Run(...) to match all the other modules in Agones, hence the weirdness in the API surface, so I totally get the comment.

logger.WithError(err).Fatal("starting allocator failed.")
}

Expand Down
15 changes: 8 additions & 7 deletions cmd/controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package main

import (
"context"
"io"
"net/http"
"os"
Expand Down Expand Up @@ -216,20 +217,20 @@ func main() {
rs = append(rs,
httpsServer, gsCounter, gsController, gsSetController, fleetController, fasController, gasController, server)

stop := signals.NewStopChannel()
ctx := signals.NewSigKillContext()

kubeInformerFactory.Start(stop)
agonesInformerFactory.Start(stop)
kubeInformerFactory.Start(ctx.Done())
agonesInformerFactory.Start(ctx.Done())

for _, r := range rs {
go func(rr runner) {
if runErr := rr.Run(ctlConf.NumWorkers, stop); runErr != nil {
if runErr := rr.Run(ctx, ctlConf.NumWorkers); runErr != nil {
logger.WithError(runErr).Fatalf("could not start runner: %T", rr)
}
}(r)
}

<-stop
<-ctx.Done()
logger.Info("Shut down agones controllers")
}

Expand Down Expand Up @@ -403,14 +404,14 @@ func (c *config) validate() []error {
}

type runner interface {
Run(workers int, stop <-chan struct{}) error
Run(ctx context.Context, workers int) error
}

type httpServer struct {
http.ServeMux
}

func (h *httpServer) Run(workers int, stop <-chan struct{}) error {
func (h *httpServer) Run(_ context.Context, _ int) error {
logger.Info("Starting http server...")
srv := &http.Server{
Addr: ":8080",
Expand Down
10 changes: 5 additions & 5 deletions cmd/ping/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ func main() {
logger.WithField("version", pkg.Version).WithField("featureGates", runtime.EncodeFeatures()).
WithField("ctlConf", ctlConf).Info("starting ping...")

stop := signals.NewStopChannel()
ctx := signals.NewSigKillContext()

udpSrv := serveUDP(ctlConf, stop)
udpSrv := serveUDP(ctx, ctlConf)
defer udpSrv.close()

h := healthcheck.NewHandler()
Expand All @@ -60,13 +60,13 @@ func main() {
cancel := serveHTTP(ctlConf, h)
defer cancel()

<-stop
<-ctx.Done()
logger.Info("shutting down...")
}

func serveUDP(ctlConf config, stop <-chan struct{}) *udpServer {
func serveUDP(ctx context.Context, ctlConf config) *udpServer {
s := newUDPServer(ctlConf.UDPRateLimit)
s.run(stop)
s.run(ctx)
return s
}

Expand Down
11 changes: 6 additions & 5 deletions cmd/ping/udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package main

import (
"bytes"
"context"
"math"
"net"
"sync"
Expand Down Expand Up @@ -67,7 +68,7 @@ func newUDPServer(rateLimit rate.Limit) *udpServer {
}

// run runs the udp server. Non blocking operation
func (u *udpServer) run(stop <-chan struct{}) {
func (u *udpServer) run(ctx context.Context) {
u.healthy()

logger.Info("starting UDP server")
Expand All @@ -79,10 +80,10 @@ func (u *udpServer) run(stop <-chan struct{}) {

go func() {
defer u.unhealthy()
wait.Until(u.cleanUp, time.Minute, stop)
wait.Until(u.cleanUp, time.Minute, ctx.Done())
}()

u.readWriteLoop(stop)
u.readWriteLoop(ctx)
}

// cleans up visitors, if they are more than a
Expand All @@ -99,12 +100,12 @@ func (u *udpServer) cleanUp() {

// readWriteLoop reads the UDP packet in, and then echos the data back
// in a rate limited way
func (u *udpServer) readWriteLoop(stop <-chan struct{}) {
func (u *udpServer) readWriteLoop(ctx context.Context) {
go func() {
defer u.unhealthy()
for {
select {
case <-stop:
case <-ctx.Done():
return
default:
b := make([]byte, 1024)
Expand Down
12 changes: 6 additions & 6 deletions cmd/ping/udp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
package main

import (
"context"
"net"
"testing"
"time"

"k8s.io/apimachinery/pkg/util/wait"

"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/util/clock"
"k8s.io/apimachinery/pkg/util/wait"
)

type mockAddr struct {
Expand Down Expand Up @@ -106,12 +106,12 @@ func TestUDPServerHealth(t *testing.T) {

assert.Error(t, u.Health())

stop := make(chan struct{})
u.run(stop)
ctx, cancel := context.WithCancel(context.Background())

assert.Nil(t, u.Health())
u.run(ctx)
assert.NoError(t, u.Health())

close(stop)
cancel()

err = wait.PollImmediate(time.Second, 5*time.Second, func() (done bool, err error) {
return u.Health() != nil, nil
Expand Down
27 changes: 8 additions & 19 deletions cmd/sdk-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package main

import (
"context"
"fmt"
"net"
"net/http"
Expand All @@ -37,7 +38,6 @@ import (
"github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/tmc/grpc-websocket-proxy/wsproxy"
"golang.org/x/net/context"
"google.golang.org/grpc"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
Expand Down Expand Up @@ -77,8 +77,7 @@ func main() {
time.Sleep(time.Duration(ctlConf.Delay) * time.Second)
}

stop := signals.NewStopChannel()
timedStop := make(chan struct{})
ctx := signals.NewSigKillContext()
grpcServer := grpc.NewServer()
// don't graceful stop, because if we get a kill signal
// then the gameserver is being shut down, and we no longer
Expand All @@ -91,8 +90,6 @@ func main() {
Handler: wsproxy.WebsocketProxy(mux),
}
defer httpServer.Close() // nolint: errcheck
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

switch {
case ctlConf.IsLocal:
Expand All @@ -103,10 +100,8 @@ func main() {
defer cancel()

if ctlConf.Timeout != 0 {
go func() {
time.Sleep(time.Duration(ctlConf.Timeout) * time.Second)
close(timedStop)
}()
ctx, cancel = context.WithTimeout(ctx, time.Duration(ctlConf.Timeout)*time.Second)
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I liked this one too 😄

defer cancel()
}
case ctlConf.Test != "":
cancel, err := registerTestSdkServer(grpcServer, ctlConf)
Expand All @@ -116,10 +111,8 @@ func main() {
defer cancel()

if ctlConf.Timeout != 0 {
go func() {
time.Sleep(time.Duration(ctlConf.Timeout) * time.Second)
close(timedStop)
}()
ctx, cancel = context.WithTimeout(ctx, time.Duration(ctlConf.Timeout)*time.Second)
defer cancel()
}
default:
var config *rest.Config
Expand Down Expand Up @@ -148,7 +141,7 @@ func main() {
}

go func() {
err := s.Run(ctx.Done())
err := s.Run(ctx)
if err != nil {
logger.WithError(err).Fatalf("Could not run sidecar")
}
Expand All @@ -162,11 +155,7 @@ func main() {
go runGrpc(grpcServer, grpcEndpoint)
go runGateway(ctx, grpcEndpoint, mux, httpServer)

select {
case <-stop:
case <-timedStop:
}

<-ctx.Done()
logger.Info("shutting down sdk server")
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/sdk-server/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
package main

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"golang.org/x/net/context"
"google.golang.org/grpc"
)

Expand Down
2 changes: 1 addition & 1 deletion examples/simple-game-server/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ WINDOWS_DOCKER_PUSH_ARGS = # When pushing set to --push.

mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
project_path := $(dir $(mkfile_path))
server_tag = $(REGISTRY)/simple-game-server:0.1
server_tag = $(REGISTRY)/simple-game-server:0.2
ifdef WITH_WINDOWS
server_tag_linux_amd64 = $(server_tag)-linux_amd64
else
Expand Down
4 changes: 2 additions & 2 deletions examples/simple-game-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ func main() {

// doSignal shutsdown on SIGTERM/SIGKILL
func doSignal() {
stop := signals.NewStopChannel()
<-stop
ctx := signals.NewSigKillContext()
<-ctx.Done()
log.Println("Exit signal received. Shutting down.")
os.Exit(0)
}
Expand Down
16 changes: 8 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/fsnotify/fsnotify v1.4.7
github.com/go-openapi/spec v0.19.3
github.com/golang/protobuf v1.3.2
github.com/googleapis/gnostic v0.1.0 // indirect
github.com/gorilla/websocket v1.4.0
github.com/grpc-ecosystem/grpc-gateway v1.11.3
github.com/hashicorp/golang-lru v0.5.1
github.com/heptiolabs/healthcheck v0.0.0-20171201210846-da5fdee475fb
Expand All @@ -31,16 +31,16 @@ require (
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4
golang.org/x/tools v0.0.0-20190920225731-5eefd052ad72
google.golang.org/genproto v0.0.0-20191009194640-548a555dbc03
google.golang.org/grpc v1.23.1
google.golang.org/grpc v1.26.0
gopkg.in/DATA-DOG/go-sqlmock.v1 v1.3.0 // indirect
gopkg.in/fsnotify.v1 v1.4.7
gopkg.in/natefinch/lumberjack.v2 v2.0.0
k8s.io/api v0.17.14
k8s.io/apiextensions-apiserver v0.17.14
k8s.io/apimachinery v0.17.14
k8s.io/client-go v0.17.14
k8s.io/api v0.18.15
k8s.io/apiextensions-apiserver v0.18.15
k8s.io/apimachinery v0.18.15
k8s.io/client-go v0.18.15
k8s.io/kube-openapi v0.0.0-20200410163147-594e756bea31 // indirect
k8s.io/utils v0.0.0-20200124190032-861946025e34
k8s.io/utils v0.0.0-20200324210504-a9aa75ae1b89
)

replace google.golang.org/grpc v1.23.1 => google.golang.org/grpc v1.20.1 // apiserver updated grpc, but we aren't using that, so it's fine.
replace google.golang.org/grpc v1.26.0 => google.golang.org/grpc v1.20.1 // apiserver updated grpc, but we aren't using that, so it's fine.
Loading