Skip to main content

Golang Client

If you want to create/get/update/delete those OKG resources in a Golang project or list-watch them using informer, you may need a Golang client for OKG.

In that way, you should use the kruise-game repository

Usage​

Firstly, import kruise-game into your go.mod file (the version better to be the kruise-game version you installed):

require github.com/openkruise/kruise-game v0.7.0

Using the kruise-game api requires Kubernetes version >= 1.16.

Use OKG api​

Here we use GameServerSet as an example, GameServer is used in the same way as GameServerSet

  1. New kruise-game client using your rest config:
kruisegameclientset "github.com/openkruise/kruise-game/pkg/client/clientset/versioned"

// cfg is the rest config defined in client-go, you should get it using kubeconfig or serviceaccount
kruisegameClient := kruisegameclientset.NewForConfigOrDie(cfg)
  1. Get/List Kruise-Game resources:
gamekruiseiov1alpha1 "github.com/openkruise/kruise-game/apis/v1alpha1"

gameServerSet, err := kruisegameClient.GameV1alpha1().GameServerSets(namespace).Get(context.TODO(), "GameServerSetName", metav1.GetOptions{})

// gss is a GameServerSet object
gssName := gss.GetName()

// The labelSelector is used to filter GameServers, in the example we use the name of the GameServerSet to filter out the GameServers it manages, you can also use a custom labelSelector.
labelSelector := labels.SelectorFromSet(map[string]string{
gamekruiseiov1alpha1.GameServerOwnerGssKey: gssName,
}).String()

gameServerList, err := kruisegameclientset.GameV1alpha1().GameServerSets(namespace).List(context.TODO(), metav1.ListOptions{})
  1. Create/Update kruise-game resources:
import gameKruiseV1alpha1 "github.com/openkruise/kruise-game/apis/v1alpha1"

cloneSet := &gameKruiseV1alpha1.GameServerSet{
// ...
}
gameServerSet, err = kruisegameclientset.GameV1alpha1().GameServerSet(namespace).Create(context.TODO(), cloneSet, metav1.CreateOptions{})

gameServerSet, err := kruisegameclientset.GameV1alpha1().GameServerSets(namespace).Get(context.TODO(), "GameServerSetName", metav1.GetOptions{})
if err != nil {
return err
}

// Modify object, such as replicas
gameServerSet.Spec.Replicas = pointer.Int32Ptr(3)

newGameServerSet, err := kruisegameclientset.GameV1alpha1().GameServerSets(namespace).Update(context.TODO(), gameServerSet, metav1.UpdateOptions{})
if err != nil{
return err
}
  1. Delete an existing GameServerSet:
// delete gss
err := kruisegameclientset.GameV1alpha1().GameServerSets(namespace).Delete(context.TODO(), "GameServerSetName", metav1.DeleteOptions{})
if err != nil {
return err
}
  1. Watch Kruise-Game resources:
import gameinformer "github.com/openkruise/kruise-api/client/informers/externalversions"

gameInformerFactory := gameinformer.NewSharedInformerFactory(kruisegameclientset, 0)
gameInformerFactory.Game().V1alpha1().GameServerSets().Informer().AddEventHandler(...)
gameInformerFactory.Start(...)

RBAC​

When your component is deployed inside a kubernetes cluster, you need to give the component permission to operate OKG resources

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
creationTimestamp: null
name: okg-role
rules:
- apiGroups:
- game.kruise.io
resources:
- gameserversets
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- game.kruise.io
resources:
- gameservers
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: okg-sa # Set the serviceAccount Name for your pod
namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: okg-rolebinding
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: okg-role
subjects:
- kind: ServiceAccount
name: okg-sa
namespace: kube-system

Code Example​

The following projects all use the OKG API, and developers can read the source code reference as an example: