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

simple-game-server: Adds a graceful termination delay #3436

Merged
merged 5 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions examples/simple-game-server/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ WITH_ARM64 ?= 1
mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
project_path := $(dir $(mkfile_path))
ifeq ($(REPOSITORY),)
server_tag := simple-game-server:0.18
server_tag := simple-game-server:0.19
else
server_tag := $(REPOSITORY)/simple-game-server:0.18
server_tag := $(REPOSITORY)/simple-game-server:0.19
endif

ifeq ($(WITH_WINDOWS), 1)
Expand Down
3 changes: 3 additions & 0 deletions examples/simple-game-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func main() {
shutdownDelaySec := flag.Int("automaticShutdownDelaySec", 0, "If greater than zero, automatically shut down the server this many seconds after the server becomes allocated (cannot be used if automaticShutdownDelayMin is set)")
readyDelaySec := flag.Int("readyDelaySec", 0, "If greater than zero, wait this many seconds each time before marking the game server as ready")
readyIterations := flag.Int("readyIterations", 0, "If greater than zero, return to a ready state this number of times before shutting down")
gracefulTerminationDelaySec := flag.Int("gracefulTerminationDelaySec", 0, "Delay after we've been asked to terminate (by SIGKILL or automaticShutdownDelaySec)")
udp := flag.Bool("udp", true, "Server will listen on UDP")
tcp := flag.Bool("tcp", false, "Server will listen on TCP")

Expand Down Expand Up @@ -122,6 +123,8 @@ func main() {
}

<-sigCtx.Done()
log.Printf("Waiting %d seconds before exiting", *gracefulTerminationDelaySec)
time.Sleep(time.Duration(*gracefulTerminationDelaySec) * time.Second)
os.Exit(0)
}

Expand Down
64 changes: 64 additions & 0 deletions test/eviction/evictpod.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2023 Google LLC All Rights Reserved.
//
// 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

// evictpod.go --pod <pod> --namespace <namespace> initiates a pod eviction
// using the k8s eviction API.
package main

import (
"context"
"flag"
"log"
"path/filepath"

policy "k8s.io/api/policy/v1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
)

// Borrowed from https://stackoverflow.com/questions/62803041/how-to-evict-or-delete-pods-from-kubernetes-using-golang-client
func evictPod(client *kubernetes.Clientset, name, namespace string) error {
zmerlynn marked this conversation as resolved.
Show resolved Hide resolved
return client.PolicyV1().Evictions(namespace).Evict(context.TODO(), &policy.Eviction{
ObjectMeta: meta_v1.ObjectMeta{
Name: name,
Namespace: namespace,
}})
}

func main() {
kubeconfig := flag.String("kubeconfig", filepath.Join(homedir.HomeDir(), ".kube", "config"), "(optional) absolute path to the kubeconfig file")
namespace := flag.String("namespace", "default", "Namespace (defaults to `default`)")
pod := flag.String("pod", "", "Pod name (required)")
flag.Parse()

if *pod == "" {
log.Fatal("--pod must be non-empty")
}

config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
log.Fatalf("Could not build config: %v", err)
}

kubeClient, err := kubernetes.NewForConfig(config)
if err != nil {
log.Fatalf("Could not create the kubernetes clientset: %v", err)
}

if err = evictPod(kubeClient, *pod, *namespace); err != nil {
zmerlynn marked this conversation as resolved.
Show resolved Hide resolved
log.Fatalf("Pod eviction failed: %v", err)
}
}