From 3aff7704f5d932ff499ef7c076450de1dd79fa16 Mon Sep 17 00:00:00 2001 From: Ashutosh Singh Date: Fri, 10 May 2024 09:19:24 +0000 Subject: [PATCH 1/7] refactor simple game server --- examples/simple-game-server/helper.go | 331 ++++++++++ examples/simple-game-server/main.go | 566 ------------------ .../simple-game-server/response-handler.go | 371 ++++++++++++ 3 files changed, 702 insertions(+), 566 deletions(-) create mode 100644 examples/simple-game-server/helper.go create mode 100644 examples/simple-game-server/response-handler.go diff --git a/examples/simple-game-server/helper.go b/examples/simple-game-server/helper.go new file mode 100644 index 0000000000..6716ae2d5b --- /dev/null +++ b/examples/simple-game-server/helper.go @@ -0,0 +1,331 @@ +package main + +import ( + "encoding/json" + "fmt" + "log" + "strconv" + "strings" + "time" + + coresdk "agones.dev/agones/pkg/sdk" + sdk "agones.dev/agones/sdks/go" +) + +// ready attempts to mark this gameserver as ready +func ready(s *sdk.SDK) { + err := s.Ready() + if err != nil { + log.Fatalf("Could not send ready message") + } +} + +// allocate attempts to allocate this gameserver +func allocate(s *sdk.SDK) { + err := s.Allocate() + if err != nil { + log.Fatalf("could not allocate gameserver: %v", err) + } +} + +// reserve for 10 seconds +func reserve(s *sdk.SDK, duration time.Duration) { + if err := s.Reserve(duration); err != nil { + log.Fatalf("could not reserve gameserver: %v", err) + } +} + +// exit shutdowns the server +func exit(s *sdk.SDK) { + log.Printf("Received EXIT command. Exiting.") + // This tells Agones to shutdown this Game Server + shutdownErr := s.Shutdown() + if shutdownErr != nil { + log.Printf("Could not shutdown") + } + // The process will exit when Agones removes the pod and the + // container receives the SIGTERM signal +} + +// gameServerName returns the GameServer name +func gameServerName(s *sdk.SDK) string { + var gs *coresdk.GameServer + gs, err := s.GameServer() + if err != nil { + log.Fatalf("Could not retrieve GameServer: %v", err) + } + var j []byte + j, err = json.Marshal(gs) + if err != nil { + log.Fatalf("error mashalling GameServer to JSON: %v", err) + } + log.Printf("GameServer: %s \n", string(j)) + return "NAME: " + gs.ObjectMeta.Name + "\n" +} + +// watchGameServerEvents creates a callback to log when +// gameserver events occur +func watchGameServerEvents(s *sdk.SDK) { + err := s.WatchGameServer(func(gs *coresdk.GameServer) { + j, err := json.Marshal(gs) + if err != nil { + log.Fatalf("error mashalling GameServer to JSON: %v", err) + } + log.Printf("GameServer Event: %s \n", string(j)) + }) + if err != nil { + log.Fatalf("Could not watch Game Server events, %v", err) + } +} + +// setAnnotation sets a given annotation +func setAnnotation(s *sdk.SDK, key, value string) { + log.Printf("Setting annotation %v=%v", key, value) + err := s.SetAnnotation(key, value) + if err != nil { + log.Fatalf("could not set annotation: %v", err) + } +} + +// setLabel sets a given label +func setLabel(s *sdk.SDK, key, value string) { + log.Printf("Setting label %v=%v", key, value) + // label values can only be alpha, - and . + err := s.SetLabel(key, value) + if err != nil { + log.Fatalf("could not set label: %v", err) + } +} + +// setPlayerCapacity sets the player capacity to the given value +func setPlayerCapacity(s *sdk.SDK, capacity int64) { + log.Printf("Setting Player Capacity to %d", capacity) + if err := s.Alpha().SetPlayerCapacity(capacity); err != nil { + log.Fatalf("could not set capacity: %v", err) + } +} + +// getPlayerCapacity returns the current player capacity as a string +func getPlayerCapacity(s *sdk.SDK) string { + log.Print("Getting Player Capacity") + capacity, err := s.Alpha().GetPlayerCapacity() + if err != nil { + log.Fatalf("could not get capacity: %v", err) + } + return strconv.FormatInt(capacity, 10) + "\n" +} + +// playerConnect connects a given player +func playerConnect(s *sdk.SDK, id string) { + log.Printf("Connecting Player: %s", id) + if _, err := s.Alpha().PlayerConnect(id); err != nil { + log.Fatalf("could not connect player: %v", err) + } +} + +// playerDisconnect disconnects a given player +func playerDisconnect(s *sdk.SDK, id string) { + log.Printf("Disconnecting Player: %s", id) + if _, err := s.Alpha().PlayerDisconnect(id); err != nil { + log.Fatalf("could not disconnect player: %v", err) + } +} + +// playerIsConnected returns a bool as a string if a player is connected +func playerIsConnected(s *sdk.SDK, id string) string { + log.Printf("Checking if player %s is connected", id) + + connected, err := s.Alpha().IsPlayerConnected(id) + if err != nil { + log.Fatalf("could not retrieve if player is connected: %v", err) + } + + return strconv.FormatBool(connected) + "\n" +} + +// getConnectedPlayers returns a comma delimeted list of connected players +func getConnectedPlayers(s *sdk.SDK) string { + log.Print("Retrieving connected player list") + list, err := s.Alpha().GetConnectedPlayers() + if err != nil { + log.Fatalf("could not retrieve connected players: %s", err) + } + + return strings.Join(list, ",") + "\n" +} + +// getPlayerCount returns the count of connected players as a string +func getPlayerCount(s *sdk.SDK) string { + log.Print("Retrieving connected player count") + count, err := s.Alpha().GetPlayerCount() + if err != nil { + log.Fatalf("could not retrieve player count: %s", err) + } + return strconv.FormatInt(count, 10) + "\n" +} + +// getCounterCount returns the Count of the given Counter as a string +func getCounterCount(s *sdk.SDK, counterName string) (string, error) { + log.Printf("Retrieving Counter %s Count", counterName) + count, err := s.Beta().GetCounterCount(counterName) + if err != nil { + log.Printf("Error getting Counter %s Count: %s", counterName, err) + return strconv.FormatInt(count, 10), err + } + return "COUNTER: " + strconv.FormatInt(count, 10) + "\n", nil +} + +// incrementCounter returns the if the Counter Count was incremented successfully or not +func incrementCounter(s *sdk.SDK, counterName string, amount string) (string, error) { + amountInt, err := strconv.ParseInt(amount, 10, 64) + if err != nil { + return "", fmt.Errorf("Could not increment Counter %s by unparseable amount %s: %s", counterName, amount, err) + } + log.Printf("Incrementing Counter %s Count by amount %d", counterName, amountInt) + err = s.Beta().IncrementCounter(counterName, amountInt) + if err != nil { + log.Printf("Error incrementing Counter %s Count by amount %d: %s", counterName, amountInt, err) + return "", err + } + return "SUCCESS\n", nil +} + +// decrementCounter returns if the Counter Count was decremented successfully or not +func decrementCounter(s *sdk.SDK, counterName string, amount string) (string, error) { + amountInt, err := strconv.ParseInt(amount, 10, 64) + if err != nil { + return "", fmt.Errorf("could not decrement Counter %s by unparseable amount %s: %s", counterName, amount, err) + } + log.Printf("Decrementing Counter %s Count by amount %d", counterName, amountInt) + err = s.Beta().DecrementCounter(counterName, amountInt) + if err != nil { + log.Printf("Error decrementing Counter %s Count by amount %d: %s", counterName, amountInt, err) + return "", err + } + return "SUCCESS\n", nil +} + +// setCounterCount returns the if the Counter was set to a new Count successfully or not +func setCounterCount(s *sdk.SDK, counterName string, amount string) (string, error) { + amountInt, err := strconv.ParseInt(amount, 10, 64) + if err != nil { + return "", fmt.Errorf("could not set Counter %s to unparseable amount %s: %s", counterName, amount, err) + } + log.Printf("Setting Counter %s Count to amount %d", counterName, amountInt) + err = s.Beta().SetCounterCount(counterName, amountInt) + if err != nil { + log.Printf("Error setting Counter %s Count by amount %d: %s", counterName, amountInt, err) + return "", err + } + return "SUCCESS\n", nil +} + +// getCounterCapacity returns the Capacity of the given Counter as a string +func getCounterCapacity(s *sdk.SDK, counterName string) (string, error) { + log.Printf("Retrieving Counter %s Capacity", counterName) + count, err := s.Beta().GetCounterCapacity(counterName) + if err != nil { + log.Printf("Error getting Counter %s Capacity: %s", counterName, err) + return strconv.FormatInt(count, 10), err + } + return "CAPACITY: " + strconv.FormatInt(count, 10) + "\n", nil +} + +// setCounterCapacity returns the if the Counter was set to a new Capacity successfully or not +func setCounterCapacity(s *sdk.SDK, counterName string, amount string) (string, error) { + amountInt, err := strconv.ParseInt(amount, 10, 64) + if err != nil { + return "", fmt.Errorf("could not set Counter %s to unparseable amount %s: %s", counterName, amount, err) + } + log.Printf("Setting Counter %s Capacity to amount %d", counterName, amountInt) + err = s.Beta().SetCounterCapacity(counterName, amountInt) + if err != nil { + log.Printf("Error setting Counter %s Capacity to amount %d: %s", counterName, amountInt, err) + return "", err + } + return "SUCCESS\n", nil +} + +// getListCapacity returns the Capacity of the given List as a string +func getListCapacity(s *sdk.SDK, listName string) (string, error) { + log.Printf("Retrieving List %s Capacity", listName) + capacity, err := s.Beta().GetListCapacity(listName) + if err != nil { + log.Printf("Error getting List %s Capacity: %s", listName, err) + return strconv.FormatInt(capacity, 10), err + } + return "CAPACITY: " + strconv.FormatInt(capacity, 10) + "\n", nil +} + +// setListCapacity returns if the List was set to a new Capacity successfully or not +func setListCapacity(s *sdk.SDK, listName string, amount string) (string, error) { + amountInt, err := strconv.ParseInt(amount, 10, 64) + if err != nil { + return "", fmt.Errorf("could not set List %s to unparseable amount %s: %s", listName, amount, err) + } + log.Printf("Setting List %s Capacity to amount %d", listName, amountInt) + err = s.Beta().SetListCapacity(listName, amountInt) + if err != nil { + log.Printf("Error setting List %s Capacity to amount %d: %s", listName, amountInt, err) + return "", err + } + return "SUCCESS\n", nil +} + +// listContains returns true if the given value is in the given List, false otherwise +func listContains(s *sdk.SDK, listName string, value string) (string, error) { + log.Printf("Getting List %s contains value %s", listName, value) + ok, err := s.Beta().ListContains(listName, value) + if err != nil { + log.Printf("Error getting List %s contains value %s: %s", listName, value, err) + return strconv.FormatBool(ok), err + } + return "FOUND: " + strconv.FormatBool(ok) + "\n", nil +} + +// getListLength returns the length (number of values) of the given List as a string +func getListLength(s *sdk.SDK, listName string) (string, error) { + log.Printf("Getting List %s length", listName) + length, err := s.Beta().GetListLength(listName) + if err != nil { + log.Printf("Error getting List %s length: %s", listName, err) + return strconv.Itoa(length), err + } + return "LENGTH: " + strconv.Itoa(length) + "\n", nil +} + +// getListValues return the values in the given List as a comma delineated string +func getListValues(s *sdk.SDK, listName string) (string, error) { + log.Printf("Getting List %s values", listName) + values, err := s.Beta().GetListValues(listName) + if err != nil { + log.Printf("Error getting List %s values: %s", listName, err) + return "INVALID LIST NAME", err + } + if len(values) > 0 { + return "VALUES: " + strings.Join(values, ",") + "\n", nil + } + return "VALUES: \n", nil +} + +// appendListValue returns if the given value was successfuly added to the List or not +func appendListValue(s *sdk.SDK, listName string, value string) (string, error) { + log.Printf("Appending Value %s to List %s", value, listName) + err := s.Beta().AppendListValue(listName, value) + if err != nil { + log.Printf("Error appending Value %s to List %s: %s", value, listName, err) + return "", err + } + return "SUCCESS\n", nil +} + +// deleteListValue returns if the given value was successfuly deleted from the List or not +func deleteListValue(s *sdk.SDK, listName string, value string) (string, error) { + log.Printf("Deleting Value %s from List %s", value, listName) + err := s.Beta().DeleteListValue(listName, value) + if err != nil { + log.Printf("Error deleting Value %s to List %s: %s", value, listName, err) + return "", err + } + return "SUCCESS\n", nil +} \ No newline at end of file diff --git a/examples/simple-game-server/main.go b/examples/simple-game-server/main.go index 3a901c02af..023b48197a 100644 --- a/examples/simple-game-server/main.go +++ b/examples/simple-game-server/main.go @@ -18,9 +18,7 @@ package main import ( "bufio" "context" - "encoding/json" "flag" - "fmt" "log" "net" "os" @@ -187,252 +185,6 @@ func shutdownAfterNAllocations(s *sdk.SDK, readyIterations, shutdownDelaySec int } } -func handleResponse(txt string, s *sdk.SDK, cancel context.CancelFunc) (response string, addACK bool, responseError error) { - parts := strings.Split(strings.TrimSpace(txt), " ") - response = txt - addACK = true - responseError = nil - var err error - - switch parts[0] { - // shuts down the gameserver - case "EXIT": - // handle elsewhere, as we respond before exiting - return - - // turns off the health pings - case "UNHEALTHY": - cancel() - - case "GAMESERVER": - response = gameServerName(s) - addACK = false - - case "READY": - ready(s) - - case "ALLOCATE": - allocate(s) - - case "RESERVE": - if len(parts) != 2 { - response = "Invalid RESERVE, should have 1 argument" - responseError = fmt.Errorf("Invalid RESERVE, should have 1 argument") - } - if dur, err := time.ParseDuration(parts[1]); err != nil { - response = fmt.Sprintf("%s\n", err) - responseError = err - } else { - reserve(s, dur) - } - - case "WATCH": - watchGameServerEvents(s) - - case "LABEL": - switch len(parts) { - case 1: - // legacy format - setLabel(s, "timestamp", strconv.FormatInt(time.Now().Unix(), 10)) - case 3: - setLabel(s, parts[1], parts[2]) - default: - response = "Invalid LABEL command, must use zero or 2 arguments" - responseError = fmt.Errorf("Invalid LABEL command, must use zero or 2 arguments") - } - - case "CRASH": - log.Print("Crashing.") - os.Exit(1) - return "", false, nil - - case "ANNOTATION": - switch len(parts) { - case 1: - // legacy format - setAnnotation(s, "timestamp", time.Now().UTC().String()) - case 3: - setAnnotation(s, parts[1], parts[2]) - default: - response = "Invalid ANNOTATION command, must use zero or 2 arguments" - responseError = fmt.Errorf("Invalid ANNOTATION command, must use zero or 2 arguments") - } - - case "PLAYER_CAPACITY": - switch len(parts) { - case 1: - response = getPlayerCapacity(s) - addACK = false - case 2: - if cap, err := strconv.Atoi(parts[1]); err != nil { - response = fmt.Sprintf("%s", err) - responseError = err - } else { - setPlayerCapacity(s, int64(cap)) - } - default: - response = "Invalid PLAYER_CAPACITY, should have 0 or 1 arguments" - responseError = fmt.Errorf("Invalid PLAYER_CAPACITY, should have 0 or 1 arguments") - } - - case "PLAYER_CONNECT": - if len(parts) < 2 { - response = "Invalid PLAYER_CONNECT, should have 1 arguments" - responseError = fmt.Errorf("Invalid PLAYER_CONNECT, should have 1 arguments") - return - } - playerConnect(s, parts[1]) - - case "PLAYER_DISCONNECT": - if len(parts) < 2 { - response = "Invalid PLAYER_DISCONNECT, should have 1 arguments" - responseError = fmt.Errorf("Invalid PLAYER_DISCONNECT, should have 1 arguments") - return - } - playerDisconnect(s, parts[1]) - - case "PLAYER_CONNECTED": - if len(parts) < 2 { - response = "Invalid PLAYER_CONNECTED, should have 1 arguments" - responseError = fmt.Errorf("Invalid PLAYER_CONNECTED, should have 1 arguments") - return - } - response = playerIsConnected(s, parts[1]) - addACK = false - - case "GET_PLAYERS": - response = getConnectedPlayers(s) - addACK = false - - case "PLAYER_COUNT": - response = getPlayerCount(s) - addACK = false - - case "GET_COUNTER_COUNT": - if len(parts) < 2 { - response = "Invalid GET_COUNTER_COUNT, should have 1 arguments" - responseError = fmt.Errorf("Invalid GET_COUNTER_COUNT, should have 1 arguments") - return - } - response, responseError = getCounterCount(s, parts[1]) - addACK = false - - case "INCREMENT_COUNTER": - if len(parts) < 3 { - response = "Invalid INCREMENT_COUNTER, should have 2 arguments" - responseError = fmt.Errorf("Invalid INCREMENT_COUNTER, should have 2 arguments") - return - } - response, err = incrementCounter(s, parts[1], parts[2]) - addACK = false - - case "DECREMENT_COUNTER": - if len(parts) < 3 { - response = "Invalid DECREMENT_COUNTER, should have 2 arguments" - responseError = fmt.Errorf("Invalid DECREMENT_COUNTER, should have 2 arguments") - return - } - response, err = decrementCounter(s, parts[1], parts[2]) - addACK = false - - case "SET_COUNTER_COUNT": - if len(parts) < 3 { - response = "Invalid SET_COUNTER_COUNT, should have 2 arguments" - responseError = fmt.Errorf("Invalid SET_COUNTER_COUNT, should have 2 arguments") - return - } - response, err = setCounterCount(s, parts[1], parts[2]) - addACK = false - - case "GET_COUNTER_CAPACITY": - if len(parts) < 2 { - response = "Invalid GET_COUNTER_CAPACITY, should have 1 arguments" - responseError = fmt.Errorf("Invalid GET_COUNTER_CAPACITY, should have 1 arguments") - return - } - response, responseError = getCounterCapacity(s, parts[1]) - addACK = false - - case "SET_COUNTER_CAPACITY": - if len(parts) < 3 { - response = "Invalid SET_COUNTER_CAPACITY, should have 2 arguments" - responseError = fmt.Errorf("Invalid SET_COUNTER_CAPACITY, should have 2 arguments") - return - } - response, err = setCounterCapacity(s, parts[1], parts[2]) - addACK = false - - case "GET_LIST_CAPACITY": - if len(parts) < 2 { - response = "Invalid GET_LIST_CAPACITY, should have 1 arguments" - responseError = fmt.Errorf("Invalid GET_LIST_CAPACITY, should have 1 arguments") - return - } - response, responseError = getListCapacity(s, parts[1]) - addACK = false - - case "SET_LIST_CAPACITY": - if len(parts) < 3 { - response = "Invalid SET_LIST_CAPACITY, should have 2 arguments" - responseError = fmt.Errorf("Invalid SET_LIST_CAPACITY, should have 2 arguments") - return - } - response, err = setListCapacity(s, parts[1], parts[2]) - addACK = false - - case "LIST_CONTAINS": - if len(parts) < 3 { - response = "Invalid LIST_CONTAINS, should have 2 arguments" - responseError = fmt.Errorf("Invalid LIST_CONTAINS, should have 2 arguments") - return - } - response, responseError = listContains(s, parts[1], parts[2]) - addACK = false - - case "GET_LIST_LENGTH": - if len(parts) < 2 { - response = "Invalid GET_LIST_LENGTH, should have 1 arguments" - responseError = fmt.Errorf("Invalid GET_LIST_LENGTH, should have 1 arguments") - return - } - response, responseError = getListLength(s, parts[1]) - addACK = false - - case "GET_LIST_VALUES": - if len(parts) < 2 { - response = "Invalid GET_LIST_VALUES, should have 1 arguments" - responseError = fmt.Errorf("Invalid GET_LIST_VALUES, should have 1 arguments") - return - } - response, responseError = getListValues(s, parts[1]) - addACK = false - - case "APPEND_LIST_VALUE": - if len(parts) < 3 { - response = "Invalid APPEND_LIST_VALUE, should have 2 arguments" - responseError = fmt.Errorf("Invalid APPEND_LIST_VALUE, should have 2 arguments") - return - } - response, err = appendListValue(s, parts[1], parts[2]) - addACK = false - - case "DELETE_LIST_VALUE": - if len(parts) < 3 { - response = "Invalid DELETE_LIST_VALUE, should have 2 arguments" - responseError = fmt.Errorf("Invalid DELETE_LIST_VALUE, should have 2 arguments") - return - } - response, err = deleteListValue(s, parts[1], parts[2]) - addACK = false - } - - if err != nil { - return err.Error(), addACK, err - } - - return -} - func udpListener(port *string, s *sdk.SDK, cancel context.CancelFunc) { log.Printf("Starting UDP server, listening on port %s", *port) conn, err := net.ListenPacket("udp", ":"+*port) @@ -524,29 +276,6 @@ func tcpRespond(conn net.Conn, txt string) { } } -// ready attempts to mark this gameserver as ready -func ready(s *sdk.SDK) { - err := s.Ready() - if err != nil { - log.Fatalf("Could not send ready message") - } -} - -// allocate attempts to allocate this gameserver -func allocate(s *sdk.SDK) { - err := s.Allocate() - if err != nil { - log.Fatalf("could not allocate gameserver: %v", err) - } -} - -// reserve for 10 seconds -func reserve(s *sdk.SDK, duration time.Duration) { - if err := s.Reserve(duration); err != nil { - log.Fatalf("could not reserve gameserver: %v", err) - } -} - // readPacket reads a string from the connection func readPacket(conn net.PacketConn, b []byte) (net.Addr, string) { n, sender, err := conn.ReadFrom(b) @@ -558,301 +287,6 @@ func readPacket(conn net.PacketConn, b []byte) (net.Addr, string) { return sender, txt } -// exit shutdowns the server -func exit(s *sdk.SDK) { - log.Printf("Received EXIT command. Exiting.") - // This tells Agones to shutdown this Game Server - shutdownErr := s.Shutdown() - if shutdownErr != nil { - log.Printf("Could not shutdown") - } - // The process will exit when Agones removes the pod and the - // container receives the SIGTERM signal -} - -// gameServerName returns the GameServer name -func gameServerName(s *sdk.SDK) string { - var gs *coresdk.GameServer - gs, err := s.GameServer() - if err != nil { - log.Fatalf("Could not retrieve GameServer: %v", err) - } - var j []byte - j, err = json.Marshal(gs) - if err != nil { - log.Fatalf("error mashalling GameServer to JSON: %v", err) - } - log.Printf("GameServer: %s \n", string(j)) - return "NAME: " + gs.ObjectMeta.Name + "\n" -} - -// watchGameServerEvents creates a callback to log when -// gameserver events occur -func watchGameServerEvents(s *sdk.SDK) { - err := s.WatchGameServer(func(gs *coresdk.GameServer) { - j, err := json.Marshal(gs) - if err != nil { - log.Fatalf("error mashalling GameServer to JSON: %v", err) - } - log.Printf("GameServer Event: %s \n", string(j)) - }) - if err != nil { - log.Fatalf("Could not watch Game Server events, %v", err) - } -} - -// setAnnotation sets a given annotation -func setAnnotation(s *sdk.SDK, key, value string) { - log.Printf("Setting annotation %v=%v", key, value) - err := s.SetAnnotation(key, value) - if err != nil { - log.Fatalf("could not set annotation: %v", err) - } -} - -// setLabel sets a given label -func setLabel(s *sdk.SDK, key, value string) { - log.Printf("Setting label %v=%v", key, value) - // label values can only be alpha, - and . - err := s.SetLabel(key, value) - if err != nil { - log.Fatalf("could not set label: %v", err) - } -} - -// setPlayerCapacity sets the player capacity to the given value -func setPlayerCapacity(s *sdk.SDK, capacity int64) { - log.Printf("Setting Player Capacity to %d", capacity) - if err := s.Alpha().SetPlayerCapacity(capacity); err != nil { - log.Fatalf("could not set capacity: %v", err) - } -} - -// getPlayerCapacity returns the current player capacity as a string -func getPlayerCapacity(s *sdk.SDK) string { - log.Print("Getting Player Capacity") - capacity, err := s.Alpha().GetPlayerCapacity() - if err != nil { - log.Fatalf("could not get capacity: %v", err) - } - return strconv.FormatInt(capacity, 10) + "\n" -} - -// playerConnect connects a given player -func playerConnect(s *sdk.SDK, id string) { - log.Printf("Connecting Player: %s", id) - if _, err := s.Alpha().PlayerConnect(id); err != nil { - log.Fatalf("could not connect player: %v", err) - } -} - -// playerDisconnect disconnects a given player -func playerDisconnect(s *sdk.SDK, id string) { - log.Printf("Disconnecting Player: %s", id) - if _, err := s.Alpha().PlayerDisconnect(id); err != nil { - log.Fatalf("could not disconnect player: %v", err) - } -} - -// playerIsConnected returns a bool as a string if a player is connected -func playerIsConnected(s *sdk.SDK, id string) string { - log.Printf("Checking if player %s is connected", id) - - connected, err := s.Alpha().IsPlayerConnected(id) - if err != nil { - log.Fatalf("could not retrieve if player is connected: %v", err) - } - - return strconv.FormatBool(connected) + "\n" -} - -// getConnectedPlayers returns a comma delimeted list of connected players -func getConnectedPlayers(s *sdk.SDK) string { - log.Print("Retrieving connected player list") - list, err := s.Alpha().GetConnectedPlayers() - if err != nil { - log.Fatalf("could not retrieve connected players: %s", err) - } - - return strings.Join(list, ",") + "\n" -} - -// getPlayerCount returns the count of connected players as a string -func getPlayerCount(s *sdk.SDK) string { - log.Print("Retrieving connected player count") - count, err := s.Alpha().GetPlayerCount() - if err != nil { - log.Fatalf("could not retrieve player count: %s", err) - } - return strconv.FormatInt(count, 10) + "\n" -} - -// getCounterCount returns the Count of the given Counter as a string -func getCounterCount(s *sdk.SDK, counterName string) (string, error) { - log.Printf("Retrieving Counter %s Count", counterName) - count, err := s.Beta().GetCounterCount(counterName) - if err != nil { - log.Printf("Error getting Counter %s Count: %s", counterName, err) - return strconv.FormatInt(count, 10), err - } - return "COUNTER: " + strconv.FormatInt(count, 10) + "\n", nil -} - -// incrementCounter returns the if the Counter Count was incremented successfully or not -func incrementCounter(s *sdk.SDK, counterName string, amount string) (string, error) { - amountInt, err := strconv.ParseInt(amount, 10, 64) - if err != nil { - return "", fmt.Errorf("Could not increment Counter %s by unparseable amount %s: %s", counterName, amount, err) - } - log.Printf("Incrementing Counter %s Count by amount %d", counterName, amountInt) - err = s.Beta().IncrementCounter(counterName, amountInt) - if err != nil { - log.Printf("Error incrementing Counter %s Count by amount %d: %s", counterName, amountInt, err) - return "", err - } - return "SUCCESS\n", nil -} - -// decrementCounter returns if the Counter Count was decremented successfully or not -func decrementCounter(s *sdk.SDK, counterName string, amount string) (string, error) { - amountInt, err := strconv.ParseInt(amount, 10, 64) - if err != nil { - return "", fmt.Errorf("could not decrement Counter %s by unparseable amount %s: %s", counterName, amount, err) - } - log.Printf("Decrementing Counter %s Count by amount %d", counterName, amountInt) - err = s.Beta().DecrementCounter(counterName, amountInt) - if err != nil { - log.Printf("Error decrementing Counter %s Count by amount %d: %s", counterName, amountInt, err) - return "", err - } - return "SUCCESS\n", nil -} - -// setCounterCount returns the if the Counter was set to a new Count successfully or not -func setCounterCount(s *sdk.SDK, counterName string, amount string) (string, error) { - amountInt, err := strconv.ParseInt(amount, 10, 64) - if err != nil { - return "", fmt.Errorf("could not set Counter %s to unparseable amount %s: %s", counterName, amount, err) - } - log.Printf("Setting Counter %s Count to amount %d", counterName, amountInt) - err = s.Beta().SetCounterCount(counterName, amountInt) - if err != nil { - log.Printf("Error setting Counter %s Count by amount %d: %s", counterName, amountInt, err) - return "", err - } - return "SUCCESS\n", nil -} - -// getCounterCapacity returns the Capacity of the given Counter as a string -func getCounterCapacity(s *sdk.SDK, counterName string) (string, error) { - log.Printf("Retrieving Counter %s Capacity", counterName) - count, err := s.Beta().GetCounterCapacity(counterName) - if err != nil { - log.Printf("Error getting Counter %s Capacity: %s", counterName, err) - return strconv.FormatInt(count, 10), err - } - return "CAPACITY: " + strconv.FormatInt(count, 10) + "\n", nil -} - -// setCounterCapacity returns the if the Counter was set to a new Capacity successfully or not -func setCounterCapacity(s *sdk.SDK, counterName string, amount string) (string, error) { - amountInt, err := strconv.ParseInt(amount, 10, 64) - if err != nil { - return "", fmt.Errorf("could not set Counter %s to unparseable amount %s: %s", counterName, amount, err) - } - log.Printf("Setting Counter %s Capacity to amount %d", counterName, amountInt) - err = s.Beta().SetCounterCapacity(counterName, amountInt) - if err != nil { - log.Printf("Error setting Counter %s Capacity to amount %d: %s", counterName, amountInt, err) - return "", err - } - return "SUCCESS\n", nil -} - -// getListCapacity returns the Capacity of the given List as a string -func getListCapacity(s *sdk.SDK, listName string) (string, error) { - log.Printf("Retrieving List %s Capacity", listName) - capacity, err := s.Beta().GetListCapacity(listName) - if err != nil { - log.Printf("Error getting List %s Capacity: %s", listName, err) - return strconv.FormatInt(capacity, 10), err - } - return "CAPACITY: " + strconv.FormatInt(capacity, 10) + "\n", nil -} - -// setListCapacity returns if the List was set to a new Capacity successfully or not -func setListCapacity(s *sdk.SDK, listName string, amount string) (string, error) { - amountInt, err := strconv.ParseInt(amount, 10, 64) - if err != nil { - return "", fmt.Errorf("could not set List %s to unparseable amount %s: %s", listName, amount, err) - } - log.Printf("Setting List %s Capacity to amount %d", listName, amountInt) - err = s.Beta().SetListCapacity(listName, amountInt) - if err != nil { - log.Printf("Error setting List %s Capacity to amount %d: %s", listName, amountInt, err) - return "", err - } - return "SUCCESS\n", nil -} - -// listContains returns true if the given value is in the given List, false otherwise -func listContains(s *sdk.SDK, listName string, value string) (string, error) { - log.Printf("Getting List %s contains value %s", listName, value) - ok, err := s.Beta().ListContains(listName, value) - if err != nil { - log.Printf("Error getting List %s contains value %s: %s", listName, value, err) - return strconv.FormatBool(ok), err - } - return "FOUND: " + strconv.FormatBool(ok) + "\n", nil -} - -// getListLength returns the length (number of values) of the given List as a string -func getListLength(s *sdk.SDK, listName string) (string, error) { - log.Printf("Getting List %s length", listName) - length, err := s.Beta().GetListLength(listName) - if err != nil { - log.Printf("Error getting List %s length: %s", listName, err) - return strconv.Itoa(length), err - } - return "LENGTH: " + strconv.Itoa(length) + "\n", nil -} - -// getListValues return the values in the given List as a comma delineated string -func getListValues(s *sdk.SDK, listName string) (string, error) { - log.Printf("Getting List %s values", listName) - values, err := s.Beta().GetListValues(listName) - if err != nil { - log.Printf("Error getting List %s values: %s", listName, err) - return "INVALID LIST NAME", err - } - if len(values) > 0 { - return "VALUES: " + strings.Join(values, ",") + "\n", nil - } - return "VALUES: \n", nil -} - -// appendListValue returns if the given value was successfuly added to the List or not -func appendListValue(s *sdk.SDK, listName string, value string) (string, error) { - log.Printf("Appending Value %s to List %s", value, listName) - err := s.Beta().AppendListValue(listName, value) - if err != nil { - log.Printf("Error appending Value %s to List %s: %s", value, listName, err) - return "", err - } - return "SUCCESS\n", nil -} - -// deleteListValue returns if the given value was successfuly deleted from the List or not -func deleteListValue(s *sdk.SDK, listName string, value string) (string, error) { - log.Printf("Deleting Value %s from List %s", value, listName) - err := s.Beta().DeleteListValue(listName, value) - if err != nil { - log.Printf("Error deleting Value %s to List %s: %s", value, listName, err) - return "", err - } - return "SUCCESS\n", nil -} - // doHealth sends the regular Health Pings func doHealth(sdk *sdk.SDK, ctx context.Context) { tick := time.Tick(2 * time.Second) diff --git a/examples/simple-game-server/response-handler.go b/examples/simple-game-server/response-handler.go new file mode 100644 index 0000000000..dc97026a35 --- /dev/null +++ b/examples/simple-game-server/response-handler.go @@ -0,0 +1,371 @@ +package main + +import ( + "context" + "fmt" + "log" + "os" + "strconv" + "strings" + "time" + + sdk "agones.dev/agones/sdks/go" +) + +type responseHandler func(s *sdk.SDK, parts []string, cancel ...context.CancelFunc) (response string, addACK bool, responseError error) + +var responseMap = map[string]responseHandler{ + "EXIT": handleExit, + "UNHEALTHY": handleUnhealthy, + "GAMESERVER": handleGameServer, + "READY": handleReady, + "ALLOCATE": handleAllocate, + "RESERVE": handleReserve, + "WATCH": handleWatch, + "LABEL": handleLabel, + "CRASH": handleCrash, + "ANNOTATION": handleAnnotation, + "PLAYER_CAPACITY": handlePlayerCapacity, + "PLAYER_CONNECT": handlePlayerConnect, + "PLAYER_DISCONNECT": handlePlayerDisconnect, + "PLAYER_CONNECTED": handlePlayerConnected, + "GET_PLAYERS": handleGetPlayers, + "PLAYER_COUNT": handlePlayerCount, + "GET_COUNTER_COUNT": handleGetCounterCount, + "INCREMENT_COUNTER": handleIncrementCounter, + "DECREMENT_COUNTER": handleDecrementCounter, + "SET_COUNTER_COUNT": handleSetCounterCount, + "GET_COUNTER_CAPACITY": handleGetCounterCapacity, + "SET_COUNTER_CAPACITY": handleSetCounterCapacity, + "GET_LIST_CAPACITY": handleGetListCapacity, + "SET_LIST_CAPACITY": handleSetListCapacity, + "LIST_CONTAINS": handleListContains, + "GET_LIST_LENGTH": handleGetListLength, + "GET_LIST_VALUES": handleGetListValues, + "APPEND_LIST_VALUE": handleAppendListValue, + "DELETE_LIST_VALUE": handleDeleteListValue, +} + +func handleResponse(txt string, s *sdk.SDK, cancel context.CancelFunc) (response string, addACK bool, responseError error) { + parts := strings.Split(strings.TrimSpace(txt), " ") + response = txt + addACK = true + responseError = nil + + handler, exists := responseMap[parts[0]] + if !exists { + return fmt.Sprintf("Unknown response command: %s", parts[0]), true, nil + } + if parts[0] == "UNHEALTHY" { + return handler(s, parts, cancel) + } + + return handler(s, parts) +} + +func handleExit(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { + // handle elsewhere, as we respond before exiting + return +} + +func handleUnhealthy(s *sdk.SDK, parts []string, cancel ...context.CancelFunc) (response string, addACK bool, responseError error) { + if len(cancel) > 0 { + cancel[0]() // Invoke cancel function if provided + } + return +} + +func handleGameServer(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { + response = gameServerName(s) + addACK = false + return +} + +func handleReady(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { + ready(s) + return +} + +func handleAllocate(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { + allocate(s) + return +} + +func handleReserve(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { + if len(parts) != 2 { + response = "Invalid RESERVE, should have 1 argument" + responseError = fmt.Errorf("Invalid RESERVE, should have 1 argument") + } + if dur, err := time.ParseDuration(parts[1]); err != nil { + response = fmt.Sprintf("%s\n", err) + responseError = err + } else { + reserve(s, dur) + } + return +} + +func handleWatch(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { + watchGameServerEvents(s) + return +} + +func handleLabel(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { + switch len(parts) { + case 0: + // Legacy format + setLabel(s, "timestamp", strconv.FormatInt(time.Now().Unix(), 10)) + case 2: + setLabel(s, parts[1], parts[2]) + default: + response = "Invalid LABEL command, must use zero or 2 arguments" + responseError = fmt.Errorf("Invalid LABEL command, must use zero or 2 arguments") + } + return +} + +func handleCrash(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { + log.Print("Crashing.") + os.Exit(1) + return "", false, nil +} + +func handleAnnotation(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { + switch len(parts) { + case 1: + // Legacy format + setAnnotation(s, "timestamp", time.Now().UTC().String()) + case 2: + setAnnotation(s, parts[1], parts[2]) + default: + response = "Invalid ANNOTATION command, must use zero or 2 arguments" + responseError = fmt.Errorf("Invalid ANNOTATION command, must use zero or 2 arguments") + } + return +} + +func handlePlayerCapacity(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { + switch len(parts) { + case 1: + response = getPlayerCapacity(s) + addACK = false + case 2: + if cap, err := strconv.Atoi(parts[0]); err != nil { + response = fmt.Sprintf("%s", err) + responseError = err + } else { + setPlayerCapacity(s, int64(cap)) + } + default: + response = "Invalid PLAYER_CAPACITY, should have 0 or 1 arguments" + responseError = fmt.Errorf("Invalid PLAYER_CAPACITY, should have 0 or 1 arguments") + } + return +} + +func handlePlayerConnect(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { + if len(parts) < 2 { + response = "Invalid PLAYER_CONNECT, should have 1 argument" + responseError = fmt.Errorf("Invalid PLAYER_CONNECT, should have 1 argument") + return + } + playerConnect(s, parts[1]) + return +} + +func handlePlayerDisconnect(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { + if len(parts) < 2 { + response = "Invalid PLAYER_DISCONNECT, should have 1 argument" + responseError = fmt.Errorf("Invalid PLAYER_DISCONNECT, should have 1 argument") + return + } + playerDisconnect(s, parts[1]) + return +} + +func handlePlayerConnected(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { + if len(parts) < 2 { + response = "Invalid PLAYER_CONNECTED, should have 1 argument" + responseError = fmt.Errorf("Invalid PLAYER_CONNECTED, should have 1 argument") + return + } + response = playerIsConnected(s, parts[1]) + addACK = false + return +} + +func handleGetPlayers(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { + response = getConnectedPlayers(s) + addACK = false + return +} + +func handlePlayerCount(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { + response = getPlayerCount(s) + addACK = false + return +} + +func handleGetCounterCount(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { + if len(parts) < 2 { + response = "Invalid GET_COUNTER_COUNT, should have 1 argument" + responseError = fmt.Errorf("Invalid GET_COUNTER_COUNT, should have 1 argument") + return + } + response, responseError = getCounterCount(s, parts[1]) + addACK = false + return +} + +func handleIncrementCounter(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { + if len(parts) < 3 { + response = "Invalid INCREMENT_COUNTER, should have 2 arguments" + responseError = fmt.Errorf("Invalid INCREMENT_COUNTER, should have 2 arguments") + return + } + response, err := incrementCounter(s, parts[1], parts[2]) + if err != nil { + responseError = err + } + addACK = false + return +} + +func handleDecrementCounter(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { + if len(parts) < 3 { + response = "Invalid DECREMENT_COUNTER, should have 2 arguments" + responseError = fmt.Errorf("Invalid DECREMENT_COUNTER, should have 2 arguments") + return + } + response, err := decrementCounter(s, parts[1], parts[2]) + if err != nil { + responseError = err + } + addACK = false + return +} + +func handleSetCounterCount(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { + if len(parts) < 3 { + response = "Invalid SET_COUNTER_COUNT, should have 2 arguments" + responseError = fmt.Errorf("Invalid SET_COUNTER_COUNT, should have 2 arguments") + return + } + response, err := setCounterCount(s, parts[1], parts[2]) + if err != nil { + responseError = err + } + addACK = false + return +} + +func handleGetCounterCapacity(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { + if len(parts) < 2 { + response = "Invalid GET_COUNTER_CAPACITY, should have 1 argument" + responseError = fmt.Errorf("Invalid GET_COUNTER_CAPACITY, should have 1 argument") + return + } + response, responseError = getCounterCapacity(s, parts[1]) + addACK = false + return +} + +func handleSetCounterCapacity(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { + if len(parts) < 3 { + response = "Invalid SET_COUNTER_CAPACITY, should have 2 arguments" + responseError = fmt.Errorf("Invalid SET_COUNTER_CAPACITY, should have 2 arguments") + return + } + response, err := setCounterCapacity(s, parts[1], parts[2]) + if err != nil { + responseError = err + } + addACK = false + return +} + +func handleGetListCapacity(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { + if len(parts) < 2 { + response = "Invalid GET_LIST_CAPACITY, should have 1 argument" + responseError = fmt.Errorf("Invalid GET_LIST_CAPACITY, should have 1 argument") + return + } + response, responseError = getListCapacity(s, parts[1]) + addACK = false + return +} + +func handleSetListCapacity(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { + if len(parts) < 3 { + response = "Invalid SET_LIST_CAPACITY, should have 2 arguments" + responseError = fmt.Errorf("Invalid SET_LIST_CAPACITY, should have 2 arguments") + return + } + response, err := setListCapacity(s, parts[1], parts[2]) + if err != nil { + responseError = err + } + addACK = false + return +} + +func handleListContains(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { + if len(parts) < 3 { + response = "Invalid LIST_CONTAINS, should have 2 arguments" + responseError = fmt.Errorf("Invalid LIST_CONTAINS, should have 2 arguments") + return + } + response, responseError = listContains(s, parts[1], parts[2]) + addACK = false + return +} + +func handleGetListLength(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { + if len(parts) < 2 { + response = "Invalid GET_LIST_LENGTH, should have 1 argument" + responseError = fmt.Errorf("Invalid GET_LIST_LENGTH, should have 1 argument") + return + } + response, responseError = getListLength(s, parts[1]) + addACK = false + return +} + +func handleGetListValues(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { + if len(parts) < 2 { + response = "Invalid GET_LIST_VALUES, should have 1 argument" + responseError = fmt.Errorf("Invalid GET_LIST_VALUES, should have 1 argument") + return + } + response, responseError = getListValues(s, parts[1]) + addACK = false + return +} + +func handleAppendListValue(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { + if len(parts) < 3 { + response = "Invalid APPEND_LIST_VALUE, should have 2 arguments" + responseError = fmt.Errorf("Invalid APPEND_LIST_VALUE, should have 2 arguments") + return + } + response, err := appendListValue(s, parts[1], parts[2]) + if err != nil { + responseError = err + } + addACK = false + return +} + +func handleDeleteListValue(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { + if len(parts) < 3 { + response = "Invalid DELETE_LIST_VALUE, should have 2 arguments" + responseError = fmt.Errorf("Invalid DELETE_LIST_VALUE, should have 2 arguments") + return + } + response, err := deleteListValue(s, parts[1], parts[2]) + if err != nil { + responseError = err + } + addACK = false + return +} From 77292f8c6ea9d4ef32a1dc8247d27b9d319f4e51 Mon Sep 17 00:00:00 2001 From: Ashutosh Singh Date: Tue, 14 May 2024 10:59:57 +0000 Subject: [PATCH 2/7] added the suggested changes --- .../{response-handler.go => handlers.go} | 274 +++++++++++++-- examples/simple-game-server/helper.go | 331 ------------------ examples/simple-game-server/main.go | 5 +- 3 files changed, 246 insertions(+), 364 deletions(-) rename examples/simple-game-server/{response-handler.go => handlers.go} (50%) delete mode 100644 examples/simple-game-server/helper.go diff --git a/examples/simple-game-server/response-handler.go b/examples/simple-game-server/handlers.go similarity index 50% rename from examples/simple-game-server/response-handler.go rename to examples/simple-game-server/handlers.go index dc97026a35..8dbe9cba03 100644 --- a/examples/simple-game-server/response-handler.go +++ b/examples/simple-game-server/handlers.go @@ -1,7 +1,22 @@ +// Copyright 2020 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. + package main import ( "context" + "encoding/json" "fmt" "log" "os" @@ -9,6 +24,7 @@ import ( "strings" "time" + coresdk "agones.dev/agones/pkg/sdk" sdk "agones.dev/agones/sdks/go" ) @@ -75,22 +91,42 @@ func handleUnhealthy(s *sdk.SDK, parts []string, cancel ...context.CancelFunc) ( return } +// handleGameServer returns the GameServer name func handleGameServer(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { - response = gameServerName(s) + gs, err := s.GameServer() + if err != nil { + log.Fatalf("Could not retrieve GameServer: %v", err) + } + var j []byte + j, err = json.Marshal(gs) + if err != nil { + log.Fatalf("error mashalling GameServer to JSON: %v", err) + } + log.Printf("GameServer: %s \n", string(j)) + response = "NAME: " + gs.ObjectMeta.Name + "\n" addACK = false return } +// handleReady attempts to mark this gameserver as ready func handleReady(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { - ready(s) + err := s.Ready() + if err != nil { + log.Fatalf("Could not send ready message: %v", err) + } return } +// handleAllocate attempts to allocate this gameserver func handleAllocate(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { - allocate(s) + err := s.Allocate() + if err != nil { + log.Fatalf("could not allocate gameserver: %v", err) + } return } +// handleReserve reserve for 10 seconds func handleReserve(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { if len(parts) != 2 { response = "Invalid RESERVE, should have 1 argument" @@ -100,13 +136,27 @@ func handleReserve(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (respons response = fmt.Sprintf("%s\n", err) responseError = err } else { - reserve(s, dur) + err := s.Reserve(dur) + if err != nil { + log.Fatalf("could not reserve gameserver: %v", err) + } } return } +// handleWatch creates a callback to log when +// gameserver events occur func handleWatch(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { - watchGameServerEvents(s) + err := s.WatchGameServer(func(gs *coresdk.GameServer) { + j, err := json.Marshal(gs) + if err != nil { + log.Fatalf("error mashalling GameServer to JSON: %v", err) + } + log.Printf("GameServer Event: %s \n", string(j)) + }) + if err != nil { + log.Fatalf("Could not watch Game Server events, %v", err) + } return } @@ -144,17 +194,27 @@ func handleAnnotation(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (resp return } +// handlePlayerCapacity sets the player capacity to the given value +// or returns the current player capacity as a string func handlePlayerCapacity(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { switch len(parts) { case 1: - response = getPlayerCapacity(s) + log.Print("Getting Player Capacity") + capacity, err := s.Alpha().GetPlayerCapacity() + if err != nil { + log.Fatalf("could not get capacity: %v", err) + } + response = strconv.FormatInt(capacity, 10) + "\n" addACK = false case 2: if cap, err := strconv.Atoi(parts[0]); err != nil { response = fmt.Sprintf("%s", err) responseError = err } else { - setPlayerCapacity(s, int64(cap)) + log.Printf("Setting Player Capacity to %d", int64(cap)) + if err := s.Alpha().SetPlayerCapacity(int64(cap)); err != nil { + log.Fatalf("could not set capacity: %v", err) + } } default: response = "Invalid PLAYER_CAPACITY, should have 0 or 1 arguments" @@ -163,209 +223,359 @@ func handlePlayerCapacity(s *sdk.SDK, parts []string, _ ...context.CancelFunc) ( return } +// handlePlayerConnect connects a given player func handlePlayerConnect(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { if len(parts) < 2 { response = "Invalid PLAYER_CONNECT, should have 1 argument" responseError = fmt.Errorf("Invalid PLAYER_CONNECT, should have 1 argument") return } - playerConnect(s, parts[1]) + log.Printf("Connecting Player: %s", parts[1]) + if _, err := s.Alpha().PlayerConnect(parts[1]); err != nil { + log.Fatalf("could not connect player: %v", err) + } return } +// handlePlayerDisconnect disconnects a given player func handlePlayerDisconnect(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { if len(parts) < 2 { response = "Invalid PLAYER_DISCONNECT, should have 1 argument" responseError = fmt.Errorf("Invalid PLAYER_DISCONNECT, should have 1 argument") return } - playerDisconnect(s, parts[1]) + log.Printf("Disconnecting Player: %s", parts[1]) + if _, err := s.Alpha().PlayerDisconnect(parts[1]); err != nil { + log.Fatalf("could not disconnect player: %v", err) + } return } +// handlePlayerConnected returns a bool as a string if a player is connected func handlePlayerConnected(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { if len(parts) < 2 { response = "Invalid PLAYER_CONNECTED, should have 1 argument" responseError = fmt.Errorf("Invalid PLAYER_CONNECTED, should have 1 argument") return } - response = playerIsConnected(s, parts[1]) + log.Printf("Checking if player %s is connected", parts[1]) + connected, err := s.Alpha().IsPlayerConnected(parts[1]) + if err != nil { + log.Fatalf("could not retrieve if player is connected: %v", err) + } + response = strconv.FormatBool(connected) + "\n" addACK = false return } +// handleGetPlayers returns a comma delimeted list of connected players func handleGetPlayers(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { - response = getConnectedPlayers(s) + log.Print("Retrieving connected player list") + list, err := s.Alpha().GetConnectedPlayers() + if err != nil { + log.Fatalf("could not retrieve connected players: %s", err) + } + response = strings.Join(list, ",") + "\n" addACK = false return } +// handlePlayerCount returns the count of connected players as a string func handlePlayerCount(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { - response = getPlayerCount(s) + log.Print("Retrieving connected player count") + count, err := s.Alpha().GetPlayerCount() + if err != nil { + log.Fatalf("could not retrieve player count: %s", err) + } + response = strconv.FormatInt(count, 10) + "\n" addACK = false return } +// handleGetCounterCount returns the Count of the given Counter as a string func handleGetCounterCount(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { if len(parts) < 2 { response = "Invalid GET_COUNTER_COUNT, should have 1 argument" responseError = fmt.Errorf("Invalid GET_COUNTER_COUNT, should have 1 argument") return } - response, responseError = getCounterCount(s, parts[1]) + log.Printf("Retrieving Counter %s Count", parts[1]) + count, err := s.Beta().GetCounterCount(parts[1]) + if err != nil { + log.Printf("Error getting Counter %s Count: %s", parts[1], err) + response, responseError = strconv.FormatInt(count, 10), err + } + response, responseError = "COUNTER: "+strconv.FormatInt(count, 10)+"\n", nil addACK = false return } +// handleIncrementCounter returns the if the Counter Count was incremented successfully or not func handleIncrementCounter(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { if len(parts) < 3 { response = "Invalid INCREMENT_COUNTER, should have 2 arguments" responseError = fmt.Errorf("Invalid INCREMENT_COUNTER, should have 2 arguments") return } - response, err := incrementCounter(s, parts[1], parts[2]) + amountInt, err := strconv.ParseInt(parts[2], 10, 64) if err != nil { - responseError = err + response, responseError = "", fmt.Errorf("Could not increment Counter %s by unparseable amount %s: %s", parts[1], parts[2], err) + } + log.Printf("Incrementing Counter %s Count by amount %d", parts[1], amountInt) + err = s.Beta().IncrementCounter(parts[1], amountInt) + if err != nil { + log.Printf("Error incrementing Counter %s Count by amount %d: %s", parts[1], amountInt, err) + response, responseError = "", err } + response, responseError = "SUCCESS\n", nil addACK = false return } +// handleDecrementCounter returns if the Counter Count was decremented successfully or not func handleDecrementCounter(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { if len(parts) < 3 { response = "Invalid DECREMENT_COUNTER, should have 2 arguments" responseError = fmt.Errorf("Invalid DECREMENT_COUNTER, should have 2 arguments") return } - response, err := decrementCounter(s, parts[1], parts[2]) + amountInt, err := strconv.ParseInt(parts[2], 10, 64) if err != nil { - responseError = err + response, responseError = "", fmt.Errorf("could not decrement Counter %s by unparseable amount %s: %s", parts[1], parts[2], err) } + log.Printf("Decrementing Counter %s Count by amount %d", parts[1], amountInt) + err = s.Beta().DecrementCounter(parts[1], amountInt) + if err != nil { + log.Printf("Error decrementing Counter %s Count by amount %d: %s", parts[1], amountInt, err) + response, responseError = "", err + } + response, responseError = "SUCCESS\n", nil addACK = false return } +// handleSetCounterCount returns the if the Counter was set to a new Count successfully or not func handleSetCounterCount(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { if len(parts) < 3 { response = "Invalid SET_COUNTER_COUNT, should have 2 arguments" responseError = fmt.Errorf("Invalid SET_COUNTER_COUNT, should have 2 arguments") return } - response, err := setCounterCount(s, parts[1], parts[2]) + amountInt, err := strconv.ParseInt(parts[2], 10, 64) if err != nil { - responseError = err + response, responseError = "", fmt.Errorf("could not set Counter %s to unparseable amount %s: %s", parts[1], parts[2], err) } + log.Printf("Setting Counter %s Count to amount %d", parts[1], amountInt) + err = s.Beta().SetCounterCount(parts[1], amountInt) + if err != nil { + log.Printf("Error setting Counter %s Count by amount %d: %s", parts[1], amountInt, err) + response, responseError = "", err + } + response, responseError = "SUCCESS\n", nil addACK = false return } +// handleGetCounterCapacity returns the Capacity of the given Counter as a string func handleGetCounterCapacity(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { if len(parts) < 2 { response = "Invalid GET_COUNTER_CAPACITY, should have 1 argument" responseError = fmt.Errorf("Invalid GET_COUNTER_CAPACITY, should have 1 argument") return } - response, responseError = getCounterCapacity(s, parts[1]) + log.Printf("Retrieving Counter %s Capacity", parts[1]) + count, err := s.Beta().GetCounterCapacity(parts[1]) + if err != nil { + log.Printf("Error getting Counter %s Capacity: %s", parts[1], err) + response, responseError = strconv.FormatInt(count, 10), err + } + response, responseError = "CAPACITY: "+strconv.FormatInt(count, 10)+"\n", nil addACK = false return } +// handleSetCounterCapacity returns the if the Counter was set to a new Capacity successfully or not func handleSetCounterCapacity(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { if len(parts) < 3 { response = "Invalid SET_COUNTER_CAPACITY, should have 2 arguments" responseError = fmt.Errorf("Invalid SET_COUNTER_CAPACITY, should have 2 arguments") return } - response, err := setCounterCapacity(s, parts[1], parts[2]) + amountInt, err := strconv.ParseInt(parts[2], 10, 64) if err != nil { - responseError = err + response, responseError = "", fmt.Errorf("could not set Counter %s to unparseable amount %s: %s", parts[1], parts[2], err) } + log.Printf("Setting Counter %s Capacity to amount %d", parts[1], amountInt) + err = s.Beta().SetCounterCapacity(parts[1], amountInt) + if err != nil { + log.Printf("Error setting Counter %s Capacity to amount %d: %s", parts[1], amountInt, err) + response, responseError = "", err + } + response, responseError = "SUCCESS\n", nil addACK = false return } +// handleGetListCapacity returns the Capacity of the given List as a string func handleGetListCapacity(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { if len(parts) < 2 { response = "Invalid GET_LIST_CAPACITY, should have 1 argument" responseError = fmt.Errorf("Invalid GET_LIST_CAPACITY, should have 1 argument") return } - response, responseError = getListCapacity(s, parts[1]) + log.Printf("Retrieving List %s Capacity", parts[1]) + capacity, err := s.Beta().GetListCapacity(parts[1]) + if err != nil { + log.Printf("Error getting List %s Capacity: %s", parts[1], err) + response, responseError = strconv.FormatInt(capacity, 10), err + } + response, responseError = "CAPACITY: "+strconv.FormatInt(capacity, 10)+"\n", nil addACK = false return } +// handleSetListCapacity returns if the List was set to a new Capacity successfully or not func handleSetListCapacity(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { if len(parts) < 3 { response = "Invalid SET_LIST_CAPACITY, should have 2 arguments" responseError = fmt.Errorf("Invalid SET_LIST_CAPACITY, should have 2 arguments") return } - response, err := setListCapacity(s, parts[1], parts[2]) + amountInt, err := strconv.ParseInt(parts[2], 10, 64) if err != nil { - responseError = err + response, responseError = "", fmt.Errorf("could not set List %s to unparseable amount %s: %s", parts[1], parts[2], err) + } + log.Printf("Setting List %s Capacity to amount %d", parts[1], amountInt) + err = s.Beta().SetListCapacity(parts[1], amountInt) + if err != nil { + log.Printf("Error setting List %s Capacity to amount %d: %s", parts[1], amountInt, err) + response, responseError = "", err } + response, responseError = "SUCCESS\n", nil addACK = false return } +// handleListContains returns true if the given value is in the given List, false otherwise func handleListContains(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { if len(parts) < 3 { response = "Invalid LIST_CONTAINS, should have 2 arguments" responseError = fmt.Errorf("Invalid LIST_CONTAINS, should have 2 arguments") return } - response, responseError = listContains(s, parts[1], parts[2]) + log.Printf("Getting List %s contains value %s", parts[1], parts[2]) + ok, err := s.Beta().ListContains(parts[1], parts[2]) + if err != nil { + log.Printf("Error getting List %s contains value %s: %s", parts[1], parts[2], err) + response, responseError = strconv.FormatBool(ok), err + } + response, responseError = "FOUND: "+strconv.FormatBool(ok)+"\n", nil addACK = false return } +// handleGetListLength returns the length (number of values) of the given List as a string func handleGetListLength(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { if len(parts) < 2 { response = "Invalid GET_LIST_LENGTH, should have 1 argument" responseError = fmt.Errorf("Invalid GET_LIST_LENGTH, should have 1 argument") return } - response, responseError = getListLength(s, parts[1]) + log.Printf("Getting List %s length", parts[1]) + length, err := s.Beta().GetListLength(parts[1]) + if err != nil { + log.Printf("Error getting List %s length: %s", parts[1], err) + response, responseError = strconv.Itoa(length), err + } + response, responseError = "LENGTH: "+strconv.Itoa(length)+"\n", nil addACK = false return } +// handleGetListValues return the values in the given List as a comma delineated string func handleGetListValues(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { if len(parts) < 2 { response = "Invalid GET_LIST_VALUES, should have 1 argument" responseError = fmt.Errorf("Invalid GET_LIST_VALUES, should have 1 argument") return } - response, responseError = getListValues(s, parts[1]) + log.Printf("Getting List %s values", parts[1]) + values, err := s.Beta().GetListValues(parts[1]) + if err != nil { + log.Printf("Error getting List %s values: %s", parts[1], err) + response, responseError = "INVALID LIST NAME", err + } + if len(values) > 0 { + response, responseError = "VALUES: "+strings.Join(values, ",")+"\n", nil + } + response, responseError = "VALUES: \n", nil addACK = false return } +// handleAppendListValue returns if the given value was successfuly added to the List or not func handleAppendListValue(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { if len(parts) < 3 { response = "Invalid APPEND_LIST_VALUE, should have 2 arguments" responseError = fmt.Errorf("Invalid APPEND_LIST_VALUE, should have 2 arguments") return } - response, err := appendListValue(s, parts[1], parts[2]) + log.Printf("Appending Value %s to List %s", parts[2], parts[1]) + err := s.Beta().AppendListValue(parts[1], parts[2]) if err != nil { - responseError = err + log.Printf("Error appending Value %s to List %s: %s", parts[2], parts[1], err) + response, responseError = "", err } + response, responseError = "SUCCESS\n", nil addACK = false return } +// handleDeleteListValue returns if the given value was successfuly deleted from the List or not func handleDeleteListValue(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { if len(parts) < 3 { response = "Invalid DELETE_LIST_VALUE, should have 2 arguments" responseError = fmt.Errorf("Invalid DELETE_LIST_VALUE, should have 2 arguments") return } - response, err := deleteListValue(s, parts[1], parts[2]) + log.Printf("Deleting Value %s from List %s", parts[2], parts[1]) + err := s.Beta().DeleteListValue(parts[1], parts[2]) if err != nil { - responseError = err + log.Printf("Error deleting Value %s to List %s: %s", parts[2], parts[1], err) + response, responseError = "", err } + response, responseError = "SUCCESS\n", nil addACK = false return } + +// setAnnotation sets a given annotation +func setAnnotation(s *sdk.SDK, key, value string) { + log.Printf("Setting annotation %v=%v", key, value) + err := s.SetAnnotation(key, value) + if err != nil { + log.Fatalf("could not set annotation: %v", err) + } +} + +// setLabel sets a given label +func setLabel(s *sdk.SDK, key, value string) { + log.Printf("Setting label %v=%v", key, value) + // label values can only be alpha, - and . + err := s.SetLabel(key, value) + if err != nil { + log.Fatalf("could not set label: %v", err) + } +} + +// exit shutdowns the server +func exit(s *sdk.SDK) { + log.Printf("Received EXIT command. Exiting.") + // This tells Agones to shutdown this Game Server + shutdownErr := s.Shutdown() + if shutdownErr != nil { + log.Printf("Could not shutdown") + } + // The process will exit when Agones removes the pod and the + // container receives the SIGTERM signal +} diff --git a/examples/simple-game-server/helper.go b/examples/simple-game-server/helper.go deleted file mode 100644 index 6716ae2d5b..0000000000 --- a/examples/simple-game-server/helper.go +++ /dev/null @@ -1,331 +0,0 @@ -package main - -import ( - "encoding/json" - "fmt" - "log" - "strconv" - "strings" - "time" - - coresdk "agones.dev/agones/pkg/sdk" - sdk "agones.dev/agones/sdks/go" -) - -// ready attempts to mark this gameserver as ready -func ready(s *sdk.SDK) { - err := s.Ready() - if err != nil { - log.Fatalf("Could not send ready message") - } -} - -// allocate attempts to allocate this gameserver -func allocate(s *sdk.SDK) { - err := s.Allocate() - if err != nil { - log.Fatalf("could not allocate gameserver: %v", err) - } -} - -// reserve for 10 seconds -func reserve(s *sdk.SDK, duration time.Duration) { - if err := s.Reserve(duration); err != nil { - log.Fatalf("could not reserve gameserver: %v", err) - } -} - -// exit shutdowns the server -func exit(s *sdk.SDK) { - log.Printf("Received EXIT command. Exiting.") - // This tells Agones to shutdown this Game Server - shutdownErr := s.Shutdown() - if shutdownErr != nil { - log.Printf("Could not shutdown") - } - // The process will exit when Agones removes the pod and the - // container receives the SIGTERM signal -} - -// gameServerName returns the GameServer name -func gameServerName(s *sdk.SDK) string { - var gs *coresdk.GameServer - gs, err := s.GameServer() - if err != nil { - log.Fatalf("Could not retrieve GameServer: %v", err) - } - var j []byte - j, err = json.Marshal(gs) - if err != nil { - log.Fatalf("error mashalling GameServer to JSON: %v", err) - } - log.Printf("GameServer: %s \n", string(j)) - return "NAME: " + gs.ObjectMeta.Name + "\n" -} - -// watchGameServerEvents creates a callback to log when -// gameserver events occur -func watchGameServerEvents(s *sdk.SDK) { - err := s.WatchGameServer(func(gs *coresdk.GameServer) { - j, err := json.Marshal(gs) - if err != nil { - log.Fatalf("error mashalling GameServer to JSON: %v", err) - } - log.Printf("GameServer Event: %s \n", string(j)) - }) - if err != nil { - log.Fatalf("Could not watch Game Server events, %v", err) - } -} - -// setAnnotation sets a given annotation -func setAnnotation(s *sdk.SDK, key, value string) { - log.Printf("Setting annotation %v=%v", key, value) - err := s.SetAnnotation(key, value) - if err != nil { - log.Fatalf("could not set annotation: %v", err) - } -} - -// setLabel sets a given label -func setLabel(s *sdk.SDK, key, value string) { - log.Printf("Setting label %v=%v", key, value) - // label values can only be alpha, - and . - err := s.SetLabel(key, value) - if err != nil { - log.Fatalf("could not set label: %v", err) - } -} - -// setPlayerCapacity sets the player capacity to the given value -func setPlayerCapacity(s *sdk.SDK, capacity int64) { - log.Printf("Setting Player Capacity to %d", capacity) - if err := s.Alpha().SetPlayerCapacity(capacity); err != nil { - log.Fatalf("could not set capacity: %v", err) - } -} - -// getPlayerCapacity returns the current player capacity as a string -func getPlayerCapacity(s *sdk.SDK) string { - log.Print("Getting Player Capacity") - capacity, err := s.Alpha().GetPlayerCapacity() - if err != nil { - log.Fatalf("could not get capacity: %v", err) - } - return strconv.FormatInt(capacity, 10) + "\n" -} - -// playerConnect connects a given player -func playerConnect(s *sdk.SDK, id string) { - log.Printf("Connecting Player: %s", id) - if _, err := s.Alpha().PlayerConnect(id); err != nil { - log.Fatalf("could not connect player: %v", err) - } -} - -// playerDisconnect disconnects a given player -func playerDisconnect(s *sdk.SDK, id string) { - log.Printf("Disconnecting Player: %s", id) - if _, err := s.Alpha().PlayerDisconnect(id); err != nil { - log.Fatalf("could not disconnect player: %v", err) - } -} - -// playerIsConnected returns a bool as a string if a player is connected -func playerIsConnected(s *sdk.SDK, id string) string { - log.Printf("Checking if player %s is connected", id) - - connected, err := s.Alpha().IsPlayerConnected(id) - if err != nil { - log.Fatalf("could not retrieve if player is connected: %v", err) - } - - return strconv.FormatBool(connected) + "\n" -} - -// getConnectedPlayers returns a comma delimeted list of connected players -func getConnectedPlayers(s *sdk.SDK) string { - log.Print("Retrieving connected player list") - list, err := s.Alpha().GetConnectedPlayers() - if err != nil { - log.Fatalf("could not retrieve connected players: %s", err) - } - - return strings.Join(list, ",") + "\n" -} - -// getPlayerCount returns the count of connected players as a string -func getPlayerCount(s *sdk.SDK) string { - log.Print("Retrieving connected player count") - count, err := s.Alpha().GetPlayerCount() - if err != nil { - log.Fatalf("could not retrieve player count: %s", err) - } - return strconv.FormatInt(count, 10) + "\n" -} - -// getCounterCount returns the Count of the given Counter as a string -func getCounterCount(s *sdk.SDK, counterName string) (string, error) { - log.Printf("Retrieving Counter %s Count", counterName) - count, err := s.Beta().GetCounterCount(counterName) - if err != nil { - log.Printf("Error getting Counter %s Count: %s", counterName, err) - return strconv.FormatInt(count, 10), err - } - return "COUNTER: " + strconv.FormatInt(count, 10) + "\n", nil -} - -// incrementCounter returns the if the Counter Count was incremented successfully or not -func incrementCounter(s *sdk.SDK, counterName string, amount string) (string, error) { - amountInt, err := strconv.ParseInt(amount, 10, 64) - if err != nil { - return "", fmt.Errorf("Could not increment Counter %s by unparseable amount %s: %s", counterName, amount, err) - } - log.Printf("Incrementing Counter %s Count by amount %d", counterName, amountInt) - err = s.Beta().IncrementCounter(counterName, amountInt) - if err != nil { - log.Printf("Error incrementing Counter %s Count by amount %d: %s", counterName, amountInt, err) - return "", err - } - return "SUCCESS\n", nil -} - -// decrementCounter returns if the Counter Count was decremented successfully or not -func decrementCounter(s *sdk.SDK, counterName string, amount string) (string, error) { - amountInt, err := strconv.ParseInt(amount, 10, 64) - if err != nil { - return "", fmt.Errorf("could not decrement Counter %s by unparseable amount %s: %s", counterName, amount, err) - } - log.Printf("Decrementing Counter %s Count by amount %d", counterName, amountInt) - err = s.Beta().DecrementCounter(counterName, amountInt) - if err != nil { - log.Printf("Error decrementing Counter %s Count by amount %d: %s", counterName, amountInt, err) - return "", err - } - return "SUCCESS\n", nil -} - -// setCounterCount returns the if the Counter was set to a new Count successfully or not -func setCounterCount(s *sdk.SDK, counterName string, amount string) (string, error) { - amountInt, err := strconv.ParseInt(amount, 10, 64) - if err != nil { - return "", fmt.Errorf("could not set Counter %s to unparseable amount %s: %s", counterName, amount, err) - } - log.Printf("Setting Counter %s Count to amount %d", counterName, amountInt) - err = s.Beta().SetCounterCount(counterName, amountInt) - if err != nil { - log.Printf("Error setting Counter %s Count by amount %d: %s", counterName, amountInt, err) - return "", err - } - return "SUCCESS\n", nil -} - -// getCounterCapacity returns the Capacity of the given Counter as a string -func getCounterCapacity(s *sdk.SDK, counterName string) (string, error) { - log.Printf("Retrieving Counter %s Capacity", counterName) - count, err := s.Beta().GetCounterCapacity(counterName) - if err != nil { - log.Printf("Error getting Counter %s Capacity: %s", counterName, err) - return strconv.FormatInt(count, 10), err - } - return "CAPACITY: " + strconv.FormatInt(count, 10) + "\n", nil -} - -// setCounterCapacity returns the if the Counter was set to a new Capacity successfully or not -func setCounterCapacity(s *sdk.SDK, counterName string, amount string) (string, error) { - amountInt, err := strconv.ParseInt(amount, 10, 64) - if err != nil { - return "", fmt.Errorf("could not set Counter %s to unparseable amount %s: %s", counterName, amount, err) - } - log.Printf("Setting Counter %s Capacity to amount %d", counterName, amountInt) - err = s.Beta().SetCounterCapacity(counterName, amountInt) - if err != nil { - log.Printf("Error setting Counter %s Capacity to amount %d: %s", counterName, amountInt, err) - return "", err - } - return "SUCCESS\n", nil -} - -// getListCapacity returns the Capacity of the given List as a string -func getListCapacity(s *sdk.SDK, listName string) (string, error) { - log.Printf("Retrieving List %s Capacity", listName) - capacity, err := s.Beta().GetListCapacity(listName) - if err != nil { - log.Printf("Error getting List %s Capacity: %s", listName, err) - return strconv.FormatInt(capacity, 10), err - } - return "CAPACITY: " + strconv.FormatInt(capacity, 10) + "\n", nil -} - -// setListCapacity returns if the List was set to a new Capacity successfully or not -func setListCapacity(s *sdk.SDK, listName string, amount string) (string, error) { - amountInt, err := strconv.ParseInt(amount, 10, 64) - if err != nil { - return "", fmt.Errorf("could not set List %s to unparseable amount %s: %s", listName, amount, err) - } - log.Printf("Setting List %s Capacity to amount %d", listName, amountInt) - err = s.Beta().SetListCapacity(listName, amountInt) - if err != nil { - log.Printf("Error setting List %s Capacity to amount %d: %s", listName, amountInt, err) - return "", err - } - return "SUCCESS\n", nil -} - -// listContains returns true if the given value is in the given List, false otherwise -func listContains(s *sdk.SDK, listName string, value string) (string, error) { - log.Printf("Getting List %s contains value %s", listName, value) - ok, err := s.Beta().ListContains(listName, value) - if err != nil { - log.Printf("Error getting List %s contains value %s: %s", listName, value, err) - return strconv.FormatBool(ok), err - } - return "FOUND: " + strconv.FormatBool(ok) + "\n", nil -} - -// getListLength returns the length (number of values) of the given List as a string -func getListLength(s *sdk.SDK, listName string) (string, error) { - log.Printf("Getting List %s length", listName) - length, err := s.Beta().GetListLength(listName) - if err != nil { - log.Printf("Error getting List %s length: %s", listName, err) - return strconv.Itoa(length), err - } - return "LENGTH: " + strconv.Itoa(length) + "\n", nil -} - -// getListValues return the values in the given List as a comma delineated string -func getListValues(s *sdk.SDK, listName string) (string, error) { - log.Printf("Getting List %s values", listName) - values, err := s.Beta().GetListValues(listName) - if err != nil { - log.Printf("Error getting List %s values: %s", listName, err) - return "INVALID LIST NAME", err - } - if len(values) > 0 { - return "VALUES: " + strings.Join(values, ",") + "\n", nil - } - return "VALUES: \n", nil -} - -// appendListValue returns if the given value was successfuly added to the List or not -func appendListValue(s *sdk.SDK, listName string, value string) (string, error) { - log.Printf("Appending Value %s to List %s", value, listName) - err := s.Beta().AppendListValue(listName, value) - if err != nil { - log.Printf("Error appending Value %s to List %s: %s", value, listName, err) - return "", err - } - return "SUCCESS\n", nil -} - -// deleteListValue returns if the given value was successfuly deleted from the List or not -func deleteListValue(s *sdk.SDK, listName string, value string) (string, error) { - log.Printf("Deleting Value %s from List %s", value, listName) - err := s.Beta().DeleteListValue(listName, value) - if err != nil { - log.Printf("Error deleting Value %s to List %s: %s", value, listName, err) - return "", err - } - return "SUCCESS\n", nil -} \ No newline at end of file diff --git a/examples/simple-game-server/main.go b/examples/simple-game-server/main.go index 023b48197a..08c1156bdd 100644 --- a/examples/simple-game-server/main.go +++ b/examples/simple-game-server/main.go @@ -117,7 +117,10 @@ func main() { time.Sleep(time.Duration(*readyDelaySec) * time.Second) } log.Print("Marking this server as ready") - ready(s) + err := s.Ready() + if err != nil { + log.Fatalf("Could not send ready message") + } } <-sigCtx.Done() From 5b5a5d78c499a5399b929939da63e73487218b74 Mon Sep 17 00:00:00 2001 From: Ashutosh Singh Date: Tue, 21 May 2024 10:51:35 +0000 Subject: [PATCH 3/7] updated the version and added the sugested change --- build/Makefile | 2 +- examples/crd-client/create-gs.yaml | 2 +- examples/fleet.yaml | 2 +- examples/gameserver.yaml | 2 +- examples/simple-game-server/Makefile | 2 +- examples/simple-game-server/dev-gameserver.yaml | 2 +- examples/simple-game-server/fleet-distributed.yaml | 2 +- examples/simple-game-server/fleet-tcp.yaml | 2 +- examples/simple-game-server/fleet.yaml | 2 +- examples/simple-game-server/gameserver-passthrough.yaml | 2 +- examples/simple-game-server/gameserver-windows.yaml | 2 +- examples/simple-game-server/gameserver.yaml | 2 +- examples/simple-game-server/handlers.go | 7 +++++-- install/helm/agones/templates/tests/test-runner.yaml | 2 +- pkg/util/webhooks/webhooks_test.go | 2 +- site/config.toml | 2 +- test/e2e/framework/framework.go | 2 +- test/e2e/gameserver_test.go | 2 +- test/load/allocation/fleet.yaml | 2 +- test/load/allocation/performance-test-fleet-template.yaml | 2 +- test/load/allocation/scenario-fleet.yaml | 2 +- 21 files changed, 25 insertions(+), 22 deletions(-) diff --git a/build/Makefile b/build/Makefile index a01bcee546..ebd1336308 100644 --- a/build/Makefile +++ b/build/Makefile @@ -64,7 +64,7 @@ KIND_PROFILE ?= agones KIND_CONTAINER_NAME=$(KIND_PROFILE)-control-plane # Game Server image to use while doing end-to-end tests -GS_TEST_IMAGE ?= us-docker.pkg.dev/agones-images/examples/simple-game-server:0.32 +GS_TEST_IMAGE ?= us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33 # Enable all beta feature gates. Keep in sync with `true` (beta) entries in pkg/util/runtime/features.go:featureDefaults BETA_FEATURE_GATES ?= "CountsAndLists=true&DisableResyncOnSDKServer=true" diff --git a/examples/crd-client/create-gs.yaml b/examples/crd-client/create-gs.yaml index edf6d096cd..6e3ff58987 100644 --- a/examples/crd-client/create-gs.yaml +++ b/examples/crd-client/create-gs.yaml @@ -38,5 +38,5 @@ spec: imagePullPolicy: Always env: - name: GAMESERVER_IMAGE - value: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.32 + value: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33 restartPolicy: Never diff --git a/examples/fleet.yaml b/examples/fleet.yaml index a21db3bbba..f1a64f82f1 100644 --- a/examples/fleet.yaml +++ b/examples/fleet.yaml @@ -109,4 +109,4 @@ spec: spec: containers: - name: simple-game-server - image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.32 + image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33 diff --git a/examples/gameserver.yaml b/examples/gameserver.yaml index 225774a002..1f20062748 100644 --- a/examples/gameserver.yaml +++ b/examples/gameserver.yaml @@ -121,7 +121,7 @@ spec: spec: containers: - name: simple-game-server - image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.32 + image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33 imagePullPolicy: Always # nodeSelector is a label that can be used to tell Kubernetes which host # OS to use. For Windows game servers uncomment the nodeSelector diff --git a/examples/simple-game-server/Makefile b/examples/simple-game-server/Makefile index 8004757813..0b1531532a 100644 --- a/examples/simple-game-server/Makefile +++ b/examples/simple-game-server/Makefile @@ -38,7 +38,7 @@ WITH_ARM64 ?= 1 mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST))) project_path := $(dir $(mkfile_path)) -version := 0.32 +version := 0.33 ifeq ($(REPOSITORY),) server_tag := simple-game-server:$(version) else diff --git a/examples/simple-game-server/dev-gameserver.yaml b/examples/simple-game-server/dev-gameserver.yaml index a37164eb4f..3bd8218587 100644 --- a/examples/simple-game-server/dev-gameserver.yaml +++ b/examples/simple-game-server/dev-gameserver.yaml @@ -31,4 +31,4 @@ spec: spec: containers: - name: simple-game-server - image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.32 + image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33 diff --git a/examples/simple-game-server/fleet-distributed.yaml b/examples/simple-game-server/fleet-distributed.yaml index 636180c298..1333985bb4 100644 --- a/examples/simple-game-server/fleet-distributed.yaml +++ b/examples/simple-game-server/fleet-distributed.yaml @@ -32,7 +32,7 @@ spec: spec: containers: - name: simple-game-server - image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.32 + image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33 resources: requests: memory: 64Mi diff --git a/examples/simple-game-server/fleet-tcp.yaml b/examples/simple-game-server/fleet-tcp.yaml index 74662be94e..6c7ae262e4 100644 --- a/examples/simple-game-server/fleet-tcp.yaml +++ b/examples/simple-game-server/fleet-tcp.yaml @@ -28,7 +28,7 @@ spec: spec: containers: - name: simple-game-server - image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.32 + image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33 env: # Disables the UDP listener (Enabled by default) - name: UDP diff --git a/examples/simple-game-server/fleet.yaml b/examples/simple-game-server/fleet.yaml index ec0a5a5f95..6e3c736c60 100644 --- a/examples/simple-game-server/fleet.yaml +++ b/examples/simple-game-server/fleet.yaml @@ -27,7 +27,7 @@ spec: spec: containers: - name: simple-game-server - image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.32 + image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33 resources: requests: memory: 64Mi diff --git a/examples/simple-game-server/gameserver-passthrough.yaml b/examples/simple-game-server/gameserver-passthrough.yaml index ea5b5265c4..8fc7666636 100644 --- a/examples/simple-game-server/gameserver-passthrough.yaml +++ b/examples/simple-game-server/gameserver-passthrough.yaml @@ -24,7 +24,7 @@ spec: spec: containers: - name: simple-game-server - image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.32 + image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33 env: - name: PASSTHROUGH value: 'TRUE' diff --git a/examples/simple-game-server/gameserver-windows.yaml b/examples/simple-game-server/gameserver-windows.yaml index 93c0496f52..ee75733a01 100644 --- a/examples/simple-game-server/gameserver-windows.yaml +++ b/examples/simple-game-server/gameserver-windows.yaml @@ -25,7 +25,7 @@ spec: spec: containers: - name: simple-game-server - image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.32 + image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33 resources: requests: memory: 64Mi diff --git a/examples/simple-game-server/gameserver.yaml b/examples/simple-game-server/gameserver.yaml index b1bbea0053..f19bc49432 100644 --- a/examples/simple-game-server/gameserver.yaml +++ b/examples/simple-game-server/gameserver.yaml @@ -25,7 +25,7 @@ spec: spec: containers: - name: simple-game-server - image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.32 + image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33 resources: requests: memory: 64Mi diff --git a/examples/simple-game-server/handlers.go b/examples/simple-game-server/handlers.go index 8dbe9cba03..150a898a44 100644 --- a/examples/simple-game-server/handlers.go +++ b/examples/simple-game-server/handlers.go @@ -1,4 +1,4 @@ -// Copyright 2020 Google LLC All Rights Reserved. +// Copyright 2024 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. @@ -69,8 +69,11 @@ func handleResponse(txt string, s *sdk.SDK, cancel context.CancelFunc) (response responseError = nil handler, exists := responseMap[parts[0]] + if exists && parts[0] == "EXIT" { + return response, addACK, responseError + } if !exists { - return fmt.Sprintf("Unknown response command: %s", parts[0]), true, nil + return response, addACK, responseError } if parts[0] == "UNHEALTHY" { return handler(s, parts, cancel) diff --git a/install/helm/agones/templates/tests/test-runner.yaml b/install/helm/agones/templates/tests/test-runner.yaml index 98507158bd..477a5ff400 100644 --- a/install/helm/agones/templates/tests/test-runner.yaml +++ b/install/helm/agones/templates/tests/test-runner.yaml @@ -29,7 +29,7 @@ spec: imagePullPolicy: Always env: - name: GAMESERVER_IMAGE - value: "us-docker.pkg.dev/agones-images/examples/simple-game-server:0.32" + value: "us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33" - name: IS_HELM_TEST value: "true" - name: GAMESERVERS_NAMESPACE diff --git a/pkg/util/webhooks/webhooks_test.go b/pkg/util/webhooks/webhooks_test.go index 8980654572..69e94adbe5 100644 --- a/pkg/util/webhooks/webhooks_test.go +++ b/pkg/util/webhooks/webhooks_test.go @@ -164,7 +164,7 @@ func TestWebHookFleetValidationHandler(t *testing.T) { "template": { "spec": { "containers": [{ - "image": "us-docker.pkg.dev/agones-images/examples/simple-game-server:0.32", + "image": "us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33", "name": false }] } diff --git a/site/config.toml b/site/config.toml index 8d7f3b493b..6be88c6655 100644 --- a/site/config.toml +++ b/site/config.toml @@ -100,7 +100,7 @@ dev_eks_example_cluster_version = "1.29" dev_minikube_example_cluster_version = "1.28.6" # example tag -example_image_tag = "us-docker.pkg.dev/agones-images/examples/simple-game-server:0.32" +example_image_tag = "us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33" # Enable syntax highlighting and copy buttons on code blocks with Prism prism_syntax_highlighting = true diff --git a/test/e2e/framework/framework.go b/test/e2e/framework/framework.go index 72f20f079c..aa0b6dd057 100644 --- a/test/e2e/framework/framework.go +++ b/test/e2e/framework/framework.go @@ -150,7 +150,7 @@ func NewFromFlags() (*Framework, error) { } viper.SetDefault(kubeconfigFlag, filepath.Join(usr.HomeDir, ".kube", "config")) - viper.SetDefault(gsimageFlag, "us-docker.pkg.dev/agones-images/examples/simple-game-server:0.32") + viper.SetDefault(gsimageFlag, "us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33") viper.SetDefault(pullSecretFlag, "") viper.SetDefault(stressTestLevelFlag, 0) viper.SetDefault(perfOutputDirFlag, "") diff --git a/test/e2e/gameserver_test.go b/test/e2e/gameserver_test.go index 564e71dd8a..cd442322a2 100644 --- a/test/e2e/gameserver_test.go +++ b/test/e2e/gameserver_test.go @@ -1133,7 +1133,7 @@ spec: preferredDuringSchedulingIgnoredDuringExecution: ERROR containers: - name: simple-game-server - image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.32 + image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33 ` err := os.WriteFile("/tmp/invalid.yaml", []byte(gsYaml), 0o644) require.NoError(t, err) diff --git a/test/load/allocation/fleet.yaml b/test/load/allocation/fleet.yaml index 93aae8f211..df5e92707d 100644 --- a/test/load/allocation/fleet.yaml +++ b/test/load/allocation/fleet.yaml @@ -33,7 +33,7 @@ spec: spec: containers: - args: [-automaticShutdownDelaySec=600] - image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.32 + image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33 name: simple-game-server resources: limits: diff --git a/test/load/allocation/performance-test-fleet-template.yaml b/test/load/allocation/performance-test-fleet-template.yaml index 12920e441a..3e4a4f44d8 100644 --- a/test/load/allocation/performance-test-fleet-template.yaml +++ b/test/load/allocation/performance-test-fleet-template.yaml @@ -33,7 +33,7 @@ spec: spec: containers: - args: [-automaticShutdownDelaySec=AUTOMATIC_SHUTDOWN_DELAY_SEC_REPLACEMENT] - image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.32 + image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33 name: simple-game-server resources: limits: diff --git a/test/load/allocation/scenario-fleet.yaml b/test/load/allocation/scenario-fleet.yaml index ac9674ac97..08248f6354 100644 --- a/test/load/allocation/scenario-fleet.yaml +++ b/test/load/allocation/scenario-fleet.yaml @@ -38,7 +38,7 @@ spec: value: 'true' containers: - name: simple-game-server - image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.32 + image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33 args: [-automaticShutdownDelaySec=60, -readyIterations=10] resources: limits: From bf736e4f111183ae0ea46937a05ac395fa849018 Mon Sep 17 00:00:00 2001 From: Ashutosh Singh Date: Mon, 3 Jun 2024 10:51:19 +0000 Subject: [PATCH 4/7] testing --- build/Makefile | 2 +- examples/crd-client/create-gs.yaml | 2 +- examples/fleet.yaml | 2 +- examples/gameserver.yaml | 2 +- examples/simple-game-server/Makefile | 2 +- .../simple-game-server/dev-gameserver.yaml | 2 +- .../simple-game-server/fleet-distributed.yaml | 2 +- examples/simple-game-server/fleet-tcp.yaml | 2 +- examples/simple-game-server/fleet.yaml | 2 +- .../gameserver-passthrough.yaml | 2 +- .../gameserver-windows.yaml | 2 +- examples/simple-game-server/gameserver.yaml | 2 +- examples/simple-game-server/handlers.go | 61 +++++++++++++------ .../agones/templates/tests/test-runner.yaml | 2 +- pkg/util/webhooks/webhooks_test.go | 2 +- site/config.toml | 2 +- test/e2e/framework/framework.go | 4 +- test/e2e/gameserver_test.go | 2 +- test/load/allocation/fleet.yaml | 2 +- .../performance-test-fleet-template.yaml | 2 +- test/load/allocation/scenario-fleet.yaml | 2 +- 21 files changed, 63 insertions(+), 40 deletions(-) diff --git a/build/Makefile b/build/Makefile index ebd1336308..56340c1091 100644 --- a/build/Makefile +++ b/build/Makefile @@ -64,7 +64,7 @@ KIND_PROFILE ?= agones KIND_CONTAINER_NAME=$(KIND_PROFILE)-control-plane # Game Server image to use while doing end-to-end tests -GS_TEST_IMAGE ?= us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33 +GS_TEST_IMAGE ?= us-central1-docker.pkg.dev/agones-ashutoshnsingh/example/simple-game-server:0.33-dev-linux-amd64 # Enable all beta feature gates. Keep in sync with `true` (beta) entries in pkg/util/runtime/features.go:featureDefaults BETA_FEATURE_GATES ?= "CountsAndLists=true&DisableResyncOnSDKServer=true" diff --git a/examples/crd-client/create-gs.yaml b/examples/crd-client/create-gs.yaml index 6e3ff58987..a851866bd5 100644 --- a/examples/crd-client/create-gs.yaml +++ b/examples/crd-client/create-gs.yaml @@ -38,5 +38,5 @@ spec: imagePullPolicy: Always env: - name: GAMESERVER_IMAGE - value: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33 + value: us-central1-docker.pkg.dev/agones-ashutoshnsingh/example/simple-game-server:0.33-dev-linux-amd64 restartPolicy: Never diff --git a/examples/fleet.yaml b/examples/fleet.yaml index f1a64f82f1..6d9dfdeb09 100644 --- a/examples/fleet.yaml +++ b/examples/fleet.yaml @@ -109,4 +109,4 @@ spec: spec: containers: - name: simple-game-server - image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33 + image: us-central1-docker.pkg.dev/agones-ashutoshnsingh/example/simple-game-server:0.33-dev-linux-amd64 diff --git a/examples/gameserver.yaml b/examples/gameserver.yaml index 1f20062748..b330aa0f77 100644 --- a/examples/gameserver.yaml +++ b/examples/gameserver.yaml @@ -121,7 +121,7 @@ spec: spec: containers: - name: simple-game-server - image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33 + image: us-central1-docker.pkg.dev/agones-ashutoshnsingh/example/simple-game-server:0.33-dev-linux-amd64 imagePullPolicy: Always # nodeSelector is a label that can be used to tell Kubernetes which host # OS to use. For Windows game servers uncomment the nodeSelector diff --git a/examples/simple-game-server/Makefile b/examples/simple-game-server/Makefile index 0b1531532a..b32a6bdbc9 100644 --- a/examples/simple-game-server/Makefile +++ b/examples/simple-game-server/Makefile @@ -38,7 +38,7 @@ WITH_ARM64 ?= 1 mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST))) project_path := $(dir $(mkfile_path)) -version := 0.33 +version := 0.33-dev ifeq ($(REPOSITORY),) server_tag := simple-game-server:$(version) else diff --git a/examples/simple-game-server/dev-gameserver.yaml b/examples/simple-game-server/dev-gameserver.yaml index 3bd8218587..ee4e205711 100644 --- a/examples/simple-game-server/dev-gameserver.yaml +++ b/examples/simple-game-server/dev-gameserver.yaml @@ -31,4 +31,4 @@ spec: spec: containers: - name: simple-game-server - image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33 + image: us-central1-docker.pkg.dev/agones-ashutoshnsingh/example/simple-game-server:0.33-dev-linux-amd64 diff --git a/examples/simple-game-server/fleet-distributed.yaml b/examples/simple-game-server/fleet-distributed.yaml index 1333985bb4..6595262d1f 100644 --- a/examples/simple-game-server/fleet-distributed.yaml +++ b/examples/simple-game-server/fleet-distributed.yaml @@ -32,7 +32,7 @@ spec: spec: containers: - name: simple-game-server - image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33 + image: us-central1-docker.pkg.dev/agones-ashutoshnsingh/example/simple-game-server:0.33-dev-linux-amd64 resources: requests: memory: 64Mi diff --git a/examples/simple-game-server/fleet-tcp.yaml b/examples/simple-game-server/fleet-tcp.yaml index 6c7ae262e4..17db6590a6 100644 --- a/examples/simple-game-server/fleet-tcp.yaml +++ b/examples/simple-game-server/fleet-tcp.yaml @@ -28,7 +28,7 @@ spec: spec: containers: - name: simple-game-server - image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33 + image: us-central1-docker.pkg.dev/agones-ashutoshnsingh/example/simple-game-server:0.33-dev-linux-amd64 env: # Disables the UDP listener (Enabled by default) - name: UDP diff --git a/examples/simple-game-server/fleet.yaml b/examples/simple-game-server/fleet.yaml index 6e3c736c60..3330f8d896 100644 --- a/examples/simple-game-server/fleet.yaml +++ b/examples/simple-game-server/fleet.yaml @@ -27,7 +27,7 @@ spec: spec: containers: - name: simple-game-server - image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33 + image: us-central1-docker.pkg.dev/agones-ashutoshnsingh/example/simple-game-server:0.33-dev-linux-amd64 resources: requests: memory: 64Mi diff --git a/examples/simple-game-server/gameserver-passthrough.yaml b/examples/simple-game-server/gameserver-passthrough.yaml index 8fc7666636..6a04b7a98f 100644 --- a/examples/simple-game-server/gameserver-passthrough.yaml +++ b/examples/simple-game-server/gameserver-passthrough.yaml @@ -24,7 +24,7 @@ spec: spec: containers: - name: simple-game-server - image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33 + image: us-central1-docker.pkg.dev/agones-ashutoshnsingh/example/simple-game-server:0.33-dev-linux-amd64 env: - name: PASSTHROUGH value: 'TRUE' diff --git a/examples/simple-game-server/gameserver-windows.yaml b/examples/simple-game-server/gameserver-windows.yaml index ee75733a01..fb93320616 100644 --- a/examples/simple-game-server/gameserver-windows.yaml +++ b/examples/simple-game-server/gameserver-windows.yaml @@ -25,7 +25,7 @@ spec: spec: containers: - name: simple-game-server - image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33 + image: us-central1-docker.pkg.dev/agones-ashutoshnsingh/example/simple-game-server:0.33-dev-linux-amd64 resources: requests: memory: 64Mi diff --git a/examples/simple-game-server/gameserver.yaml b/examples/simple-game-server/gameserver.yaml index f19bc49432..26981bde58 100644 --- a/examples/simple-game-server/gameserver.yaml +++ b/examples/simple-game-server/gameserver.yaml @@ -25,7 +25,7 @@ spec: spec: containers: - name: simple-game-server - image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33 + image: us-central1-docker.pkg.dev/agones-ashutoshnsingh/example/simple-game-server:0.33-dev-linux-amd64 resources: requests: memory: 64Mi diff --git a/examples/simple-game-server/handlers.go b/examples/simple-game-server/handlers.go index 150a898a44..952e58fc2e 100644 --- a/examples/simple-game-server/handlers.go +++ b/examples/simple-game-server/handlers.go @@ -69,9 +69,6 @@ func handleResponse(txt string, s *sdk.SDK, cancel context.CancelFunc) (response responseError = nil handler, exists := responseMap[parts[0]] - if exists && parts[0] == "EXIT" { - return response, addACK, responseError - } if !exists { return response, addACK, responseError } @@ -84,10 +81,12 @@ func handleResponse(txt string, s *sdk.SDK, cancel context.CancelFunc) (response func handleExit(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { // handle elsewhere, as we respond before exiting + response, addACK = defaultReply(parts) return } func handleUnhealthy(s *sdk.SDK, parts []string, cancel ...context.CancelFunc) (response string, addACK bool, responseError error) { + response, addACK = defaultReply(parts) if len(cancel) > 0 { cancel[0]() // Invoke cancel function if provided } @@ -113,6 +112,7 @@ func handleGameServer(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (resp // handleReady attempts to mark this gameserver as ready func handleReady(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { + response, addACK = defaultReply(parts) err := s.Ready() if err != nil { log.Fatalf("Could not send ready message: %v", err) @@ -122,6 +122,7 @@ func handleReady(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response // handleAllocate attempts to allocate this gameserver func handleAllocate(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { + response, addACK = defaultReply(parts) err := s.Allocate() if err != nil { log.Fatalf("could not allocate gameserver: %v", err) @@ -131,13 +132,18 @@ func handleAllocate(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (respon // handleReserve reserve for 10 seconds func handleReserve(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { + response, addACK = defaultReply(parts) if len(parts) != 2 { response = "Invalid RESERVE, should have 1 argument" responseError = fmt.Errorf("Invalid RESERVE, should have 1 argument") + addACK = false + return } if dur, err := time.ParseDuration(parts[1]); err != nil { response = fmt.Sprintf("%s\n", err) responseError = err + addACK = false + return } else { err := s.Reserve(dur) if err != nil { @@ -150,6 +156,7 @@ func handleReserve(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (respons // handleWatch creates a callback to log when // gameserver events occur func handleWatch(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { + response, addACK = defaultReply(parts) err := s.WatchGameServer(func(gs *coresdk.GameServer) { j, err := json.Marshal(gs) if err != nil { @@ -164,11 +171,12 @@ func handleWatch(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response } func handleLabel(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { + response, addACK = defaultReply(parts) switch len(parts) { - case 0: + case 1: // Legacy format setLabel(s, "timestamp", strconv.FormatInt(time.Now().Unix(), 10)) - case 2: + case 3: setLabel(s, parts[1], parts[2]) default: response = "Invalid LABEL command, must use zero or 2 arguments" @@ -184,11 +192,12 @@ func handleCrash(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response } func handleAnnotation(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { + response, addACK = defaultReply(parts) switch len(parts) { case 1: // Legacy format setAnnotation(s, "timestamp", time.Now().UTC().String()) - case 2: + case 3: setAnnotation(s, parts[1], parts[2]) default: response = "Invalid ANNOTATION command, must use zero or 2 arguments" @@ -210,7 +219,7 @@ func handlePlayerCapacity(s *sdk.SDK, parts []string, _ ...context.CancelFunc) ( response = strconv.FormatInt(capacity, 10) + "\n" addACK = false case 2: - if cap, err := strconv.Atoi(parts[0]); err != nil { + if cap, err := strconv.Atoi(parts[1]); err != nil { response = fmt.Sprintf("%s", err) responseError = err } else { @@ -307,6 +316,7 @@ func handleGetCounterCount(s *sdk.SDK, parts []string, _ ...context.CancelFunc) if err != nil { log.Printf("Error getting Counter %s Count: %s", parts[1], err) response, responseError = strconv.FormatInt(count, 10), err + return } response, responseError = "COUNTER: "+strconv.FormatInt(count, 10)+"\n", nil addACK = false @@ -323,15 +333,16 @@ func handleIncrementCounter(s *sdk.SDK, parts []string, _ ...context.CancelFunc) amountInt, err := strconv.ParseInt(parts[2], 10, 64) if err != nil { response, responseError = "", fmt.Errorf("Could not increment Counter %s by unparseable amount %s: %s", parts[1], parts[2], err) + return } log.Printf("Incrementing Counter %s Count by amount %d", parts[1], amountInt) err = s.Beta().IncrementCounter(parts[1], amountInt) if err != nil { log.Printf("Error incrementing Counter %s Count by amount %d: %s", parts[1], amountInt, err) response, responseError = "", err + return } response, responseError = "SUCCESS\n", nil - addACK = false return } @@ -345,15 +356,16 @@ func handleDecrementCounter(s *sdk.SDK, parts []string, _ ...context.CancelFunc) amountInt, err := strconv.ParseInt(parts[2], 10, 64) if err != nil { response, responseError = "", fmt.Errorf("could not decrement Counter %s by unparseable amount %s: %s", parts[1], parts[2], err) + return } log.Printf("Decrementing Counter %s Count by amount %d", parts[1], amountInt) err = s.Beta().DecrementCounter(parts[1], amountInt) if err != nil { log.Printf("Error decrementing Counter %s Count by amount %d: %s", parts[1], amountInt, err) response, responseError = "", err + return } response, responseError = "SUCCESS\n", nil - addACK = false return } @@ -367,15 +379,16 @@ func handleSetCounterCount(s *sdk.SDK, parts []string, _ ...context.CancelFunc) amountInt, err := strconv.ParseInt(parts[2], 10, 64) if err != nil { response, responseError = "", fmt.Errorf("could not set Counter %s to unparseable amount %s: %s", parts[1], parts[2], err) + return } log.Printf("Setting Counter %s Count to amount %d", parts[1], amountInt) err = s.Beta().SetCounterCount(parts[1], amountInt) if err != nil { log.Printf("Error setting Counter %s Count by amount %d: %s", parts[1], amountInt, err) response, responseError = "", err + return } response, responseError = "SUCCESS\n", nil - addACK = false return } @@ -391,9 +404,9 @@ func handleGetCounterCapacity(s *sdk.SDK, parts []string, _ ...context.CancelFun if err != nil { log.Printf("Error getting Counter %s Capacity: %s", parts[1], err) response, responseError = strconv.FormatInt(count, 10), err + return } response, responseError = "CAPACITY: "+strconv.FormatInt(count, 10)+"\n", nil - addACK = false return } @@ -407,15 +420,16 @@ func handleSetCounterCapacity(s *sdk.SDK, parts []string, _ ...context.CancelFun amountInt, err := strconv.ParseInt(parts[2], 10, 64) if err != nil { response, responseError = "", fmt.Errorf("could not set Counter %s to unparseable amount %s: %s", parts[1], parts[2], err) + return } log.Printf("Setting Counter %s Capacity to amount %d", parts[1], amountInt) err = s.Beta().SetCounterCapacity(parts[1], amountInt) if err != nil { log.Printf("Error setting Counter %s Capacity to amount %d: %s", parts[1], amountInt, err) response, responseError = "", err + return } response, responseError = "SUCCESS\n", nil - addACK = false return } @@ -431,9 +445,9 @@ func handleGetListCapacity(s *sdk.SDK, parts []string, _ ...context.CancelFunc) if err != nil { log.Printf("Error getting List %s Capacity: %s", parts[1], err) response, responseError = strconv.FormatInt(capacity, 10), err + return } response, responseError = "CAPACITY: "+strconv.FormatInt(capacity, 10)+"\n", nil - addACK = false return } @@ -447,15 +461,16 @@ func handleSetListCapacity(s *sdk.SDK, parts []string, _ ...context.CancelFunc) amountInt, err := strconv.ParseInt(parts[2], 10, 64) if err != nil { response, responseError = "", fmt.Errorf("could not set List %s to unparseable amount %s: %s", parts[1], parts[2], err) + return } log.Printf("Setting List %s Capacity to amount %d", parts[1], amountInt) err = s.Beta().SetListCapacity(parts[1], amountInt) if err != nil { log.Printf("Error setting List %s Capacity to amount %d: %s", parts[1], amountInt, err) response, responseError = "", err + return } response, responseError = "SUCCESS\n", nil - addACK = false return } @@ -471,9 +486,9 @@ func handleListContains(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (re if err != nil { log.Printf("Error getting List %s contains value %s: %s", parts[1], parts[2], err) response, responseError = strconv.FormatBool(ok), err + return } response, responseError = "FOUND: "+strconv.FormatBool(ok)+"\n", nil - addACK = false return } @@ -489,9 +504,9 @@ func handleGetListLength(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (r if err != nil { log.Printf("Error getting List %s length: %s", parts[1], err) response, responseError = strconv.Itoa(length), err + return } response, responseError = "LENGTH: "+strconv.Itoa(length)+"\n", nil - addACK = false return } @@ -507,12 +522,13 @@ func handleGetListValues(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (r if err != nil { log.Printf("Error getting List %s values: %s", parts[1], err) response, responseError = "INVALID LIST NAME", err + return } if len(values) > 0 { response, responseError = "VALUES: "+strings.Join(values, ",")+"\n", nil + return } response, responseError = "VALUES: \n", nil - addACK = false return } @@ -528,9 +544,9 @@ func handleAppendListValue(s *sdk.SDK, parts []string, _ ...context.CancelFunc) if err != nil { log.Printf("Error appending Value %s to List %s: %s", parts[2], parts[1], err) response, responseError = "", err + return } response, responseError = "SUCCESS\n", nil - addACK = false return } @@ -546,9 +562,9 @@ func handleDeleteListValue(s *sdk.SDK, parts []string, _ ...context.CancelFunc) if err != nil { log.Printf("Error deleting Value %s to List %s: %s", parts[2], parts[1], err) response, responseError = "", err + return } response, responseError = "SUCCESS\n", nil - addACK = false return } @@ -582,3 +598,10 @@ func exit(s *sdk.SDK) { // The process will exit when Agones removes the pod and the // container receives the SIGTERM signal } + +// defaultReply is default handler response +func defaultReply(parts []string) (string, bool) { + response := strings.Join(parts, " ") + addACK := true + return response, addACK +} diff --git a/install/helm/agones/templates/tests/test-runner.yaml b/install/helm/agones/templates/tests/test-runner.yaml index 477a5ff400..714e33cefb 100644 --- a/install/helm/agones/templates/tests/test-runner.yaml +++ b/install/helm/agones/templates/tests/test-runner.yaml @@ -29,7 +29,7 @@ spec: imagePullPolicy: Always env: - name: GAMESERVER_IMAGE - value: "us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33" + value: "us-central1-docker.pkg.dev/agones-ashutoshnsingh/example/simple-game-server:0.33-dev-linux-amd64" - name: IS_HELM_TEST value: "true" - name: GAMESERVERS_NAMESPACE diff --git a/pkg/util/webhooks/webhooks_test.go b/pkg/util/webhooks/webhooks_test.go index 69e94adbe5..5101b1c7b3 100644 --- a/pkg/util/webhooks/webhooks_test.go +++ b/pkg/util/webhooks/webhooks_test.go @@ -164,7 +164,7 @@ func TestWebHookFleetValidationHandler(t *testing.T) { "template": { "spec": { "containers": [{ - "image": "us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33", + "image": "us-central1-docker.pkg.dev/agones-ashutoshnsingh/example/simple-game-server:0.33-dev-linux-amd64", "name": false }] } diff --git a/site/config.toml b/site/config.toml index 6be88c6655..51cf0042c0 100644 --- a/site/config.toml +++ b/site/config.toml @@ -100,7 +100,7 @@ dev_eks_example_cluster_version = "1.29" dev_minikube_example_cluster_version = "1.28.6" # example tag -example_image_tag = "us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33" +example_image_tag = "us-central1-docker.pkg.dev/agones-ashutoshnsingh/example/simple-game-server:0.33-dev-linux-amd64" # Enable syntax highlighting and copy buttons on code blocks with Prism prism_syntax_highlighting = true diff --git a/test/e2e/framework/framework.go b/test/e2e/framework/framework.go index aa0b6dd057..cad972649d 100644 --- a/test/e2e/framework/framework.go +++ b/test/e2e/framework/framework.go @@ -150,7 +150,7 @@ func NewFromFlags() (*Framework, error) { } viper.SetDefault(kubeconfigFlag, filepath.Join(usr.HomeDir, ".kube", "config")) - viper.SetDefault(gsimageFlag, "us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33") + viper.SetDefault(gsimageFlag, "us-central1-docker.pkg.dev/agones-ashutoshnsingh/example/simple-game-server:0.33-dev-linux-amd64") viper.SetDefault(pullSecretFlag, "") viper.SetDefault(stressTestLevelFlag, 0) viper.SetDefault(perfOutputDirFlag, "") @@ -799,7 +799,7 @@ func (f *Framework) DefaultGameServer(namespace string) *agonesv1.GameServer { Containers: []corev1.Container{{ Name: "game-server", Image: f.GameServerImage, - ImagePullPolicy: corev1.PullIfNotPresent, + ImagePullPolicy: corev1.PullAlways, Resources: corev1.ResourceRequirements{ Requests: corev1.ResourceList{ corev1.ResourceCPU: resource.MustParse("30m"), diff --git a/test/e2e/gameserver_test.go b/test/e2e/gameserver_test.go index cd442322a2..851680787c 100644 --- a/test/e2e/gameserver_test.go +++ b/test/e2e/gameserver_test.go @@ -1133,7 +1133,7 @@ spec: preferredDuringSchedulingIgnoredDuringExecution: ERROR containers: - name: simple-game-server - image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33 + image: us-central1-docker.pkg.dev/agones-ashutoshnsingh/example/simple-game-server:0.33-dev-linux-amd64 ` err := os.WriteFile("/tmp/invalid.yaml", []byte(gsYaml), 0o644) require.NoError(t, err) diff --git a/test/load/allocation/fleet.yaml b/test/load/allocation/fleet.yaml index df5e92707d..7489ef32f5 100644 --- a/test/load/allocation/fleet.yaml +++ b/test/load/allocation/fleet.yaml @@ -33,7 +33,7 @@ spec: spec: containers: - args: [-automaticShutdownDelaySec=600] - image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33 + image: us-central1-docker.pkg.dev/agones-ashutoshnsingh/example/simple-game-server:0.33-dev-linux-amd64 name: simple-game-server resources: limits: diff --git a/test/load/allocation/performance-test-fleet-template.yaml b/test/load/allocation/performance-test-fleet-template.yaml index 3e4a4f44d8..1326bce0f7 100644 --- a/test/load/allocation/performance-test-fleet-template.yaml +++ b/test/load/allocation/performance-test-fleet-template.yaml @@ -33,7 +33,7 @@ spec: spec: containers: - args: [-automaticShutdownDelaySec=AUTOMATIC_SHUTDOWN_DELAY_SEC_REPLACEMENT] - image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33 + image: us-central1-docker.pkg.dev/agones-ashutoshnsingh/example/simple-game-server:0.33-dev-linux-amd64 name: simple-game-server resources: limits: diff --git a/test/load/allocation/scenario-fleet.yaml b/test/load/allocation/scenario-fleet.yaml index 08248f6354..0f48bdb6fd 100644 --- a/test/load/allocation/scenario-fleet.yaml +++ b/test/load/allocation/scenario-fleet.yaml @@ -38,7 +38,7 @@ spec: value: 'true' containers: - name: simple-game-server - image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33 + image: us-central1-docker.pkg.dev/agones-ashutoshnsingh/example/simple-game-server:0.33-dev-linux-amd64 args: [-automaticShutdownDelaySec=60, -readyIterations=10] resources: limits: From 83c0c353faa01552647c1b504d3ac035c7ce3fb9 Mon Sep 17 00:00:00 2001 From: Ashutosh Singh Date: Tue, 11 Jun 2024 05:53:14 +0000 Subject: [PATCH 5/7] testing-1 --- examples/simple-game-server/handlers.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/examples/simple-game-server/handlers.go b/examples/simple-game-server/handlers.go index 952e58fc2e..a4dadb0c8f 100644 --- a/examples/simple-game-server/handlers.go +++ b/examples/simple-game-server/handlers.go @@ -209,6 +209,7 @@ func handleAnnotation(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (resp // handlePlayerCapacity sets the player capacity to the given value // or returns the current player capacity as a string func handlePlayerCapacity(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { + response, addACK = defaultReply(parts) switch len(parts) { case 1: log.Print("Getting Player Capacity") @@ -237,6 +238,7 @@ func handlePlayerCapacity(s *sdk.SDK, parts []string, _ ...context.CancelFunc) ( // handlePlayerConnect connects a given player func handlePlayerConnect(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { + response, addACK = defaultReply(parts) if len(parts) < 2 { response = "Invalid PLAYER_CONNECT, should have 1 argument" responseError = fmt.Errorf("Invalid PLAYER_CONNECT, should have 1 argument") @@ -251,6 +253,7 @@ func handlePlayerConnect(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (r // handlePlayerDisconnect disconnects a given player func handlePlayerDisconnect(s *sdk.SDK, parts []string, _ ...context.CancelFunc) (response string, addACK bool, responseError error) { + response, addACK = defaultReply(parts) if len(parts) < 2 { response = "Invalid PLAYER_DISCONNECT, should have 1 argument" responseError = fmt.Errorf("Invalid PLAYER_DISCONNECT, should have 1 argument") From bc317f468bbffc24c7f53a65afda94c4ffc39ba4 Mon Sep 17 00:00:00 2001 From: Ashutosh Singh Date: Tue, 11 Jun 2024 11:47:21 +0000 Subject: [PATCH 6/7] testing-2 --- examples/simple-game-server/handlers.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/examples/simple-game-server/handlers.go b/examples/simple-game-server/handlers.go index a4dadb0c8f..c1ccd20688 100644 --- a/examples/simple-game-server/handlers.go +++ b/examples/simple-game-server/handlers.go @@ -335,14 +335,14 @@ func handleIncrementCounter(s *sdk.SDK, parts []string, _ ...context.CancelFunc) } amountInt, err := strconv.ParseInt(parts[2], 10, 64) if err != nil { - response, responseError = "", fmt.Errorf("Could not increment Counter %s by unparseable amount %s: %s", parts[1], parts[2], err) + response, responseError = err.Error(), fmt.Errorf("Could not increment Counter %s by unparseable amount %s: %s", parts[1], parts[2], err) return } log.Printf("Incrementing Counter %s Count by amount %d", parts[1], amountInt) err = s.Beta().IncrementCounter(parts[1], amountInt) if err != nil { log.Printf("Error incrementing Counter %s Count by amount %d: %s", parts[1], amountInt, err) - response, responseError = "", err + response, responseError = err.Error(), err return } response, responseError = "SUCCESS\n", nil @@ -358,14 +358,14 @@ func handleDecrementCounter(s *sdk.SDK, parts []string, _ ...context.CancelFunc) } amountInt, err := strconv.ParseInt(parts[2], 10, 64) if err != nil { - response, responseError = "", fmt.Errorf("could not decrement Counter %s by unparseable amount %s: %s", parts[1], parts[2], err) + response, responseError = err.Error(), fmt.Errorf("could not decrement Counter %s by unparseable amount %s: %s", parts[1], parts[2], err) return } log.Printf("Decrementing Counter %s Count by amount %d", parts[1], amountInt) err = s.Beta().DecrementCounter(parts[1], amountInt) if err != nil { log.Printf("Error decrementing Counter %s Count by amount %d: %s", parts[1], amountInt, err) - response, responseError = "", err + response, responseError = err.Error(), err return } response, responseError = "SUCCESS\n", nil @@ -381,14 +381,14 @@ func handleSetCounterCount(s *sdk.SDK, parts []string, _ ...context.CancelFunc) } amountInt, err := strconv.ParseInt(parts[2], 10, 64) if err != nil { - response, responseError = "", fmt.Errorf("could not set Counter %s to unparseable amount %s: %s", parts[1], parts[2], err) + response, responseError = err.Error(), fmt.Errorf("could not set Counter %s to unparseable amount %s: %s", parts[1], parts[2], err) return } log.Printf("Setting Counter %s Count to amount %d", parts[1], amountInt) err = s.Beta().SetCounterCount(parts[1], amountInt) if err != nil { log.Printf("Error setting Counter %s Count by amount %d: %s", parts[1], amountInt, err) - response, responseError = "", err + response, responseError = err.Error(), err return } response, responseError = "SUCCESS\n", nil @@ -422,14 +422,14 @@ func handleSetCounterCapacity(s *sdk.SDK, parts []string, _ ...context.CancelFun } amountInt, err := strconv.ParseInt(parts[2], 10, 64) if err != nil { - response, responseError = "", fmt.Errorf("could not set Counter %s to unparseable amount %s: %s", parts[1], parts[2], err) + response, responseError = err.Error(), fmt.Errorf("could not set Counter %s to unparseable amount %s: %s", parts[1], parts[2], err) return } log.Printf("Setting Counter %s Capacity to amount %d", parts[1], amountInt) err = s.Beta().SetCounterCapacity(parts[1], amountInt) if err != nil { log.Printf("Error setting Counter %s Capacity to amount %d: %s", parts[1], amountInt, err) - response, responseError = "", err + response, responseError = err.Error(), err return } response, responseError = "SUCCESS\n", nil @@ -463,14 +463,14 @@ func handleSetListCapacity(s *sdk.SDK, parts []string, _ ...context.CancelFunc) } amountInt, err := strconv.ParseInt(parts[2], 10, 64) if err != nil { - response, responseError = "", fmt.Errorf("could not set List %s to unparseable amount %s: %s", parts[1], parts[2], err) + response, responseError = err.Error(), fmt.Errorf("could not set List %s to unparseable amount %s: %s", parts[1], parts[2], err) return } log.Printf("Setting List %s Capacity to amount %d", parts[1], amountInt) err = s.Beta().SetListCapacity(parts[1], amountInt) if err != nil { log.Printf("Error setting List %s Capacity to amount %d: %s", parts[1], amountInt, err) - response, responseError = "", err + response, responseError = err.Error(), err return } response, responseError = "SUCCESS\n", nil @@ -546,7 +546,7 @@ func handleAppendListValue(s *sdk.SDK, parts []string, _ ...context.CancelFunc) err := s.Beta().AppendListValue(parts[1], parts[2]) if err != nil { log.Printf("Error appending Value %s to List %s: %s", parts[2], parts[1], err) - response, responseError = "", err + response, responseError = err.Error(), err return } response, responseError = "SUCCESS\n", nil @@ -564,7 +564,7 @@ func handleDeleteListValue(s *sdk.SDK, parts []string, _ ...context.CancelFunc) err := s.Beta().DeleteListValue(parts[1], parts[2]) if err != nil { log.Printf("Error deleting Value %s to List %s: %s", parts[2], parts[1], err) - response, responseError = "", err + response, responseError = err.Error(), err return } response, responseError = "SUCCESS\n", nil From 1614bf059d698ef6035b3b15b7bc57fcbee89b22 Mon Sep 17 00:00:00 2001 From: Ashutosh Singh Date: Mon, 17 Jun 2024 10:58:49 +0000 Subject: [PATCH 7/7] final refactored code --- build/Makefile | 2 +- examples/crd-client/create-gs.yaml | 2 +- examples/fleet.yaml | 2 +- examples/gameserver.yaml | 2 +- examples/simple-game-server/Dockerfile.windows | 2 +- examples/simple-game-server/Makefile | 2 +- examples/simple-game-server/dev-gameserver.yaml | 2 +- examples/simple-game-server/fleet-distributed.yaml | 2 +- examples/simple-game-server/fleet-tcp.yaml | 2 +- examples/simple-game-server/fleet.yaml | 2 +- examples/simple-game-server/gameserver-none.yaml | 2 +- examples/simple-game-server/gameserver-passthrough.yaml | 2 +- examples/simple-game-server/gameserver-windows.yaml | 2 +- examples/simple-game-server/gameserver.yaml | 2 +- install/helm/agones/templates/tests/test-runner.yaml | 2 +- pkg/util/webhooks/webhooks_test.go | 2 +- site/config.toml | 2 +- site/content/en/blog/releases/1.41.0.md | 2 +- test/e2e/framework/framework.go | 4 ++-- test/e2e/gameserver_test.go | 2 +- test/load/allocation/fleet.yaml | 2 +- test/load/allocation/performance-test-fleet-template.yaml | 2 +- test/load/allocation/scenario-fleet.yaml | 2 +- 23 files changed, 24 insertions(+), 24 deletions(-) diff --git a/build/Makefile b/build/Makefile index 56340c1091..ebd1336308 100644 --- a/build/Makefile +++ b/build/Makefile @@ -64,7 +64,7 @@ KIND_PROFILE ?= agones KIND_CONTAINER_NAME=$(KIND_PROFILE)-control-plane # Game Server image to use while doing end-to-end tests -GS_TEST_IMAGE ?= us-central1-docker.pkg.dev/agones-ashutoshnsingh/example/simple-game-server:0.33-dev-linux-amd64 +GS_TEST_IMAGE ?= us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33 # Enable all beta feature gates. Keep in sync with `true` (beta) entries in pkg/util/runtime/features.go:featureDefaults BETA_FEATURE_GATES ?= "CountsAndLists=true&DisableResyncOnSDKServer=true" diff --git a/examples/crd-client/create-gs.yaml b/examples/crd-client/create-gs.yaml index a851866bd5..6e3ff58987 100644 --- a/examples/crd-client/create-gs.yaml +++ b/examples/crd-client/create-gs.yaml @@ -38,5 +38,5 @@ spec: imagePullPolicy: Always env: - name: GAMESERVER_IMAGE - value: us-central1-docker.pkg.dev/agones-ashutoshnsingh/example/simple-game-server:0.33-dev-linux-amd64 + value: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33 restartPolicy: Never diff --git a/examples/fleet.yaml b/examples/fleet.yaml index 6d9dfdeb09..f1a64f82f1 100644 --- a/examples/fleet.yaml +++ b/examples/fleet.yaml @@ -109,4 +109,4 @@ spec: spec: containers: - name: simple-game-server - image: us-central1-docker.pkg.dev/agones-ashutoshnsingh/example/simple-game-server:0.33-dev-linux-amd64 + image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33 diff --git a/examples/gameserver.yaml b/examples/gameserver.yaml index b330aa0f77..1f20062748 100644 --- a/examples/gameserver.yaml +++ b/examples/gameserver.yaml @@ -121,7 +121,7 @@ spec: spec: containers: - name: simple-game-server - image: us-central1-docker.pkg.dev/agones-ashutoshnsingh/example/simple-game-server:0.33-dev-linux-amd64 + image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33 imagePullPolicy: Always # nodeSelector is a label that can be used to tell Kubernetes which host # OS to use. For Windows game servers uncomment the nodeSelector diff --git a/examples/simple-game-server/Dockerfile.windows b/examples/simple-game-server/Dockerfile.windows index b20770d6e4..63cb2bfa40 100644 --- a/examples/simple-game-server/Dockerfile.windows +++ b/examples/simple-game-server/Dockerfile.windows @@ -26,7 +26,7 @@ COPY . agones.dev/agones WORKDIR /go/src/agones.dev/agones/examples/simple-game-server -RUN GOOS=windows GOARCH=amd64 CGO_ENABLED=0 go build -o simple-game-server.exe main.go +RUN GOOS=windows GOARCH=amd64 CGO_ENABLED=0 go build -o simple-game-server.exe *.go RUN ls -ltr /go/src/agones.dev/agones/examples/simple-game-server FROM mcr.microsoft.com/windows/servercore:${WINDOWS_VERSION} diff --git a/examples/simple-game-server/Makefile b/examples/simple-game-server/Makefile index b32a6bdbc9..0b1531532a 100644 --- a/examples/simple-game-server/Makefile +++ b/examples/simple-game-server/Makefile @@ -38,7 +38,7 @@ WITH_ARM64 ?= 1 mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST))) project_path := $(dir $(mkfile_path)) -version := 0.33-dev +version := 0.33 ifeq ($(REPOSITORY),) server_tag := simple-game-server:$(version) else diff --git a/examples/simple-game-server/dev-gameserver.yaml b/examples/simple-game-server/dev-gameserver.yaml index ee4e205711..3bd8218587 100644 --- a/examples/simple-game-server/dev-gameserver.yaml +++ b/examples/simple-game-server/dev-gameserver.yaml @@ -31,4 +31,4 @@ spec: spec: containers: - name: simple-game-server - image: us-central1-docker.pkg.dev/agones-ashutoshnsingh/example/simple-game-server:0.33-dev-linux-amd64 + image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33 diff --git a/examples/simple-game-server/fleet-distributed.yaml b/examples/simple-game-server/fleet-distributed.yaml index 6595262d1f..1333985bb4 100644 --- a/examples/simple-game-server/fleet-distributed.yaml +++ b/examples/simple-game-server/fleet-distributed.yaml @@ -32,7 +32,7 @@ spec: spec: containers: - name: simple-game-server - image: us-central1-docker.pkg.dev/agones-ashutoshnsingh/example/simple-game-server:0.33-dev-linux-amd64 + image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33 resources: requests: memory: 64Mi diff --git a/examples/simple-game-server/fleet-tcp.yaml b/examples/simple-game-server/fleet-tcp.yaml index 17db6590a6..6c7ae262e4 100644 --- a/examples/simple-game-server/fleet-tcp.yaml +++ b/examples/simple-game-server/fleet-tcp.yaml @@ -28,7 +28,7 @@ spec: spec: containers: - name: simple-game-server - image: us-central1-docker.pkg.dev/agones-ashutoshnsingh/example/simple-game-server:0.33-dev-linux-amd64 + image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33 env: # Disables the UDP listener (Enabled by default) - name: UDP diff --git a/examples/simple-game-server/fleet.yaml b/examples/simple-game-server/fleet.yaml index 3330f8d896..6e3c736c60 100644 --- a/examples/simple-game-server/fleet.yaml +++ b/examples/simple-game-server/fleet.yaml @@ -27,7 +27,7 @@ spec: spec: containers: - name: simple-game-server - image: us-central1-docker.pkg.dev/agones-ashutoshnsingh/example/simple-game-server:0.33-dev-linux-amd64 + image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33 resources: requests: memory: 64Mi diff --git a/examples/simple-game-server/gameserver-none.yaml b/examples/simple-game-server/gameserver-none.yaml index 5a5746ffac..1cd1057f21 100644 --- a/examples/simple-game-server/gameserver-none.yaml +++ b/examples/simple-game-server/gameserver-none.yaml @@ -29,7 +29,7 @@ spec: spec: containers: - name: simple-game-server - image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.28 + image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33 resources: requests: memory: 64Mi diff --git a/examples/simple-game-server/gameserver-passthrough.yaml b/examples/simple-game-server/gameserver-passthrough.yaml index 6a04b7a98f..8fc7666636 100644 --- a/examples/simple-game-server/gameserver-passthrough.yaml +++ b/examples/simple-game-server/gameserver-passthrough.yaml @@ -24,7 +24,7 @@ spec: spec: containers: - name: simple-game-server - image: us-central1-docker.pkg.dev/agones-ashutoshnsingh/example/simple-game-server:0.33-dev-linux-amd64 + image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33 env: - name: PASSTHROUGH value: 'TRUE' diff --git a/examples/simple-game-server/gameserver-windows.yaml b/examples/simple-game-server/gameserver-windows.yaml index fb93320616..ee75733a01 100644 --- a/examples/simple-game-server/gameserver-windows.yaml +++ b/examples/simple-game-server/gameserver-windows.yaml @@ -25,7 +25,7 @@ spec: spec: containers: - name: simple-game-server - image: us-central1-docker.pkg.dev/agones-ashutoshnsingh/example/simple-game-server:0.33-dev-linux-amd64 + image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33 resources: requests: memory: 64Mi diff --git a/examples/simple-game-server/gameserver.yaml b/examples/simple-game-server/gameserver.yaml index 26981bde58..f19bc49432 100644 --- a/examples/simple-game-server/gameserver.yaml +++ b/examples/simple-game-server/gameserver.yaml @@ -25,7 +25,7 @@ spec: spec: containers: - name: simple-game-server - image: us-central1-docker.pkg.dev/agones-ashutoshnsingh/example/simple-game-server:0.33-dev-linux-amd64 + image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33 resources: requests: memory: 64Mi diff --git a/install/helm/agones/templates/tests/test-runner.yaml b/install/helm/agones/templates/tests/test-runner.yaml index 714e33cefb..477a5ff400 100644 --- a/install/helm/agones/templates/tests/test-runner.yaml +++ b/install/helm/agones/templates/tests/test-runner.yaml @@ -29,7 +29,7 @@ spec: imagePullPolicy: Always env: - name: GAMESERVER_IMAGE - value: "us-central1-docker.pkg.dev/agones-ashutoshnsingh/example/simple-game-server:0.33-dev-linux-amd64" + value: "us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33" - name: IS_HELM_TEST value: "true" - name: GAMESERVERS_NAMESPACE diff --git a/pkg/util/webhooks/webhooks_test.go b/pkg/util/webhooks/webhooks_test.go index 5101b1c7b3..69e94adbe5 100644 --- a/pkg/util/webhooks/webhooks_test.go +++ b/pkg/util/webhooks/webhooks_test.go @@ -164,7 +164,7 @@ func TestWebHookFleetValidationHandler(t *testing.T) { "template": { "spec": { "containers": [{ - "image": "us-central1-docker.pkg.dev/agones-ashutoshnsingh/example/simple-game-server:0.33-dev-linux-amd64", + "image": "us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33", "name": false }] } diff --git a/site/config.toml b/site/config.toml index 51cf0042c0..6be88c6655 100644 --- a/site/config.toml +++ b/site/config.toml @@ -100,7 +100,7 @@ dev_eks_example_cluster_version = "1.29" dev_minikube_example_cluster_version = "1.28.6" # example tag -example_image_tag = "us-central1-docker.pkg.dev/agones-ashutoshnsingh/example/simple-game-server:0.33-dev-linux-amd64" +example_image_tag = "us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33" # Enable syntax highlighting and copy buttons on code blocks with Prism prism_syntax_highlighting = true diff --git a/site/content/en/blog/releases/1.41.0.md b/site/content/en/blog/releases/1.41.0.md index a1582812ac..1cefe1b520 100644 --- a/site/content/en/blog/releases/1.41.0.md +++ b/site/content/en/blog/releases/1.41.0.md @@ -49,7 +49,7 @@ Images available with this release: - [us-docker.pkg.dev/agones-images/examples/crd-client:0.16](https://us-docker.pkg.dev/agones-images/examples/crd-client:0.16) - [us-docker.pkg.dev/agones-images/examples/nodejs-simple-server:0.10](https://us-docker.pkg.dev/agones-images/examples/nodejs-simple-server:0.10) - [us-docker.pkg.dev/agones-images/examples/rust-simple-server:0.13](https://us-docker.pkg.dev/agones-images/examples/rust-simple-server:0.13) -- [us-docker.pkg.dev/agones-images/examples/simple-game-server:0.32](https://us-docker.pkg.dev/agones-images/examples/simple-game-server:0.32) +- [us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33](https://us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33) - [us-docker.pkg.dev/agones-images/examples/supertuxkart-example:0.13](https://us-docker.pkg.dev/agones-images/examples/supertuxkart-example:0.13) - [us-docker.pkg.dev/agones-images/examples/unity-simple-server:0.3](https://us-docker.pkg.dev/agones-images/examples/unity-simple-server:0.3) - [us-docker.pkg.dev/agones-images/examples/xonotic-example:1.9](https://us-docker.pkg.dev/agones-images/examples/xonotic-example:1.9) diff --git a/test/e2e/framework/framework.go b/test/e2e/framework/framework.go index cad972649d..aa0b6dd057 100644 --- a/test/e2e/framework/framework.go +++ b/test/e2e/framework/framework.go @@ -150,7 +150,7 @@ func NewFromFlags() (*Framework, error) { } viper.SetDefault(kubeconfigFlag, filepath.Join(usr.HomeDir, ".kube", "config")) - viper.SetDefault(gsimageFlag, "us-central1-docker.pkg.dev/agones-ashutoshnsingh/example/simple-game-server:0.33-dev-linux-amd64") + viper.SetDefault(gsimageFlag, "us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33") viper.SetDefault(pullSecretFlag, "") viper.SetDefault(stressTestLevelFlag, 0) viper.SetDefault(perfOutputDirFlag, "") @@ -799,7 +799,7 @@ func (f *Framework) DefaultGameServer(namespace string) *agonesv1.GameServer { Containers: []corev1.Container{{ Name: "game-server", Image: f.GameServerImage, - ImagePullPolicy: corev1.PullAlways, + ImagePullPolicy: corev1.PullIfNotPresent, Resources: corev1.ResourceRequirements{ Requests: corev1.ResourceList{ corev1.ResourceCPU: resource.MustParse("30m"), diff --git a/test/e2e/gameserver_test.go b/test/e2e/gameserver_test.go index 851680787c..cd442322a2 100644 --- a/test/e2e/gameserver_test.go +++ b/test/e2e/gameserver_test.go @@ -1133,7 +1133,7 @@ spec: preferredDuringSchedulingIgnoredDuringExecution: ERROR containers: - name: simple-game-server - image: us-central1-docker.pkg.dev/agones-ashutoshnsingh/example/simple-game-server:0.33-dev-linux-amd64 + image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33 ` err := os.WriteFile("/tmp/invalid.yaml", []byte(gsYaml), 0o644) require.NoError(t, err) diff --git a/test/load/allocation/fleet.yaml b/test/load/allocation/fleet.yaml index 7489ef32f5..df5e92707d 100644 --- a/test/load/allocation/fleet.yaml +++ b/test/load/allocation/fleet.yaml @@ -33,7 +33,7 @@ spec: spec: containers: - args: [-automaticShutdownDelaySec=600] - image: us-central1-docker.pkg.dev/agones-ashutoshnsingh/example/simple-game-server:0.33-dev-linux-amd64 + image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33 name: simple-game-server resources: limits: diff --git a/test/load/allocation/performance-test-fleet-template.yaml b/test/load/allocation/performance-test-fleet-template.yaml index 1326bce0f7..3e4a4f44d8 100644 --- a/test/load/allocation/performance-test-fleet-template.yaml +++ b/test/load/allocation/performance-test-fleet-template.yaml @@ -33,7 +33,7 @@ spec: spec: containers: - args: [-automaticShutdownDelaySec=AUTOMATIC_SHUTDOWN_DELAY_SEC_REPLACEMENT] - image: us-central1-docker.pkg.dev/agones-ashutoshnsingh/example/simple-game-server:0.33-dev-linux-amd64 + image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33 name: simple-game-server resources: limits: diff --git a/test/load/allocation/scenario-fleet.yaml b/test/load/allocation/scenario-fleet.yaml index 0f48bdb6fd..08248f6354 100644 --- a/test/load/allocation/scenario-fleet.yaml +++ b/test/load/allocation/scenario-fleet.yaml @@ -38,7 +38,7 @@ spec: value: 'true' containers: - name: simple-game-server - image: us-central1-docker.pkg.dev/agones-ashutoshnsingh/example/simple-game-server:0.33-dev-linux-amd64 + image: us-docker.pkg.dev/agones-images/examples/simple-game-server:0.33 args: [-automaticShutdownDelaySec=60, -readyIterations=10] resources: limits: