-
Notifications
You must be signed in to change notification settings - Fork 858
Feat: Add new binary processor #4222
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
Merged
markmandel
merged 15 commits into
googleforgames:main
from
lacroixthomas:features/add-processor-scafolding-with-leader-election
Aug 4, 2025
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
be7edf1
Feat: Add new binary processor
lacroixthomas e5b619a
feat: fix apache year and binary name
lacroixthomas 8e64144
feat: fix lint issue
lacroixthomas 1f017d1
feat: update install and leader election
lacroixthomas 912dd31
Merge branch 'main' into features/add-processor-scafolding-with-leade…
lacroixthomas 100c1bb
feat: add featureGates in log
lacroixthomas 838ca62
feat: add lease config
lacroixthomas c234b28
feat: update helm values to embed processor in allocator
lacroixthomas 741834d
Merge branch 'main' into features/add-processor-scafolding-with-leade…
lacroixthomas 71fd8b4
Merge branch 'main' into features/add-processor-scafolding-with-leade…
lacroixthomas eb00606
Merge branch 'main' into features/add-processor-scafolding-with-leade…
lacroixthomas d77c96d
Merge branch 'main' into features/add-processor-scafolding-with-leade…
lacroixthomas a9b200f
feat: add comment about todo for next PR
lacroixthomas 013c35a
Merge branch 'main' into features/add-processor-scafolding-with-leade…
markmandel b089ea4
Merge branch 'main' into features/add-processor-scafolding-with-leade…
lacroixthomas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Copyright 2025 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. | ||
|
||
FROM gcr.io/distroless/static-debian12:nonroot | ||
|
||
ARG ARCH=amd64 | ||
WORKDIR / | ||
|
||
COPY ./bin/processor.linux.$ARCH /processor | ||
COPY ./bin/LICENSES . | ||
COPY ./bin/dependencies-src.tgz . | ||
|
||
USER nonroot:nonroot | ||
ENTRYPOINT ["/processor"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,208 @@ | ||||||||
// Copyright 2025 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. | ||||||||
|
||||||||
// Processor | ||||||||
package main | ||||||||
|
||||||||
import ( | ||||||||
"context" | ||||||||
"os" | ||||||||
"strings" | ||||||||
"time" | ||||||||
|
||||||||
"agones.dev/agones/pkg" | ||||||||
"agones.dev/agones/pkg/util/httpserver" | ||||||||
"agones.dev/agones/pkg/util/runtime" | ||||||||
"agones.dev/agones/pkg/util/signals" | ||||||||
|
||||||||
"github.com/google/uuid" | ||||||||
"github.com/heptiolabs/healthcheck" | ||||||||
"github.com/sirupsen/logrus" | ||||||||
"github.com/spf13/pflag" | ||||||||
"github.com/spf13/viper" | ||||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||||||||
"k8s.io/client-go/kubernetes" | ||||||||
"k8s.io/client-go/rest" | ||||||||
leaderelection "k8s.io/client-go/tools/leaderelection" | ||||||||
"k8s.io/client-go/tools/leaderelection/resourcelock" | ||||||||
) | ||||||||
|
||||||||
const ( | ||||||||
logLevelFlag = "log-level" | ||||||||
leaderElectionFlag = "leader-election" | ||||||||
podNamespace = "pod-namespace" | ||||||||
leaseDurationFlag = "lease-duration" | ||||||||
renewDeadlineFlag = "renew-deadline" | ||||||||
retryPeriodFlag = "retry-period" | ||||||||
) | ||||||||
|
||||||||
var ( | ||||||||
logger = runtime.NewLoggerWithSource("main") | ||||||||
) | ||||||||
|
||||||||
type processorConfig struct { | ||||||||
LogLevel string | ||||||||
LeaderElection bool | ||||||||
PodNamespace string | ||||||||
LeaseDuration time.Duration | ||||||||
RenewDeadline time.Duration | ||||||||
RetryPeriod time.Duration | ||||||||
} | ||||||||
|
||||||||
func parseEnvFlags() processorConfig { | ||||||||
viper.SetDefault(logLevelFlag, "Info") | ||||||||
viper.SetDefault(leaderElectionFlag, false) | ||||||||
viper.SetDefault(podNamespace, "") | ||||||||
viper.SetDefault(leaseDurationFlag, 15*time.Second) | ||||||||
viper.SetDefault(renewDeadlineFlag, 10*time.Second) | ||||||||
viper.SetDefault(retryPeriodFlag, 2*time.Second) | ||||||||
|
||||||||
pflag.String(logLevelFlag, viper.GetString(logLevelFlag), "Log level") | ||||||||
pflag.Bool(leaderElectionFlag, viper.GetBool(leaderElectionFlag), "Enable leader election") | ||||||||
pflag.String(podNamespace, viper.GetString(podNamespace), "Pod namespace") | ||||||||
pflag.Duration(leaseDurationFlag, viper.GetDuration(leaseDurationFlag), "Leader election lease duration") | ||||||||
pflag.Duration(renewDeadlineFlag, viper.GetDuration(renewDeadlineFlag), "Leader election renew deadline") | ||||||||
pflag.Duration(retryPeriodFlag, viper.GetDuration(retryPeriodFlag), "Leader election retry period") | ||||||||
pflag.Parse() | ||||||||
|
||||||||
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_")) | ||||||||
viper.AutomaticEnv() | ||||||||
_ = viper.BindPFlags(pflag.CommandLine) | ||||||||
|
||||||||
return processorConfig{ | ||||||||
LogLevel: viper.GetString(logLevelFlag), | ||||||||
LeaderElection: viper.GetBool(leaderElectionFlag), | ||||||||
PodNamespace: viper.GetString(podNamespace), | ||||||||
LeaseDuration: viper.GetDuration(leaseDurationFlag), | ||||||||
RenewDeadline: viper.GetDuration(renewDeadlineFlag), | ||||||||
RetryPeriod: viper.GetDuration(retryPeriodFlag), | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
func main() { | ||||||||
ctx, cancelCtx := context.WithCancel(context.Background()) | ||||||||
defer cancelCtx() | ||||||||
|
||||||||
conf := parseEnvFlags() | ||||||||
|
||||||||
logger.WithField("version", pkg.Version).WithField("processorConf", conf). | ||||||||
WithField("featureGates", runtime.EncodeFeatures()). | ||||||||
Info("Starting agones-processor") | ||||||||
|
||||||||
logger.WithField("logLevel", conf.LogLevel).Info("Setting LogLevel configuration") | ||||||||
level, err := logrus.ParseLevel(strings.ToLower(conf.LogLevel)) | ||||||||
if err == nil { | ||||||||
runtime.SetLevel(level) | ||||||||
} else { | ||||||||
logger.WithError(err).Info("Specified wrong Logging. Setting default loglevel - Info") | ||||||||
runtime.SetLevel(logrus.InfoLevel) | ||||||||
} | ||||||||
|
||||||||
healthserver := &httpserver.Server{Logger: logger} | ||||||||
health := healthcheck.NewHandler() | ||||||||
|
||||||||
config, err := rest.InClusterConfig() | ||||||||
if err != nil { | ||||||||
logger.WithError(err).Fatal("Failed to create in-cluster config") | ||||||||
panic("Failed to create in-cluster config: " + err.Error()) | ||||||||
} | ||||||||
kubeClient, err := kubernetes.NewForConfig(config) | ||||||||
if err != nil { | ||||||||
logger.WithError(err).Fatal("Failed to create Kubernetes client") | ||||||||
panic("Failed to create Kubernetes client: " + err.Error()) | ||||||||
} | ||||||||
|
||||||||
go func() { | ||||||||
healthserver.Handle("/", health) | ||||||||
_ = healthserver.Run(context.Background(), 0) | ||||||||
}() | ||||||||
|
||||||||
signals.NewSigTermHandler(func() { | ||||||||
logger.Info("Pod shutdown has been requested, failing readiness check") | ||||||||
cancelCtx() | ||||||||
os.Exit(0) | ||||||||
}) | ||||||||
|
||||||||
whenLeader(ctx, cancelCtx, logger, conf, kubeClient, func(ctx context.Context) { | ||||||||
logger.Info("Starting processor work as leader") | ||||||||
|
||||||||
// Simulate processor work (to ensure the leader is working) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (non blocking suggestion)
Suggested change
Just to make it extra clear that more work is coming. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated: a9b200f |
||||||||
// TODO: implement processor work | ||||||||
ticker := time.NewTicker(5 * time.Second) | ||||||||
lacroixthomas marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
defer ticker.Stop() | ||||||||
|
||||||||
for { | ||||||||
select { | ||||||||
case <-ctx.Done(): | ||||||||
logger.Info("Processor work completed") | ||||||||
return | ||||||||
case <-ticker.C: | ||||||||
logger.Info("Processor is active as leader") | ||||||||
} | ||||||||
} | ||||||||
}) | ||||||||
|
||||||||
logger.Info("Processor exited gracefully.") | ||||||||
} | ||||||||
|
||||||||
func whenLeader(ctx context.Context, cancel context.CancelFunc, logger *logrus.Entry, | ||||||||
conf processorConfig, kubeClient *kubernetes.Clientset, start func(_ context.Context)) { | ||||||||
if !conf.LeaderElection { | ||||||||
start(ctx) | ||||||||
return | ||||||||
} | ||||||||
|
||||||||
id := uuid.New().String() | ||||||||
|
||||||||
lock := &resourcelock.LeaseLock{ | ||||||||
LeaseMeta: metav1.ObjectMeta{ | ||||||||
Name: "agones-processor-lock", | ||||||||
Namespace: conf.PodNamespace, | ||||||||
}, | ||||||||
Client: kubeClient.CoordinationV1(), | ||||||||
LockConfig: resourcelock.ResourceLockConfig{ | ||||||||
Identity: id, | ||||||||
}, | ||||||||
} | ||||||||
|
||||||||
logger.WithField("id", id).Info("Leader Election ID") | ||||||||
|
||||||||
leaderelection.RunOrDie(ctx, leaderelection.LeaderElectionConfig{ | ||||||||
Lock: lock, | ||||||||
// IMPORTANT: you MUST ensure that any code you have that | ||||||||
// is protected by the lease must terminate **before** | ||||||||
// you call cancel. Otherwise, you could have a background | ||||||||
// loop still running and another process could | ||||||||
// get elected before your background loop finished, violating | ||||||||
// the stated goal of the lease. | ||||||||
ReleaseOnCancel: true, | ||||||||
LeaseDuration: conf.LeaseDuration, | ||||||||
RenewDeadline: conf.RenewDeadline, | ||||||||
RetryPeriod: conf.RetryPeriod, | ||||||||
Callbacks: leaderelection.LeaderCallbacks{ | ||||||||
OnStartedLeading: start, | ||||||||
OnStoppedLeading: func() { | ||||||||
logger.WithField("id", id).Info("Leader Lost") | ||||||||
cancel() | ||||||||
os.Exit(0) | ||||||||
}, | ||||||||
OnNewLeader: func(identity string) { | ||||||||
if identity == id { | ||||||||
return | ||||||||
} | ||||||||
logger.WithField("id", id).Info("New Leader Elected") | ||||||||
}, | ||||||||
}, | ||||||||
}) | ||||||||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.