Skip to content

Commit

Permalink
fixing controller issues
Browse files Browse the repository at this point in the history
  • Loading branch information
dgkanatsios committed Dec 1, 2021
1 parent b460a56 commit d3e2895
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 37 deletions.
7 changes: 4 additions & 3 deletions docs/development.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,10 @@ make deletekindcluster && make builddockerlocal && make createkindcluster && mak

## Run controller locally

export INIT_CONTAINER_TAG=...
export SIDECAR_TAG=...
cd operator && THUNDERNETES_SIDECAR_IMAGE=ghcr.io/playfab/thundernetes-sidecar-go:${SIDECAR_TAG} THUNDERNETES_INIT_CONTAINER_IMAGE=ghcr.io/playfab/thundernetes-initcontainer:${INIT_CONTAINER_TAG} go run main.go
```bash
cd operator
THUNDERNETES_SIDECAR_IMAGE=ghcr.io/playfab/thundernetes-sidecar-go:0.1.0 THUNDERNETES_INIT_CONTAINER_IMAGE=ghcr.io/playfab/thundernetes-initcontainer:0.1.0 go run main.go
```

## [ADVANCED] Install thundernetes via cloning this repository

Expand Down
2 changes: 1 addition & 1 deletion installfiles/operator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8080,7 +8080,7 @@ spec:
value: ghcr.io/playfab/thundernetes-sidecar-go:0.1.0
- name: THUNDERNETES_INIT_CONTAINER_IMAGE
value: ghcr.io/playfab/thundernetes-initcontainer:0.1.0
- name: POD_NAMESPACE
- name: TLS_SECRET_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
Expand Down
2 changes: 1 addition & 1 deletion installfiles/operator_with_monitoring.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8080,7 +8080,7 @@ spec:
value: ghcr.io/playfab/thundernetes-sidecar-go:0.1.0
- name: THUNDERNETES_INIT_CONTAINER_IMAGE
value: ghcr.io/playfab/thundernetes-initcontainer:0.1.0
- name: POD_NAMESPACE
- name: TLS_SECRET_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
Expand Down
2 changes: 1 addition & 1 deletion installfiles/operator_with_security.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8080,7 +8080,7 @@ spec:
value: ghcr.io/playfab/thundernetes-sidecar-go:0.1.0
- name: THUNDERNETES_INIT_CONTAINER_IMAGE
value: ghcr.io/playfab/thundernetes-initcontainer:0.1.0
- name: POD_NAMESPACE
- name: TLS_SECRET_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
Expand Down
2 changes: 1 addition & 1 deletion installfiles/operator_with_security_and_monitoring.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8080,7 +8080,7 @@ spec:
value: ghcr.io/playfab/thundernetes-sidecar-go:0.1.0
- name: THUNDERNETES_INIT_CONTAINER_IMAGE
value: ghcr.io/playfab/thundernetes-initcontainer:0.1.0
- name: POD_NAMESPACE
- name: TLS_SECRET_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
Expand Down
1 change: 1 addition & 0 deletions operator/api/v1alpha1/gameserverbuild_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ type GameServerBuildSpec struct {
type GameServerBuildStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file
CurrentPending int `json:"currentPending,omitempty"`
CurrentInitializing int `json:"currentInitializing"`
CurrentStandingBy int `json:"currentStandingBy"`
CurrentStandingByReadyDesired string `json:"currentStandingByReadyDesired"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6104,6 +6104,8 @@ spec:
currentActive:
type: integer
currentInitializing:
type: integer
currentPending:
description: 'INSERT ADDITIONAL STATUS FIELD - define observed state
of cluster Important: Run "make" to regenerate code after modifying
this file'
Expand Down
2 changes: 1 addition & 1 deletion operator/config/manager/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ kind: Kustomization
images:
- name: controller
newName: thundernetes-operator
newTag: ae80381
newTag: b460a56
2 changes: 1 addition & 1 deletion operator/config/manager/manager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ spec:
value: ${IMAGE_NAME_SIDECAR}:${SIDECAR_TAG}
- name: THUNDERNETES_INIT_CONTAINER_IMAGE
value: ${IMAGE_NAME_INIT_CONTAINER}:${INIT_CONTAINER_TAG}
- name: POD_NAMESPACE
- name: TLS_SECRET_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
Expand Down
2 changes: 1 addition & 1 deletion operator/controllers/gameserver_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func (r *GameServerReconciler) Reconcile(ctx context.Context, req ctrl.Request)
// if a game server is active, there are players present.
// When using the cluster autoscaler, an annotation will be added
// to prevent the node from being scaled down.
r.Recorder.Eventf(&gs, corev1.EventTypeNormal, "Update", "Gameserver %s state is %s", gs.Name, gs.Status.State)
//r.Recorder.Eventf(&gs, corev1.EventTypeNormal, "Update", "Gameserver %s state is %s", gs.Name, gs.Status.State)
podAnnotations := pod.GetAnnotations()
if podAnnotations == nil {
podAnnotations = make(map[string]string)
Expand Down
38 changes: 17 additions & 21 deletions operator/controllers/gameserverbuild_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"time"

mpsv1alpha1 "github.com/playfab/thundernetes/operator/api/v1alpha1"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -32,8 +33,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/log"

mpsv1alpha1 "github.com/playfab/thundernetes/operator/api/v1alpha1"

hm "github.com/cornelk/hashmap"
)

Expand Down Expand Up @@ -120,11 +119,13 @@ func (r *GameServerBuildReconciler) Reconcile(ctx context.Context, req ctrl.Requ
}

// calculate counts by state so we can update .status accordingly
var activeCount, standingByCount, crashesCount, initializingCount int
var activeCount, standingByCount, crashesCount, initializingCount, pendingCount int
for i := 0; i < len(gameServers.Items); i++ {
gs := gameServers.Items[i]

if gs.Status.State == "" || gs.Status.State == mpsv1alpha1.GameServerStateInitializing {
if gs.Status.State == "" {
pendingCount++
} else if gs.Status.State == mpsv1alpha1.GameServerStateInitializing {
initializingCount++
} else if gs.Status.State == mpsv1alpha1.GameServerStateStandingBy {
standingByCount++
Expand All @@ -148,20 +149,13 @@ func (r *GameServerBuildReconciler) Reconcile(ctx context.Context, req ctrl.Requ
}
}

// if at least one gameServer doesn't have a State, this means that it's initializing
// update the gameServerBuild status and exit the reconcile loop
// once this gameServer gets a State, the reconcile loop will be re-triggered again
if initializingCount > 0 {
return r.updateStatus(ctx, &gsb, initializingCount, standingByCount, activeCount, crashesCount)
}

// user has decreased standingBy numbers
if standingByCount > gsb.Spec.StandingBy {
deletedCount := 0
for i := 0; i < standingByCount-gsb.Spec.StandingBy; i++ {
gs := gameServers.Items[i]
// we're deleting only standingBy servers
if gs.Status.State == "" || gs.Status.State == mpsv1alpha1.GameServerStateStandingBy {
// we're deleting only standingBy/initializing servers
if gs.Status.State == "" || gs.Status.State == mpsv1alpha1.GameServerStateInitializing || gs.Status.State == mpsv1alpha1.GameServerStateStandingBy {
if err := r.Delete(ctx, &gs); err != nil {
return ctrl.Result{}, err
}
Expand All @@ -185,7 +179,7 @@ func (r *GameServerBuildReconciler) Reconcile(ctx context.Context, req ctrl.Requ
for i := 0; i <= standingByCount+activeCount-gsb.Spec.Max; i++ {
gs := gameServers.Items[i]
// we're deleting only standingBy or initializing servers
if gs.Status.State == "" || gs.Status.State == mpsv1alpha1.GameServerStateStandingBy {
if gs.Status.State == "" || gs.Status.State == mpsv1alpha1.GameServerStateInitializing || gs.Status.State == mpsv1alpha1.GameServerStateStandingBy {
if err := r.Delete(ctx, &gs); err != nil {
return ctrl.Result{}, err
}
Expand All @@ -202,7 +196,8 @@ func (r *GameServerBuildReconciler) Reconcile(ctx context.Context, req ctrl.Requ
}

// we are in need of standingBy servers, so we're creating them here
for i := 0; i < gsb.Spec.StandingBy-standingByCount && i+standingByCount+activeCount < gsb.Spec.Max; i++ {
for i := 0; i < gsb.Spec.StandingBy-pendingCount-initializingCount-standingByCount &&
i+pendingCount+initializingCount+standingByCount+activeCount < gsb.Spec.Max; i++ {
newgs, err := NewGameServerForGameServerBuild(&gsb, r.PortRegistry)
if err != nil {
return ctrl.Result{}, err
Expand All @@ -213,19 +208,21 @@ func (r *GameServerBuildReconciler) Reconcile(ctx context.Context, req ctrl.Requ
}
addGameServerToUnderCreationMap(gsb.Name, newgs.Name)
GameServersCreatedCounter.WithLabelValues(gsb.Name).Inc()
r.Recorder.Eventf(&gsb, corev1.EventTypeNormal, "Creating", "Creating GameServer %s", newgs.Name)
//r.Recorder.Eventf(&gsb, corev1.EventTypeNormal, "Creating", "Creating GameServer %s", newgs.Name)
}

return r.updateStatus(ctx, &gsb, initializingCount, standingByCount, activeCount, crashesCount)
return r.updateStatus(ctx, &gsb, pendingCount, initializingCount, standingByCount, activeCount, crashesCount)
}

func (r *GameServerBuildReconciler) updateStatus(ctx context.Context, gsb *mpsv1alpha1.GameServerBuild, initializingCount, standingByCount, activeCount, crashesCount int) (ctrl.Result, error) {
func (r *GameServerBuildReconciler) updateStatus(ctx context.Context, gsb *mpsv1alpha1.GameServerBuild, pendingCount, initializingCount, standingByCount, activeCount, crashesCount int) (ctrl.Result, error) {
// update GameServerBuild status only if one of the fields has changed
if gsb.Status.CurrentInitializing != initializingCount ||
if gsb.Status.CurrentPending != pendingCount ||
gsb.Status.CurrentInitializing != initializingCount ||
gsb.Status.CurrentActive != activeCount ||
gsb.Status.CurrentStandingBy != standingByCount ||
crashesCount > 0 {

gsb.Status.CurrentPending = pendingCount
gsb.Status.CurrentInitializing = initializingCount
gsb.Status.CurrentActive = activeCount
gsb.Status.CurrentStandingBy = standingByCount
Expand Down Expand Up @@ -354,9 +351,8 @@ func (r *GameServerBuildReconciler) gameServersUnderCreationWereCreated(ctx cont
// so it's safe to remove the GameServerBuild entry from the map
gameServersUnderCreation.Del(gsb.Name)
return true, nil
} else {
return false, nil
}
return false, nil
}
return true, nil
}
12 changes: 6 additions & 6 deletions operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@ func main() {
os.Exit(1)
}

namespace := os.Getenv("POD_NAMESPACE")
if namespace == "" {
setupLog.Error(err, "unable to start live API client")
os.Exit(1)
}
var crt, key []byte
apiServiceSecurity := os.Getenv("API_SERVICE_SECURITY")

if apiServiceSecurity == "usetls" {
namespace := os.Getenv("TLS_SECRET_NAMESPACE")
if namespace == "" {
setupLog.Error(err, "unable get TLS_SECRET_NAMESPACE env variable")
os.Exit(1)
}
crt, key, err = getTlsSecret(k8sClient, namespace)
if err != nil {
setupLog.Error(err, "unable to get TLS secret")
Expand Down Expand Up @@ -142,7 +142,7 @@ func main() {

err = http.NewApiServer(mgr, crt, key)
if err != nil {
setupLog.Error(err, "unable to create HTTP API Server", "API Server", "HTTP API Server")
setupLog.Error(err, "unable to create HTTP allocation API Server", "Allocation API Server", "HTTP Allocation API Server")
os.Exit(1)
}

Expand Down

0 comments on commit d3e2895

Please sign in to comment.