Skip to content

Commit

Permalink
Create NodePort service for each pod created on development environment
Browse files Browse the repository at this point in the history
  • Loading branch information
Luiz Felipe Takakura authored and lftakakura committed Aug 14, 2018
1 parent df2c2cd commit 0942f62
Show file tree
Hide file tree
Showing 27 changed files with 1,672 additions and 199 deletions.
9 changes: 6 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ build-docker:

build-dev-room:
@cd dev-room && docker build -t maestro-dev-room .
@eval $(minikube docker-env -u)

delete-dev-room:
@docker rmi -f maestro-dev-room
Expand Down Expand Up @@ -73,7 +72,7 @@ run:

run-dev:
@echo "serverUrl: http://localhost:8080" > ~/.maestro/config-local.yaml
@env MAESTRO_OAUTH_ENABLED=false go run main.go start -v3
@env MAESTRO_OAUTH_ENABLED=false MAESTRO_ENVIRONMENT=development go run main.go start -v3

stop-deps:
@env MY_IP=${MY_IP} docker-compose --project-name maestro down
Expand All @@ -97,7 +96,8 @@ drop:

drop-test:
@-docker exec maestro_test_postgres_1 psql -d postgres -h localhost -p 5432 -U postgres -c "SELECT pg_terminate_backend(pid.pid) FROM pg_stat_activity, (SELECT pid FROM pg_stat_activity where pid <> pg_backend_pid()) pid WHERE datname='maestro_test';"
@docker exec maestro_test_postgres_1 psql -d postgres -h localhost -p 5432 -U postgres -f scripts/drop-test.sql > /dev/null
@docker cp scripts/drop-test.sql maestro_test_postgres_1:.
@docker exec maestro_test_postgres_1 psql -d postgres -h localhost -p 5432 -U postgres -f drop-test.sql > /dev/null
@echo "Test Database created successfully!"

unit: unit-board clear-coverage-profiles unit-run gather-unit-profiles
Expand Down Expand Up @@ -172,6 +172,9 @@ minikube-ci:
work:
@go run main.go worker -v3

work-dev:
@env MAESTRO_ENVIRONMENT=development go run main.go worker -v3

clean-int-tests:
@echo 'deleting maestro-test-* namespaces'
@kubectl --context minikube get namespace | grep maestro-test- | awk '{print $$1}' | xargs kubectl --context minikube delete namespace
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ make drop
make migrate
```

4) Start worker with `make work` and api with `make run-dev`
4) Start worker with `make work-dev` and api with `make run-dev`

5) With [maestro-cli](https://github.com/topfreegames/maestro-cli), access maestro with context `local`. For example:

Expand Down
17 changes: 12 additions & 5 deletions api/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type App struct {
RedisClient *redis.Client
KubernetesClient kubernetes.Interface
RoomAddrGetter models.AddrGetter
RoomManager models.RoomManager
Logger logrus.FieldLogger
NewRelic newrelic.Application
Router *mux.Router
Expand Down Expand Up @@ -410,7 +411,7 @@ func (a *App) configureApp(

a.configureServer(showProfile)

a.configureRoomAddrGetter()
a.configureEnvironment()

return nil
}
Expand Down Expand Up @@ -546,12 +547,18 @@ func (a *App) configureServer(showProfile bool) {
}
}

func (a *App) configureRoomAddrGetter() {
if a.Config.GetString(EnvironmentConfig) != DevEnvironment {
a.RoomAddrGetter = &models.RoomAddressesFromHostPort{}
} else {
func (a *App) configureEnvironment() {
a.RoomAddrGetter = &models.RoomAddressesFromHostPort{}
a.RoomManager = &models.GameRoom{}

if a.Config.GetString(EnvironmentConfig) == DevEnvironment {
a.RoomAddrGetter = &models.RoomAddressesFromNodePort{}
a.RoomManager = &models.GameRoomWithService{}
a.Logger.Info("development environment")
return
}

a.Logger.Info("production environment")
}

func (a *App) configureLogin() {
Expand Down
1 change: 1 addition & 0 deletions api/app_int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ var _ = Describe("App", func() {
// Update the scheduler
configYaml.AutoScaling.Min = 5
configYaml.Image = "nginx:latest"
configYaml.ImagePullPolicy = "Always"
bts, err := json.Marshal(configYaml)
Expect(err).NotTo(HaveOccurred())
bodyRdr := bytes.NewReader(bts)
Expand Down
2 changes: 2 additions & 0 deletions api/room_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func (g *RoomPingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
room := models.NewRoom(params.Name, params.Scheduler)
err := controller.SetRoomStatus(
g.App.Logger,
g.App.RoomManager,
g.App.RedisClient.Trace(r.Context()),
g.App.DBClient.WithContext(r.Context()),
mr,
Expand Down Expand Up @@ -240,6 +241,7 @@ func (g *RoomStatusHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
room := models.NewRoom(params.Name, params.Scheduler)
err := controller.SetRoomStatus(
g.App.Logger,
g.App.RoomManager,
g.App.RedisClient.Trace(r.Context()),
g.App.DBClient.WithContext(r.Context()),
mr,
Expand Down
6 changes: 3 additions & 3 deletions api/scheduler_diff_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ image: image2`

