Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved gameserver unit tests #1485

Merged
merged 4 commits into from
Apr 28, 2020
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
2 changes: 1 addition & 1 deletion pkg/apis/agones/v1/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
// Block of const Error messages
const (
ErrContainerRequired = "Container is required when using multiple containers in the pod template"
ErrHostPortDynamic = "HostPort cannot be specified with a Dynamic PortPolicy"
ErrHostPort = "HostPort cannot be specified with a Dynamic or Passthrough PortPolicy"
ErrPortPolicyStatic = "PortPolicy must be Static"
ErrContainerPortRequired = "ContainerPort must be defined for Dynamic and Static PortPolicies"
ErrContainerPortPassthrough = "ContainerPort cannot be specified with Passthrough PortPolicy"
Expand Down
37 changes: 20 additions & 17 deletions pkg/apis/agones/v1/gameserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,18 +360,30 @@ func (gss *GameServerSpec) Validate(devAddress string) ([]metav1.StatusCause, bo
causes = append(causes, metav1.StatusCause{
Type: metav1.CauseTypeFieldValueNotSupported,
Field: "players",
Message: fmt.Sprintf("Value cannot be set unless feature flag %s is enabled,", runtime.FeaturePlayerTracking),
Message: fmt.Sprintf("Value cannot be set unless feature flag %s is enabled", runtime.FeaturePlayerTracking),
})
}
}

if !runtime.FeatureEnabled(runtime.FeatureContainerPortAllocation) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

That's a great way to separate this Feature logic.

for _, p := range gss.Ports {
if p.Container != nil {
causes = append(causes, metav1.StatusCause{
Type: metav1.CauseTypeFieldValueNotSupported,
Field: fmt.Sprintf("%s.container", p.Name),
Message: fmt.Sprintf("Value cannot be set unless feature flag %s is enabled", runtime.FeatureContainerPortAllocation),
})
}
}
}

if devAddress != "" {
// verify that the value is a valid IP address.
if net.ParseIP(devAddress) == nil {
causes = append(causes, metav1.StatusCause{
Type: metav1.CauseTypeFieldValueInvalid,
Field: fmt.Sprintf("annotations.%s", DevAddressAnnotation),
Message: fmt.Sprintf("Value '%s' of annotation '%s' must be a valid IP address.", DevAddressAnnotation, devAddress),
Message: fmt.Sprintf("Value '%s' of annotation '%s' must be a valid IP address", devAddress, DevAddressAnnotation),
})
}

Expand All @@ -380,7 +392,7 @@ func (gss *GameServerSpec) Validate(devAddress string) ([]metav1.StatusCause, bo
causes = append(causes, metav1.StatusCause{
Type: metav1.CauseTypeFieldValueRequired,
Field: fmt.Sprintf("%s.hostPort", p.Name),
Message: fmt.Sprintf("HostPort is required if GameServer is annotated with %s", DevAddressAnnotation),
Message: fmt.Sprintf("HostPort is required if GameServer is annotated with '%s'", DevAddressAnnotation),
})
}
if p.PortPolicy != Static {
Expand Down Expand Up @@ -435,11 +447,11 @@ func (gss *GameServerSpec) Validate(devAddress string) ([]metav1.StatusCause, bo
causes = append(causes, metav1.StatusCause{
Type: metav1.CauseTypeFieldValueInvalid,
Field: fmt.Sprintf("%s.hostPort", p.Name),
Message: ErrHostPortDynamic,
Message: ErrHostPort,
})
}

if p.Container != nil && gss.Container != "" {
if p.Container != nil && gss.Container != "" && runtime.FeatureEnabled(runtime.FeatureContainerPortAllocation) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍

_, _, err := gss.FindContainer(*p.Container)
if err != nil {
causes = append(causes, metav1.StatusCause{
Expand Down Expand Up @@ -572,22 +584,13 @@ func (gs *GameServer) FindGameServerContainer() (int, corev1.Container, error) {
// ApplyToPodContainer applies func(v1.Container) to the specified container in the pod.
// Returns an error if the container is not found.
func (gs *GameServer) ApplyToPodContainer(pod *corev1.Pod, containerName string, f func(corev1.Container) corev1.Container) error {
var container corev1.Container
Copy link
Contributor Author

@akremsa akremsa Apr 22, 2020

Choose a reason for hiding this comment

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

Just decided to make this code a bit easier. In the previous implementation, containerIndex was the last match of container names. But in general, having 2 containers with the same name is not valid, so it's ok to return the first occurrence.

Copy link
Collaborator

Choose a reason for hiding this comment

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

There is a Pod validation which prohibit having two containers in one Pod with the same name.

containerIndex := -1
for i, c := range pod.Spec.Containers {
if c.Name == containerName {
container = c
containerIndex = i
pod.Spec.Containers[i] = f(c)
return nil
}
}
if containerIndex == -1 {
return errors.Errorf("failed to find container named %q in pod spec", containerName)
}

container = f(container)
pod.Spec.Containers[containerIndex] = container

return nil
return errors.Errorf("failed to find container named %s in pod spec", containerName)
}

// Pod creates a new Pod from the PodTemplateSpec
Expand Down
Loading