Skip to content

Commit

Permalink
feat: add labels to the maestro scheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
reinaldooli committed Nov 28, 2023
1 parent 2529a31 commit 9cb5982
Show file tree
Hide file tree
Showing 30 changed files with 1,112 additions and 782 deletions.
13 changes: 7 additions & 6 deletions e2e/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ require (
github.com/go-redis/redis/v8 v8.11.5
github.com/golang/protobuf v1.5.3
github.com/google/uuid v1.3.0
github.com/stretchr/testify v1.8.1
github.com/stretchr/testify v1.8.2
github.com/testcontainers/testcontainers-go v0.11.1
github.com/topfreegames/maestro v0.0.0-00010101000000-000000000000
go.uber.org/zap v1.18.1
google.golang.org/protobuf v1.30.0
k8s.io/api v0.22.1
k8s.io/apimachinery v0.22.1
k8s.io/api v0.23.0
k8s.io/apimachinery v0.23.0
k8s.io/apiserver v0.22.1
k8s.io/client-go v0.23.0
)
Expand Down Expand Up @@ -44,7 +44,6 @@ require (
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/googleapis/gnostic v0.5.5 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.11.3 // indirect
github.com/imdario/mergo v0.3.11 // indirect
github.com/json-iterator/go v1.1.12 // indirect
Expand Down Expand Up @@ -85,8 +84,10 @@ require (
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/klog/v2 v2.9.0 // indirect
k8s.io/utils v0.0.0-20210707171843-4b05e18ac7d9 // indirect
k8s.io/klog/v2 v2.30.0 // indirect
k8s.io/kube-openapi v0.0.0-20211115234752-e816edb12b65 // indirect
k8s.io/utils v0.0.0-20210930125809-cb0fa318a74b // indirect
sigs.k8s.io/json v0.0.0-20211020170558-c049b76a60c6 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.1.2 // indirect
sigs.k8s.io/yaml v1.2.0 // indirect
)
Expand Down
146 changes: 141 additions & 5 deletions e2e/go.sum

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion e2e/suites/create_new_scheduler_version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,8 @@ func createMinorVersionAndAssertNoPodsReplace(t *testing.T, kubeClient kubernete
},
},
},
Annotations: scheduler.Annotations,
Labels: scheduler.Labels,
}
updateResponse := &maestroApiV1.NewSchedulerVersionResponse{}

Expand Down Expand Up @@ -557,8 +559,10 @@ func createMinorVersionAndAssertNoPodsReplace(t *testing.T, kubeClient kubernete
for _, pod := range podsAfterUpdate.Items {
podsNameAfterUpdate = append(podsNameAfterUpdate, pod.Name)
}

require.ElementsMatch(t, podsNameAfterUpdate, podsNameBeforeUpdate)

require.Contains(t, getSchedulerResponse.Scheduler.Annotations, updateRequest.Annotations)
require.Contains(t, getSchedulerResponse.Scheduler.Labels, updateRequest.Labels)

require.Equal(t, expectedNewVersion, getSchedulerResponse.Scheduler.Spec.Version)
}
6 changes: 6 additions & 0 deletions e2e/suites/create_scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ func TestCreateScheduler(t *testing.T) {
Start: 40000,
End: 60000,
},
Annotations: map[string]string{
"annotation1": "value1",
},
Labels: map[string]string{
"label1": "value1",
},
}

