diff --git a/docs/development.md b/docs/development.md index 5128a6bb..820d5f93 100644 --- a/docs/development.md +++ b/docs/development.md @@ -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 diff --git a/installfiles/operator.yaml b/installfiles/operator.yaml index 06ed2926..12f3c991 100755 --- a/installfiles/operator.yaml +++ b/installfiles/operator.yaml @@ -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 diff --git a/installfiles/operator_with_monitoring.yaml b/installfiles/operator_with_monitoring.yaml index 13d7822f..d7d8369c 100644 --- a/installfiles/operator_with_monitoring.yaml +++ b/installfiles/operator_with_monitoring.yaml @@ -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 diff --git a/installfiles/operator_with_security.yaml b/installfiles/operator_with_security.yaml index 68860244..36a6bbda 100644 --- a/installfiles/operator_with_security.yaml +++ b/installfiles/operator_with_security.yaml @@ -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 diff --git a/installfiles/operator_with_security_and_monitoring.yaml b/installfiles/operator_with_security_and_monitoring.yaml index bfd5f125..e9297c70 100644 --- a/installfiles/operator_with_security_and_monitoring.yaml +++ b/installfiles/operator_with_security_and_monitoring.yaml @@ -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 diff --git a/operator/api/v1alpha1/gameserverbuild_types.go b/operator/api/v1alpha1/gameserverbuild_types.go index 82dcc8d9..c2edc557 100644 --- a/operator/api/v1alpha1/gameserverbuild_types.go +++ b/operator/api/v1alpha1/gameserverbuild_types.go @@ -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"` diff --git a/operator/config/crd/bases/mps.playfab.com_gameserverbuilds.yaml b/operator/config/crd/bases/mps.playfab.com_gameserverbuilds.yaml index f8e226aa..ec947427 100644 --- a/operator/config/crd/bases/mps.playfab.com_gameserverbuilds.yaml +++ b/operator/config/crd/bases/mps.playfab.com_gameserverbuilds.yaml @@ -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' diff --git a/operator/config/manager/kustomization.yaml b/operator/config/manager/kustomization.yaml index e75e8d61..1ad8e90f 100755 --- a/operator/config/manager/kustomization.yaml +++ b/operator/config/manager/kustomization.yaml @@ -11,4 +11,4 @@ kind: Kustomization images: - name: controller newName: thundernetes-operator - newTag: ae80381 + newTag: b460a56 diff --git a/operator/config/manager/manager.yaml b/operator/config/manager/manager.yaml index f30e4cab..8633e843 100644 --- a/operator/config/manager/manager.yaml +++ b/operator/config/manager/manager.yaml @@ -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 diff --git a/operator/controllers/gameserverbuild_controller.go b/operator/controllers/gameserverbuild_controller.go index 7dd56b6a..b70d421d 100644 --- a/operator/controllers/gameserverbuild_controller.go +++ b/operator/controllers/gameserverbuild_controller.go @@ -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" @@ -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" ) @@ -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++ @@ -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 } @@ -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 } @@ -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 @@ -216,16 +211,18 @@ func (r *GameServerBuildReconciler) Reconcile(ctx context.Context, req ctrl.Requ 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 @@ -323,9 +320,8 @@ func (r *GameServerBuildReconciler) gameServersUnderDeletionWereDeleted(ctx cont // so it's safe to remove the GameServerBuild entry from the map gameServersUnderDeletion.Del(gsb.Name) return true, nil - } else { - return false, nil } + return false, nil } return true, nil } @@ -354,9 +350,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 } diff --git a/operator/main.go b/operator/main.go index ed160b09..9476c46d 100644 --- a/operator/main.go +++ b/operator/main.go @@ -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 to 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") @@ -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) }