Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Commit 5284477

Browse files
committed
feat(api): Replace Get and List call with informer
Replace the Get and List k8s api call with calls to a shared gameserver informer
1 parent 671c1c6 commit 5284477

4 files changed

Lines changed: 35 additions & 11 deletions

File tree

api/controllers/api/v1/game.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func ListGames(c *gin.Context) {
1616
c.Errors = append(c.Errors, errors.NewInternalServerError(err))
1717
return
1818
}
19-
c.JSON(http.StatusOK, gameServers.Items)
19+
c.JSON(http.StatusOK, gameServers)
2020
}
2121

2222
func GetGame(c *gin.Context) {

api/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ github.com/golang/groupcache v0.0.0-20160516000752-02826c3e7903/go.mod h1:cIg4er
275275
github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
276276
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
277277
github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
278+
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e h1:1r7pUrabqp18hOBcwBwiTsbnFeTZHV9eER/QT5JVZxY=
278279
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
279280
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
280281
github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
@@ -382,6 +383,7 @@ github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/b
382383
github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
383384
github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90=
384385
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
386+
github.com/hashicorp/golang-lru v0.5.1 h1:0hERBMJE1eitiLkihrMvRVBYAkpHzc/J3QdDN+dAcgU=
385387
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
386388
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
387389
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=

api/services/k8s/agones/agones.go

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@ import (
66

77
agonesv1 "agones.dev/agones/pkg/apis/agones/v1"
88
"agones.dev/agones/pkg/client/clientset/versioned"
9+
v1Informers "agones.dev/agones/pkg/client/informers/externalversions/agones/v1"
910
"go.uber.org/zap"
1011
corev1 "k8s.io/api/core/v1"
1112
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
13+
"k8s.io/apimachinery/pkg/labels"
14+
"k8s.io/apimachinery/pkg/util/wait"
1215
"k8s.io/client-go/rest"
16+
"k8s.io/client-go/tools/cache"
1317

1418
k8s "agones-minecraft/services/k8s"
1519
)
@@ -61,6 +65,7 @@ var agonesClient *AgonesClient
6165
// Agones clientset wrapper
6266
type AgonesClient struct {
6367
clientSet *versioned.Clientset
68+
informer v1Informers.GameServerInformer
6469
}
6570

6671
// Initializes Agones client
@@ -69,6 +74,8 @@ func Init() {
6974
if err != nil {
7075
zap.L().Fatal("error initializing agones client", zap.Error(err))
7176
}
77+
go c.informer.Informer().Run(wait.NeverStop)
78+
cache.WaitForCacheSync(wait.NeverStop, c.informer.Informer().HasSynced)
7279
agonesClient = c
7380
}
7481

@@ -80,27 +87,31 @@ func Client() *AgonesClient {
8087
// Creates new Agones client
8188
func New(config *rest.Config) (*AgonesClient, error) {
8289
agonesClient, err := versioned.NewForConfig(config)
90+
gameServerInformer := NewGameServerInformer(agonesClient)
8391
if err != nil {
8492
return nil, err
8593
}
8694

87-
return &AgonesClient{agonesClient}, nil
95+
return &AgonesClient{agonesClient, gameServerInformer}, nil
8896
}
8997

9098
// Gets a GameServer by name
9199
func (c *AgonesClient) Get(serverName string) (*agonesv1.GameServer, error) {
92-
return c.clientSet.
93-
AgonesV1().
94-
GameServers(metav1.NamespaceDefault).
95-
Get(context.Background(), serverName, metav1.GetOptions{})
100+
return c.informer.Lister().GameServers(metav1.NamespaceDefault).Get(serverName)
101+
102+
// return c.clientSet.
103+
// AgonesV1().
104+
// GameServers(metav1.NamespaceDefault).
105+
// Get(context.Background(), serverName, metav1.GetOptions{})
96106
}
97107

98108
// Gets all GameServers for default namespace
99-
func (c *AgonesClient) List() (*agonesv1.GameServerList, error) {
100-
return c.clientSet.
101-
AgonesV1().
102-
GameServers(metav1.NamespaceDefault).
103-
List(context.Background(), metav1.ListOptions{})
109+
func (c *AgonesClient) List() ([]*agonesv1.GameServer, error) {
110+
return c.informer.Lister().GameServers(metav1.NamespaceDefault).List(labels.Everything())
111+
// return c.clientSet.
112+
// AgonesV1().
113+
// GameServers(metav1.NamespaceDefault).
114+
// List(context.Background(), metav1.ListOptions{})
104115
}
105116

106117
// Creates a new GameServer
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package agones
2+
3+
import (
4+
"agones.dev/agones/pkg/client/clientset/versioned"
5+
"agones.dev/agones/pkg/client/informers/externalversions"
6+
v1 "agones.dev/agones/pkg/client/informers/externalversions/agones/v1"
7+
)
8+
9+
func NewGameServerInformer(clientset *versioned.Clientset) v1.GameServerInformer {
10+
return externalversions.NewSharedInformerFactory(clientset, 0).Agones().V1().GameServers()
11+
}

0 commit comments

Comments
 (0)