createResponse := &maestrov1.CreateSchedulerResponse{}
Expand Down
2 changes: 0 additions & 2 deletions e2e/suites/switch_active_version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ import (
"testing"
"time"

"github.com/golang/protobuf/ptypes/duration"

_struct "github.com/golang/protobuf/ptypes/struct"
"google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/structpb"
Expand Down
19 changes: 10 additions & 9 deletions internal/adapters/runtime/kubernetes/game_room_convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,18 @@ var invalidPodWaitingStates = []string{

func convertGameRoomSpec(scheduler entities.Scheduler, gameRoomName string, gameRoomSpec game_room.Spec) (*v1.Pod, error) {
defaultAnnotations := map[string]string{safeToEvictAnnotation: safeToEvictValue}
defaultLabels := map[string]string{
maestroLabelKey: maestroLabelValue,
schedulerLabelKey: scheduler.Name,
versionLabelKey: gameRoomSpec.Version,
}

pod := &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: gameRoomName,
Namespace: scheduler.Name,
Labels: map[string]string{
maestroLabelKey: maestroLabelValue,
schedulerLabelKey: scheduler.Name,
versionLabelKey: gameRoomSpec.Version,
},
Annotations: mergeAnnotations(defaultAnnotations, scheduler.Annotations),
Name: gameRoomName,
Namespace: scheduler.Name,
Labels: mergeMaps(defaultLabels, scheduler.Labels),
Annotations: mergeMaps(defaultAnnotations, scheduler.Annotations),
},
Spec: v1.PodSpec{
TerminationGracePeriodSeconds: convertTerminationGracePeriod(gameRoomSpec),
Expand Down Expand Up @@ -379,7 +380,7 @@ func convertPod(pod *v1.Pod, nodeAddress string) (*game_room.Instance, error) {
}, nil
}

func mergeAnnotations(target map[string]string, source map[string]string) map[string]string {
func mergeMaps(target map[string]string, source map[string]string) map[string]string {
if target == nil {
return source
}
Expand Down
83 changes: 65 additions & 18 deletions internal/adapters/runtime/kubernetes/game_room_convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,16 +372,18 @@ func TestConvertContainer(t *testing.T) {

func TestConvertGameSpec(t *testing.T) {
cases := map[string]struct {
schedulerID string
scheduler entities.Scheduler
roomName string
annotations map[string]string
gameSpec game_room.Spec
expectedPod v1.Pod
withError bool
}{
"without containers": {
schedulerID: "sample",
roomName: "roomName",
scheduler: entities.Scheduler{
Name: "sample",
},
roomName: "roomName",
gameSpec: game_room.Spec{
Version: "version",
},
Expand All @@ -401,8 +403,10 @@ func TestConvertGameSpec(t *testing.T) {
},
},
"with containers": {
schedulerID: "sample",
roomName: "roomName",
scheduler: entities.Scheduler{
Name: "sample",
},
roomName: "roomName",
gameSpec: game_room.Spec{
Version: "version",
Containers: []game_room.Container{
Expand Down Expand Up @@ -432,8 +436,10 @@ func TestConvertGameSpec(t *testing.T) {
},
},
"with toleration": {
schedulerID: "sample",
roomName: "roomName",
scheduler: entities.Scheduler{
Name: "sample",
},
roomName: "roomName",
gameSpec: game_room.Spec{
Version: "version",
Toleration: "some-toleration",
Expand All @@ -459,8 +465,10 @@ func TestConvertGameSpec(t *testing.T) {
},
},
"with affinity": {
schedulerID: "sample",
roomName: "roomName",
scheduler: entities.Scheduler{
Name: "sample",
},
roomName: "roomName",
gameSpec: game_room.Spec{
Version: "version",
Affinity: "sample-affinity",
Expand All @@ -484,8 +492,13 @@ func TestConvertGameSpec(t *testing.T) {
},
},
"with annotations": {
schedulerID: "sample",
roomName: "roomName",
scheduler: entities.Scheduler{
Name: "sample",
Annotations: map[string]string{
"imageregistry": "https://hub.docker.com/",
},
},
roomName: "roomName",
gameSpec: game_room.Spec{
Version: "version",
Affinity: "sample-affinity",
Expand All @@ -509,9 +522,42 @@ func TestConvertGameSpec(t *testing.T) {
},
},
},
"with labels": {
scheduler: entities.Scheduler{
Name: "sample",
Labels: map[string]string{
"my-label": "my-label-value",
},
},
roomName: "roomName",
gameSpec: game_room.Spec{
Version: "version",
Affinity: "sample-affinity",
},
expectedPod: v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "roomName",
Namespace: "sample",
Labels: map[string]string{
maestroLabelKey: maestroLabelValue,
schedulerLabelKey: "sample",
versionLabelKey: "version",
"my-label": "my-label-value",
},
Annotations: map[string]string{
safeToEvictAnnotation: safeToEvictValue,
},
},
Spec: v1.PodSpec{
Affinity: &v1.Affinity{},
},
},
},
"with termination grace period": {
schedulerID: "sample",
roomName: "roomName",
scheduler: entities.Scheduler{
Name: "sample",
},
roomName: "roomName",
gameSpec: game_room.Spec{
Version: "version",
TerminationGracePeriod: 10 * time.Second,
Expand Down Expand Up @@ -539,11 +585,11 @@ func TestConvertGameSpec(t *testing.T) {
for name, test := range cases {
t.Run(name, func(t *testing.T) {

scheduler := entities.Scheduler{
Name: test.schedulerID,
Annotations: test.expectedPod.ObjectMeta.Annotations,
}
res, err := convertGameRoomSpec(scheduler, test.roomName, test.gameSpec)
//scheduler := entities.Scheduler{
// Name: test.scheduler,
// Annotations: test.expectedPod.ObjectMeta.Annotations,
//}
res, err := convertGameRoomSpec(test.scheduler, test.roomName, test.gameSpec)
if test.withError {
require.Error(t, err)
return
Expand All @@ -554,6 +600,7 @@ func TestConvertGameSpec(t *testing.T) {
require.Equal(t, test.expectedPod.ObjectMeta.Name, res.ObjectMeta.Name)
require.Equal(t, test.expectedPod.ObjectMeta.Namespace, res.ObjectMeta.Namespace)
require.Equal(t, test.expectedPod.ObjectMeta.Annotations, res.ObjectMeta.Annotations)
require.Equal(t, test.expectedPod.ObjectMeta.Labels, res.ObjectMeta.Labels)
require.Equal(t, len(test.expectedPod.Spec.Containers), len(res.Spec.Containers))
require.Equal(t, len(test.expectedPod.Spec.Tolerations), len(res.Spec.Tolerations))

Expand Down
3 changes: 3 additions & 0 deletions internal/adapters/storage/postgres/scheduler/db_scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ type schedulerInfo struct {
Forwarders []*forwarder.Forwarder
Autoscaling *autoscaling.Autoscaling
Annotations map[string]string
Labels map[string]string
}

func NewDBScheduler(scheduler *entities.Scheduler) *Scheduler {
Expand All @@ -77,6 +78,7 @@ func NewDBScheduler(scheduler *entities.Scheduler) *Scheduler {
Forwarders: scheduler.Forwarders,
Autoscaling: scheduler.Autoscaling,
Annotations: scheduler.Annotations,
Labels: scheduler.Labels,
}
yamlBytes, _ := yaml.Marshal(info)
return &Scheduler{
Expand All @@ -100,6 +102,7 @@ func (s *Scheduler) ToScheduler() (*entities.Scheduler, error) {
Game: s.Game,
State: s.State,
Annotations: info.Annotations,
Labels: info.Labels,
Spec: game_room.Spec{
Version: s.Version,
TerminationGracePeriod: info.TerminationGracePeriod,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ func TestScheduler_ToScheduler(t *testing.T) {
},
},
Annotations: map[string]string{"imageregistry": "https://hub.docker.com/"},
Labels: map[string]string{"scheduler": "scheduler-name"},
},
}

Expand Down
7 changes: 7 additions & 0 deletions internal/api/handlers/requestadapters/schedulers.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ func FromApiPatchSchedulerRequestToChangeMap(request *api.PatchSchedulerRequest)
patchMap[patch.LabelAnnotations] = request.GetAnnotations()
}

if request.Labels != nil {
patchMap[patch.LabelLabels] = request.GetLabels()
}

return patchMap
}

Expand Down Expand Up @@ -121,6 +125,7 @@ func FromApiCreateSchedulerRequestToEntity(request *api.CreateSchedulerRequest)
schedulerAutoscaling,
fromApiForwarders(request.GetForwarders()),
request.GetAnnotations(),
request.GetLabels(),
)
}

Expand Down Expand Up @@ -157,6 +162,7 @@ func FromApiNewSchedulerVersionRequestToEntity(request *api.NewSchedulerVersionR
schedulerAutoscaling,
fromApiForwarders(request.GetForwarders()),
request.GetAnnotations(),
request.GetLabels(),
)
}

Expand All @@ -179,6 +185,7 @@ func FromEntitySchedulerToResponse(entity *entities.Scheduler) (*api.Scheduler,
Autoscaling: getAutoscaling(entity.Autoscaling),
Forwarders: forwarders,
Annotations: entity.Annotations,
Labels: entity.Labels,
}, nil
}

Expand Down
Loading

0 comments on commit 9cb5982

Please sign in to comment.