Expect(response["version1"]).To(Equal("v1.0"))
Expect(response["version2"]).To(Equal("v0.1"))
Expect(response["diff"]).To(Equal("name: scheduler-name\ngame: \"\"\nshutdownTimeout: 0\nautoscaling: null\naffinity: \"\"\ntoleration: \"\"\noccupiedTimeout: 0\nforwarders: {}\nauthorizedUsers: []\nportRange: null\nimage: image\x1b[31m2\x1b[0m\x1b[32m1\x1b[0m\nports: []\nlimits: null\nrequests: null\nenv: []\ncmd: []\ncontainers: []\n"))
Expect(response["diff"]).To(Equal("name: scheduler-name\ngame: \"\"\nshutdownTimeout: 0\nautoscaling: null\naffinity: \"\"\ntoleration: \"\"\noccupiedTimeout: 0\nforwarders: {}\nauthorizedUsers: []\nportRange: null\nimage: image\x1b[31m2\x1b[0m\x1b[32m1\x1b[0m\nimagePullPolicy: \"\"\nports: []\nlimits: null\nrequests: null\nenv: []\ncmd: []\ncontainers: []\n"))
})

It("should return schedulers diff between v2 and previous", func() {
Expand All @@ -86,7 +86,7 @@ image: image2`

Expect(response["version1"]).To(Equal("v2.0"))
Expect(response["version2"]).To(Equal("v1.0"))
Expect(response["diff"]).To(Equal("name: scheduler-name\ngame: \"\"\nshutdownTimeout: 0\nautoscaling: null\naffinity: \"\"\ntoleration: \"\"\noccupiedTimeout: 0\nforwarders: {}\nauthorizedUsers: []\nportRange: null\nimage: image\x1b[31m2\x1b[0m\x1b[32m1\x1b[0m\nports: []\nlimits: null\nrequests: null\nenv: []\ncmd: []\ncontainers: []\n"))
Expect(response["diff"]).To(Equal("name: scheduler-name\ngame: \"\"\nshutdownTimeout: 0\nautoscaling: null\naffinity: \"\"\ntoleration: \"\"\noccupiedTimeout: 0\nforwarders: {}\nauthorizedUsers: []\nportRange: null\nimage: image\x1b[31m2\x1b[0m\x1b[32m1\x1b[0m\nimagePullPolicy: \"\"\nports: []\nlimits: null\nrequests: null\nenv: []\ncmd: []\ncontainers: []\n"))
})

It("should return schedulers diff between v2 and v1", func() {
Expand All @@ -106,7 +106,7 @@ image: image2`

Expect(response["version1"]).To(Equal("v2.0"))
Expect(response["version2"]).To(Equal("v1.0"))
Expect(response["diff"]).To(Equal("name: scheduler-name\ngame: \"\"\nshutdownTimeout: 0\nautoscaling: null\naffinity: \"\"\ntoleration: \"\"\noccupiedTimeout: 0\nforwarders: {}\nauthorizedUsers: []\nportRange: null\nimage: image\x1b[31m2\x1b[0m\x1b[32m1\x1b[0m\nports: []\nlimits: null\nrequests: null\nenv: []\ncmd: []\ncontainers: []\n"))
Expect(response["diff"]).To(Equal("name: scheduler-name\ngame: \"\"\nshutdownTimeout: 0\nautoscaling: null\naffinity: \"\"\ntoleration: \"\"\noccupiedTimeout: 0\nforwarders: {}\nauthorizedUsers: []\nportRange: null\nimage: image\x1b[31m2\x1b[0m\x1b[32m1\x1b[0m\nimagePullPolicy: \"\"\nports: []\nlimits: null\nrequests: null\nenv: []\ncmd: []\ncontainers: []\n"))
})

