Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 16 additions & 17 deletions pkg/apis/agones/v1/gameserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"agones.dev/agones/pkg/apis"
"agones.dev/agones/pkg/apis/agones"
"agones.dev/agones/pkg/util/runtime"
"github.com/pkg/errors"
"gomodules.xyz/jsonpatch/v2"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
Expand Down Expand Up @@ -761,7 +760,7 @@ func (gs *GameServer) ApplyToPodContainer(pod *corev1.Pod, containerName string,
return nil
}
}
return errors.Errorf("failed to find container named %s in pod spec", containerName)
return gameserverErrors.Errorf("failed to find container named %s in pod spec", containerName)
}

// Pod creates a new Pod from the PodTemplateSpec
Expand Down Expand Up @@ -976,17 +975,17 @@ func (gs *GameServer) Patch(delta *GameServer) ([]byte, error) {

oldJSON, err := json.Marshal(gs)
if err != nil {
return result, errors.Wrapf(err, "error marshalling to json current GameServer %s", gs.ObjectMeta.Name)
return result, gameserverErrors.Wrapf(err, "error marshalling to json current GameServer %s", gs.ObjectMeta.Name)
}

newJSON, err := json.Marshal(delta)
if err != nil {
return result, errors.Wrapf(err, "error marshalling to json delta GameServer %s", delta.ObjectMeta.Name)
return result, gameserverErrors.Wrapf(err, "error marshalling to json delta GameServer %s", delta.ObjectMeta.Name)
}

patch, err := jsonpatch.CreatePatch(oldJSON, newJSON)
if err != nil {
return result, errors.Wrapf(err, "error creating patch for GameServer %s", gs.ObjectMeta.Name)
return result, gameserverErrors.Wrapf(err, "error creating patch for GameServer %s", gs.ObjectMeta.Name)
}

// Per https://jsonpatch.com/ "Tests that the specified value is set in the document. If the test
Expand All @@ -996,16 +995,16 @@ func (gs *GameServer) Patch(delta *GameServer) ([]byte, error) {
patches = append(patches, patch...)

result, err = json.Marshal(patches)
return result, errors.Wrapf(err, "error creating json for patch for GameServer %s", gs.ObjectMeta.Name)
return result, gameserverErrors.Wrapf(err, "error creating json for patch for GameServer %s", gs.ObjectMeta.Name)
}

// UpdateCount increments or decrements a CounterStatus on a Game Server by the given amount.
func (gs *GameServer) UpdateCount(name string, action string, amount int64) error {
if action != GameServerPriorityIncrement && action != GameServerPriorityDecrement {
return errors.Errorf("unable to UpdateCount with Name %s, Action %s, Amount %d. Allocation action must be one of %s or %s", name, action, amount, GameServerPriorityIncrement, GameServerPriorityDecrement)
return gameserverErrors.Errorf("unable to UpdateCount with Name %s, Action %s, Amount %d. Allocation action must be one of %s or %s", name, action, amount, GameServerPriorityIncrement, GameServerPriorityDecrement)
}
if amount < 0 {
return errors.Errorf("unable to UpdateCount with Name %s, Action %s, Amount %d. Amount must be greater than 0", name, action, amount)
return gameserverErrors.Errorf("unable to UpdateCount with Name %s, Action %s, Amount %d. Amount must be greater than 0", name, action, amount)
}
if counter, ok := gs.Status.Counters[name]; ok {
cnt := counter.Count
Expand All @@ -1026,13 +1025,13 @@ func (gs *GameServer) UpdateCount(name string, action string, amount int64) erro
gs.Status.Counters[name] = counter
return nil
}
return errors.Errorf("unable to UpdateCount with Name %s, Action %s, Amount %d. Counter not found in GameServer %s", name, action, amount, gs.ObjectMeta.GetName())
return gameserverErrors.Errorf("unable to UpdateCount with Name %s, Action %s, Amount %d. Counter not found in GameServer %s", name, action, amount, gs.ObjectMeta.GetName())
}

// UpdateCounterCapacity updates the CounterStatus Capacity to the given capacity.
func (gs *GameServer) UpdateCounterCapacity(name string, capacity int64) error {
if capacity < 0 {
return errors.Errorf("unable to UpdateCounterCapacity: Name %s, Capacity %d. Capacity must be greater than or equal to 0", name, capacity)
return gameserverErrors.Errorf("unable to UpdateCounterCapacity: Name %s, Capacity %d. Capacity must be greater than or equal to 0", name, capacity)
}
if counter, ok := gs.Status.Counters[name]; ok {
counter.Capacity = capacity
Expand All @@ -1043,28 +1042,28 @@ func (gs *GameServer) UpdateCounterCapacity(name string, capacity int64) error {
gs.Status.Counters[name] = counter
return nil
}
return errors.Errorf("unable to UpdateCounterCapacity: Name %s, Capacity %d. Counter not found in GameServer %s", name, capacity, gs.ObjectMeta.GetName())
return gameserverErrors.Errorf("unable to UpdateCounterCapacity: Name %s, Capacity %d. Counter not found in GameServer %s", name, capacity, gs.ObjectMeta.GetName())
}

// UpdateListCapacity updates the ListStatus Capacity to the given capacity.
func (gs *GameServer) UpdateListCapacity(name string, capacity int64) error {

if capacity < 0 || capacity > ListMaxCapacity {
return errors.Errorf("unable to UpdateListCapacity: Name %s, Capacity %d. Capacity must be between 0 and 1000, inclusive", name, capacity)
return gameserverErrors.Errorf("unable to UpdateListCapacity: Name %s, Capacity %d. Capacity must be between 0 and 1000, inclusive", name, capacity)
}
if list, ok := gs.Status.Lists[name]; ok {
list.Capacity = capacity
list.Values = truncateList(list.Capacity, list.Values)
gs.Status.Lists[name] = list
return nil
}
return errors.Errorf("unable to UpdateListCapacity: Name %s, Capacity %d. List not found in GameServer %s", name, capacity, gs.ObjectMeta.GetName())
return gameserverErrors.Errorf("unable to UpdateListCapacity: Name %s, Capacity %d. List not found in GameServer %s", name, capacity, gs.ObjectMeta.GetName())
}

// AppendListValues adds unique values to the ListStatus Values list.
func (gs *GameServer) AppendListValues(name string, values []string) error {
if values == nil {
return errors.Errorf("unable to AppendListValues: Name %s, Values %s. Values must not be nil", name, values)
return gameserverErrors.Errorf("unable to AppendListValues: Name %s, Values %s. Values must not be nil", name, values)
}
if list, ok := gs.Status.Lists[name]; ok {
mergedList := MergeRemoveDuplicates(list.Values, values)
Expand All @@ -1074,14 +1073,14 @@ func (gs *GameServer) AppendListValues(name string, values []string) error {
gs.Status.Lists[name] = list
return nil
}
return errors.Errorf("unable to AppendListValues: Name %s, Values %s. List not found in GameServer %s", name, values, gs.ObjectMeta.GetName())
return gameserverErrors.Errorf("unable to AppendListValues: Name %s, Values %s. List not found in GameServer %s", name, values, gs.ObjectMeta.GetName())
}

// DeleteListValues removes values from the ListStatus Values list. Values in the DeleteListValues
// list that are not in the ListStatus Values list are ignored.
func (gs *GameServer) DeleteListValues(name string, values []string) error {
if values == nil {
return errors.Errorf("unable to DeleteListValues: Name %s, Values %s. Values must not be nil", name, values)
return gameserverErrors.Errorf("unable to DeleteListValues: Name %s, Values %s. Values must not be nil", name, values)
}
if list, ok := gs.Status.Lists[name]; ok {
deleteValuesMap := make(map[string]bool)
Expand All @@ -1093,7 +1092,7 @@ func (gs *GameServer) DeleteListValues(name string, values []string) error {
gs.Status.Lists[name] = list
return nil
}
return errors.Errorf("unable to DeleteListValues: Name %s, Values %s. List not found in GameServer %s", name, values, gs.ObjectMeta.GetName())
return gameserverErrors.Errorf("unable to DeleteListValues: Name %s, Values %s. List not found in GameServer %s", name, values, gs.ObjectMeta.GetName())
}

// deleteValues returns a new list with all the values in valuesList that are not keys in deleteValuesMap.
Expand Down
4 changes: 2 additions & 2 deletions pkg/apis/agones/v1/gameserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1630,7 +1630,7 @@ func TestGameServerPodContainerNotFoundErrReturned(t *testing.T) {

_, err := fixture.Pod(fakeAPIHooks{})
if assert.Error(t, err, "Pod should return an error") {
assert.Equal(t, "failed to find container named Container1 in pod spec", err.Error())
assert.ErrorContains(t, err, "failed to find container named Container1 in pod spec")
}
}

Expand Down Expand Up @@ -2060,7 +2060,7 @@ func TestGameServerApplyToPodContainer(t *testing.T) {
})

if tc.expected.err != "" && assert.Error(t, result) {
assert.Equal(t, tc.expected.err, result.Error())
assert.ErrorContains(t, result, tc.expected.err)
}
assert.Equal(t, tc.expected.tty, pod.Spec.Containers[0].TTY)
assert.False(t, pod.Spec.Containers[1].TTY)
Expand Down
19 changes: 19 additions & 0 deletions pkg/apis/agones/v1/v1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright Contributors to Agones a Series of LF Projects, LLC.
//
// 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 v1

import "agones.dev/agones/pkg/util/errors"

var gameserverErrors = errors.FromStruct[GameServer](nil)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice use of a generic!