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

add metadata to agones webhook autoscaler request #3957

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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 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 @@ -440,7 +440,7 @@ steps:
declare -A versionsAndRegions=( [1.29]=europe-west1 [1.30]=asia-east1 [1.31]=us-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
3 changes: 2 additions & 1 deletion site/content/en/docs/Guides/feature-stages.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ The current set of `alpha` and `beta` feature gates:
| [Rolling Update Fixes](https://github.com/googleforgames/agones/issues/3688) | `RollingUpdateFix` | Disabled | `Alpha` | 1.41.0 |
| [Multiple dynamic port ranges](https://github.com/googleforgames/agones/issues/1911) | `PortRanges` | Disabled | `Alpha` | 1.41.0 |
| [Port Policy None](https://github.com/googleforgames/agones/issues/3804) | `PortPolicyNone` | Disabled | `Alpha` | 1.41.0 |
| [Scheduled Fleet Autoscaling](https://github.com/googleforgames/agones/issues/3008) | `ScheduledAutoscaler` | Disabled | `Alpha` | 1.43.0 |
| [Scheduled Fleet Autoscaling](https://github.com/googleforgames/agones/issues/3951) | `ScheduledAutoscaler` | Disabled | `Alpha` | 1.43.0 |
| [Extend Webhook autoscaler to send fleet metadata with the request](https://github.com/googleforgames/agones/issues/3008) | `FleetAutoscaleRequestMetaData` | Disabled | `Alpha` | 1.47.0 |
| Example Gate (not in use) | `Example` | Disabled | None | 0.13.0 |


Expand Down
4 changes: 4 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,10 @@ type FleetAutoscaleRequest struct {
Namespace string `json:"namespace"`
// The Fleet's status values
Status v1.FleetStatus `json:"status"`
{{% feature publishVersion="1.47.0" %}}
// Standard object metadata; More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Super late to this party, but new additions will need a feature shortcode. See https://agones.dev/site/docs/contribute/#within-a-page for an example.

Also an addition here: https://github.com/googleforgames/agones/blob/main/site/content/en/docs/Guides/feature-stages.md?plain=1#L41 (also feature shortcode - may need to copy the whole table).

This is because docs get published intermediately, so we need to hold back documentation on unreleased features.

MetaData *metav1.ObjectMeta `json:"metadata,omitempty"`
{{% /feature %}}
}

type FleetAutoscaleResponse struct {
Expand Down