It("should return error if version1 not found", func() {
Expand Down
7 changes: 5 additions & 2 deletions api/scheduler_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func (g *SchedulerCreateHandler) ServeHTTP(w http.ResponseWriter, r *http.Reques
timeoutSec := g.App.Config.GetInt("scaleUpTimeoutSeconds")
err := controller.CreateScheduler(
l,
g.App.RoomManager,
mr,
db,
g.App.RedisClient.Trace(r.Context()),
Expand Down Expand Up @@ -260,6 +261,7 @@ func updateSchedulerConfigCommon(
err = controller.UpdateSchedulerConfig(
r.Context(),
logger,
app.RoomManager,
mr,
db,
app.RedisClient,
Expand Down Expand Up @@ -590,6 +592,7 @@ func (g *SchedulerScaleHandler) ServeHTTP(w http.ResponseWriter, r *http.Request
db := g.App.DBClient.WithContext(r.Context())
err := controller.ScaleScheduler(
logger,
g.App.RoomManager,
mr,
db,
g.App.RedisClient.Trace(r.Context()),
Expand Down Expand Up @@ -644,7 +647,7 @@ func (g *SchedulerImageHandler) update(
status = http.StatusOK

db := g.App.DBClient.WithContext(r.Context())
err = controller.UpdateSchedulerImage(r.Context(), logger, mr, db, g.App.RedisClient, g.App.KubernetesClient,
err = controller.UpdateSchedulerImage(r.Context(), logger, g.App.RoomManager, mr, db, g.App.RedisClient, g.App.KubernetesClient,
schedulerName, imageParams, maxSurge, &clock.Clock{}, g.App.Config, operationManager)
if err != nil {
status = http.StatusInternalServerError
Expand Down Expand Up @@ -749,7 +752,7 @@ func (g *SchedulerUpdateMinHandler) update(

logger.Info("starting controllers update min")
db := g.App.DBClient.WithContext(r.Context())
err = controller.UpdateSchedulerMin(r.Context(), logger, mr, db, g.App.RedisClient, schedulerName,
err = controller.UpdateSchedulerMin(r.Context(), logger, g.App.RoomManager, mr, db, g.App.RedisClient, schedulerName,
schedulerMin, &clock.Clock{}, g.App.Config, operationManager)
if err != nil {
status = http.StatusInternalServerError
Expand Down
12 changes: 7 additions & 5 deletions api/scheduler_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ autoscaling:
It("returns a status code of 200 and success body", func() {
mockRedisTraceWrapper.EXPECT().WithContext(gomock.Any(), mockRedisClient).Return(mockRedisClient).Times(2)
MockCreateScheduler(clientset, mockRedisClient, mockPipeline, mockDb,
logger, mmr, yamlString, timeoutSec, nil, workerPortRange, portStart, portEnd)
logger, app.RoomManager, mmr, yamlString, timeoutSec, nil, workerPortRange, portStart, portEnd)

err := yaml.Unmarshal([]byte(yamlString), &configYaml)
Expect(err).NotTo(HaveOccurred())
Expand Down Expand Up @@ -991,7 +991,7 @@ autoscaling:
It("should asynchronously update scheduler", func() {
mockRedisTraceWrapper.EXPECT().WithContext(gomock.Any(), mockRedisClient).Return(mockRedisClient).AnyTimes()
MockCreateScheduler(clientset, mockRedisClient, mockPipeline, mockDb,
logger, mmr, yamlString, timeoutSec, nil, workerPortRange, portStart, portEnd)
logger, app.RoomManager, mmr, yamlString, timeoutSec, nil, workerPortRange, portStart, portEnd)

err := yaml.Unmarshal([]byte(yamlString), &configYaml)
Expect(err).NotTo(HaveOccurred())
Expand Down Expand Up @@ -1081,7 +1081,7 @@ autoscaling:
It("should asynchronously update scheduler and show error when occurred", func() {
mockRedisTraceWrapper.EXPECT().WithContext(gomock.Any(), mockRedisClient).Return(mockRedisClient).AnyTimes()
MockCreateScheduler(clientset, mockRedisClient, mockPipeline, mockDb,
logger, mmr, yamlString, timeoutSec, nil, workerPortRange, portStart, portEnd)
logger, app.RoomManager, mmr, yamlString, timeoutSec, nil, workerPortRange, portStart, portEnd)

err := yaml.Unmarshal([]byte(yamlString), &configYaml)
Expect(err).NotTo(HaveOccurred())
Expand Down Expand Up @@ -1535,6 +1535,7 @@ forwarders: {}
authorizedUsers: []
portRange: null
image: ""
imagePullPolicy: ""
ports: []
limits: null
requests: null
Expand Down Expand Up @@ -1583,6 +1584,7 @@ authorizedUsers: []
containers:
- name: container1
image: image/image
imagePullPolicy: ""
ports:
- containerPort: 8080
protocol: TCP
Expand Down Expand Up @@ -2157,7 +2159,7 @@ game: game-name
Expect(err).NotTo(HaveOccurred())

MockCreateScheduler(clientset, mockRedisClient, mockPipeline, mockDb,
logger, mmr, yamlString, timeoutSec, nil, workerPortRange, portStart, portEnd)
logger, app.RoomManager, mmr, yamlString, timeoutSec, nil, workerPortRange, portStart, portEnd)

scheduler1 = models.NewScheduler(configYaml1.Name, configYaml1.Game, yamlString)

Expand Down Expand Up @@ -2640,7 +2642,7 @@ game: game-name

mockRedisTraceWrapper.EXPECT().WithContext(gomock.Any(), mockRedisClient).Return(mockRedisClient).AnyTimes()
MockCreateScheduler(clientset, mockRedisClient, mockPipeline, mockDb,
logger, mmr, jsonString, timeoutSec, nil, workerPortRange, portStart, portEnd)
logger, app.RoomManager, mmr, jsonString, timeoutSec, nil, workerPortRange, portStart, portEnd)

err := json.Unmarshal([]byte(jsonString), &configYaml1)
Expect(err).NotTo(HaveOccurred())
Expand Down

0 comments on commit 0942f62

Please sign in to comment.