Skip to content

Commit

Permalink
add metadata to agones webhook autoscaler request
Browse files Browse the repository at this point in the history
  • Loading branch information
swermin committed Oct 21, 2024
1 parent a27aeab commit 9e74cc5
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 8 deletions.
2 changes: 1 addition & 1 deletion build/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ GS_TEST_IMAGE ?= us-docker.pkg.dev/agones-images/examples/simple-game-server:0.3
BETA_FEATURE_GATES ?= "AutopilotPassthroughPort=true&CountsAndLists=true&DisableResyncOnSDKServer=true&GKEAutopilotExtendedDurationPods=true"

# Enable all alpha feature gates. Keep in sync with `false` (alpha) entries in pkg/util/runtime/features.go:featureDefaults
ALPHA_FEATURE_GATES ?= "PlayerAllocationFilter=true&PlayerTracking=true&RollingUpdateFix=true&PortRanges=true&PortPolicyNone=true&ScheduledAutoscaler=true&Example=true"
ALPHA_FEATURE_GATES ?= "PlayerAllocationFilter=true&FleetAutoscaleRequestMetaData=true&PlayerTracking=true&RollingUpdateFix=true&PortRanges=true&PortPolicyNone=true&ScheduledAutoscaler=true&Example=true"

# Build with Windows support
WITH_WINDOWS=1
Expand Down
2 changes: 1 addition & 1 deletion cloudbuild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ steps:
declare -A versionsAndRegions=( [1.28]=us-west1 [1.29]=europe-west1 [1.30]=asia-east1 )
# Keep in sync with (the inverse of) pkg/util/runtime/features.go:featureDefaults
featureWithGate="PlayerAllocationFilter=true&PlayerTracking=true&CountsAndLists=false&RollingUpdateFix=true&PortRanges=true&PortPolicyNone=true&ScheduledAutoscaler=true&DisableResyncOnSDKServer=false&AutopilotPassthroughPort=false&GKEAutopilotExtendedDurationPods=false&Example=true"
featureWithGate="PlayerAllocationFilter=true&FleetAutoscaleRequestMetaData=true&PlayerTracking=true&CountsAndLists=false&RollingUpdateFix=true&PortRanges=true&PortPolicyNone=true&ScheduledAutoscaler=true&DisableResyncOnSDKServer=false&AutopilotPassthroughPort=false&GKEAutopilotExtendedDurationPods=false&Example=true"
featureWithoutGate=""
# Use this if specific feature gates can only be supported on specific Kubernetes versions.
Expand Down
1 change: 1 addition & 0 deletions install/helm/agones/defaultfeaturegates.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ CountsAndLists: true
DisableResyncOnSDKServer: true

# Alpha features
FleetAutoscaleRequestMetaData: false
PlayerAllocationFilter: false
PlayerTracking: false
RollingUpdateFix: false
Expand Down
2 changes: 2 additions & 0 deletions pkg/apis/autoscaling/v1/fleetautoscaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@ type FleetAutoscaleRequest struct {
Namespace string `json:"namespace"`
// The Fleet's status values
Status agonesv1.FleetStatus `json:"status"`
// Standard object metadata; More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata.
MetaData *metav1.ObjectMeta `json:"metadata,omitempty"`
}

// FleetAutoscaleResponse defines the response of webhook autoscaler endpoint
Expand Down
9 changes: 9 additions & 0 deletions pkg/fleetautoscalers/fleetautoscalers.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (

"github.com/pkg/errors"
"github.com/robfig/cron/v3"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/util/uuid"

Expand Down Expand Up @@ -172,6 +173,14 @@ func applyWebhookPolicy(w *autoscalingv1.WebhookPolicy, f *agonesv1.Fleet) (repl
Response: nil,
}

if runtime.FeatureEnabled(runtime.FeatureFleetAutoscaleRequestMetaData) {
faReq.Request.MetaData = &metav1.ObjectMeta{
Name: f.ObjectMeta.Name,
Labels: f.ObjectMeta.Labels,
Annotations: f.ObjectMeta.Annotations,
}
}

b, err := json.Marshal(faReq)
if err != nil {
return 0, false, err
Expand Down
45 changes: 45 additions & 0 deletions pkg/fleetautoscalers/fleetautoscalers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"io"
"net/http"
"net/http/httptest"
"strconv"
"testing"
"time"

Expand Down Expand Up @@ -92,6 +93,15 @@ func (t testServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
faResp.Replicas = faReq.Status.Replicas * scaleFactor
}

// override value for tests
if faReq.MetaData != nil {
if value, ok := faReq.MetaData.Annotations["fixedReplicas"]; ok {
faResp.Scale = true
replicas, _ := strconv.Atoi(value)
faResp.Replicas = int32(replicas)
}
}

review := &autoscalingv1.FleetAutoscaleReview{
Request: faReq,
Response: &faResp,
Expand Down Expand Up @@ -579,6 +589,41 @@ func TestApplyWebhookPolicy(t *testing.T) {
}
}

func TestApplyWebhookPolicyWithMetadata(t *testing.T) {
t.Parallel()

utilruntime.FeatureTestMutex.Lock()
defer utilruntime.FeatureTestMutex.Unlock()

err := utilruntime.ParseFeatures(string(utilruntime.FeatureFleetAutoscaleRequestMetaData) + "=true")
assert.NoError(t, err)
defer func() {
_ = utilruntime.ParseFeatures("")
}()

ts := testServer{}
server := httptest.NewServer(ts)
defer server.Close()

_, fleet := defaultWebhookFixtures()

fixedReplicas := int32(11)
fleet.ObjectMeta.Annotations = map[string]string{
"fixedReplicas": fmt.Sprintf("%d", fixedReplicas),
}

webhookPolicy := &autoscalingv1.WebhookPolicy{
Service: nil,
URL: &(server.URL),
}

replicas, limited, err := applyWebhookPolicy(webhookPolicy, fleet)

assert.Nil(t, err)
assert.False(t, limited)
assert.Equal(t, fixedReplicas, replicas)
}

func TestApplyWebhookPolicyNilFleet(t *testing.T) {
t.Parallel()

Expand Down
16 changes: 10 additions & 6 deletions pkg/util/runtime/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ const (
////////////////
// Alpha features

// FeatureFleetAutoscaleRequestMetaData is a feature flag that enables/disables fleet metadata on webhook autoscaler request.
FeatureFleetAutoscaleRequestMetaData Feature = "FleetAutoscaleRequestMetaData"

// FeaturePlayerAllocationFilter is a feature flag that enables the ability for Allocations to filter based on
// player capacity.
FeaturePlayerAllocationFilter Feature = "PlayerAllocationFilter"
Expand Down Expand Up @@ -134,12 +137,13 @@ var (
FeatureDisableResyncOnSDKServer: true,

// Alpha features
FeaturePlayerAllocationFilter: false,
FeaturePlayerTracking: false,
FeatureRollingUpdateFix: false,
FeaturePortRanges: false,
FeaturePortPolicyNone: false,
FeatureScheduledAutoscaler: false,
FeatureFleetAutoscaleRequestMetaData: false,
FeaturePlayerAllocationFilter: false,
FeaturePlayerTracking: false,
FeatureRollingUpdateFix: false,
FeaturePortRanges: false,
FeaturePortPolicyNone: false,
FeatureScheduledAutoscaler: false,

// Dev features

Expand Down
2 changes: 2 additions & 0 deletions site/content/en/docs/Reference/fleetautoscaler.md
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,8 @@ type FleetAutoscaleRequest struct {
Namespace string `json:"namespace"`
// The Fleet's status values
Status v1.FleetStatus `json:"status"`
// Standard object metadata; More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata.
MetaData *metav1.ObjectMeta `json:"metadata,omitempty"`
}

type FleetAutoscaleResponse struct {
Expand Down

0 comments on commit 9e74cc5

Please sign in to comment.