Skip to content

Commit

Permalink
Create Controller Example (#3680)
Browse files Browse the repository at this point in the history
* Create Controller Example
* modify build/includes/examples.mk
* log gameserver status
* Custom-Controller in GameServer Implementations
* gameServer.Status.State isn't String
  • Loading branch information
Kalaiselvi84 authored Mar 11, 2024
1 parent a9cd5b5 commit 340c39a
Show file tree
Hide file tree
Showing 9 changed files with 1,164 additions and 1 deletion.
7 changes: 6 additions & 1 deletion build/includes/examples.mk
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ test-examples-on-gar: example-image-test.xonotic
test-examples-on-gar: example-image-test.crd-client
test-examples-on-gar: example-image-test.supertuxkart
test-examples-on-gar: example-image-test.simple-game-server
test-examples-on-gar: example-image-test.custom-controller

push-example-golang-images: example-image-push.allocation-endpoint
push-example-golang-images: example-image-push.autoscaler-webhook
push-example-golang-images: example-image-push.crd-client
push-example-golang-images: example-image-push.simple-game-server
push-example-golang-images: example-image-push.supertuxkart
push-example-golang-images: example-image-push.xonotic
push-example-golang-images: example-image-push.custom-controller

# Test to ensure the example image found in the % folder is on GAR. Fails if it is not.
example-image-test.%:
Expand All @@ -50,7 +52,7 @@ example-image-push.%:
$(DOCKER_RUN) bash -c "cd examples/$* && make push"

# Perform make build for golang examples
build-go-examples: build-example-allocation-endpoint build-example-autoscaler-webhook build-example-crd-client build-example-simple-game-server build-example-supertuxkart build-example-xonotic
build-go-examples: build-example-allocation-endpoint build-example-autoscaler-webhook build-example-crd-client build-example-simple-game-server build-example-supertuxkart build-example-xonotic build-example-custom-controller

# Perform make build for all examples
build-examples: build-example-allocation-endpoint build-example-autoscaler-webhook build-example-cpp-simple build-example-crd-client build-example-nodejs-simple build-example-rust-simple build-example-simple-game-server build-example-supertuxkart build-example-xonotic
Expand Down Expand Up @@ -90,3 +92,6 @@ build-example-supertuxkart:

build-example-xonotic:
$(MAKE) build-example EXAMPLE=xonotic

build-example-custom-controller:
$(MAKE) build-example EXAMPLE=custom-controller
28 changes: 28 additions & 0 deletions examples/custom-controller/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright 2024 Google LLC All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Gather dependencies and build the executable
FROM golang:1.21.6 as builder
WORKDIR /go/src/custom-controller
COPY . .

RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o client .

# Create the final image that will run the webhook server for FleetAutoscaler webhook policy
FROM gcr.io/distroless/static-debian12:nonroot

COPY --from=builder /go/src/custom-controller/client /

USER nonroot:nonroot
ENTRYPOINT ["/client"]
59 changes: 59 additions & 0 deletions examples/custom-controller/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Copyright 2024 Google LLC All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#
# Makefile for building a simple udp client and server
#

# __ __ _ _ _
# \ \ / /_ _ _ __(_) __ _| |__ | | ___ ___
# \ \ / / _` | '__| |/ _` | '_ \| |/ _ \ __|
# \ V / (_| | | | | (_| | |_) | | __\__ \
# \_/ \__,_|_| |_|\__,_|_.__/|_|\___|___/
#

REPOSITORY ?=
PROD_REPO ?= us-docker.pkg.dev/agones-images/examples
version := 0.1
ifeq ($(REPOSITORY),)
server_tag := custom-controller:$(version)
else
server_tag := $(REPOSITORY)/custom-controller:$(version)
endif

# _____ _
# |_ _|_ _ _ __ __ _ ___| |_ ___
# | |/ _` | '__/ _` |/ _ \ __/ __|
# | | (_| | | | (_| | __/ |_\__ \
# |_|\__,_|_| \__, |\___|\__|___/
# |___/

# Build a docker image for the server, and tag it
build:
docker build --tag=$(server_tag) .

push: build
docker push $(server_tag)

# check if hosted on Google Artifact Registry
gar-check:
gcloud container images describe $(PROD_REPO)/$(server_tag)

#output the tag for this image
echo-image-tag:
@echo $(PROD_REPO)/$(server_tag)

# build and push the example custom controller image with specified tag
cloud-build:
gcloud builds submit --config=cloudbuild.yaml
38 changes: 38 additions & 0 deletions examples/custom-controller/cloudbuild.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
steps:
#
# Creates the initial make + docker build platform
#
- name: ubuntu
script: |
echo 'FROM gcr.io/cloud-builders/docker\nRUN apt-get install make\nENTRYPOINT [\"/usr/bin/make\"]' > Dockerfile.build
- name: gcr.io/cloud-builders/docker
id: build-make-docker
entrypoint: docker
args: [build, -f, Dockerfile.build, -t, make-docker, .]

# build and push crd-client image to GCR
- name: make-docker
id: push
dir: .
env: ['REPOSITORY=${_REPOSITORY}']
script: |
make push
options:
dynamic_substitutions: true
substitutions:
_REPOSITORY: us-docker.pkg.dev/${PROJECT_ID}/examples
timeout: 1800s
65 changes: 65 additions & 0 deletions examples/custom-controller/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
apiVersion: v1
kind: ServiceAccount
metadata:
name: custom-controller-sa
namespace: agones-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: custom-controller-role
rules:
- apiGroups: [""]
resources: ["events"]
verbs: ["create", "patch"]
- apiGroups: ["agones.dev"]
resources: ["gameservers"]
verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: custom-controller-access
subjects:
- kind: ServiceAccount
name: custom-controller-sa
namespace: agones-system
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: custom-controller-role
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: custom-controller
namespace: agones-system
spec:
replicas: 1
selector:
matchLabels:
app: custom-controller
template:
metadata:
labels:
app: custom-controller
spec:
serviceAccountName: custom-controller-sa
containers:
- name: custom-controller
image: us-docker.pkg.dev/agones-images/examples/custom-controller:0.1
imagePullPolicy: Always
89 changes: 89 additions & 0 deletions examples/custom-controller/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
module custom-controller

go 1.21

require (
agones.dev/agones v1.38.0
github.com/go-logr/logr v1.4.1
k8s.io/apimachinery v0.29.0
sigs.k8s.io/controller-runtime v0.17.2
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/evanphx/json-patch/v5 v5.8.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-logr/zapr v1.3.0 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/spec v0.19.5 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.17.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/imdario/mergo v0.3.6 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/magiconair/properties v1.8.1 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattbaird/jsonpatch v0.0.0-20230413205102-771768614e91 // indirect
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
github.com/mitchellh/mapstructure v1.4.1 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pelletier/go-toml v1.2.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.18.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.45.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/spf13/afero v1.9.2 // indirect
github.com/spf13/cast v1.3.0 // indirect
github.com/spf13/jwalterweatherman v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.7.0 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.26.0 // indirect
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/oauth2 v0.12.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/term v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.3.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230822172742-b8732ec3820d // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d // indirect
google.golang.org/grpc v1.58.3 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/fsnotify.v1 v1.4.7 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.51.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.29.0 // indirect
k8s.io/apiextensions-apiserver v0.29.0 // indirect
k8s.io/client-go v0.29.0 // indirect
k8s.io/component-base v0.29.0 // indirect
k8s.io/klog/v2 v2.110.1 // indirect
k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 // indirect
k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)

replace sigs.k8s.io/controller-runtime v0.17.2 => sigs.k8s.io/controller-runtime v0.17.2
Loading

0 comments on commit 340c39a

Please sign in to comment.