mirror of
https://github.com/Octops/quake-kube.git
synced 2026-04-13 13:40:47 +00:00
Support Agones
This commit is contained in:
@ -1,8 +1,10 @@
|
|||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
sdk "agones.dev/agones/sdks/go"
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
quakenet "github.com/criticalstack/quake-kube/internal/quake/net"
|
||||||
"log"
|
"log"
|
||||||
"net/url"
|
"net/url"
|
||||||
"time"
|
"time"
|
||||||
@ -15,8 +17,6 @@ import (
|
|||||||
quakeserver "github.com/criticalstack/quake-kube/internal/quake/server"
|
quakeserver "github.com/criticalstack/quake-kube/internal/quake/server"
|
||||||
httputil "github.com/criticalstack/quake-kube/internal/util/net/http"
|
httputil "github.com/criticalstack/quake-kube/internal/util/net/http"
|
||||||
"github.com/criticalstack/quake-kube/public"
|
"github.com/criticalstack/quake-kube/public"
|
||||||
|
|
||||||
sdk "agones.dev/agones/sdks/go"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var opts struct {
|
var opts struct {
|
||||||
@ -57,34 +57,9 @@ func NewCommand() *cobra.Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if opts.WithAgones {
|
if opts.WithAgones {
|
||||||
log.Println("starting Agones SDK client")
|
if err := startAgones(ctx, opts.ServerAddr); err != nil {
|
||||||
s, err := sdk.NewSDK()
|
log.Printf("[Agones] Error: %v", err)
|
||||||
if err != nil {
|
|
||||||
return errors.Wrap(err, "Agones SDK could not be initialized")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := s.Ready(); err != nil {
|
|
||||||
log.Println("failed to make the server Ready")
|
|
||||||
}
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
tick := time.Tick(2 * time.Second)
|
|
||||||
maxAttempts := 0
|
|
||||||
for {
|
|
||||||
if err := s.Health(); err != nil {
|
|
||||||
if maxAttempts > 5 {
|
|
||||||
log.Fatalf("Could not send health ping: %v", err)
|
|
||||||
}
|
|
||||||
maxAttempts++
|
|
||||||
}
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
log.Print("Stopped health pings")
|
|
||||||
return
|
|
||||||
case <-tick:
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
@ -126,3 +101,66 @@ func NewCommand() *cobra.Command {
|
|||||||
cmd.Flags().BoolVar(&opts.WithAgones, "with-agones", false, "use Agones SDK integration")
|
cmd.Flags().BoolVar(&opts.WithAgones, "with-agones", false, "use Agones SDK integration")
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func startAgones(ctx context.Context, addr string) error {
|
||||||
|
log.Println("[Agones] starting Agones SDK client")
|
||||||
|
s, err := sdk.NewSDK()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.Ready(); err != nil {
|
||||||
|
log.Println("[Agones] failed to make the server Ready")
|
||||||
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
tick := time.Tick(2 * time.Second)
|
||||||
|
maxAttempts := 0
|
||||||
|
for {
|
||||||
|
if err := s.Health(); err != nil {
|
||||||
|
if maxAttempts > 5 {
|
||||||
|
log.Fatalf("[Agones] could not send health ping: %v", err)
|
||||||
|
}
|
||||||
|
maxAttempts++
|
||||||
|
} else {
|
||||||
|
maxAttempts = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
log.Print("[Agones] stopped health pings")
|
||||||
|
return
|
||||||
|
case <-tick:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
tick := time.Tick(5 * time.Second)
|
||||||
|
for {
|
||||||
|
if status, err := quakenet.GetStatus(addr); err == nil {
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("[Agones] failed to get status from server: %s", err.Error())
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
for _, p := range status.Players {
|
||||||
|
log.Printf("[Agones] player: %s", p.Name)
|
||||||
|
|
||||||
|
if _, err := s.Alpha().PlayerConnect(p.Name); err != nil {
|
||||||
|
log.Printf("[Agones] failed to register player: %s", err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
log.Println("[Agones] status checked")
|
||||||
|
}
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
log.Print("[Agones] stopped status checks")
|
||||||
|
return
|
||||||
|
case <-tick:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user