├── hack └── boilerplate.go.txt ├── .idea └── copyright │ ├── profiles_settings.xml │ └── AGPLv3.xml ├── charts └── singularity-operator │ ├── Chart.yaml │ └── crds │ └── singularity.innit.gg_gameserverinstances.yaml ├── .gitignore ├── pkg ├── apis │ ├── singularity │ │ ├── register.go │ │ └── v1 │ │ │ ├── groupversion.go │ │ │ ├── gameserverinstance.go │ │ │ ├── gameserverset.go │ │ │ ├── fleet.go │ │ │ ├── gameserver.go │ │ │ └── zz_generated.deepcopy.go │ ├── port.go │ └── scheduling.go ├── ingressprovider │ ├── provider.go │ └── tcpshield │ │ ├── model.go │ │ └── provider.go └── operator │ ├── gameserverset │ ├── parallelism.go │ └── controller.go │ ├── gameserverinstance │ └── controller.go │ ├── fleet │ ├── rolling.go │ └── controller.go │ └── gameserver │ └── controller.go ├── README.md ├── PROJECT ├── .github └── workflows │ └── generate-crd-java.yml ├── go.mod ├── Makefile ├── cmd └── singularity-operator │ └── main.go └── COPYING /hack/boilerplate.go.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /charts/singularity-operator/Chart.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v2 2 | name: singularity-operator 3 | description: A Helm chart for Singularity operator 4 | type: application 5 | version: 0.1.0 6 | appVersion: "0.1.0" 7 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # Binaries for programs and plugins 3 | *.exe 4 | *.exe~ 5 | *.dll 6 | *.so 7 | *.dylib 8 | bin 9 | testbin/* 10 | 11 | # Test binary, build with `go test -c` 12 | *.test 13 | 14 | # Output of the go coverage tool, specifically when used with LiteIDE 15 | *.out 16 | 17 | # Kubernetes Generated files - skip generated files, except for vendored files 18 | 19 | !vendor/**/zz_generated.* 20 | 21 | # editor and IDE paraphernalia 22 | .idea/* 23 | !.idea/copyright 24 | *.swp 25 | *.swo 26 | *~ 27 | 28 | /test -------------------------------------------------------------------------------- /.idea/copyright/AGPLv3.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | -------------------------------------------------------------------------------- /pkg/apis/singularity/register.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Singularity is an open-source game server orchestration framework 3 | * Copyright (C) 2022 Innit Incorporated 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Affero General Public License as published 7 | * by the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Affero General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Affero General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | package singularity 20 | 21 | const ( 22 | GroupName = "singularity.innit.gg" 23 | RoleLabel = GroupName + "/role" 24 | ) 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # singularity 2 | 3 | A framework for running game servers on top of Kubernetes. This project is heavily inspired 4 | by [Agones](https://github.com/googleforgames/agones). Singularity is licensed under [GNU AGPLv3](COPYING). 5 | 6 | > **Note:** this project is heavily W.I.P and is not ready for production. 7 | 8 | ## Operator 9 | 10 | Operator is the main component of **singularity**. It is responsible for reconciling CRDs within the cluster. 11 | 12 | ### CRDs 13 | 14 | * **Fleet** manages multiple game servers (technically **GameServerSets**) by using the specified GameServer template. 15 | The fleet is responsible for rolling out updates. This can be compared to a **Deployment**. 16 | * **GameServerSet** contains multiple **GameServers**. This can be compared with a **ReplicaSet**. 17 | * **GameServer** manages a single game server (technically **Pod**). 18 | A GameServer may contain multiple **GameServerInstances**. 19 | * **GameServerInstance** is owned by a **GameServer** and is the smallest "unit" within singularity. 20 | This can be used to host multiple games within the same Pod at once. 21 | -------------------------------------------------------------------------------- /pkg/apis/port.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Singularity is an open-source game server orchestration framework 3 | * Copyright (C) 2022 Innit Incorporated 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Affero General Public License as published 7 | * by the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Affero General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Affero General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | package apis 20 | 21 | const ( 22 | Internal PortPolicy = "Internal" 23 | Dynamic SchedulingStrategy = "Dynamic" 24 | ) 25 | 26 | // PortPolicy determines how Singularity should expose the game server. 27 | type PortPolicy string 28 | -------------------------------------------------------------------------------- /PROJECT: -------------------------------------------------------------------------------- 1 | domain: innit.gg 2 | layout: 3 | - go.kubebuilder.io/v3 4 | projectName: singularity 5 | repo: innit.gg/singularity 6 | resources: 7 | - api: 8 | crdVersion: v1 9 | namespaced: true 10 | controller: true 11 | domain: innit.gg 12 | group: singularity 13 | kind: Fleet 14 | path: innit.gg/singularity/pkg/apis/singularity/v1 15 | version: v1 16 | - api: 17 | crdVersion: v1 18 | namespaced: true 19 | controller: true 20 | domain: innit.gg 21 | group: singularity 22 | kind: GameServerSet 23 | path: innit.gg/singularity/pkg/apis/singularity/v1 24 | version: v1 25 | - api: 26 | crdVersion: v1 27 | namespaced: true 28 | controller: true 29 | domain: innit.gg 30 | group: singularity 31 | kind: GameServer 32 | path: innit.gg/singularity/pkg/apis/singularity/v1 33 | version: v1 34 | - api: 35 | crdVersion: v1 36 | namespaced: true 37 | controller: true 38 | domain: innit.gg 39 | group: singularity 40 | kind: GameServerInstance 41 | path: innit.gg/singularity/pkg/apis/singularity/v1 42 | version: v1 43 | version: "3" 44 | -------------------------------------------------------------------------------- /pkg/apis/scheduling.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Singularity is an open-source game server orchestration framework 3 | * Copyright (C) 2022 Innit Incorporated 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Affero General Public License as published 7 | * by the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Affero General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Affero General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | package apis 20 | 21 | const ( 22 | Packed SchedulingStrategy = "Packed" 23 | Distributed SchedulingStrategy = "Distributed" 24 | ) 25 | 26 | // SchedulingStrategy determines how Singularity should schedule Pods across the cluster. 27 | type SchedulingStrategy string 28 | -------------------------------------------------------------------------------- /.github/workflows/generate-crd-java.yml: -------------------------------------------------------------------------------- 1 | name: Generate Java CRD Model 2 | on: 3 | workflow_dispatch: 4 | 5 | env: 6 | IMAGE_NAME: ghcr.io/kubernetes-client/java/crd-model-gen 7 | IMAGE_TAG: v1.0.6 8 | GEN_DIR: crd-gen 9 | 10 | permissions: 11 | contents: read 12 | 13 | jobs: 14 | generate: 15 | runs-on: ubuntu-latest 16 | steps: 17 | - name: Checkout 18 | uses: actions/checkout@v3 19 | - name: Run CRD Model Generation 20 | run: | 21 | mkdir -p ${GEN_DIR} 22 | docker run \ 23 | --rm \ 24 | -v /var/run/docker.sock:/var/run/docker.sock \ 25 | -v "$(pwd)":"$(pwd)" \ 26 | --network host \ 27 | ${IMAGE_NAME}:${IMAGE_TAG} \ 28 | /generate.sh \ 29 | $(find "$(pwd)/charts/singularity-operator/crds" -type f | sed 's/^/-u /') 30 | -n gg.innit.singularity \ 31 | -p gg.innit.singularity.impl.k8s.client \ 32 | -o "$(pwd)/${GEN_DIR}" 33 | ls -lh ${GEN_DIR} 34 | - uses: actions/upload-artifact@v3 35 | with: 36 | name: generated-java-crd-model 37 | path: | 38 | ${{ env.GEN_DIR }} 39 | -------------------------------------------------------------------------------- /pkg/ingressprovider/provider.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Singularity is an open-source game server orchestration framework 3 | * Copyright (C) 2022 Innit Incorporated 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Affero General Public License as published 7 | * by the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Affero General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Affero General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | package ingressprovider 20 | 21 | import "net" 22 | 23 | type Provider interface { 24 | // Create creates an ingress and return the id 25 | Create(hostName string, backendSet []*Backend) (string, error) 26 | 27 | // Update updates an existing ingress' backends 28 | Update(hostName string, backendSet []*Backend) error 29 | 30 | // Delete deletes an existing ingress 31 | Delete(id string) error 32 | } 33 | 34 | // Backend represents a Minecraft server's connection details 35 | type Backend struct { 36 | IP net.IP 37 | Port uint16 38 | } 39 | -------------------------------------------------------------------------------- /pkg/apis/singularity/v1/groupversion.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Singularity is an open-source game server orchestration framework 3 | * Copyright (C) 2022 Innit Incorporated 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Affero General Public License as published 7 | * by the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Affero General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Affero General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | // Package v1 contains API Schema definitions for the singularity v1 API group 20 | //+kubebuilder:object:generate=true 21 | //+groupName=singularity.innit.gg 22 | package v1 23 | 24 | import ( 25 | "innit.gg/singularity/pkg/apis/singularity" 26 | "k8s.io/apimachinery/pkg/runtime/schema" 27 | "sigs.k8s.io/controller-runtime/pkg/scheme" 28 | ) 29 | 30 | var ( 31 | // GroupVersion is group version used to register these objects 32 | GroupVersion = schema.GroupVersion{Group: singularity.GroupName, Version: "v1"} 33 | 34 | // SchemeBuilder is used to add go types to the GroupVersionKind scheme 35 | SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion} 36 | 37 | // AddToScheme adds the types in this group-version to the given scheme. 38 | AddToScheme = SchemeBuilder.AddToScheme 39 | ) 40 | -------------------------------------------------------------------------------- /pkg/ingressprovider/tcpshield/model.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Singularity is an open-source game server orchestration framework 3 | * Copyright (C) 2022 Innit Incorporated 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Affero General Public License as published 7 | * by the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Affero General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Affero General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | package tcpshield 20 | 21 | import "time" 22 | 23 | type DomainDescriptor struct { 24 | Name string `json:"name"` 25 | BackendSetId uint32 `json:"backend_set_id,omitempty"` 26 | BAC bool `json:"bac"` 27 | } 28 | 29 | type Domain struct { 30 | Id uint32 `json:"id"` 31 | Verified bool `json:"verified"` 32 | UpdatedAt time.Time `json:"updated_at"` 33 | CreatedAt time.Time `json:"created_at"` 34 | DomainDescriptor 35 | } 36 | 37 | type DomainList []*Domain 38 | 39 | type DomainResponse struct { 40 | Data *Domain `json:"data"` 41 | } 42 | 43 | type BackendSetDescriptor struct { 44 | Name string `json:"name"` 45 | ProxyProtocol bool `json:"proxy_protocol"` 46 | Backends []string `json:"backends"` 47 | } 48 | 49 | type BackendSet struct { 50 | Id uint32 `json:"id"` 51 | CreatedAt time.Time `json:"created_at"` 52 | UpdatedAt time.Time `json:"updated_at"` 53 | DeletedAt *time.Time `json:"deleted_at,omitempty"` 54 | BackendSetDescriptor 55 | } 56 | 57 | type BackendSetList []*BackendSet 58 | 59 | type BackendSetResponse struct { 60 | Data *struct { 61 | Id uint32 `json:"id"` 62 | } `json:"data"` 63 | } 64 | -------------------------------------------------------------------------------- /charts/singularity-operator/crds/singularity.innit.gg_gameserverinstances.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | apiVersion: apiextensions.k8s.io/v1 3 | kind: CustomResourceDefinition 4 | metadata: 5 | annotations: 6 | controller-gen.kubebuilder.io/version: v0.9.0 7 | creationTimestamp: null 8 | name: gameserverinstances.singularity.innit.gg 9 | spec: 10 | group: singularity.innit.gg 11 | names: 12 | kind: GameServerInstance 13 | listKind: GameServerInstanceList 14 | plural: gameserverinstances 15 | singular: gameserverinstance 16 | scope: Namespaced 17 | versions: 18 | - additionalPrinterColumns: 19 | - jsonPath: .status.state 20 | name: Status 21 | type: string 22 | - jsonPath: .metadata.creationTimestamp 23 | name: Age 24 | type: date 25 | name: v1 26 | schema: 27 | openAPIV3Schema: 28 | description: GameServerInstance is the Schema for the GameServerInstances 29 | API 30 | properties: 31 | apiVersion: 32 | description: 'APIVersion defines the versioned schema of this representation 33 | of an object. Servers should convert recognized schemas to the latest 34 | internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources' 35 | type: string 36 | kind: 37 | description: 'Kind is a string value representing the REST resource this 38 | object represents. Servers may infer this from the endpoint the client 39 | submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds' 40 | type: string 41 | metadata: 42 | type: object 43 | spec: 44 | description: GameServerInstanceSpec defines the desired state of GameServerInstance 45 | properties: 46 | capacity: 47 | format: int32 48 | type: integer 49 | extra: 50 | type: string 51 | map: 52 | type: string 53 | required: 54 | - capacity 55 | - map 56 | type: object 57 | status: 58 | description: GameServerInstanceStatus defines the observed state of GameServerInstance 59 | properties: 60 | state: 61 | type: string 62 | required: 63 | - state 64 | type: object 65 | type: object 66 | served: true 67 | storage: true 68 | subresources: 69 | status: {} 70 | -------------------------------------------------------------------------------- /pkg/operator/gameserverset/parallelism.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Singularity is an open-source game server orchestration framework 3 | * Copyright (C) 2022 Innit Incorporated 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Affero General Public License as published 7 | * by the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Affero General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Affero General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | package gameserverset 20 | 21 | import ( 22 | singularityv1 "innit.gg/singularity/pkg/apis/singularity/v1" 23 | "sync" 24 | ) 25 | 26 | // parallelize processes a channel of game server objects, invoking the provided callback for items in the channel with the specified degree of parallelism up to a limit. 27 | // Returns nil if all callbacks returned nil or one of the error responses, not necessarily the first one. 28 | func parallelize(gameServers chan *singularityv1.GameServer, parallelism int, work func(gs *singularityv1.GameServer) error) error { 29 | errch := make(chan error, parallelism) 30 | 31 | var wg sync.WaitGroup 32 | 33 | for i := 0; i < parallelism; i++ { 34 | wg.Add(1) 35 | 36 | go func() { 37 | defer wg.Done() 38 | for it := range gameServers { 39 | err := work(it) 40 | if err != nil { 41 | errch <- err 42 | break 43 | } 44 | } 45 | }() 46 | } 47 | wg.Wait() 48 | close(errch) 49 | 50 | for range gameServers { 51 | // drain any remaining game servers in the channel, in case we did not consume them all 52 | } 53 | 54 | // return first error from the channel, or nil if all successful. 55 | return <-errch 56 | } 57 | 58 | // newGameServersChannel returns a channel producing n amount of GameServers 59 | func newGameServersChannel(n int, gsSet *singularityv1.GameServerSet) chan *singularityv1.GameServer { 60 | gameServers := make(chan *singularityv1.GameServer) 61 | go func() { 62 | defer close(gameServers) 63 | 64 | for i := 0; i < n; i++ { 65 | gameServers <- gsSet.GameServer() 66 | } 67 | }() 68 | 69 | return gameServers 70 | } 71 | 72 | // gameServerListToChannel returns a channel of GameServers from list 73 | func gameServerListToChannel(list []*singularityv1.GameServer) chan *singularityv1.GameServer { 74 | gameServers := make(chan *singularityv1.GameServer) 75 | go func() { 76 | defer close(gameServers) 77 | 78 | for _, gs := range list { 79 | gameServers <- gs 80 | } 81 | }() 82 | 83 | return gameServers 84 | } 85 | -------------------------------------------------------------------------------- /pkg/operator/gameserverinstance/controller.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Singularity is an open-source game server orchestration framework 3 | * Copyright (C) 2022 Innit Incorporated 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Affero General Public License as published 7 | * by the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Affero General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Affero General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | package gameserverinstance 20 | 21 | import ( 22 | "context" 23 | "github.com/go-logr/logr" 24 | singularityv1 "innit.gg/singularity/pkg/apis/singularity/v1" 25 | "k8s.io/client-go/tools/record" 26 | ctrl "sigs.k8s.io/controller-runtime" 27 | "sigs.k8s.io/controller-runtime/pkg/client" 28 | "sigs.k8s.io/controller-runtime/pkg/reconcile" 29 | ) 30 | 31 | // Reconciler reconciles a GameServerInstance object 32 | type Reconciler struct { 33 | client.Client 34 | Recorder record.EventRecorder 35 | Log logr.Logger 36 | } 37 | 38 | //+kubebuilder:rbac:groups=singularity.innit.gg,resources=GameServerInstances,verbs=get;list;watch;create;update;patch;delete 39 | //+kubebuilder:rbac:groups=singularity.innit.gg,resources=GameServerInstances/status,verbs=get;update;patch 40 | //+kubebuilder:rbac:groups=singularity.innit.gg,resources=GameServerInstances/finalizers,verbs=update 41 | 42 | // Reconcile is part of the main kubernetes reconciliation loop which aims to 43 | // move the current state of the cluster closer to the desired state. 44 | // TODO(user): Modify the Reconcile function to compare the state specified by 45 | // the GameServerInstance object against the actual cluster state, and then 46 | // perform operations to make the cluster state reflect the state specified by 47 | // the user. 48 | // 49 | // For more details, check Reconcile and its Result here: 50 | // - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.12.1/pkg/reconcile 51 | func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { 52 | // TODO(user): your logic here 53 | 54 | return ctrl.Result{}, nil 55 | } 56 | 57 | // SetupWithManager sets up the controller with the Manager. 58 | func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error { 59 | return ctrl.NewControllerManagedBy(mgr). 60 | For(&singularityv1.GameServerInstance{}). 61 | WithLogConstructor(func(req *reconcile.Request) logr.Logger { 62 | if req != nil { 63 | return r.Log.WithValues("req", req) 64 | } 65 | return r.Log 66 | }). 67 | Complete(r) 68 | } 69 | -------------------------------------------------------------------------------- /pkg/apis/singularity/v1/gameserverinstance.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Singularity is an open-source game server orchestration framework 3 | * Copyright (C) 2022 Innit Incorporated 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Affero General Public License as published 7 | * by the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Affero General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Affero General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | package v1 20 | 21 | import ( 22 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 23 | ) 24 | 25 | const ( 26 | // GameServerInstanceStateStarting indicates that the GameServer is starting 27 | GameServerInstanceStateStarting GameServerInstanceState = "Starting" 28 | // GameServerInstanceStateReady indicates that the GameServerInstance is ready to accept players 29 | GameServerInstanceStateReady GameServerInstanceState = "Ready" 30 | // GameServerInstanceStateAllocated indicates that the GameServerInstance is currently running a game 31 | GameServerInstanceStateAllocated GameServerInstanceState = "Allocated" 32 | ) 33 | 34 | //+kubebuilder:object:root=true 35 | //+kubebuilder:subresource:status 36 | //+kubebuilder:printcolumn:name="Status",type=string,JSONPath=`.status.state` 37 | //+kubebuilder:printcolumn:name="Age",type=date,JSONPath=`.metadata.creationTimestamp` 38 | 39 | // GameServerInstance is the Schema for the GameServerInstances API 40 | type GameServerInstance struct { 41 | metav1.TypeMeta `json:",inline"` 42 | metav1.ObjectMeta `json:"metadata,omitempty"` 43 | 44 | Spec GameServerInstanceSpec `json:"spec,omitempty"` 45 | Status GameServerInstanceStatus `json:"status,omitempty"` 46 | } 47 | 48 | // GameServerInstanceTemplate is the template for the GameServerInstances API 49 | type GameServerInstanceTemplate struct { 50 | metav1.ObjectMeta `json:"metadata,omitempty"` 51 | 52 | Spec GameServerInstanceSpec `json:"spec,omitempty"` 53 | } 54 | 55 | //+kubebuilder:object:root=true 56 | 57 | // GameServerInstanceList contains a list of GameServerInstance 58 | type GameServerInstanceList struct { 59 | metav1.TypeMeta `json:",inline"` 60 | metav1.ListMeta `json:"metadata,omitempty"` 61 | Items []GameServerInstance `json:"items"` 62 | } 63 | 64 | // GameServerInstanceSpec defines the desired state of GameServerInstance 65 | type GameServerInstanceSpec struct { 66 | Capacity uint32 `json:"capacity"` 67 | Map string `json:"map"` 68 | Extra string `json:"extra,omitempty"` 69 | } 70 | 71 | type GameServerInstanceState string 72 | 73 | // GameServerInstanceStatus defines the observed state of GameServerInstance 74 | type GameServerInstanceStatus struct { 75 | State GameServerInstanceState `json:"state"` 76 | } 77 | 78 | func init() { 79 | SchemeBuilder.Register(&GameServerInstance{}, &GameServerInstanceList{}) 80 | } 81 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module innit.gg/singularity 2 | 3 | go 1.18 4 | 5 | require ( 6 | github.com/go-logr/logr v1.2.0 7 | github.com/gofiber/fiber/v2 v2.36.0 8 | github.com/pkg/errors v0.9.1 9 | k8s.io/api v0.24.0 10 | k8s.io/apimachinery v0.24.0 11 | k8s.io/client-go v0.24.0 12 | k8s.io/utils v0.0.0-20220210201930-3a6ce19ff2f9 13 | sigs.k8s.io/controller-runtime v0.12.1 14 | ) 15 | 16 | require ( 17 | cloud.google.com/go v0.81.0 // indirect 18 | github.com/Azure/go-autorest v14.2.0+incompatible // indirect 19 | github.com/Azure/go-autorest/autorest v0.11.18 // indirect 20 | github.com/Azure/go-autorest/autorest/adal v0.9.13 // indirect 21 | github.com/Azure/go-autorest/autorest/date v0.3.0 // indirect 22 | github.com/Azure/go-autorest/logger v0.2.1 // indirect 23 | github.com/Azure/go-autorest/tracing v0.6.0 // indirect 24 | github.com/PuerkitoBio/purell v1.1.1 // indirect 25 | github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect 26 | github.com/andybalholm/brotli v1.0.4 // indirect 27 | github.com/beorn7/perks v1.0.1 // indirect 28 | github.com/cespare/xxhash/v2 v2.1.2 // indirect 29 | github.com/davecgh/go-spew v1.1.1 // indirect 30 | github.com/emicklei/go-restful v2.9.5+incompatible // indirect 31 | github.com/evanphx/json-patch v4.12.0+incompatible // indirect 32 | github.com/form3tech-oss/jwt-go v3.2.3+incompatible // indirect 33 | github.com/fsnotify/fsnotify v1.5.1 // indirect 34 | github.com/go-logr/zapr v1.2.0 // indirect 35 | github.com/go-openapi/jsonpointer v0.19.5 // indirect 36 | github.com/go-openapi/jsonreference v0.19.5 // indirect 37 | github.com/go-openapi/swag v0.19.14 // indirect 38 | github.com/gogo/protobuf v1.3.2 // indirect 39 | github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect 40 | github.com/golang/protobuf v1.5.2 // indirect 41 | github.com/google/gnostic v0.5.7-v3refs // indirect 42 | github.com/google/go-cmp v0.5.5 // indirect 43 | github.com/google/gofuzz v1.1.0 // indirect 44 | github.com/google/uuid v1.1.2 // indirect 45 | github.com/imdario/mergo v0.3.12 // indirect 46 | github.com/josharian/intern v1.0.0 // indirect 47 | github.com/json-iterator/go v1.1.12 // indirect 48 | github.com/klauspost/compress v1.15.0 // indirect 49 | github.com/mailru/easyjson v0.7.6 // indirect 50 | github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect 51 | github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect 52 | github.com/modern-go/reflect2 v1.0.2 // indirect 53 | github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect 54 | github.com/prometheus/client_golang v1.12.1 // indirect 55 | github.com/prometheus/client_model v0.2.0 // indirect 56 | github.com/prometheus/common v0.32.1 // indirect 57 | github.com/prometheus/procfs v0.7.3 // indirect 58 | github.com/spf13/pflag v1.0.5 // indirect 59 | github.com/valyala/bytebufferpool v1.0.0 // indirect 60 | github.com/valyala/fasthttp v1.38.0 // indirect 61 | github.com/valyala/tcplisten v1.0.0 // indirect 62 | go.uber.org/atomic v1.7.0 // indirect 63 | go.uber.org/multierr v1.6.0 // indirect 64 | go.uber.org/zap v1.19.1 // indirect 65 | golang.org/x/crypto v0.0.0-20220214200702-86341886e292 // indirect 66 | golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect 67 | golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect 68 | golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9 // indirect 69 | golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect 70 | golang.org/x/text v0.3.7 // indirect 71 | golang.org/x/time v0.0.0-20220210224613-90d013bbcef8 // indirect 72 | gomodules.xyz/jsonpatch/v2 v2.2.0 // indirect 73 | google.golang.org/appengine v1.6.7 // indirect 74 | google.golang.org/protobuf v1.27.1 // indirect 75 | gopkg.in/inf.v0 v0.9.1 // indirect 76 | gopkg.in/yaml.v2 v2.4.0 // indirect 77 | gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect 78 | k8s.io/apiextensions-apiserver v0.24.0 // indirect 79 | k8s.io/component-base v0.24.0 // indirect 80 | k8s.io/klog/v2 v2.60.1 // indirect 81 | k8s.io/kube-openapi v0.0.0-20220328201542-3ee0da9b0b42 // indirect 82 | sigs.k8s.io/json v0.0.0-20211208200746-9f7c6b3444d2 // indirect 83 | sigs.k8s.io/structured-merge-diff/v4 v4.2.1 // indirect 84 | sigs.k8s.io/yaml v1.3.0 // indirect 85 | ) 86 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # Image URL to use all building/pushing image targets 2 | IMG ?= singularity-operator:latest 3 | # ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary. 4 | ENVTEST_K8S_VERSION = 1.24.1 5 | 6 | # Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set) 7 | ifeq (,$(shell go env GOBIN)) 8 | GOBIN=$(shell go env GOPATH)/bin 9 | else 10 | GOBIN=$(shell go env GOBIN) 11 | endif 12 | 13 | # Setting SHELL to bash allows bash commands to be executed by recipes. 14 | # This is a requirement for 'setup-envtest.sh' in the test target. 15 | # Options are set to exit when a recipe line exits non-zero or a piped command fails. 16 | SHELL = /usr/bin/env bash -o pipefail 17 | .SHELLFLAGS = -ec 18 | 19 | .PHONY: all 20 | all: build 21 | 22 | ##@ General 23 | 24 | # The help target prints out all targets with their descriptions organized 25 | # beneath their categories. The categories are represented by '##@' and the 26 | # target descriptions by '##'. The awk commands is responsible for reading the 27 | # entire set of makefiles included in this invocation, looking for lines of the 28 | # file as xyz: ## something, and then pretty-format the target and help. Then, 29 | # if there's a line with ##@ something, that gets pretty-printed as a category. 30 | # More info on the usage of ANSI control characters for terminal formatting: 31 | # https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters 32 | # More info on the awk command: 33 | # http://linuxcommand.org/lc3_adv_awk.php 34 | 35 | .PHONY: help 36 | help: ## Display this help. 37 | @awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST) 38 | 39 | ##@ Development 40 | 41 | .PHONY: manifests 42 | manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects. 43 | $(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=charts/singularity-operator/crds 44 | 45 | .PHONY: generate 46 | generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations. 47 | $(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..." 48 | 49 | .PHONY: fmt 50 | fmt: ## Run go fmt against code. 51 | go fmt ./... 52 | 53 | .PHONY: vet 54 | vet: ## Run go vet against code. 55 | go vet ./... 56 | 57 | .PHONY: test 58 | test: manifests generate fmt vet envtest ## Run tests. 59 | KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./... -coverprofile cover.out 60 | 61 | ##@ Build 62 | 63 | .PHONY: build 64 | build: generate fmt vet ## Build manager binary. 65 | go build -o bin/manager main.go 66 | 67 | .PHONY: run 68 | run: manifests generate fmt vet ## Run a controller from your host. 69 | go run ./main.go 70 | 71 | .PHONY: docker-build 72 | docker-build: test ## Build docker image with the manager. 73 | docker build -t ${IMG} . 74 | 75 | .PHONY: docker-push 76 | docker-push: ## Push docker image with the manager. 77 | docker push ${IMG} 78 | 79 | ##@ Build Dependencies 80 | 81 | ## Location to install dependencies to 82 | LOCALBIN ?= $(shell pwd)/bin 83 | $(LOCALBIN): 84 | mkdir -p $(LOCALBIN) 85 | 86 | ## Tool Binaries 87 | KUSTOMIZE ?= $(LOCALBIN)/kustomize 88 | CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen 89 | ENVTEST ?= $(LOCALBIN)/setup-envtest 90 | 91 | ## Tool Versions 92 | KUSTOMIZE_VERSION ?= v3.8.7 93 | CONTROLLER_TOOLS_VERSION ?= v0.9.0 94 | 95 | KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh" 96 | .PHONY: kustomize 97 | kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary. 98 | $(KUSTOMIZE): $(LOCALBIN) 99 | curl -s $(KUSTOMIZE_INSTALL_SCRIPT) | bash -s -- $(subst v,,$(KUSTOMIZE_VERSION)) $(LOCALBIN) 100 | 101 | .PHONY: controller-gen 102 | controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary. 103 | $(CONTROLLER_GEN): $(LOCALBIN) 104 | GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION) 105 | 106 | .PHONY: envtest 107 | envtest: $(ENVTEST) ## Download envtest-setup locally if necessary. 108 | $(ENVTEST): $(LOCALBIN) 109 | GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest 110 | -------------------------------------------------------------------------------- /pkg/apis/singularity/v1/gameserverset.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Singularity is an open-source game server orchestration framework 3 | * Copyright (C) 2022 Innit Incorporated 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Affero General Public License as published 7 | * by the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Affero General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Affero General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | package v1 20 | 21 | import ( 22 | "context" 23 | "innit.gg/singularity/pkg/apis" 24 | "innit.gg/singularity/pkg/apis/singularity" 25 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 26 | "sigs.k8s.io/controller-runtime/pkg/client" 27 | ) 28 | 29 | const ( 30 | // GameServerSetNameLabel is the name of GameServerSet which owns resources like GameServer 31 | GameServerSetNameLabel = singularity.GroupName + "/gameserverset" 32 | ) 33 | 34 | //+kubebuilder:object:root=true 35 | //+kubebuilder:subresource:status 36 | //+kubebuilder:printcolumn:name="Scheduling",type=string,JSONPath=`.spec.scheduling` 37 | //+kubebuilder:printcolumn:name="Desired",type=integer,JSONPath=`.spec.replicas` 38 | //+kubebuilder:printcolumn:name="Current",type=integer,JSONPath=`.status.replicas` 39 | //+kubebuilder:printcolumn:name="Ready",type=integer,JSONPath=`.status.readyReplicas` 40 | //+kubebuilder:printcolumn:name="Allocated",type=integer,JSONPath=`.status.allocatedReplicas` 41 | //+kubebuilder:printcolumn:name="Age",type=date,JSONPath=`.metadata.creationTimestamp` 42 | 43 | // GameServerSet is the Schema for the GameServerSets API 44 | type GameServerSet struct { 45 | metav1.TypeMeta `json:",inline"` 46 | metav1.ObjectMeta `json:"metadata,omitempty"` 47 | 48 | Spec GameServerSetSpec `json:"spec,omitempty"` 49 | Status GameServerSetStatus `json:"status,omitempty"` 50 | } 51 | 52 | // GameServerSetTemplate is the template for the GameServerSets API 53 | type GameServerSetTemplate struct { 54 | metav1.ObjectMeta `json:"metadata,omitempty"` 55 | 56 | Spec GameServerSetSpec `json:"spec,omitempty"` 57 | } 58 | 59 | //+kubebuilder:object:root=true 60 | 61 | // GameServerSetList contains a list of GameServerSet 62 | type GameServerSetList struct { 63 | metav1.TypeMeta `json:",inline"` 64 | metav1.ListMeta `json:"metadata,omitempty"` 65 | Items []GameServerSet `json:"items"` 66 | } 67 | 68 | // GameServerSetSpec defines the desired state of GameServerSet 69 | type GameServerSetSpec struct { 70 | Replicas int32 `json:"replicas"` 71 | Scheduling apis.SchedulingStrategy `json:"scheduling"` 72 | Template GameServerTemplate `json:"template"` 73 | } 74 | 75 | // GameServerSetStatus defines the observed state of GameServerSet 76 | type GameServerSetStatus struct { 77 | Replicas int32 `json:"replicas"` 78 | ReadyReplicas int32 `json:"readyReplicas"` 79 | AllocatedReplicas int32 `json:"allocatedReplicas"` 80 | ShutdownReplicas int32 `json:"shutdownReplicas"` 81 | Instances int32 `json:"instances"` 82 | ReadyInstances int32 `json:"readyInstances"` 83 | AllocatedInstances int32 `json:"allocatedInstances"` 84 | ShutdownInstances int32 `json:"shutdownInstances"` 85 | } 86 | 87 | // GameServer returns a single GameServer for this GameServerSet specification 88 | func (gsSet *GameServerSet) GameServer() *GameServer { 89 | gs := &GameServer{ 90 | ObjectMeta: *gsSet.Spec.Template.ObjectMeta.DeepCopy(), 91 | Spec: *gsSet.Spec.Template.Spec.DeepCopy(), 92 | } 93 | 94 | gs.Spec.Scheduling = gsSet.Spec.Scheduling 95 | 96 | // Generate a unique name for GameServerSet, ensuring there are no collisions. 97 | // Also, reset the ObjectMeta. 98 | gs.ObjectMeta.GenerateName = gsSet.ObjectMeta.Name + "-" 99 | gs.ObjectMeta.Name = "" 100 | gs.ObjectMeta.Namespace = gsSet.ObjectMeta.Namespace 101 | gs.ObjectMeta.ResourceVersion = "" 102 | gs.ObjectMeta.UID = "" 103 | 104 | ref := metav1.NewControllerRef(gsSet, GroupVersion.WithKind("GameServerSet")) 105 | gs.ObjectMeta.OwnerReferences = append(gs.ObjectMeta.OwnerReferences, *ref) 106 | 107 | // Append Fleet name and GameServerSet name labels 108 | if gs.ObjectMeta.Labels == nil { 109 | gs.ObjectMeta.Labels = make(map[string]string, 2) 110 | } 111 | 112 | gs.ObjectMeta.Labels[FleetNameLabel] = gsSet.ObjectMeta.Labels[FleetNameLabel] 113 | gs.ObjectMeta.Labels[GameServerSetNameLabel] = gsSet.ObjectMeta.Name 114 | return gs 115 | } 116 | 117 | // ListGameServer lists all owned GameServer 118 | func (gsSet *GameServerSet) ListGameServer(ctx context.Context, c client.Client) ([]*GameServer, error) { 119 | list := &GameServerList{} 120 | labelSelector := client.MatchingLabels{ 121 | GameServerSetNameLabel: gsSet.ObjectMeta.Name, 122 | } 123 | if err := c.List(ctx, list, labelSelector); err != nil { 124 | return []*GameServer{}, err 125 | } 126 | 127 | // Make sure that the Fleet actually owns it 128 | var result []*GameServer 129 | for i := range list.Items { 130 | gs := &list.Items[i] 131 | if metav1.IsControlledBy(gs, gsSet) { 132 | result = append(result, gs) 133 | } 134 | } 135 | 136 | return result, nil 137 | } 138 | 139 | func init() { 140 | SchemeBuilder.Register(&GameServerSet{}, &GameServerSetList{}) 141 | } 142 | -------------------------------------------------------------------------------- /cmd/singularity-operator/main.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Singularity is an open-source game server orchestration framework 3 | * Copyright (C) 2022 Innit Incorporated 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Affero General Public License as published 7 | * by the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Affero General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Affero General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | package main 20 | 21 | import ( 22 | "flag" 23 | singularityv1 "innit.gg/singularity/pkg/apis/singularity/v1" 24 | "innit.gg/singularity/pkg/operator/fleet" 25 | "innit.gg/singularity/pkg/operator/gameserver" 26 | "innit.gg/singularity/pkg/operator/gameserverinstance" 27 | "innit.gg/singularity/pkg/operator/gameserverset" 28 | "os" 29 | 30 | // Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.) 31 | // to ensure that exec-entrypoint and run can make use of them. 32 | _ "k8s.io/client-go/plugin/pkg/client/auth" 33 | 34 | "k8s.io/apimachinery/pkg/runtime" 35 | utilruntime "k8s.io/apimachinery/pkg/util/runtime" 36 | clientgoscheme "k8s.io/client-go/kubernetes/scheme" 37 | ctrl "sigs.k8s.io/controller-runtime" 38 | "sigs.k8s.io/controller-runtime/pkg/healthz" 39 | "sigs.k8s.io/controller-runtime/pkg/log/zap" 40 | //+kubebuilder:scaffold:imports 41 | ) 42 | 43 | var ( 44 | scheme = runtime.NewScheme() 45 | setupLog = ctrl.Log.WithName("setup") 46 | ) 47 | 48 | func init() { 49 | utilruntime.Must(clientgoscheme.AddToScheme(scheme)) 50 | 51 | utilruntime.Must(singularityv1.AddToScheme(scheme)) 52 | //+kubebuilder:scaffold:scheme 53 | } 54 | 55 | func main() { 56 | var metricsAddr string 57 | var enableLeaderElection bool 58 | var probeAddr string 59 | flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.") 60 | flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.") 61 | flag.BoolVar(&enableLeaderElection, "leader-elect", false, 62 | "Enable leader election for controller manager. "+ 63 | "Enabling this will ensure there is only one active controller manager.") 64 | opts := zap.Options{ 65 | Development: true, 66 | } 67 | opts.BindFlags(flag.CommandLine) 68 | flag.Parse() 69 | 70 | ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts))) 71 | 72 | mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{ 73 | Scheme: scheme, 74 | MetricsBindAddress: metricsAddr, 75 | Port: 9443, 76 | HealthProbeBindAddress: probeAddr, 77 | LeaderElection: enableLeaderElection, 78 | LeaderElectionID: "9090e9ab.innit.gg", 79 | // LeaderElectionReleaseOnCancel defines if the leader should step down voluntarily 80 | // when the Manager ends. This requires the binary to immediately end when the 81 | // Manager is stopped, otherwise, this setting is unsafe. Setting this significantly 82 | // speeds up voluntary leader transitions as the new leader don't have to wait 83 | // LeaseDuration time first. 84 | // 85 | // In the default scaffold provided, the program ends immediately after 86 | // the manager stops, so would be fine to enable this option. However, 87 | // if you are doing or is intended to do any operation such as perform cleanups 88 | // after the manager stops then its usage might be unsafe. 89 | // LeaderElectionReleaseOnCancel: true, 90 | }) 91 | if err != nil { 92 | setupLog.Error(err, "unable to start manager") 93 | os.Exit(1) 94 | } 95 | 96 | if err = (&fleet.Reconciler{ 97 | Client: mgr.GetClient(), 98 | Recorder: mgr.GetEventRecorderFor("fleet-controller"), 99 | Log: ctrl.Log.WithName("controllers").WithValues("controller", "Fleet"), 100 | }).SetupWithManager(mgr); err != nil { 101 | setupLog.Error(err, "unable to create controller", "controller", "Fleet") 102 | os.Exit(1) 103 | } 104 | 105 | if err = (&gameserverset.Reconciler{ 106 | Client: mgr.GetClient(), 107 | Recorder: mgr.GetEventRecorderFor("gameserverset-controller"), 108 | Log: ctrl.Log.WithName("controllers").WithValues("controller", "GameServerSet"), 109 | }).SetupWithManager(mgr); err != nil { 110 | setupLog.Error(err, "unable to create controller", "controller", "GameServerSet") 111 | os.Exit(1) 112 | } 113 | 114 | if err = (&gameserver.Reconciler{ 115 | Client: mgr.GetClient(), 116 | Recorder: mgr.GetEventRecorderFor("gameserver-controller"), 117 | Log: ctrl.Log.WithName("controllers").WithValues("controller", "GameServer"), 118 | }).SetupWithManager(mgr); err != nil { 119 | setupLog.Error(err, "unable to create controller", "controller", "GameServer") 120 | os.Exit(1) 121 | } 122 | 123 | if err = (&gameserverinstance.Reconciler{ 124 | Client: mgr.GetClient(), 125 | Recorder: mgr.GetEventRecorderFor("gameserverinstance-controller"), 126 | Log: ctrl.Log.WithName("controllers").WithValues("controller", "GameServerInstance"), 127 | }).SetupWithManager(mgr); err != nil { 128 | setupLog.Error(err, "unable to create controller", "controller", "GameServerInstance") 129 | os.Exit(1) 130 | } 131 | 132 | //+kubebuilder:scaffold:builder 133 | 134 | if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil { 135 | setupLog.Error(err, "unable to set up health check") 136 | os.Exit(1) 137 | } 138 | if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil { 139 | setupLog.Error(err, "unable to set up ready check") 140 | os.Exit(1) 141 | } 142 | 143 | setupLog.Info("starting manager") 144 | if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil { 145 | setupLog.Error(err, "problem running manager") 146 | os.Exit(1) 147 | } 148 | } 149 | -------------------------------------------------------------------------------- /pkg/ingressprovider/tcpshield/provider.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Singularity is an open-source game server orchestration framework 3 | * Copyright (C) 2022 Innit Incorporated 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Affero General Public License as published 7 | * by the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Affero General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Affero General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | package tcpshield 20 | 21 | import ( 22 | "fmt" 23 | "github.com/gofiber/fiber/v2" 24 | "github.com/pkg/errors" 25 | "innit.gg/singularity/pkg/ingressprovider" 26 | "strconv" 27 | ) 28 | 29 | const ( 30 | Endpoint = "https://api.tcpshield.com" 31 | ResourcePrefix = "singularity-" 32 | ) 33 | 34 | var ( 35 | ErrorDomainNotFound = errors.New("domain not found") 36 | ) 37 | 38 | type provider struct { 39 | apiKey string 40 | networkId uint32 41 | client *fiber.Client 42 | } 43 | 44 | func (p *provider) Create(hostName string, backendSet []*ingressprovider.Backend) (string, error) { 45 | backendSetId, err := p.updateBackendSet(hostName, backendSet) 46 | if err != nil { 47 | return "", err 48 | } 49 | 50 | descriptor := &DomainDescriptor{ 51 | Name: hostName, 52 | BackendSetId: backendSetId, 53 | BAC: false, 54 | } 55 | res := &DomainResponse{} 56 | code, _, errs := p.client.Post(fmt.Sprintf("%s/networks/%d/domains", Endpoint, p.networkId)). 57 | Add("X-API-Key", p.apiKey). 58 | JSON(descriptor). 59 | Struct(res) 60 | 61 | if len(errs) != 0 { 62 | return "", errs[0] 63 | } 64 | 65 | if code != 200 { 66 | return "", errors.Errorf("unexpected status code: %d", code) 67 | } 68 | 69 | if res.Data == nil { 70 | return "", errors.New("error creating domain") 71 | } 72 | 73 | return strconv.Itoa(int(res.Data.Id)), nil 74 | } 75 | 76 | func (p *provider) Update(hostName string, backendSet []*ingressprovider.Backend) error { 77 | var list DomainList 78 | code, _, errs := p.client.Get(fmt.Sprintf("%s/networks/%d/domains", Endpoint, p.networkId)). 79 | Add("X-API-Key", p.apiKey). 80 | Struct(&list) 81 | 82 | if len(errs) != 0 { 83 | return errs[0] 84 | } 85 | 86 | if code != 200 { 87 | return errors.Errorf("unexpected status code: %d", code) 88 | } 89 | 90 | var id uint32 91 | for _, domain := range list { 92 | if domain.Name == hostName { 93 | id = domain.Id 94 | break 95 | } 96 | } 97 | 98 | if id == 0 { 99 | return ErrorDomainNotFound 100 | } 101 | 102 | backendSetId, err := p.updateBackendSet(hostName, backendSet) 103 | if err != nil { 104 | return err 105 | } 106 | 107 | descriptor := &DomainDescriptor{ 108 | Name: hostName, 109 | BackendSetId: backendSetId, 110 | BAC: false, 111 | } 112 | 113 | code, _, errs = p.client.Patch(fmt.Sprintf("%s/networks/%d/domains/%d", Endpoint, p.networkId, id)). 114 | Add("X-API-Key", p.apiKey). 115 | JSON(descriptor). 116 | Bytes() 117 | 118 | if len(errs) != 0 { 119 | return errs[0] 120 | } 121 | 122 | if code != 200 { 123 | return errors.Errorf("unexpected status code: %d", code) 124 | } 125 | 126 | return nil 127 | } 128 | 129 | func (p *provider) Delete(id string) error { 130 | // TODO: Delete backendset 131 | code, _, errs := p.client.Delete(fmt.Sprintf("%s/networks/%d/domains/%s", Endpoint, p.networkId, id)). 132 | Add("X-API-Key", p.apiKey). 133 | Bytes() 134 | 135 | if len(errs) != 0 { 136 | return errs[0] 137 | } 138 | 139 | if code != 200 { 140 | return errors.Errorf("unexpected status code: %d", code) 141 | } 142 | 143 | return nil 144 | } 145 | 146 | func CreateProvider(apiKey string, networkId uint32) ingressprovider.Provider { 147 | return &provider{ 148 | apiKey: apiKey, 149 | networkId: networkId, 150 | client: fiber.AcquireClient(), 151 | } 152 | } 153 | 154 | func (p *provider) updateBackendSet(hostName string, backendSet []*ingressprovider.Backend) (uint32, error) { 155 | var list BackendSetList 156 | code, _, errs := p.client.Get(fmt.Sprintf("%s/networks/%d/backendSets", Endpoint, p.networkId)). 157 | Add("X-API-Key", p.apiKey). 158 | Struct(&list) 159 | 160 | if len(errs) != 0 { 161 | return 0, errs[0] 162 | } 163 | 164 | if code != 200 { 165 | return 0, errors.Errorf("unexpected status code: %d", code) 166 | } 167 | 168 | // Check if there is an existing backend set. 169 | var id uint32 170 | for _, set := range list { 171 | if set.Name == ResourcePrefix+hostName { 172 | id = set.Id 173 | break 174 | } 175 | } 176 | 177 | backends := convertBackendSet(backendSet) 178 | descriptor := &BackendSetDescriptor{ 179 | Name: ResourcePrefix + hostName, 180 | ProxyProtocol: false, 181 | Backends: backends, 182 | } 183 | 184 | if id == 0 { 185 | // We need to create a new backend set. 186 | res := &BackendSetResponse{} 187 | code, _, errs = p.client.Post(fmt.Sprintf("%s/networks/%d/backendSets", Endpoint, p.networkId)). 188 | Add("X-API-Key", p.apiKey). 189 | JSON(descriptor). 190 | Struct(res) 191 | 192 | if code != 200 { 193 | return 0, errors.Errorf("unexpected status code: %d", code) 194 | } 195 | 196 | if res.Data == nil { 197 | return 0, errors.New("error creating backendset") 198 | } 199 | 200 | id = res.Data.Id 201 | } else { 202 | // We can update an existing backend set. 203 | code, _, errs = p.client.Patch(fmt.Sprintf("%s/networks/%d/backendSets/%d", Endpoint, p.networkId, id)). 204 | Add("X-API-Key", p.apiKey). 205 | JSON(descriptor). 206 | Bytes() 207 | 208 | if code != 200 { 209 | return 0, errors.Errorf("unexpected status code: %d", code) 210 | } 211 | } 212 | 213 | return id, nil 214 | } 215 | 216 | func convertBackendSet(set []*ingressprovider.Backend) []string { 217 | newSet := make([]string, len(set)) 218 | for i, descriptor := range set { 219 | newSet[i] = fmt.Sprintf("%s:%d", descriptor.IP, descriptor.Port) 220 | } 221 | return newSet 222 | } 223 | -------------------------------------------------------------------------------- /pkg/apis/singularity/v1/fleet.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Singularity is an open-source game server orchestration framework 3 | * Copyright (C) 2022 Innit Incorporated 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Affero General Public License as published 7 | * by the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Affero General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Affero General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | package v1 20 | 21 | import ( 22 | "context" 23 | "innit.gg/singularity/pkg/apis" 24 | "innit.gg/singularity/pkg/apis/singularity" 25 | appsv1 "k8s.io/api/apps/v1" 26 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 27 | "sigs.k8s.io/controller-runtime/pkg/client" 28 | ) 29 | 30 | const ( 31 | // FleetNameLabel is the name of Fleet which owns resources like GameServerSet and GameServer 32 | FleetNameLabel = singularity.GroupName + "/fleet" 33 | ) 34 | 35 | //+kubebuilder:object:root=true 36 | //+kubebuilder:subresource:status 37 | //+kubebuilder:subresource:scale:specpath=.spec.replicas,statuspath=.status.replicas,selectorpath=.status.labelSelector 38 | //+kubebuilder:printcolumn:name="Scheduling",type=string,JSONPath=`.spec.scheduling` 39 | //+kubebuilder:printcolumn:name="Desired",type=string,JSONPath=`.spec.replicas` 40 | //+kubebuilder:printcolumn:name="Current",type=string,JSONPath=`.status.replicas` 41 | //+kubebuilder:printcolumn:name="Age",type=date,JSONPath=`.metadata.creationTimestamp` 42 | 43 | // Fleet is the Schema for the fleets API 44 | type Fleet struct { 45 | metav1.TypeMeta `json:",inline"` 46 | metav1.ObjectMeta `json:"metadata,omitempty"` 47 | 48 | Spec FleetSpec `json:"spec,omitempty"` 49 | Status FleetStatus `json:"status,omitempty"` 50 | } 51 | 52 | //+kubebuilder:object:root=true 53 | 54 | // FleetList contains a list of Fleet 55 | type FleetList struct { 56 | metav1.TypeMeta `json:",inline"` 57 | metav1.ListMeta `json:"metadata,omitempty"` 58 | Items []Fleet `json:"items"` 59 | } 60 | 61 | // FleetSpec defines the desired state of Fleet 62 | type FleetSpec struct { 63 | Replicas int32 `json:"replicas"` 64 | Strategy appsv1.DeploymentStrategy `json:"strategy"` 65 | Scheduling apis.SchedulingStrategy `json:"scheduling"` 66 | Template GameServerTemplate `json:"template"` 67 | } 68 | 69 | // FleetStatus defines the observed state of Fleet 70 | type FleetStatus struct { 71 | Replicas int32 `json:"replicas"` 72 | ReadyReplicas int32 `json:"readyReplicas"` 73 | AllocatedReplicas int32 `json:"allocatedReplicas"` 74 | Instances int32 `json:"instances"` 75 | ReadyInstances int32 `json:"readyInstances"` 76 | AllocatedInstances int32 `json:"allocatedInstances"` 77 | } 78 | 79 | // GameServerSet returns a single GameServerSet for this Fleet definition 80 | func (f *Fleet) GameServerSet() *GameServerSet { 81 | gsSet := &GameServerSet{ 82 | ObjectMeta: *f.Spec.Template.ObjectMeta.DeepCopy(), 83 | Spec: GameServerSetSpec{ 84 | Template: f.Spec.Template, 85 | Scheduling: f.Spec.Scheduling, 86 | }, 87 | } 88 | 89 | // Generate a unique name for GameServerSet, ensuring there are no collisions. 90 | // Also, reset the ObjectMeta. 91 | gsSet.ObjectMeta.GenerateName = f.ObjectMeta.Name + "-" 92 | gsSet.ObjectMeta.Name = "" 93 | gsSet.ObjectMeta.Namespace = f.ObjectMeta.Namespace 94 | gsSet.ObjectMeta.ResourceVersion = "" 95 | gsSet.ObjectMeta.UID = "" 96 | 97 | ref := metav1.NewControllerRef(f, GroupVersion.WithKind("Fleet")) 98 | gsSet.ObjectMeta.OwnerReferences = append(gsSet.ObjectMeta.OwnerReferences, *ref) 99 | 100 | // Append Fleet name 101 | if gsSet.ObjectMeta.Labels == nil { 102 | gsSet.ObjectMeta.Labels = make(map[string]string, 1) 103 | } 104 | 105 | gsSet.ObjectMeta.Labels[FleetNameLabel] = f.ObjectMeta.Name 106 | 107 | return gsSet 108 | } 109 | 110 | // ListGameServerSet lists all owned GameServerSet 111 | func (f *Fleet) ListGameServerSet(ctx context.Context, c client.Client) ([]*GameServerSet, error) { 112 | list := &GameServerSetList{} 113 | labelSelector := client.MatchingLabels{ 114 | FleetNameLabel: f.ObjectMeta.Name, 115 | } 116 | if err := c.List(ctx, list, labelSelector); err != nil { 117 | return []*GameServerSet{}, err 118 | } 119 | 120 | // Make sure that the Fleet actually owns it 121 | var result []*GameServerSet 122 | for i := range list.Items { 123 | gsSet := &list.Items[i] 124 | if metav1.IsControlledBy(gsSet, f) { 125 | result = append(result, gsSet) 126 | } 127 | } 128 | 129 | return result, nil 130 | } 131 | 132 | // CountStatusReadyReplicas returns the count of GameServer with GameServerStateReady in a list of GameServerSet 133 | func CountStatusReadyReplicas(list []*GameServerSet) int32 { 134 | total := int32(0) 135 | for _, gsSet := range list { 136 | if gsSet != nil { 137 | total += gsSet.Status.ReadyReplicas 138 | } 139 | } 140 | 141 | return total 142 | } 143 | 144 | // CountStatusAllocatedReplicas returns the count of GameServer with GameServerStateAllocated in a list of GameServerSet 145 | func CountStatusAllocatedReplicas(list []*GameServerSet) int32 { 146 | total := int32(0) 147 | for _, gsSet := range list { 148 | if gsSet != nil { 149 | total += gsSet.Status.AllocatedReplicas 150 | } 151 | } 152 | 153 | return total 154 | } 155 | 156 | func CountStatusReplicas(list []*GameServerSet) int32 { 157 | total := int32(0) 158 | for _, gsSet := range list { 159 | if gsSet != nil { 160 | total += gsSet.Status.Replicas 161 | } 162 | } 163 | 164 | return total 165 | } 166 | 167 | func CountSpecReplicas(list []*GameServerSet) int32 { 168 | total := int32(0) 169 | for _, gsSet := range list { 170 | if gsSet != nil { 171 | total += gsSet.Spec.Replicas 172 | } 173 | } 174 | 175 | return total 176 | } 177 | 178 | // UpperBoundReplicas returns whichever is smaller, the value i, or the Fleet's desired replicas. 179 | func (f *Fleet) UpperBoundReplicas(i int32) int32 { 180 | if i > f.Spec.Replicas { 181 | return f.Spec.Replicas 182 | } 183 | return i 184 | } 185 | 186 | // LowerBoundReplicas returns 0 if parameter i is less than zero 187 | func (f *Fleet) LowerBoundReplicas(i int32) int32 { 188 | if i < 0 { 189 | return 0 190 | } 191 | return i 192 | } 193 | 194 | func init() { 195 | SchemeBuilder.Register(&Fleet{}, &FleetList{}) 196 | } 197 | -------------------------------------------------------------------------------- /pkg/operator/fleet/rolling.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Singularity is an open-source game server orchestration framework 3 | * Copyright (C) 2022 Innit Incorporated 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Affero General Public License as published 7 | * by the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Affero General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Affero General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | package fleet 20 | 21 | import ( 22 | "context" 23 | "fmt" 24 | "github.com/pkg/errors" 25 | singularityv1 "innit.gg/singularity/pkg/apis/singularity/v1" 26 | v1 "k8s.io/api/core/v1" 27 | "k8s.io/apimachinery/pkg/util/intstr" 28 | "k8s.io/utils/integer" 29 | ) 30 | 31 | // https://github.com/googleforgames/agones/blob/8d01f2ce9c34ffadfdf22ab2fb3b1bafae7e6389/pkg/fleets/controller.go#L415 32 | func (r *Reconciler) handleRollingUpdateDeployment(ctx context.Context, fleet *singularityv1.Fleet, active *singularityv1.GameServerSet, rest []*singularityv1.GameServerSet) (int32, error) { 33 | // First, start by rolling out update for the current active GameServerSet 34 | replicas, err := r.handleRollingUpdateActive(fleet, active, rest) 35 | if err != nil { 36 | return 0, err 37 | } 38 | if err := r.handleRollingUpdateRest(ctx, fleet, active, rest); err != nil { 39 | return 0, err 40 | } 41 | 42 | return replicas, nil 43 | } 44 | 45 | // https://github.com/googleforgames/agones/blob/8d01f2ce9c34ffadfdf22ab2fb3b1bafae7e6389/pkg/fleets/controller.go#L428 46 | func (r *Reconciler) handleRollingUpdateActive(fleet *singularityv1.Fleet, active *singularityv1.GameServerSet, rest []*singularityv1.GameServerSet) (int32, error) { 47 | desiredReplicas := active.Spec.Replicas 48 | 49 | // Leave room for Allocated GameServers in old GameServerSets. 50 | allocatedReplicas := singularityv1.CountStatusAllocatedReplicas(rest) 51 | 52 | // If the state doesn't match the desired replicas, ignore. 53 | // This means we're in the middle of a rolling update, and we should wait. 54 | if active.Spec.Replicas != active.Status.Replicas { 55 | return desiredReplicas, nil 56 | } 57 | 58 | // If there are no desired replicas, ignore. 59 | // The dangling GameServerSet will be removed at a later stage. 60 | if fleet.Spec.Replicas == 0 { 61 | return 0, nil 62 | } 63 | 64 | // If desired replicas in the active GameServerSet is greater or equal to the Fleet's desired replicas, 65 | // then we don't need to continue anymore. 66 | if active.Spec.Replicas >= (fleet.Spec.Replicas - allocatedReplicas) { 67 | return fleet.Spec.Replicas - allocatedReplicas, nil 68 | } 69 | 70 | // Determine how many more GameServers than the desired replicas is acceptable during a rolling update. 71 | sr, err := intstr.GetScaledValueFromIntOrPercent(fleet.Spec.Strategy.RollingUpdate.MaxSurge, int(fleet.Spec.Replicas), true) 72 | if err != nil { 73 | return 0, errors.Wrapf(err, "error parsing MaxSurge value: %s", fleet.Spec.Strategy.RollingUpdate.MaxSurge) 74 | } 75 | surge := int32(sr) 76 | 77 | desiredReplicas = fleet.UpperBoundReplicas(desiredReplicas + surge) 78 | total := singularityv1.CountStatusReplicas(rest) + desiredReplicas 79 | 80 | // Make sure that we don't exceed the max surge. 81 | maxSurge := fleet.Spec.Replicas + surge 82 | if total > maxSurge { 83 | desiredReplicas = fleet.LowerBoundReplicas(desiredReplicas - (total - maxSurge)) 84 | } 85 | 86 | // Take allocated GameServers into consideration. 87 | // Ensure the total active GameServers will not exceed the desired amount. 88 | if desiredReplicas+allocatedReplicas > fleet.Spec.Replicas { 89 | desiredReplicas = fleet.LowerBoundReplicas(fleet.Spec.Replicas - allocatedReplicas) 90 | } 91 | 92 | return desiredReplicas, nil 93 | } 94 | 95 | // https://github.com/googleforgames/agones/blob/8d01f2ce9c34ffadfdf22ab2fb3b1bafae7e6389/pkg/fleets/controller.go#L514 96 | // https://github.com/kubernetes/kubernetes/blob/3aafe756986232ee9208681ee22b38f5c19424a2/pkg/controller/deployment/rolling.go#L87 97 | func (r *Reconciler) handleRollingUpdateRest(ctx context.Context, fleet *singularityv1.Fleet, active *singularityv1.GameServerSet, rest []*singularityv1.GameServerSet) error { 98 | // https://github.com/kubernetes/kubernetes/blob/3ffdfbe286ebcea5d75617da6accaf67f815e0cf/staging/src/k8s.io/kubectl/pkg/util/deployment/deployment.go#L238 99 | ur, err := intstr.GetScaledValueFromIntOrPercent(fleet.Spec.Strategy.RollingUpdate.MaxUnavailable, int(fleet.Spec.Replicas), false) 100 | if err != nil { 101 | return errors.Wrapf(err, "error parsing MaxUnavailable value: %s", fleet.Spec.Strategy.RollingUpdate.MaxUnavailable) 102 | } 103 | unavailable := int32(ur) 104 | 105 | if unavailable == 0 { 106 | unavailable = 1 107 | } 108 | 109 | // MaxUnavailable should not exceed the desired replicas. 110 | if unavailable > fleet.Spec.Replicas { 111 | unavailable = fleet.Spec.Replicas 112 | } 113 | 114 | // Check if we can scale down. 115 | gsSets := rest 116 | gsSets = append(gsSets, active) 117 | minAvailable := fleet.Spec.Replicas - unavailable 118 | 119 | desiredReplicas := singularityv1.CountSpecReplicas(gsSets) 120 | unavailableGSCount := active.Spec.Replicas - active.Status.ReadyReplicas - active.Status.AllocatedReplicas 121 | maxScaleDown := desiredReplicas - minAvailable - unavailableGSCount 122 | 123 | // We don't have the room to scale down. 124 | if maxScaleDown <= 0 { 125 | return nil 126 | } 127 | 128 | if _, err = r.cleanupUnhealthyReplicas(ctx, rest, fleet, maxScaleDown); err != nil { 129 | // There could be the case when GameServerSet would be updated from another place, say Status or Spec would be updated 130 | // We don't want to propagate such errors further 131 | // And this set in sync with reconcileOldReplicaSets() Kubernetes code 132 | return nil 133 | } 134 | // TODO: scale down 135 | 136 | return nil 137 | } 138 | 139 | func (r *Reconciler) cleanupUnhealthyReplicas(ctx context.Context, rest []*singularityv1.GameServerSet, 140 | fleet *singularityv1.Fleet, maxCleanupCount int32) (int32, error) { 141 | 142 | // Safely scale down all old GameServerSets with unhealthy replicas. 143 | totalScaledDown := int32(0) 144 | for i, gsSet := range rest { 145 | if totalScaledDown >= maxCleanupCount { 146 | // We have scaled down enough. 147 | break 148 | } 149 | if gsSet.Spec.Replicas == 0 { 150 | // Cannot scale down this replica set. 151 | continue 152 | } 153 | if gsSet.Spec.Replicas == gsSet.Status.ReadyReplicas { 154 | // No unhealthy replicas found, no scaling required 155 | continue 156 | } 157 | 158 | scaledDownCount := int32(integer.IntMin(int(maxCleanupCount-totalScaledDown), int(gsSet.Spec.Replicas-gsSet.Status.ReadyReplicas))) 159 | newReplicasCount := gsSet.Spec.Replicas - scaledDownCount 160 | if newReplicasCount > gsSet.Spec.Replicas { 161 | return 0, fmt.Errorf("invalid scale down request for gameserverset %s: %d -> %d", gsSet.Name, gsSet.Spec.Replicas, newReplicasCount) 162 | } 163 | 164 | gsSetCopy := gsSet.DeepCopy() 165 | gsSetCopy.Spec.Replicas = newReplicasCount 166 | totalScaledDown += scaledDownCount 167 | if err := r.Update(ctx, gsSetCopy); err != nil { 168 | return totalScaledDown, errors.Wrapf(err, "error updating gameserverset %s/%s", gsSetCopy.Namespace, gsSetCopy.ObjectMeta.Name) 169 | } 170 | 171 | r.Recorder.Eventf(fleet, v1.EventTypeNormal, "ScalingGameServerSet", 172 | "Scaling inactive GameServerSet %s from %d to %d", gsSetCopy.ObjectMeta.Name, gsSet.Spec.Replicas, gsSetCopy.Spec.Replicas) 173 | 174 | rest[i] = gsSetCopy 175 | } 176 | 177 | return totalScaledDown, nil 178 | } 179 | -------------------------------------------------------------------------------- /pkg/operator/fleet/controller.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Singularity is an open-source game server orchestration framework 3 | * Copyright (C) 2022 Innit Incorporated 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Affero General Public License as published 7 | * by the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Affero General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Affero General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | package fleet 20 | 21 | import ( 22 | "context" 23 | "github.com/go-logr/logr" 24 | "github.com/pkg/errors" 25 | singularityv1 "innit.gg/singularity/pkg/apis/singularity/v1" 26 | appsv1 "k8s.io/api/apps/v1" 27 | v1 "k8s.io/api/core/v1" 28 | "k8s.io/apimachinery/pkg/api/equality" 29 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 30 | "k8s.io/client-go/tools/record" 31 | ctrl "sigs.k8s.io/controller-runtime" 32 | "sigs.k8s.io/controller-runtime/pkg/client" 33 | "sigs.k8s.io/controller-runtime/pkg/log" 34 | "sigs.k8s.io/controller-runtime/pkg/reconcile" 35 | "time" 36 | ) 37 | 38 | // Reconciler reconciles a Fleet object 39 | type Reconciler struct { 40 | client.Client 41 | Recorder record.EventRecorder 42 | Log logr.Logger 43 | } 44 | 45 | //+kubebuilder:rbac:groups=singularity.innit.gg,resources=fleets,verbs=get;list;watch;create;update;patch;delete 46 | //+kubebuilder:rbac:groups=singularity.innit.gg,resources=fleets/status,verbs=get;update;patch 47 | //+kubebuilder:rbac:groups=singularity.innit.gg,resources=fleets/finalizers,verbs=update 48 | 49 | func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { 50 | // TODO: retry on error? 51 | l := log.FromContext(ctx) 52 | l.Info("reconcile") 53 | 54 | // Retrieve the Fleet resource from the cluster, ignoring if it was deleted 55 | fleet := &singularityv1.Fleet{} 56 | if err := r.Get(ctx, req.NamespacedName, fleet); err != nil { 57 | l.Info("reconcile: resource deleted", "fleet", req.Name) 58 | return ctrl.Result{}, client.IgnoreNotFound(err) 59 | } 60 | 61 | // If Fleet is marked for deletion, don't do anything. 62 | if !fleet.DeletionTimestamp.IsZero() { 63 | return ctrl.Result{}, nil 64 | } 65 | 66 | // Retrieve GameServerSets associated with this Fleet 67 | list, err := fleet.ListGameServerSet(ctx, r.Client) 68 | if err != nil { 69 | l.Error(err, "reconcile: unable to list GameServerSet", "fleet", req.Name) 70 | 71 | // TODO: is this the correct way? 72 | return ctrl.Result{ 73 | Requeue: true, 74 | RequeueAfter: 3 * time.Second, 75 | }, err 76 | } 77 | 78 | // Find the active GameServerSet and return the rest 79 | active, rest := r.filterActiveGameServerSet(fleet, list) 80 | if active == nil { 81 | l.Info("reconcile: creating GameServerSet", "fleet", req.Name) 82 | 83 | // If there isn't an active GameServerSet, create one. 84 | // However, don't apply it to the cluster yet. 85 | active = fleet.GameServerSet() 86 | } 87 | 88 | // Run the deployment cycle 89 | replicas, err := r.handleDeployment(ctx, fleet, active, rest) 90 | if err != nil { 91 | l.Error(err, "reconcile: deployment cycle failed", "fleet", req.Name) 92 | return ctrl.Result{}, err 93 | } 94 | 95 | if err = r.deleteEmptyGameServerSets(ctx, fleet, rest); err != nil { 96 | return ctrl.Result{}, err 97 | } 98 | 99 | if err = r.upsertGameServerSet(ctx, fleet, active, replicas); err != nil { 100 | return ctrl.Result{}, err 101 | } 102 | 103 | if err = r.updateStatus(ctx, fleet, list); err != nil { 104 | return ctrl.Result{}, err 105 | } 106 | 107 | return ctrl.Result{}, nil 108 | } 109 | 110 | // SetupWithManager sets up the controller with the Manager. 111 | func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error { 112 | return ctrl.NewControllerManagedBy(mgr). 113 | For(&singularityv1.Fleet{}). 114 | Owns(&singularityv1.GameServerSet{}). 115 | WithLogConstructor(func(req *reconcile.Request) logr.Logger { 116 | if req != nil { 117 | return r.Log.WithValues("req", req) 118 | } 119 | return r.Log 120 | }). 121 | Complete(r) 122 | } 123 | 124 | // handleDeployment performs the deployment strategy 125 | // https://github.com/googleforgames/agones/blob/8d01f2ce9c34ffadfdf22ab2fb3b1bafae7e6389/pkg/fleets/controller.go#L356 126 | func (r *Reconciler) handleDeployment(ctx context.Context, fleet *singularityv1.Fleet, active *singularityv1.GameServerSet, rest []*singularityv1.GameServerSet) (int32, error) { 127 | if len(rest) == 0 { 128 | // There is only one GameServerSet which matches the desired state. 129 | // Further action is not required. 130 | return fleet.Spec.Replicas, nil 131 | } 132 | 133 | switch fleet.Spec.Strategy.Type { 134 | case appsv1.RollingUpdateDeploymentStrategyType: 135 | return r.handleRollingUpdateDeployment(ctx, fleet, active, rest) 136 | } 137 | 138 | return 0, errors.Errorf("unexpected deployment strategy type: %s", fleet.Spec.Strategy.Type) 139 | } 140 | 141 | func (r *Reconciler) filterActiveGameServerSet(fleet *singularityv1.Fleet, list []*singularityv1.GameServerSet) (*singularityv1.GameServerSet, []*singularityv1.GameServerSet) { 142 | var active *singularityv1.GameServerSet 143 | var rest []*singularityv1.GameServerSet 144 | 145 | for _, gsSet := range list { 146 | // If the actual state is equal to the desired state 147 | if equality.Semantic.DeepEqual(gsSet.Spec.Template, fleet.Spec.Template) { 148 | active = gsSet 149 | } else { 150 | rest = append(rest, gsSet) 151 | } 152 | } 153 | 154 | return active, rest 155 | } 156 | 157 | // deleteEmptyGameServerSets deletes all GameServerSets with 0 replicas 158 | func (r *Reconciler) deleteEmptyGameServerSets(ctx context.Context, fleet *singularityv1.Fleet, list []*singularityv1.GameServerSet) error { 159 | policy := client.PropagationPolicy(metav1.DeletePropagationBackground) 160 | for _, gsSet := range list { 161 | if gsSet.Status.Replicas == 0 && gsSet.Status.ShutdownReplicas == 0 { 162 | if err := r.Delete(ctx, gsSet, policy); err != nil { 163 | return errors.Wrapf(err, "error deleting gameserverset %s", gsSet.ObjectMeta.Name) 164 | } 165 | 166 | r.Recorder.Eventf(fleet, v1.EventTypeNormal, "DeletingGameServerSet", "Deleting inactive GameServerSet %s", gsSet.ObjectMeta.Name) 167 | } 168 | } 169 | 170 | return nil 171 | } 172 | 173 | // upsertGameServerSet inserts the new GameServerSet (if required) 174 | // and updates the active GameServerSet to match the desired state 175 | func (r *Reconciler) upsertGameServerSet(ctx context.Context, fleet *singularityv1.Fleet, active *singularityv1.GameServerSet, replicas int32) error { 176 | if active.UID == "" { 177 | active.Spec.Replicas = replicas 178 | if err := r.Create(ctx, active); err != nil { 179 | return errors.Wrapf(err, "error creating gameserverset %s", active.ObjectMeta.Name) 180 | } 181 | 182 | gsSetCopy := active.DeepCopy() 183 | gsSetCopy.Status.Replicas = 0 184 | gsSetCopy.Status.ReadyReplicas = 0 185 | gsSetCopy.Status.AllocatedReplicas = 0 186 | gsSetCopy.Status.ShutdownReplicas = 0 187 | gsSetCopy.Status.Instances = 0 188 | gsSetCopy.Status.ReadyInstances = 0 189 | gsSetCopy.Status.AllocatedInstances = 0 190 | gsSetCopy.Status.ShutdownInstances = 0 191 | if err := r.Status().Update(ctx, gsSetCopy); err != nil { 192 | return errors.Wrapf(err, "error updating status for gameserverset %s", active.ObjectMeta.Name) 193 | } 194 | 195 | r.Recorder.Eventf(fleet, v1.EventTypeNormal, "CreatingGameServerSet", "Created GameServerSet %s", active.ObjectMeta.Name) 196 | return nil 197 | } 198 | 199 | if replicas != active.Spec.Replicas || active.Spec.Scheduling != fleet.Spec.Scheduling { 200 | gsSetCopy := active.DeepCopy() 201 | gsSetCopy.Spec.Replicas = replicas 202 | gsSetCopy.Spec.Scheduling = fleet.Spec.Scheduling 203 | if err := r.Update(ctx, gsSetCopy); err != nil { 204 | return errors.Wrapf(err, "error updating replicas for gameserverset %s", active.ObjectMeta.Name) 205 | } 206 | r.Recorder.Eventf(fleet, v1.EventTypeNormal, "ScalingGameServerSet", 207 | "Scaling active GameServerSet %s from %d to %d", active.ObjectMeta.Name, active.Spec.Replicas, gsSetCopy.Spec.Replicas) 208 | } 209 | 210 | return nil 211 | } 212 | 213 | func (r *Reconciler) updateStatus(ctx context.Context, fleet *singularityv1.Fleet, list []*singularityv1.GameServerSet) error { 214 | var status singularityv1.FleetStatus 215 | 216 | for _, gsSet := range list { 217 | status.Replicas += gsSet.Status.Replicas 218 | status.ReadyReplicas += gsSet.Status.ReadyReplicas 219 | status.AllocatedReplicas += gsSet.Status.AllocatedReplicas 220 | status.Instances += gsSet.Status.Instances 221 | status.ReadyInstances += gsSet.Status.ReadyInstances 222 | status.AllocatedInstances += gsSet.Status.AllocatedInstances 223 | } 224 | 225 | // TODO: Aggregate player status 226 | if fleet.Status != status { 227 | fleet.Status = status 228 | if err := r.Status().Update(ctx, fleet); err != nil { 229 | return errors.Wrapf(err, "error updating status") 230 | } 231 | } 232 | 233 | return nil 234 | } 235 | -------------------------------------------------------------------------------- /pkg/operator/gameserverset/controller.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Singularity is an open-source game server orchestration framework 3 | * Copyright (C) 2022 Innit Incorporated 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Affero General Public License as published 7 | * by the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Affero General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Affero General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | package gameserverset 20 | 21 | import ( 22 | "context" 23 | "github.com/go-logr/logr" 24 | "github.com/pkg/errors" 25 | singularityv1 "innit.gg/singularity/pkg/apis/singularity/v1" 26 | v1 "k8s.io/api/core/v1" 27 | "k8s.io/client-go/tools/record" 28 | ctrl "sigs.k8s.io/controller-runtime" 29 | "sigs.k8s.io/controller-runtime/pkg/client" 30 | "sigs.k8s.io/controller-runtime/pkg/log" 31 | "sigs.k8s.io/controller-runtime/pkg/reconcile" 32 | ) 33 | 34 | const ( 35 | maxCreationParalellism = 16 36 | maxGameServerCreationsPerBatch = 64 37 | 38 | maxDeletionParallelism = 64 39 | maxGameServerDeletionsPerBatch = 64 40 | 41 | // maxPodPendingCount is the maximum number of pending pods per game server set 42 | maxPodPendingCount = 5000 43 | ) 44 | 45 | // Reconciler reconciles a GameServerSet object 46 | type Reconciler struct { 47 | client.Client 48 | Recorder record.EventRecorder 49 | Log logr.Logger 50 | } 51 | 52 | //+kubebuilder:rbac:groups=singularity.innit.gg,resources=GameServerSets,verbs=get;list;watch;create;update;patch;delete 53 | //+kubebuilder:rbac:groups=singularity.innit.gg,resources=GameServerSets/status,verbs=get;update;patch 54 | //+kubebuilder:rbac:groups=singularity.innit.gg,resources=GameServerSets/finalizers,verbs=update 55 | 56 | func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { 57 | // TODO: retry on error? 58 | 59 | l := log.FromContext(ctx) 60 | l.Info("reconcile") 61 | 62 | // Retrieve the GameServerSet resource from the cluster, ignoring if it was deleted 63 | gsSet := &singularityv1.GameServerSet{} 64 | if err := r.Get(ctx, req.NamespacedName, gsSet); err != nil { 65 | l.Info("reconcile: resource deleted", "gsSet", req.Name) 66 | return ctrl.Result{}, client.IgnoreNotFound(err) 67 | } 68 | 69 | list, err := gsSet.ListGameServer(ctx, r.Client) 70 | if err != nil { 71 | return ctrl.Result{}, err 72 | } 73 | 74 | createCount, toDelete, isPartial := computeReconciliationAction(list, int(gsSet.Spec.Replicas)) 75 | l.Info("reconcile action", "create", createCount, "delete", len(toDelete), "partial", isPartial) 76 | 77 | // If GameServerSet is marked for deletion, don't do anything. 78 | if !gsSet.DeletionTimestamp.IsZero() { 79 | return ctrl.Result{}, nil 80 | } 81 | 82 | if createCount > 0 { 83 | if err = r.createGameServers(ctx, gsSet, createCount); err != nil { 84 | l.Error(err, "reconcile: error creating GameServers") 85 | } 86 | } 87 | 88 | if len(toDelete) > 0 { 89 | if err := r.deleteGameServers(ctx, gsSet, toDelete); err != nil { 90 | l.Error(err, "reconcile: error deleting GameServers") 91 | } 92 | // TODO 93 | } 94 | 95 | if err := r.updateStatus(ctx, gsSet, list); err != nil { 96 | return ctrl.Result{}, nil 97 | } 98 | 99 | if isPartial { 100 | // We have more work to do, reschedule reconciliation for this GameServerSet. 101 | return ctrl.Result{ 102 | Requeue: true, 103 | }, nil 104 | } 105 | 106 | return ctrl.Result{}, nil 107 | } 108 | 109 | // SetupWithManager sets up the controller with the Manager. 110 | func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error { 111 | return ctrl.NewControllerManagedBy(mgr). 112 | For(&singularityv1.GameServerSet{}). 113 | Owns(&singularityv1.GameServer{}). 114 | WithLogConstructor(func(req *reconcile.Request) logr.Logger { 115 | if req != nil { 116 | return r.Log.WithValues("req", req) 117 | } 118 | return r.Log 119 | }). 120 | Complete(r) 121 | } 122 | 123 | // computeReconciliationAction computes the action to take in the reconcilation cycle 124 | func computeReconciliationAction(list []*singularityv1.GameServer, targetReplicaCount int) (int, []*singularityv1.GameServer, bool) { 125 | var upCount int // up == Ready or will become ready 126 | var deleteCount int // number of gameservers to delete 127 | 128 | // track the number of pods that are being created at any given moment by the GameServerSet 129 | // so we can limit it at a throughput that Kubernetes can handle 130 | var podPendingCount int // podPending == "up" but don't have a Pod running yet 131 | 132 | var potentialDeletions []*singularityv1.GameServer 133 | var toDelete []*singularityv1.GameServer 134 | 135 | scheduleDeletion := func(gs *singularityv1.GameServer) { 136 | toDelete = append(toDelete, gs) 137 | deleteCount-- 138 | } 139 | 140 | handleGameServerUp := func(gs *singularityv1.GameServer) { 141 | if upCount >= targetReplicaCount { 142 | deleteCount++ 143 | } else { 144 | upCount++ 145 | } 146 | 147 | // Track gameservers that could be potentially deleted 148 | potentialDeletions = append(potentialDeletions, gs) 149 | } 150 | 151 | // pass 1 - count allocated/reserved servers only, since those can't be touched 152 | for _, gs := range list { 153 | if !gs.IsDeletable() { 154 | upCount++ 155 | } 156 | } 157 | 158 | // pass 2 - handle all other statuses 159 | for _, gs := range list { 160 | if !gs.IsDeletable() { 161 | // already handled above 162 | continue 163 | } 164 | 165 | // GS being deleted don't count. 166 | if gs.IsBeingDeleted() { 167 | continue 168 | } 169 | 170 | switch gs.Status.State { 171 | //case singularityv1.GameServerStatePortAllocation: 172 | // podPendingCount++ 173 | // handleGameServerUp(gs) 174 | case singularityv1.GameServerStateCreating, 175 | singularityv1.GameServerStateStarting, 176 | singularityv1.GameServerStateScheduled: 177 | podPendingCount++ 178 | handleGameServerUp(gs) 179 | case singularityv1.GameServerStateRequestReady, 180 | singularityv1.GameServerStateReady: 181 | handleGameServerUp(gs) 182 | //case singularityv1.GameServerStateReserved: 183 | // handleGameServerUp(gs) 184 | 185 | // GameServerStateShutdown - already handled above 186 | // GameServerStateAllocated - already handled above 187 | case singularityv1.GameServerStateError, singularityv1.GameServerStateUnhealthy: 188 | scheduleDeletion(gs) 189 | default: 190 | // unrecognized state, assume it's up. 191 | handleGameServerUp(gs) 192 | } 193 | } 194 | 195 | var partialReconciliation bool 196 | var numServersToAdd int 197 | 198 | if upCount < targetReplicaCount { 199 | numServersToAdd = targetReplicaCount - upCount 200 | originalNumServersToAdd := numServersToAdd 201 | 202 | if numServersToAdd > maxGameServerCreationsPerBatch { 203 | numServersToAdd = maxGameServerCreationsPerBatch 204 | } 205 | 206 | if numServersToAdd+podPendingCount > maxPodPendingCount { 207 | numServersToAdd = maxPodPendingCount - podPendingCount 208 | if numServersToAdd < 0 { 209 | numServersToAdd = 0 210 | } 211 | } 212 | 213 | if originalNumServersToAdd != numServersToAdd { 214 | partialReconciliation = true 215 | } 216 | } 217 | 218 | if deleteCount > 0 { 219 | potentialDeletions = singularityv1.SortDescending(potentialDeletions) 220 | toDelete = append(toDelete, potentialDeletions[0:deleteCount]...) 221 | } 222 | 223 | if len(toDelete) > maxGameServerDeletionsPerBatch { 224 | toDelete = toDelete[0:maxGameServerDeletionsPerBatch] 225 | partialReconciliation = true 226 | } 227 | 228 | return numServersToAdd, toDelete, partialReconciliation 229 | } 230 | 231 | func (r *Reconciler) createGameServers(ctx context.Context, gsSet *singularityv1.GameServerSet, count int) error { 232 | l := log.FromContext(ctx) 233 | l.WithValues("count", count).Info("reconcile: creating GameServers") 234 | 235 | return parallelize(newGameServersChannel(count, gsSet), maxCreationParalellism, func(gs *singularityv1.GameServer) error { 236 | if err := r.Create(ctx, gs); err != nil { 237 | return errors.Wrapf(err, "error creating gameserver for gameserverset %s", gsSet.ObjectMeta.Name) 238 | } 239 | 240 | r.Recorder.Eventf(gsSet, v1.EventTypeNormal, "SuccessfulCreate", "Created GameServer: %s", gs.ObjectMeta.Name) 241 | return nil 242 | }) 243 | } 244 | 245 | func (r *Reconciler) deleteGameServers(ctx context.Context, gsSet *singularityv1.GameServerSet, toDelete []*singularityv1.GameServer) error { 246 | l := log.FromContext(ctx) 247 | l.Info("reconcile: deleting gameservers from gameserverset", "count", len(toDelete), "gsSet", gsSet.ObjectMeta.Name) 248 | 249 | return parallelize(gameServerListToChannel(toDelete), maxDeletionParallelism, func(gs *singularityv1.GameServer) error { 250 | // We should not delete the GameServers directly, as we would like the GameServer controller to handle deletion. 251 | gsCopy := gs.DeepCopy() 252 | 253 | // TODO: Draining 254 | gsCopy.Status.State = singularityv1.GameServerStateShutdown 255 | if err := r.Status().Update(ctx, gsCopy); err != nil { 256 | return errors.Wrapf(err, "error updating gameserver %s from status %s to Shutdown status", gs.ObjectMeta.Name, gs.Status.State) 257 | } 258 | 259 | r.Recorder.Eventf(gsSet, v1.EventTypeNormal, "SuccessfulDelete", "Deleted GameServer in state %s: %v", gs.Status.State, gs.ObjectMeta.Name) 260 | return nil 261 | }) 262 | } 263 | 264 | func (r *Reconciler) updateStatus(ctx context.Context, gsSet *singularityv1.GameServerSet, list []*singularityv1.GameServer) error { 265 | // We don't need to take the reconciliation action into account here. 266 | // The changed list will be reflected upon in the next cycle. 267 | var status singularityv1.GameServerSetStatus 268 | 269 | for _, gs := range list { 270 | if gs.IsBeingDeleted() { 271 | status.ShutdownReplicas++ 272 | 273 | // Don't count replicas that are being deleted 274 | continue 275 | } 276 | 277 | status.Replicas++ 278 | switch gs.Status.State { 279 | case singularityv1.GameServerStateReady: 280 | status.ReadyReplicas++ 281 | case singularityv1.GameServerStateAllocated: 282 | status.AllocatedReplicas++ 283 | } 284 | 285 | // TODO: Instances 286 | } 287 | 288 | if gsSet.Status != status { 289 | // Only change the status if it's not equal to the current one. 290 | gsSetCopy := gsSet.DeepCopy() 291 | gsSetCopy.Status = status 292 | if err := r.Status().Update(ctx, gsSetCopy); err != nil { 293 | return errors.Wrapf(err, "error updating status for gameserverset %s", gsSet.ObjectMeta.Name) 294 | } 295 | } 296 | 297 | return nil 298 | } 299 | -------------------------------------------------------------------------------- /pkg/apis/singularity/v1/gameserver.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Singularity is an open-source game server orchestration framework 3 | * Copyright (C) 2022 Innit Incorporated 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Affero General Public License as published 7 | * by the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Affero General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Affero General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | package v1 20 | 21 | import ( 22 | "fmt" 23 | "innit.gg/singularity/pkg/apis" 24 | "innit.gg/singularity/pkg/apis/singularity" 25 | v1 "k8s.io/api/core/v1" 26 | rbacv1 "k8s.io/api/rbac/v1" 27 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 28 | "sort" 29 | ) 30 | 31 | const ( 32 | // GameServerTypeGame describes a game server which utilizes the allocation system 33 | GameServerTypeGame GameServerType = "Game" 34 | // GameServerTypeEphemeral describes a game server which is stateless 35 | GameServerTypeEphemeral GameServerType = "Ephemeral" 36 | // GameServerTypeStatic describes a game server which is manually controlled by the user 37 | GameServerTypeStatic GameServerType = "Static" 38 | 39 | // GameServerStateCreating indicates that the Pod is not yet created 40 | GameServerStateCreating GameServerState = "Creating" 41 | // GameServerStateStarting indicates that the Pod is created, but not yet scheduled 42 | GameServerStateStarting GameServerState = "Starting" 43 | // GameServerStateScheduled indicates that the Pod is scheduled in the cluster, basically belonging to a Node 44 | GameServerStateScheduled GameServerState = "Scheduled" 45 | // GameServerStateRequestReady indicates that the server is requesting to be Ready 46 | GameServerStateRequestReady GameServerState = "RequestReady" 47 | // GameServerStateReady indicates that the server is ready to accept player (and optionally Allocated) 48 | GameServerStateReady GameServerState = "Ready" 49 | // GameServerStateAllocated indicates that the server has been allocated and shall not be removed 50 | GameServerStateAllocated GameServerState = "Allocated" 51 | // GameServerStateDrain indicates the server is no longer accepting new players, and is waiting for existing 52 | // instances to be shut down. 53 | GameServerStateDrain GameServerState = "Drain" 54 | // GameServerStateShutdown indicates that the server has shutdown and everything has to be removed from the cluster 55 | GameServerStateShutdown GameServerState = "Shutdown" 56 | // GameServerStateError indicates that something irrecoverable occurred 57 | GameServerStateError GameServerState = "Error" 58 | // GameServerStateUnhealthy indicates that the server failed its health checks 59 | GameServerStateUnhealthy GameServerState = "Unhealthy" 60 | 61 | // GameServerRole is the GameServer label value for singularity.RoleLabel 62 | GameServerRole = "gameserver" 63 | // GameServerNameLabel is the name of GameServer which owns resources like v1.Pod 64 | GameServerNameLabel = singularity.GroupName + "/fleet" 65 | 66 | // GameServerEnvNamespace is the namespace of GameServer which owns the pod 67 | GameServerEnvNamespace = "SINGULARITY_GAMESERVER_NAMESPACE" 68 | // GameServerEnvName is the name of GameServer which owns the pod 69 | GameServerEnvName = "SINGULARITY_GAMESERVER_NAME" 70 | ) 71 | 72 | //+kubebuilder:object:root=true 73 | //+kubebuilder:subresource:status 74 | //+kubebuilder:printcolumn:name="Status",type=string,JSONPath=`.status.state` 75 | //+kubebuilder:printcolumn:name="Desired",type=string,JSONPath=`.spec.instances` 76 | //+kubebuilder:printcolumn:name="Age",type=date,JSONPath=`.metadata.creationTimestamp` 77 | 78 | // GameServer is the Schema for the GameServers API 79 | type GameServer struct { 80 | metav1.TypeMeta `json:",inline"` 81 | metav1.ObjectMeta `json:"metadata,omitempty"` 82 | 83 | Spec GameServerSpec `json:"spec,omitempty"` 84 | Status GameServerStatus `json:"status,omitempty"` 85 | } 86 | 87 | // GameServerTemplate is the template for the GameServers API 88 | type GameServerTemplate struct { 89 | metav1.ObjectMeta `json:"metadata,omitempty"` 90 | 91 | Spec GameServerSpec `json:"spec,omitempty"` 92 | } 93 | 94 | //+kubebuilder:object:root=true 95 | 96 | // GameServerList contains a list of GameServer 97 | type GameServerList struct { 98 | metav1.TypeMeta `json:",inline"` 99 | metav1.ListMeta `json:"metadata,omitempty"` 100 | Items []GameServer `json:"items"` 101 | } 102 | 103 | // GameServerSpec defines the desired state of GameServer 104 | type GameServerSpec struct { 105 | Type GameServerType `json:"type"` 106 | Scheduling apis.SchedulingStrategy `json:"scheduling"` 107 | DrainStrategy GameServerDrainStrategy `json:"drainStrategy"` 108 | Ports []GameServerPort `json:"ports"` 109 | Instances int32 `json:"instances"` 110 | InstanceTemplate GameServerInstanceTemplate `json:"instanceTemplate"` 111 | Template v1.PodTemplateSpec `json:"template"` 112 | } 113 | 114 | type GameServerType string 115 | type GameServerState string 116 | 117 | // GameServerStatus defines the observed state of GameServer 118 | type GameServerStatus struct { 119 | State GameServerState `json:"state"` 120 | } 121 | 122 | type GameServerDrainStrategy struct { 123 | Timeout int32 `json:"timeout,omitempty"` 124 | Instances int32 `json:"instances"` 125 | ReadyInstances int32 `json:"readyInstances"` 126 | AllocatedInstances int32 `json:"allocatedInstances"` 127 | } 128 | 129 | type GameServerPort struct { 130 | Name string `json:"name"` 131 | PortPolicy apis.PortPolicy `json:"portPolicy"` 132 | ContainerPort string `json:"containerPort"` 133 | } 134 | 135 | // IsDeletable returns whether the server is currently allocated/reserved and is not already in the 136 | // process of being deleted 137 | func (gs *GameServer) IsDeletable() bool { 138 | if gs.Status.State == GameServerStateAllocated { 139 | return !gs.ObjectMeta.DeletionTimestamp.IsZero() 140 | } 141 | 142 | return true 143 | } 144 | 145 | // IsBeingDeleted returns true if the server is in the process of being deleted. 146 | func (gs *GameServer) IsBeingDeleted() bool { 147 | return !gs.ObjectMeta.DeletionTimestamp.IsZero() || gs.Status.State == GameServerStateShutdown 148 | } 149 | 150 | // Pod creates a Pod according to the template specified in the GameServer resource 151 | func (gs *GameServer) Pod() *v1.Pod { 152 | pod := &v1.Pod{ 153 | ObjectMeta: *gs.Spec.Template.ObjectMeta.DeepCopy(), 154 | Spec: *gs.Spec.Template.Spec.DeepCopy(), 155 | } 156 | 157 | gs.configurePodMeta(pod) 158 | 159 | // Make sure that the ServiceAccount is bound 160 | pod.Spec.ServiceAccountName = gs.ObjectMeta.Name 161 | 162 | // TODO: Only select one container? 163 | envName := v1.EnvVar{ 164 | Name: GameServerEnvName, 165 | Value: gs.ObjectMeta.Name, 166 | } 167 | envNamespace := v1.EnvVar{ 168 | Name: GameServerEnvNamespace, 169 | Value: gs.ObjectMeta.Namespace, 170 | } 171 | for i := range pod.Spec.Containers { 172 | container := &pod.Spec.Containers[i] 173 | container.Env = append(container.Env, envName, envNamespace) 174 | } 175 | 176 | // TODO: hostPort allocation 177 | 178 | return pod 179 | } 180 | 181 | func (gs *GameServer) ServiceAccount() *v1.ServiceAccount { 182 | ref := metav1.NewControllerRef(gs, GroupVersion.WithKind("GameServer")) 183 | 184 | return &v1.ServiceAccount{ 185 | ObjectMeta: metav1.ObjectMeta{ 186 | Name: gs.Name, 187 | Namespace: gs.Namespace, 188 | Labels: map[string]string{ 189 | GameServerNameLabel: gs.ObjectMeta.Name, 190 | }, 191 | OwnerReferences: []metav1.OwnerReference{*ref}, 192 | }, 193 | } 194 | } 195 | 196 | func (gs *GameServer) Role() *rbacv1.Role { 197 | ref := metav1.NewControllerRef(gs, GroupVersion.WithKind("GameServer")) 198 | 199 | return &rbacv1.Role{ 200 | ObjectMeta: metav1.ObjectMeta{ 201 | Name: gs.Name, 202 | Namespace: gs.Namespace, 203 | Labels: map[string]string{ 204 | GameServerNameLabel: gs.ObjectMeta.Name, 205 | }, 206 | OwnerReferences: []metav1.OwnerReference{*ref}, 207 | }, 208 | // Only allow access to its own GameServer and Pod resources 209 | Rules: []rbacv1.PolicyRule{ 210 | { 211 | Verbs: []string{"get", "update", "patch", "list", "watch"}, 212 | APIGroups: []string{singularity.GroupName}, 213 | Resources: []string{"gameservers", "gameservers/status"}, 214 | ResourceNames: []string{gs.ObjectMeta.Name}, 215 | NonResourceURLs: nil, 216 | }, 217 | { 218 | Verbs: []string{"get", "update", "patch", "list", "watch"}, 219 | APIGroups: []string{""}, // Default Kubernetes API group 220 | Resources: []string{"pods", "pods/status"}, 221 | ResourceNames: []string{gs.ObjectMeta.Name}, 222 | NonResourceURLs: nil, 223 | }, 224 | }, 225 | } 226 | } 227 | 228 | func (gs *GameServer) RoleBinding() *rbacv1.RoleBinding { 229 | ref := metav1.NewControllerRef(gs, GroupVersion.WithKind("GameServer")) 230 | 231 | return &rbacv1.RoleBinding{ 232 | ObjectMeta: metav1.ObjectMeta{ 233 | Name: gs.ObjectMeta.Name, 234 | Namespace: gs.ObjectMeta.Namespace, 235 | Labels: map[string]string{ 236 | GameServerNameLabel: gs.ObjectMeta.Name, 237 | }, 238 | OwnerReferences: []metav1.OwnerReference{*ref}, 239 | }, 240 | Subjects: []rbacv1.Subject{ 241 | { 242 | Kind: "ServiceAccount", 243 | Name: gs.ObjectMeta.Name, 244 | }, 245 | }, 246 | RoleRef: rbacv1.RoleRef{ 247 | APIGroup: "rbac.authorization.k8s.io", 248 | Kind: "Role", 249 | Name: gs.ObjectMeta.Name, 250 | }, 251 | } 252 | } 253 | 254 | func (gs *GameServer) GameServerInstance(id int) *GameServerInstance { 255 | ref := metav1.NewControllerRef(gs, GroupVersion.WithKind("GameServer")) 256 | 257 | // TODO: Copy metadata from instance template 258 | return &GameServerInstance{ 259 | ObjectMeta: metav1.ObjectMeta{ 260 | Name: fmt.Sprintf("%s-%d", gs.ObjectMeta.Name, id), 261 | Namespace: gs.ObjectMeta.Namespace, 262 | Labels: map[string]string{ 263 | GameServerNameLabel: gs.ObjectMeta.Name, 264 | }, 265 | OwnerReferences: []metav1.OwnerReference{*ref}, 266 | }, 267 | Spec: *gs.Spec.InstanceTemplate.Spec.DeepCopy(), 268 | } 269 | } 270 | 271 | // SortDescending returns GameServers sorted by newest created 272 | func SortDescending(list []*GameServer) []*GameServer { 273 | sort.Slice(list, func(i, j int) bool { 274 | a := list[i] 275 | b := list[j] 276 | 277 | return a.ObjectMeta.CreationTimestamp.Before(&b.ObjectMeta.CreationTimestamp) 278 | }) 279 | 280 | return list 281 | } 282 | 283 | func (gs *GameServer) configurePodMeta(pod *v1.Pod) { 284 | // Name and namespace needs to match the GameServer 285 | pod.ObjectMeta.GenerateName = "" 286 | pod.ObjectMeta.Name = gs.ObjectMeta.Name 287 | pod.ObjectMeta.Namespace = gs.ObjectMeta.Namespace 288 | 289 | // Reset these, just in case 290 | pod.ObjectMeta.ResourceVersion = "" 291 | pod.ObjectMeta.UID = "" 292 | 293 | // Append labels 294 | if pod.ObjectMeta.Labels == nil { 295 | pod.ObjectMeta.Labels = make(map[string]string, 2) 296 | } 297 | pod.ObjectMeta.Labels[singularity.RoleLabel] = GameServerRole 298 | pod.ObjectMeta.Labels[GameServerNameLabel] = gs.ObjectMeta.Name 299 | 300 | // Append GameServer owner reference 301 | ref := metav1.NewControllerRef(gs, GroupVersion.WithKind("GameServer")) 302 | pod.ObjectMeta.OwnerReferences = append(pod.ObjectMeta.OwnerReferences, *ref) 303 | } 304 | 305 | func init() { 306 | SchemeBuilder.Register(&GameServer{}, &GameServerList{}) 307 | } 308 | -------------------------------------------------------------------------------- /pkg/operator/gameserver/controller.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Singularity is an open-source game server orchestration framework 3 | * Copyright (C) 2022 Innit Incorporated 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Affero General Public License as published 7 | * by the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Affero General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Affero General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | package gameserver 20 | 21 | import ( 22 | "context" 23 | "fmt" 24 | "github.com/go-logr/logr" 25 | "github.com/pkg/errors" 26 | singularityv1 "innit.gg/singularity/pkg/apis/singularity/v1" 27 | v1 "k8s.io/api/core/v1" 28 | k8serrors "k8s.io/apimachinery/pkg/api/errors" 29 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 30 | 31 | "k8s.io/client-go/tools/record" 32 | ctrl "sigs.k8s.io/controller-runtime" 33 | "sigs.k8s.io/controller-runtime/pkg/client" 34 | "sigs.k8s.io/controller-runtime/pkg/log" 35 | "sigs.k8s.io/controller-runtime/pkg/reconcile" 36 | ) 37 | 38 | // Reconciler reconciles a GameServer object 39 | type Reconciler struct { 40 | client.Client 41 | Recorder record.EventRecorder 42 | Log logr.Logger 43 | } 44 | 45 | //+kubebuilder:rbac:groups=singularity.innit.gg,resources=GameServers,verbs=get;list;watch;create;update;patch;delete 46 | //+kubebuilder:rbac:groups=singularity.innit.gg,resources=GameServers/status,verbs=get;update;patch 47 | //+kubebuilder:rbac:groups=singularity.innit.gg,resources=GameServers/finalizers,verbs=update 48 | 49 | func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { 50 | l := log.FromContext(ctx) 51 | l.Info("reconcile") 52 | 53 | // Retrieve the GameServer resource from the cluster, ignoring if it was deleted 54 | gs := &singularityv1.GameServer{} 55 | if err := r.Get(ctx, req.NamespacedName, gs); err != nil { 56 | l.Info("reconcile: resource deleted", "gs", req.Name) 57 | return ctrl.Result{}, client.IgnoreNotFound(err) 58 | } 59 | 60 | // if gs, err = c.syncGameServerDeletionTimestamp(ctx, gs); err != nil { 61 | // return err 62 | // } 63 | // if gs, err = c.syncGameServerPortAllocationState(ctx, gs); err != nil { 64 | // return err 65 | // } 66 | // if gs, err = c.syncGameServerCreatingState(ctx, gs); err != nil { 67 | // return err 68 | // } 69 | // if gs, err = c.syncGameServerStartingState(ctx, gs); err != nil { 70 | // return err 71 | // } 72 | // if gs, err = c.syncGameServerRequestReadyState(ctx, gs); err != nil { 73 | // return err 74 | // } 75 | // if gs, err = c.syncDevelopmentGameServer(ctx, gs); err != nil { 76 | // return err 77 | // } 78 | // if err := c.syncGameServerShutdownState(ctx, gs); err != nil { 79 | // return err 80 | // } 81 | 82 | if err := r.reconcileGameServerDeletion(ctx, gs); err != nil { 83 | return ctrl.Result{}, err 84 | } 85 | 86 | switch gs.Status.State { 87 | case singularityv1.GameServerStateCreating: 88 | if gs.ObjectMeta.DeletionTimestamp.IsZero() { 89 | if err := r.reconcileGameServerCreating(ctx, gs); err != nil { 90 | return ctrl.Result{}, err 91 | } 92 | } 93 | break 94 | case singularityv1.GameServerStateStarting: 95 | break 96 | case singularityv1.GameServerStateRequestReady: 97 | if err := r.reconcileGameServerRequestReady(ctx, gs); err != nil { 98 | return ctrl.Result{}, err 99 | } 100 | break 101 | case singularityv1.GameServerStateShutdown: 102 | if err := r.reconcileGameServerShutdown(ctx, gs); err != nil { 103 | return ctrl.Result{}, err 104 | } 105 | break 106 | case "": 107 | if err := r.reconcileGameServerState(ctx, gs); err != nil { 108 | return ctrl.Result{}, err 109 | } 110 | break 111 | } 112 | 113 | if err := r.reconcileGameServerInstances(ctx, gs); err != nil { 114 | return ctrl.Result{}, err 115 | } 116 | 117 | return ctrl.Result{}, nil 118 | } 119 | 120 | // SetupWithManager sets up the controller with the Manager. 121 | func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error { 122 | return ctrl.NewControllerManagedBy(mgr). 123 | For(&singularityv1.GameServer{}). 124 | Owns(&singularityv1.GameServerInstance{}). 125 | WithLogConstructor(func(req *reconcile.Request) logr.Logger { 126 | if req != nil { 127 | return r.Log.WithValues("req", req) 128 | } 129 | return r.Log 130 | }). 131 | Complete(r) 132 | } 133 | 134 | func (r *Reconciler) reconcileGameServerDeletion(ctx context.Context, gs *singularityv1.GameServer) error { 135 | if gs.ObjectMeta.DeletionTimestamp.IsZero() { 136 | return nil 137 | } 138 | 139 | l := log.FromContext(ctx) 140 | l.Info("reconcile: deletion timestamp") 141 | 142 | pod, err := r.getGameServerPod(ctx, gs) 143 | if pod != nil { 144 | // We only need to delete the Pod once 145 | if pod.ObjectMeta.DeletionTimestamp.IsZero() { 146 | if err = r.Delete(ctx, pod); err != nil { 147 | return err 148 | } 149 | r.Recorder.Eventf(gs, v1.EventTypeNormal, string(gs.Status.State), "Deleting Pod %s", pod.ObjectMeta.Name) 150 | } 151 | 152 | return nil 153 | } 154 | 155 | // TODO: Delete ServiceAccount, Roles, etc 156 | // TODO: Remove finalizers 157 | 158 | return nil 159 | } 160 | 161 | func (r *Reconciler) reconcileGameServerCreating(ctx context.Context, gs *singularityv1.GameServer) error { 162 | _, err := r.getGameServerPod(ctx, gs) 163 | if k8serrors.IsNotFound(err) { 164 | // Only create resources if the backing Pod doesn't exist 165 | // TODO: Perhaps check if Role, ServiceAccount, and RoleBinding also exist? 166 | if err = r.createGameServerResources(ctx, gs); err != nil { 167 | return err 168 | } 169 | } 170 | 171 | gsCopy := gs.DeepCopy() 172 | gsCopy.Status.State = singularityv1.GameServerStateStarting 173 | if err = r.Status().Update(ctx, gsCopy); err != nil { 174 | return errors.Wrapf(err, "error updating GameServer %s to Starting state", gs.Name) 175 | } 176 | return nil 177 | } 178 | 179 | func (r *Reconciler) reconcileGameServerRequestReady(ctx context.Context, gs *singularityv1.GameServer) error { 180 | // TODO: Track ready container ID, etc 181 | 182 | gsCopy := gs.DeepCopy() 183 | gsCopy.Status.State = singularityv1.GameServerStateReady 184 | if err := r.Status().Update(ctx, gsCopy); err != nil { 185 | return errors.Wrapf(err, "error updating GameServer %s to Ready state", gs.Name) 186 | } 187 | return nil 188 | } 189 | 190 | func (r *Reconciler) reconcileGameServerShutdown(ctx context.Context, gs *singularityv1.GameServer) error { 191 | if err := r.Delete(ctx, gs, client.PropagationPolicy(metav1.DeletePropagationBackground)); err != nil { 192 | return errors.Wrapf(err, "error deleting GameServer %s", gs.Name) 193 | } 194 | 195 | r.Recorder.Event(gs, v1.EventTypeNormal, string(gs.Status.State), "Deletion started") 196 | 197 | return nil 198 | } 199 | 200 | func (r *Reconciler) reconcileGameServerState(ctx context.Context, gs *singularityv1.GameServer) error { 201 | // TODO: Is this the correct way to default state? 202 | gsCopy := gs.DeepCopy() 203 | gsCopy.Status.State = singularityv1.GameServerStateCreating 204 | 205 | if err := r.Status().Update(ctx, gsCopy); err != nil { 206 | return errors.Wrapf(err, "error updating GameServer %s to Creating state", gs.Name) 207 | } 208 | 209 | return nil 210 | } 211 | 212 | // getGameServerPod returns the Pod associated with the GameServer 213 | func (r *Reconciler) getGameServerPod(ctx context.Context, gs *singularityv1.GameServer) (*v1.Pod, error) { 214 | var pod v1.Pod 215 | key := client.ObjectKey{ 216 | Namespace: gs.ObjectMeta.Namespace, 217 | Name: gs.ObjectMeta.Name, 218 | } 219 | if err := r.Get(ctx, key, &pod); err != nil { 220 | // The Pod is not found 221 | return nil, err 222 | } 223 | 224 | // Check if the Pod is actually controlled by this GameServer 225 | if !metav1.IsControlledBy(&pod, gs) { 226 | return nil, k8serrors.NewNotFound(v1.Resource("pod"), gs.ObjectMeta.Name) 227 | } 228 | 229 | return &pod, nil 230 | } 231 | 232 | func (r *Reconciler) createGameServerResources(ctx context.Context, gs *singularityv1.GameServer) error { 233 | l := log.FromContext(ctx) 234 | 235 | // TODO: Make it possible for gameservers to specify their own role. This would be useful for proxies. 236 | role := gs.Role() 237 | serviceAccount := gs.ServiceAccount() 238 | roleBinding := gs.RoleBinding() 239 | pod := gs.Pod() 240 | 241 | l.Info("reconcile: creating role") 242 | err := r.Create(ctx, role) 243 | if err != nil && !k8serrors.IsAlreadyExists(err) { 244 | // TODO: Record event 245 | l.Error(err, "reconcile: error creating role", "role", role) 246 | return errors.Wrapf(err, "error creating Role for GameServer %s", gs.ObjectMeta.Name) 247 | } 248 | 249 | r.Recorder.Event(gs, v1.EventTypeNormal, string(gs.Status.State), fmt.Sprintf("Role %s created", role.ObjectMeta.Name)) 250 | 251 | l.Info("reconcile: creating serviceaccount") 252 | err = r.Create(ctx, serviceAccount) 253 | if err != nil && !k8serrors.IsAlreadyExists(err) { 254 | // TODO: Record event 255 | l.Error(err, "reconcile: error creating serviceaccount", "serviceaccount", serviceAccount) 256 | return errors.Wrapf(err, "error creating ServiceAccount for GameServer %s", gs.ObjectMeta.Name) 257 | } 258 | 259 | r.Recorder.Event(gs, v1.EventTypeNormal, string(gs.Status.State), fmt.Sprintf("ServiceAccount %s created", serviceAccount.ObjectMeta.Name)) 260 | 261 | l.Info("reconcile: creating rolebinding") 262 | err = r.Create(ctx, roleBinding) 263 | if err != nil && !k8serrors.IsAlreadyExists(err) { 264 | // TODO: Record event 265 | l.Error(err, "reconcile: error creating rolebinding", "rolebinding", roleBinding) 266 | return errors.Wrapf(err, "error creating RoleBinding for GameServer %s", gs.ObjectMeta.Name) 267 | } 268 | 269 | r.Recorder.Event(gs, v1.EventTypeNormal, string(gs.Status.State), fmt.Sprintf("RoleBinding %s created", roleBinding.ObjectMeta.Name)) 270 | 271 | l.Info("reconcile: creating pod") 272 | err = r.Create(ctx, pod) 273 | if err != nil && !k8serrors.IsAlreadyExists(err) { 274 | // TODO: Record event 275 | l.Error(err, "reconcile: error creating pod", "pod", pod) 276 | return errors.Wrapf(err, "error creating Pod for GameServer %s", gs.ObjectMeta.Name) 277 | } 278 | 279 | r.Recorder.Event(gs, v1.EventTypeNormal, string(gs.Status.State), fmt.Sprintf("Pod %s created", pod.ObjectMeta.Name)) 280 | 281 | // TODO: network policy 282 | 283 | return nil 284 | } 285 | 286 | // getGameServerInstance returns the GameServerInstance associated with the GameServer 287 | func (r *Reconciler) getGameServerInstance(ctx context.Context, gs *singularityv1.GameServer, id int) (*singularityv1.GameServerInstance, error) { 288 | var gsInstance singularityv1.GameServerInstance 289 | key := client.ObjectKey{ 290 | Namespace: gs.ObjectMeta.Namespace, 291 | Name: fmt.Sprintf("%s-%d", gs.ObjectMeta.Name, id), 292 | } 293 | if err := r.Get(ctx, key, &gsInstance); err != nil { 294 | // The GameServerInstance is not found 295 | return nil, err 296 | } 297 | 298 | // Check if the Pod is actually controlled by this GameServer 299 | if !metav1.IsControlledBy(&gsInstance, gs) { 300 | return nil, k8serrors.NewNotFound(v1.Resource("gameserver"), gs.ObjectMeta.Name) 301 | } 302 | 303 | return &gsInstance, nil 304 | } 305 | 306 | func (r *Reconciler) reconcileGameServerInstances(ctx context.Context, gs *singularityv1.GameServer) error { 307 | l := log.FromContext(ctx) 308 | 309 | instances := int(gs.Spec.Instances) 310 | for i := 0; i < instances; i++ { 311 | instance, err := r.getGameServerInstance(ctx, gs, i) 312 | if err != nil && !k8serrors.IsNotFound(err) { 313 | return err 314 | } 315 | 316 | if instance == nil { 317 | gsInstance := gs.GameServerInstance(i) 318 | 319 | l.Info("reconcile: creating gameserverinstance", "id", i) 320 | err = r.Create(ctx, gsInstance) 321 | if err != nil && !k8serrors.IsAlreadyExists(err) { 322 | l.Error(err, "reconcile: error creating gameserverinstance", "gameserverinstance", gsInstance) 323 | return errors.Wrapf(err, "error creating GameServerInstance for GameServer %s", gs.ObjectMeta.Name) 324 | } 325 | } 326 | } 327 | 328 | return nil 329 | } 330 | -------------------------------------------------------------------------------- /pkg/apis/singularity/v1/zz_generated.deepcopy.go: -------------------------------------------------------------------------------- 1 | //go:build !ignore_autogenerated 2 | // +build !ignore_autogenerated 3 | 4 | // Code generated by controller-gen. DO NOT EDIT. 5 | 6 | package v1 7 | 8 | import ( 9 | runtime "k8s.io/apimachinery/pkg/runtime" 10 | ) 11 | 12 | // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. 13 | func (in *Fleet) DeepCopyInto(out *Fleet) { 14 | *out = *in 15 | out.TypeMeta = in.TypeMeta 16 | in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) 17 | in.Spec.DeepCopyInto(&out.Spec) 18 | out.Status = in.Status 19 | } 20 | 21 | // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Fleet. 22 | func (in *Fleet) DeepCopy() *Fleet { 23 | if in == nil { 24 | return nil 25 | } 26 | out := new(Fleet) 27 | in.DeepCopyInto(out) 28 | return out 29 | } 30 | 31 | // DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. 32 | func (in *Fleet) DeepCopyObject() runtime.Object { 33 | if c := in.DeepCopy(); c != nil { 34 | return c 35 | } 36 | return nil 37 | } 38 | 39 | // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. 40 | func (in *FleetList) DeepCopyInto(out *FleetList) { 41 | *out = *in 42 | out.TypeMeta = in.TypeMeta 43 | in.ListMeta.DeepCopyInto(&out.ListMeta) 44 | if in.Items != nil { 45 | in, out := &in.Items, &out.Items 46 | *out = make([]Fleet, len(*in)) 47 | for i := range *in { 48 | (*in)[i].DeepCopyInto(&(*out)[i]) 49 | } 50 | } 51 | } 52 | 53 | // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new FleetList. 54 | func (in *FleetList) DeepCopy() *FleetList { 55 | if in == nil { 56 | return nil 57 | } 58 | out := new(FleetList) 59 | in.DeepCopyInto(out) 60 | return out 61 | } 62 | 63 | // DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. 64 | func (in *FleetList) DeepCopyObject() runtime.Object { 65 | if c := in.DeepCopy(); c != nil { 66 | return c 67 | } 68 | return nil 69 | } 70 | 71 | // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. 72 | func (in *FleetSpec) DeepCopyInto(out *FleetSpec) { 73 | *out = *in 74 | in.Strategy.DeepCopyInto(&out.Strategy) 75 | in.Template.DeepCopyInto(&out.Template) 76 | } 77 | 78 | // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new FleetSpec. 79 | func (in *FleetSpec) DeepCopy() *FleetSpec { 80 | if in == nil { 81 | return nil 82 | } 83 | out := new(FleetSpec) 84 | in.DeepCopyInto(out) 85 | return out 86 | } 87 | 88 | // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. 89 | func (in *FleetStatus) DeepCopyInto(out *FleetStatus) { 90 | *out = *in 91 | } 92 | 93 | // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new FleetStatus. 94 | func (in *FleetStatus) DeepCopy() *FleetStatus { 95 | if in == nil { 96 | return nil 97 | } 98 | out := new(FleetStatus) 99 | in.DeepCopyInto(out) 100 | return out 101 | } 102 | 103 | // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. 104 | func (in *GameServer) DeepCopyInto(out *GameServer) { 105 | *out = *in 106 | out.TypeMeta = in.TypeMeta 107 | in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) 108 | in.Spec.DeepCopyInto(&out.Spec) 109 | out.Status = in.Status 110 | } 111 | 112 | // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new GameServer. 113 | func (in *GameServer) DeepCopy() *GameServer { 114 | if in == nil { 115 | return nil 116 | } 117 | out := new(GameServer) 118 | in.DeepCopyInto(out) 119 | return out 120 | } 121 | 122 | // DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. 123 | func (in *GameServer) DeepCopyObject() runtime.Object { 124 | if c := in.DeepCopy(); c != nil { 125 | return c 126 | } 127 | return nil 128 | } 129 | 130 | // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. 131 | func (in *GameServerDrainStrategy) DeepCopyInto(out *GameServerDrainStrategy) { 132 | *out = *in 133 | } 134 | 135 | // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new GameServerDrainStrategy. 136 | func (in *GameServerDrainStrategy) DeepCopy() *GameServerDrainStrategy { 137 | if in == nil { 138 | return nil 139 | } 140 | out := new(GameServerDrainStrategy) 141 | in.DeepCopyInto(out) 142 | return out 143 | } 144 | 145 | // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. 146 | func (in *GameServerInstance) DeepCopyInto(out *GameServerInstance) { 147 | *out = *in 148 | out.TypeMeta = in.TypeMeta 149 | in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) 150 | out.Spec = in.Spec 151 | out.Status = in.Status 152 | } 153 | 154 | // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new GameServerInstance. 155 | func (in *GameServerInstance) DeepCopy() *GameServerInstance { 156 | if in == nil { 157 | return nil 158 | } 159 | out := new(GameServerInstance) 160 | in.DeepCopyInto(out) 161 | return out 162 | } 163 | 164 | // DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. 165 | func (in *GameServerInstance) DeepCopyObject() runtime.Object { 166 | if c := in.DeepCopy(); c != nil { 167 | return c 168 | } 169 | return nil 170 | } 171 | 172 | // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. 173 | func (in *GameServerInstanceList) DeepCopyInto(out *GameServerInstanceList) { 174 | *out = *in 175 | out.TypeMeta = in.TypeMeta 176 | in.ListMeta.DeepCopyInto(&out.ListMeta) 177 | if in.Items != nil { 178 | in, out := &in.Items, &out.Items 179 | *out = make([]GameServerInstance, len(*in)) 180 | for i := range *in { 181 | (*in)[i].DeepCopyInto(&(*out)[i]) 182 | } 183 | } 184 | } 185 | 186 | // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new GameServerInstanceList. 187 | func (in *GameServerInstanceList) DeepCopy() *GameServerInstanceList { 188 | if in == nil { 189 | return nil 190 | } 191 | out := new(GameServerInstanceList) 192 | in.DeepCopyInto(out) 193 | return out 194 | } 195 | 196 | // DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. 197 | func (in *GameServerInstanceList) DeepCopyObject() runtime.Object { 198 | if c := in.DeepCopy(); c != nil { 199 | return c 200 | } 201 | return nil 202 | } 203 | 204 | // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. 205 | func (in *GameServerInstanceSpec) DeepCopyInto(out *GameServerInstanceSpec) { 206 | *out = *in 207 | } 208 | 209 | // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new GameServerInstanceSpec. 210 | func (in *GameServerInstanceSpec) DeepCopy() *GameServerInstanceSpec { 211 | if in == nil { 212 | return nil 213 | } 214 | out := new(GameServerInstanceSpec) 215 | in.DeepCopyInto(out) 216 | return out 217 | } 218 | 219 | // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. 220 | func (in *GameServerInstanceStatus) DeepCopyInto(out *GameServerInstanceStatus) { 221 | *out = *in 222 | } 223 | 224 | // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new GameServerInstanceStatus. 225 | func (in *GameServerInstanceStatus) DeepCopy() *GameServerInstanceStatus { 226 | if in == nil { 227 | return nil 228 | } 229 | out := new(GameServerInstanceStatus) 230 | in.DeepCopyInto(out) 231 | return out 232 | } 233 | 234 | // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. 235 | func (in *GameServerInstanceTemplate) DeepCopyInto(out *GameServerInstanceTemplate) { 236 | *out = *in 237 | in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) 238 | out.Spec = in.Spec 239 | } 240 | 241 | // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new GameServerInstanceTemplate. 242 | func (in *GameServerInstanceTemplate) DeepCopy() *GameServerInstanceTemplate { 243 | if in == nil { 244 | return nil 245 | } 246 | out := new(GameServerInstanceTemplate) 247 | in.DeepCopyInto(out) 248 | return out 249 | } 250 | 251 | // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. 252 | func (in *GameServerList) DeepCopyInto(out *GameServerList) { 253 | *out = *in 254 | out.TypeMeta = in.TypeMeta 255 | in.ListMeta.DeepCopyInto(&out.ListMeta) 256 | if in.Items != nil { 257 | in, out := &in.Items, &out.Items 258 | *out = make([]GameServer, len(*in)) 259 | for i := range *in { 260 | (*in)[i].DeepCopyInto(&(*out)[i]) 261 | } 262 | } 263 | } 264 | 265 | // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new GameServerList. 266 | func (in *GameServerList) DeepCopy() *GameServerList { 267 | if in == nil { 268 | return nil 269 | } 270 | out := new(GameServerList) 271 | in.DeepCopyInto(out) 272 | return out 273 | } 274 | 275 | // DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. 276 | func (in *GameServerList) DeepCopyObject() runtime.Object { 277 | if c := in.DeepCopy(); c != nil { 278 | return c 279 | } 280 | return nil 281 | } 282 | 283 | // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. 284 | func (in *GameServerPort) DeepCopyInto(out *GameServerPort) { 285 | *out = *in 286 | } 287 | 288 | // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new GameServerPort. 289 | func (in *GameServerPort) DeepCopy() *GameServerPort { 290 | if in == nil { 291 | return nil 292 | } 293 | out := new(GameServerPort) 294 | in.DeepCopyInto(out) 295 | return out 296 | } 297 | 298 | // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. 299 | func (in *GameServerSet) DeepCopyInto(out *GameServerSet) { 300 | *out = *in 301 | out.TypeMeta = in.TypeMeta 302 | in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) 303 | in.Spec.DeepCopyInto(&out.Spec) 304 | out.Status = in.Status 305 | } 306 | 307 | // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new GameServerSet. 308 | func (in *GameServerSet) DeepCopy() *GameServerSet { 309 | if in == nil { 310 | return nil 311 | } 312 | out := new(GameServerSet) 313 | in.DeepCopyInto(out) 314 | return out 315 | } 316 | 317 | // DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. 318 | func (in *GameServerSet) DeepCopyObject() runtime.Object { 319 | if c := in.DeepCopy(); c != nil { 320 | return c 321 | } 322 | return nil 323 | } 324 | 325 | // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. 326 | func (in *GameServerSetList) DeepCopyInto(out *GameServerSetList) { 327 | *out = *in 328 | out.TypeMeta = in.TypeMeta 329 | in.ListMeta.DeepCopyInto(&out.ListMeta) 330 | if in.Items != nil { 331 | in, out := &in.Items, &out.Items 332 | *out = make([]GameServerSet, len(*in)) 333 | for i := range *in { 334 | (*in)[i].DeepCopyInto(&(*out)[i]) 335 | } 336 | } 337 | } 338 | 339 | // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new GameServerSetList. 340 | func (in *GameServerSetList) DeepCopy() *GameServerSetList { 341 | if in == nil { 342 | return nil 343 | } 344 | out := new(GameServerSetList) 345 | in.DeepCopyInto(out) 346 | return out 347 | } 348 | 349 | // DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. 350 | func (in *GameServerSetList) DeepCopyObject() runtime.Object { 351 | if c := in.DeepCopy(); c != nil { 352 | return c 353 | } 354 | return nil 355 | } 356 | 357 | // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. 358 | func (in *GameServerSetSpec) DeepCopyInto(out *GameServerSetSpec) { 359 | *out = *in 360 | in.Template.DeepCopyInto(&out.Template) 361 | } 362 | 363 | // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new GameServerSetSpec. 364 | func (in *GameServerSetSpec) DeepCopy() *GameServerSetSpec { 365 | if in == nil { 366 | return nil 367 | } 368 | out := new(GameServerSetSpec) 369 | in.DeepCopyInto(out) 370 | return out 371 | } 372 | 373 | // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. 374 | func (in *GameServerSetStatus) DeepCopyInto(out *GameServerSetStatus) { 375 | *out = *in 376 | } 377 | 378 | // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new GameServerSetStatus. 379 | func (in *GameServerSetStatus) DeepCopy() *GameServerSetStatus { 380 | if in == nil { 381 | return nil 382 | } 383 | out := new(GameServerSetStatus) 384 | in.DeepCopyInto(out) 385 | return out 386 | } 387 | 388 | // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. 389 | func (in *GameServerSetTemplate) DeepCopyInto(out *GameServerSetTemplate) { 390 | *out = *in 391 | in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) 392 | in.Spec.DeepCopyInto(&out.Spec) 393 | } 394 | 395 | // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new GameServerSetTemplate. 396 | func (in *GameServerSetTemplate) DeepCopy() *GameServerSetTemplate { 397 | if in == nil { 398 | return nil 399 | } 400 | out := new(GameServerSetTemplate) 401 | in.DeepCopyInto(out) 402 | return out 403 | } 404 | 405 | // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. 406 | func (in *GameServerSpec) DeepCopyInto(out *GameServerSpec) { 407 | *out = *in 408 | out.DrainStrategy = in.DrainStrategy 409 | if in.Ports != nil { 410 | in, out := &in.Ports, &out.Ports 411 | *out = make([]GameServerPort, len(*in)) 412 | copy(*out, *in) 413 | } 414 | in.InstanceTemplate.DeepCopyInto(&out.InstanceTemplate) 415 | in.Template.DeepCopyInto(&out.Template) 416 | } 417 | 418 | // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new GameServerSpec. 419 | func (in *GameServerSpec) DeepCopy() *GameServerSpec { 420 | if in == nil { 421 | return nil 422 | } 423 | out := new(GameServerSpec) 424 | in.DeepCopyInto(out) 425 | return out 426 | } 427 | 428 | // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. 429 | func (in *GameServerStatus) DeepCopyInto(out *GameServerStatus) { 430 | *out = *in 431 | } 432 | 433 | // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new GameServerStatus. 434 | func (in *GameServerStatus) DeepCopy() *GameServerStatus { 435 | if in == nil { 436 | return nil 437 | } 438 | out := new(GameServerStatus) 439 | in.DeepCopyInto(out) 440 | return out 441 | } 442 | 443 | // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. 444 | func (in *GameServerTemplate) DeepCopyInto(out *GameServerTemplate) { 445 | *out = *in 446 | in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) 447 | in.Spec.DeepCopyInto(&out.Spec) 448 | } 449 | 450 | // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new GameServerTemplate. 451 | func (in *GameServerTemplate) DeepCopy() *GameServerTemplate { 452 | if in == nil { 453 | return nil 454 | } 455 | out := new(GameServerTemplate) 456 | in.DeepCopyInto(out) 457 | return out 458 | } 459 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | GNU AFFERO GENERAL PUBLIC LICENSE 2 | Version 3, 19 November 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU Affero General Public License is a free, copyleft license for 11 | software and other kinds of works, specifically designed to ensure 12 | cooperation with the community in the case of network server software. 13 | 14 | The licenses for most software and other practical works are designed 15 | to take away your freedom to share and change the works. By contrast, 16 | our General Public Licenses are intended to guarantee your freedom to 17 | share and change all versions of a program--to make sure it remains free 18 | software for all its users. 19 | 20 | When we speak of free software, we are referring to freedom, not 21 | price. Our General Public Licenses are designed to make sure that you 22 | have the freedom to distribute copies of free software (and charge for 23 | them if you wish), that you receive source code or can get it if you 24 | want it, that you can change the software or use pieces of it in new 25 | free programs, and that you know you can do these things. 26 | 27 | Developers that use our General Public Licenses protect your rights 28 | with two steps: (1) assert copyright on the software, and (2) offer 29 | you this License which gives you legal permission to copy, distribute 30 | and/or modify the software. 31 | 32 | A secondary benefit of defending all users' freedom is that 33 | improvements made in alternate versions of the program, if they 34 | receive widespread use, become available for other developers to 35 | incorporate. Many developers of free software are heartened and 36 | encouraged by the resulting cooperation. However, in the case of 37 | software used on network servers, this result may fail to come about. 38 | The GNU General Public License permits making a modified version and 39 | letting the public access it on a server without ever releasing its 40 | source code to the public. 41 | 42 | The GNU Affero General Public License is designed specifically to 43 | ensure that, in such cases, the modified source code becomes available 44 | to the community. It requires the operator of a network server to 45 | provide the source code of the modified version running there to the 46 | users of that server. Therefore, public use of a modified version, on 47 | a publicly accessible server, gives the public access to the source 48 | code of the modified version. 49 | 50 | An older license, called the Affero General Public License and 51 | published by Affero, was designed to accomplish similar goals. This is 52 | a different license, not a version of the Affero GPL, but Affero has 53 | released a new version of the Affero GPL which permits relicensing under 54 | this license. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | TERMS AND CONDITIONS 60 | 61 | 0. Definitions. 62 | 63 | "This License" refers to version 3 of the GNU Affero General Public License. 64 | 65 | "Copyright" also means copyright-like laws that apply to other kinds of 66 | works, such as semiconductor masks. 67 | 68 | "The Program" refers to any copyrightable work licensed under this 69 | License. Each licensee is addressed as "you". "Licensees" and 70 | "recipients" may be individuals or organizations. 71 | 72 | To "modify" a work means to copy from or adapt all or part of the work 73 | in a fashion requiring copyright permission, other than the making of an 74 | exact copy. The resulting work is called a "modified version" of the 75 | earlier work or a work "based on" the earlier work. 76 | 77 | A "covered work" means either the unmodified Program or a work based 78 | on the Program. 79 | 80 | To "propagate" a work means to do anything with it that, without 81 | permission, would make you directly or secondarily liable for 82 | infringement under applicable copyright law, except executing it on a 83 | computer or modifying a private copy. Propagation includes copying, 84 | distribution (with or without modification), making available to the 85 | public, and in some countries other activities as well. 86 | 87 | To "convey" a work means any kind of propagation that enables other 88 | parties to make or receive copies. Mere interaction with a user through 89 | a computer network, with no transfer of a copy, is not conveying. 90 | 91 | An interactive user interface displays "Appropriate Legal Notices" 92 | to the extent that it includes a convenient and prominently visible 93 | feature that (1) displays an appropriate copyright notice, and (2) 94 | tells the user that there is no warranty for the work (except to the 95 | extent that warranties are provided), that licensees may convey the 96 | work under this License, and how to view a copy of this License. If 97 | the interface presents a list of user commands or options, such as a 98 | menu, a prominent item in the list meets this criterion. 99 | 100 | 1. Source Code. 101 | 102 | The "source code" for a work means the preferred form of the work 103 | for making modifications to it. "Object code" means any non-source 104 | form of a work. 105 | 106 | A "Standard Interface" means an interface that either is an official 107 | standard defined by a recognized standards body, or, in the case of 108 | interfaces specified for a particular programming language, one that 109 | is widely used among developers working in that language. 110 | 111 | The "System Libraries" of an executable work include anything, other 112 | than the work as a whole, that (a) is included in the normal form of 113 | packaging a Major Component, but which is not part of that Major 114 | Component, and (b) serves only to enable use of the work with that 115 | Major Component, or to implement a Standard Interface for which an 116 | implementation is available to the public in source code form. A 117 | "Major Component", in this context, means a major essential component 118 | (kernel, window system, and so on) of the specific operating system 119 | (if any) on which the executable work runs, or a compiler used to 120 | produce the work, or an object code interpreter used to run it. 121 | 122 | The "Corresponding Source" for a work in object code form means all 123 | the source code needed to generate, install, and (for an executable 124 | work) run the object code and to modify the work, including scripts to 125 | control those activities. However, it does not include the work's 126 | System Libraries, or general-purpose tools or generally available free 127 | programs which are used unmodified in performing those activities but 128 | which are not part of the work. For example, Corresponding Source 129 | includes interface definition files associated with source files for 130 | the work, and the source code for shared libraries and dynamically 131 | linked subprograms that the work is specifically designed to require, 132 | such as by intimate data communication or control flow between those 133 | subprograms and other parts of the work. 134 | 135 | The Corresponding Source need not include anything that users 136 | can regenerate automatically from other parts of the Corresponding 137 | Source. 138 | 139 | The Corresponding Source for a work in source code form is that 140 | same work. 141 | 142 | 2. Basic Permissions. 143 | 144 | All rights granted under this License are granted for the term of 145 | copyright on the Program, and are irrevocable provided the stated 146 | conditions are met. This License explicitly affirms your unlimited 147 | permission to run the unmodified Program. The output from running a 148 | covered work is covered by this License only if the output, given its 149 | content, constitutes a covered work. This License acknowledges your 150 | rights of fair use or other equivalent, as provided by copyright law. 151 | 152 | You may make, run and propagate covered works that you do not 153 | convey, without conditions so long as your license otherwise remains 154 | in force. You may convey covered works to others for the sole purpose 155 | of having them make modifications exclusively for you, or provide you 156 | with facilities for running those works, provided that you comply with 157 | the terms of this License in conveying all material for which you do 158 | not control copyright. Those thus making or running the covered works 159 | for you must do so exclusively on your behalf, under your direction 160 | and control, on terms that prohibit them from making any copies of 161 | your copyrighted material outside their relationship with you. 162 | 163 | Conveying under any other circumstances is permitted solely under 164 | the conditions stated below. Sublicensing is not allowed; section 10 165 | makes it unnecessary. 166 | 167 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 168 | 169 | No covered work shall be deemed part of an effective technological 170 | measure under any applicable law fulfilling obligations under article 171 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 172 | similar laws prohibiting or restricting circumvention of such 173 | measures. 174 | 175 | When you convey a covered work, you waive any legal power to forbid 176 | circumvention of technological measures to the extent such circumvention 177 | is effected by exercising rights under this License with respect to 178 | the covered work, and you disclaim any intention to limit operation or 179 | modification of the work as a means of enforcing, against the work's 180 | users, your or third parties' legal rights to forbid circumvention of 181 | technological measures. 182 | 183 | 4. Conveying Verbatim Copies. 184 | 185 | You may convey verbatim copies of the Program's source code as you 186 | receive it, in any medium, provided that you conspicuously and 187 | appropriately publish on each copy an appropriate copyright notice; 188 | keep intact all notices stating that this License and any 189 | non-permissive terms added in accord with section 7 apply to the code; 190 | keep intact all notices of the absence of any warranty; and give all 191 | recipients a copy of this License along with the Program. 192 | 193 | You may charge any price or no price for each copy that you convey, 194 | and you may offer support or warranty protection for a fee. 195 | 196 | 5. Conveying Modified Source Versions. 197 | 198 | You may convey a work based on the Program, or the modifications to 199 | produce it from the Program, in the form of source code under the 200 | terms of section 4, provided that you also meet all of these conditions: 201 | 202 | a) The work must carry prominent notices stating that you modified 203 | it, and giving a relevant date. 204 | 205 | b) The work must carry prominent notices stating that it is 206 | released under this License and any conditions added under section 207 | 7. This requirement modifies the requirement in section 4 to 208 | "keep intact all notices". 209 | 210 | c) You must license the entire work, as a whole, under this 211 | License to anyone who comes into possession of a copy. This 212 | License will therefore apply, along with any applicable section 7 213 | additional terms, to the whole of the work, and all its parts, 214 | regardless of how they are packaged. This License gives no 215 | permission to license the work in any other way, but it does not 216 | invalidate such permission if you have separately received it. 217 | 218 | d) If the work has interactive user interfaces, each must display 219 | Appropriate Legal Notices; however, if the Program has interactive 220 | interfaces that do not display Appropriate Legal Notices, your 221 | work need not make them do so. 222 | 223 | A compilation of a covered work with other separate and independent 224 | works, which are not by their nature extensions of the covered work, 225 | and which are not combined with it such as to form a larger program, 226 | in or on a volume of a storage or distribution medium, is called an 227 | "aggregate" if the compilation and its resulting copyright are not 228 | used to limit the access or legal rights of the compilation's users 229 | beyond what the individual works permit. Inclusion of a covered work 230 | in an aggregate does not cause this License to apply to the other 231 | parts of the aggregate. 232 | 233 | 6. Conveying Non-Source Forms. 234 | 235 | You may convey a covered work in object code form under the terms 236 | of sections 4 and 5, provided that you also convey the 237 | machine-readable Corresponding Source under the terms of this License, 238 | in one of these ways: 239 | 240 | a) Convey the object code in, or embodied in, a physical product 241 | (including a physical distribution medium), accompanied by the 242 | Corresponding Source fixed on a durable physical medium 243 | customarily used for software interchange. 244 | 245 | b) Convey the object code in, or embodied in, a physical product 246 | (including a physical distribution medium), accompanied by a 247 | written offer, valid for at least three years and valid for as 248 | long as you offer spare parts or customer support for that product 249 | model, to give anyone who possesses the object code either (1) a 250 | copy of the Corresponding Source for all the software in the 251 | product that is covered by this License, on a durable physical 252 | medium customarily used for software interchange, for a price no 253 | more than your reasonable cost of physically performing this 254 | conveying of source, or (2) access to copy the 255 | Corresponding Source from a network server at no charge. 256 | 257 | c) Convey individual copies of the object code with a copy of the 258 | written offer to provide the Corresponding Source. This 259 | alternative is allowed only occasionally and noncommercially, and 260 | only if you received the object code with such an offer, in accord 261 | with subsection 6b. 262 | 263 | d) Convey the object code by offering access from a designated 264 | place (gratis or for a charge), and offer equivalent access to the 265 | Corresponding Source in the same way through the same place at no 266 | further charge. You need not require recipients to copy the 267 | Corresponding Source along with the object code. If the place to 268 | copy the object code is a network server, the Corresponding Source 269 | may be on a different server (operated by you or a third party) 270 | that supports equivalent copying facilities, provided you maintain 271 | clear directions next to the object code saying where to find the 272 | Corresponding Source. Regardless of what server hosts the 273 | Corresponding Source, you remain obligated to ensure that it is 274 | available for as long as needed to satisfy these requirements. 275 | 276 | e) Convey the object code using peer-to-peer transmission, provided 277 | you inform other peers where the object code and Corresponding 278 | Source of the work are being offered to the general public at no 279 | charge under subsection 6d. 280 | 281 | A separable portion of the object code, whose source code is excluded 282 | from the Corresponding Source as a System Library, need not be 283 | included in conveying the object code work. 284 | 285 | A "User Product" is either (1) a "consumer product", which means any 286 | tangible personal property which is normally used for personal, family, 287 | or household purposes, or (2) anything designed or sold for incorporation 288 | into a dwelling. In determining whether a product is a consumer product, 289 | doubtful cases shall be resolved in favor of coverage. For a particular 290 | product received by a particular user, "normally used" refers to a 291 | typical or common use of that class of product, regardless of the status 292 | of the particular user or of the way in which the particular user 293 | actually uses, or expects or is expected to use, the product. A product 294 | is a consumer product regardless of whether the product has substantial 295 | commercial, industrial or non-consumer uses, unless such uses represent 296 | the only significant mode of use of the product. 297 | 298 | "Installation Information" for a User Product means any methods, 299 | procedures, authorization keys, or other information required to install 300 | and execute modified versions of a covered work in that User Product from 301 | a modified version of its Corresponding Source. The information must 302 | suffice to ensure that the continued functioning of the modified object 303 | code is in no case prevented or interfered with solely because 304 | modification has been made. 305 | 306 | If you convey an object code work under this section in, or with, or 307 | specifically for use in, a User Product, and the conveying occurs as 308 | part of a transaction in which the right of possession and use of the 309 | User Product is transferred to the recipient in perpetuity or for a 310 | fixed term (regardless of how the transaction is characterized), the 311 | Corresponding Source conveyed under this section must be accompanied 312 | by the Installation Information. But this requirement does not apply 313 | if neither you nor any third party retains the ability to install 314 | modified object code on the User Product (for example, the work has 315 | been installed in ROM). 316 | 317 | The requirement to provide Installation Information does not include a 318 | requirement to continue to provide support service, warranty, or updates 319 | for a work that has been modified or installed by the recipient, or for 320 | the User Product in which it has been modified or installed. Access to a 321 | network may be denied when the modification itself materially and 322 | adversely affects the operation of the network or violates the rules and 323 | protocols for communication across the network. 324 | 325 | Corresponding Source conveyed, and Installation Information provided, 326 | in accord with this section must be in a format that is publicly 327 | documented (and with an implementation available to the public in 328 | source code form), and must require no special password or key for 329 | unpacking, reading or copying. 330 | 331 | 7. Additional Terms. 332 | 333 | "Additional permissions" are terms that supplement the terms of this 334 | License by making exceptions from one or more of its conditions. 335 | Additional permissions that are applicable to the entire Program shall 336 | be treated as though they were included in this License, to the extent 337 | that they are valid under applicable law. If additional permissions 338 | apply only to part of the Program, that part may be used separately 339 | under those permissions, but the entire Program remains governed by 340 | this License without regard to the additional permissions. 341 | 342 | When you convey a copy of a covered work, you may at your option 343 | remove any additional permissions from that copy, or from any part of 344 | it. (Additional permissions may be written to require their own 345 | removal in certain cases when you modify the work.) You may place 346 | additional permissions on material, added by you to a covered work, 347 | for which you have or can give appropriate copyright permission. 348 | 349 | Notwithstanding any other provision of this License, for material you 350 | add to a covered work, you may (if authorized by the copyright holders of 351 | that material) supplement the terms of this License with terms: 352 | 353 | a) Disclaiming warranty or limiting liability differently from the 354 | terms of sections 15 and 16 of this License; or 355 | 356 | b) Requiring preservation of specified reasonable legal notices or 357 | author attributions in that material or in the Appropriate Legal 358 | Notices displayed by works containing it; or 359 | 360 | c) Prohibiting misrepresentation of the origin of that material, or 361 | requiring that modified versions of such material be marked in 362 | reasonable ways as different from the original version; or 363 | 364 | d) Limiting the use for publicity purposes of names of licensors or 365 | authors of the material; or 366 | 367 | e) Declining to grant rights under trademark law for use of some 368 | trade names, trademarks, or service marks; or 369 | 370 | f) Requiring indemnification of licensors and authors of that 371 | material by anyone who conveys the material (or modified versions of 372 | it) with contractual assumptions of liability to the recipient, for 373 | any liability that these contractual assumptions directly impose on 374 | those licensors and authors. 375 | 376 | All other non-permissive additional terms are considered "further 377 | restrictions" within the meaning of section 10. If the Program as you 378 | received it, or any part of it, contains a notice stating that it is 379 | governed by this License along with a term that is a further 380 | restriction, you may remove that term. If a license document contains 381 | a further restriction but permits relicensing or conveying under this 382 | License, you may add to a covered work material governed by the terms 383 | of that license document, provided that the further restriction does 384 | not survive such relicensing or conveying. 385 | 386 | If you add terms to a covered work in accord with this section, you 387 | must place, in the relevant source files, a statement of the 388 | additional terms that apply to those files, or a notice indicating 389 | where to find the applicable terms. 390 | 391 | Additional terms, permissive or non-permissive, may be stated in the 392 | form of a separately written license, or stated as exceptions; 393 | the above requirements apply either way. 394 | 395 | 8. Termination. 396 | 397 | You may not propagate or modify a covered work except as expressly 398 | provided under this License. Any attempt otherwise to propagate or 399 | modify it is void, and will automatically terminate your rights under 400 | this License (including any patent licenses granted under the third 401 | paragraph of section 11). 402 | 403 | However, if you cease all violation of this License, then your 404 | license from a particular copyright holder is reinstated (a) 405 | provisionally, unless and until the copyright holder explicitly and 406 | finally terminates your license, and (b) permanently, if the copyright 407 | holder fails to notify you of the violation by some reasonable means 408 | prior to 60 days after the cessation. 409 | 410 | Moreover, your license from a particular copyright holder is 411 | reinstated permanently if the copyright holder notifies you of the 412 | violation by some reasonable means, this is the first time you have 413 | received notice of violation of this License (for any work) from that 414 | copyright holder, and you cure the violation prior to 30 days after 415 | your receipt of the notice. 416 | 417 | Termination of your rights under this section does not terminate the 418 | licenses of parties who have received copies or rights from you under 419 | this License. If your rights have been terminated and not permanently 420 | reinstated, you do not qualify to receive new licenses for the same 421 | material under section 10. 422 | 423 | 9. Acceptance Not Required for Having Copies. 424 | 425 | You are not required to accept this License in order to receive or 426 | run a copy of the Program. Ancillary propagation of a covered work 427 | occurring solely as a consequence of using peer-to-peer transmission 428 | to receive a copy likewise does not require acceptance. However, 429 | nothing other than this License grants you permission to propagate or 430 | modify any covered work. These actions infringe copyright if you do 431 | not accept this License. Therefore, by modifying or propagating a 432 | covered work, you indicate your acceptance of this License to do so. 433 | 434 | 10. Automatic Licensing of Downstream Recipients. 435 | 436 | Each time you convey a covered work, the recipient automatically 437 | receives a license from the original licensors, to run, modify and 438 | propagate that work, subject to this License. You are not responsible 439 | for enforcing compliance by third parties with this License. 440 | 441 | An "entity transaction" is a transaction transferring control of an 442 | organization, or substantially all assets of one, or subdividing an 443 | organization, or merging organizations. If propagation of a covered 444 | work results from an entity transaction, each party to that 445 | transaction who receives a copy of the work also receives whatever 446 | licenses to the work the party's predecessor in interest had or could 447 | give under the previous paragraph, plus a right to possession of the 448 | Corresponding Source of the work from the predecessor in interest, if 449 | the predecessor has it or can get it with reasonable efforts. 450 | 451 | You may not impose any further restrictions on the exercise of the 452 | rights granted or affirmed under this License. For example, you may 453 | not impose a license fee, royalty, or other charge for exercise of 454 | rights granted under this License, and you may not initiate litigation 455 | (including a cross-claim or counterclaim in a lawsuit) alleging that 456 | any patent claim is infringed by making, using, selling, offering for 457 | sale, or importing the Program or any portion of it. 458 | 459 | 11. Patents. 460 | 461 | A "contributor" is a copyright holder who authorizes use under this 462 | License of the Program or a work on which the Program is based. The 463 | work thus licensed is called the contributor's "contributor version". 464 | 465 | A contributor's "essential patent claims" are all patent claims 466 | owned or controlled by the contributor, whether already acquired or 467 | hereafter acquired, that would be infringed by some manner, permitted 468 | by this License, of making, using, or selling its contributor version, 469 | but do not include claims that would be infringed only as a 470 | consequence of further modification of the contributor version. For 471 | purposes of this definition, "control" includes the right to grant 472 | patent sublicenses in a manner consistent with the requirements of 473 | this License. 474 | 475 | Each contributor grants you a non-exclusive, worldwide, royalty-free 476 | patent license under the contributor's essential patent claims, to 477 | make, use, sell, offer for sale, import and otherwise run, modify and 478 | propagate the contents of its contributor version. 479 | 480 | In the following three paragraphs, a "patent license" is any express 481 | agreement or commitment, however denominated, not to enforce a patent 482 | (such as an express permission to practice a patent or covenant not to 483 | sue for patent infringement). To "grant" such a patent license to a 484 | party means to make such an agreement or commitment not to enforce a 485 | patent against the party. 486 | 487 | If you convey a covered work, knowingly relying on a patent license, 488 | and the Corresponding Source of the work is not available for anyone 489 | to copy, free of charge and under the terms of this License, through a 490 | publicly available network server or other readily accessible means, 491 | then you must either (1) cause the Corresponding Source to be so 492 | available, or (2) arrange to deprive yourself of the benefit of the 493 | patent license for this particular work, or (3) arrange, in a manner 494 | consistent with the requirements of this License, to extend the patent 495 | license to downstream recipients. "Knowingly relying" means you have 496 | actual knowledge that, but for the patent license, your conveying the 497 | covered work in a country, or your recipient's use of the covered work 498 | in a country, would infringe one or more identifiable patents in that 499 | country that you have reason to believe are valid. 500 | 501 | If, pursuant to or in connection with a single transaction or 502 | arrangement, you convey, or propagate by procuring conveyance of, a 503 | covered work, and grant a patent license to some of the parties 504 | receiving the covered work authorizing them to use, propagate, modify 505 | or convey a specific copy of the covered work, then the patent license 506 | you grant is automatically extended to all recipients of the covered 507 | work and works based on it. 508 | 509 | A patent license is "discriminatory" if it does not include within 510 | the scope of its coverage, prohibits the exercise of, or is 511 | conditioned on the non-exercise of one or more of the rights that are 512 | specifically granted under this License. You may not convey a covered 513 | work if you are a party to an arrangement with a third party that is 514 | in the business of distributing software, under which you make payment 515 | to the third party based on the extent of your activity of conveying 516 | the work, and under which the third party grants, to any of the 517 | parties who would receive the covered work from you, a discriminatory 518 | patent license (a) in connection with copies of the covered work 519 | conveyed by you (or copies made from those copies), or (b) primarily 520 | for and in connection with specific products or compilations that 521 | contain the covered work, unless you entered into that arrangement, 522 | or that patent license was granted, prior to 28 March 2007. 523 | 524 | Nothing in this License shall be construed as excluding or limiting 525 | any implied license or other defenses to infringement that may 526 | otherwise be available to you under applicable patent law. 527 | 528 | 12. No Surrender of Others' Freedom. 529 | 530 | If conditions are imposed on you (whether by court order, agreement or 531 | otherwise) that contradict the conditions of this License, they do not 532 | excuse you from the conditions of this License. If you cannot convey a 533 | covered work so as to satisfy simultaneously your obligations under this 534 | License and any other pertinent obligations, then as a consequence you may 535 | not convey it at all. For example, if you agree to terms that obligate you 536 | to collect a royalty for further conveying from those to whom you convey 537 | the Program, the only way you could satisfy both those terms and this 538 | License would be to refrain entirely from conveying the Program. 539 | 540 | 13. Remote Network Interaction; Use with the GNU General Public License. 541 | 542 | Notwithstanding any other provision of this License, if you modify the 543 | Program, your modified version must prominently offer all users 544 | interacting with it remotely through a computer network (if your version 545 | supports such interaction) an opportunity to receive the Corresponding 546 | Source of your version by providing access to the Corresponding Source 547 | from a network server at no charge, through some standard or customary 548 | means of facilitating copying of software. This Corresponding Source 549 | shall include the Corresponding Source for any work covered by version 3 550 | of the GNU General Public License that is incorporated pursuant to the 551 | following paragraph. 552 | 553 | Notwithstanding any other provision of this License, you have 554 | permission to link or combine any covered work with a work licensed 555 | under version 3 of the GNU General Public License into a single 556 | combined work, and to convey the resulting work. The terms of this 557 | License will continue to apply to the part which is the covered work, 558 | but the work with which it is combined will remain governed by version 559 | 3 of the GNU General Public License. 560 | 561 | 14. Revised Versions of this License. 562 | 563 | The Free Software Foundation may publish revised and/or new versions of 564 | the GNU Affero General Public License from time to time. Such new versions 565 | will be similar in spirit to the present version, but may differ in detail to 566 | address new problems or concerns. 567 | 568 | Each version is given a distinguishing version number. If the 569 | Program specifies that a certain numbered version of the GNU Affero General 570 | Public License "or any later version" applies to it, you have the 571 | option of following the terms and conditions either of that numbered 572 | version or of any later version published by the Free Software 573 | Foundation. If the Program does not specify a version number of the 574 | GNU Affero General Public License, you may choose any version ever published 575 | by the Free Software Foundation. 576 | 577 | If the Program specifies that a proxy can decide which future 578 | versions of the GNU Affero General Public License can be used, that proxy's 579 | public statement of acceptance of a version permanently authorizes you 580 | to choose that version for the Program. 581 | 582 | Later license versions may give you additional or different 583 | permissions. However, no additional obligations are imposed on any 584 | author or copyright holder as a result of your choosing to follow a 585 | later version. 586 | 587 | 15. Disclaimer of Warranty. 588 | 589 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 590 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 591 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 592 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 593 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 594 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 595 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 596 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 597 | 598 | 16. Limitation of Liability. 599 | 600 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 601 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 602 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 603 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 604 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 605 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 606 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 607 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 608 | SUCH DAMAGES. 609 | 610 | 17. Interpretation of Sections 15 and 16. 611 | 612 | If the disclaimer of warranty and limitation of liability provided 613 | above cannot be given local legal effect according to their terms, 614 | reviewing courts shall apply local law that most closely approximates 615 | an absolute waiver of all civil liability in connection with the 616 | Program, unless a warranty or assumption of liability accompanies a 617 | copy of the Program in return for a fee. 618 | 619 | END OF TERMS AND CONDITIONS 620 | 621 | How to Apply These Terms to Your New Programs 622 | 623 | If you develop a new program, and you want it to be of the greatest 624 | possible use to the public, the best way to achieve this is to make it 625 | free software which everyone can redistribute and change under these terms. 626 | 627 | To do so, attach the following notices to the program. It is safest 628 | to attach them to the start of each source file to most effectively 629 | state the exclusion of warranty; and each file should have at least 630 | the "copyright" line and a pointer to where the full notice is found. 631 | 632 | 633 | Copyright (C) 634 | 635 | This program is free software: you can redistribute it and/or modify 636 | it under the terms of the GNU Affero General Public License as published 637 | by the Free Software Foundation, either version 3 of the License, or 638 | (at your option) any later version. 639 | 640 | This program is distributed in the hope that it will be useful, 641 | but WITHOUT ANY WARRANTY; without even the implied warranty of 642 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 643 | GNU Affero General Public License for more details. 644 | 645 | You should have received a copy of the GNU Affero General Public License 646 | along with this program. If not, see . 647 | 648 | Also add information on how to contact you by electronic and paper mail. 649 | 650 | If your software can interact with users remotely through a computer 651 | network, you should also make sure that it provides a way for users to 652 | get its source. For example, if your program is a web application, its 653 | interface could display a "Source" link that leads users to an archive 654 | of the code. There are many ways you could offer source, and different 655 | solutions will be better for different programs; see section 13 for the 656 | specific requirements. 657 | 658 | You should also get your employer (if you work as a programmer) or school, 659 | if any, to sign a "copyright disclaimer" for the program, if necessary. 660 | For more information on this, and how to apply and follow the GNU AGPL, see 661 | . --------------------------------------------------------------------------------