Skip to content

Commit

Permalink
Merge 833e319 into 5de2ad7
Browse files Browse the repository at this point in the history
  • Loading branch information
lftakakura authored Feb 21, 2019
2 parents 5de2ad7 + 833e319 commit ac7ffc3
Show file tree
Hide file tree
Showing 14 changed files with 467 additions and 276 deletions.
4 changes: 2 additions & 2 deletions api/app_int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ var _ = Describe("App", func() {
Expect(pod.Spec.Containers[0].Ports[0].HostPort).NotTo(BeZero())

room := models.NewRoom(pod.GetName(), ns)
err := room.Create(app.RedisClient.Client, mockDb, mmr, configYaml)
err := room.Create(app.RedisClient.Client, mockDb, mmr, sch)
Expect(err).NotTo(HaveOccurred())

pipe := app.RedisClient.Client.TxPipeline()
Expand Down Expand Up @@ -217,7 +217,7 @@ var _ = Describe("App", func() {
Expect(pod.Spec.Containers[0].Ports[0].HostPort).NotTo(BeZero())

room := models.NewRoom(pod.GetName(), ns)
err := room.Create(app.RedisClient.Client, mockDb, mmr, configYaml)
err := room.Create(app.RedisClient.Client, mockDb, mmr, sch)
Expect(err).NotTo(HaveOccurred())

pipe := app.RedisClient.Client.TxPipeline()
Expand Down
2 changes: 0 additions & 2 deletions api/room_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ func (g *RoomPingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
g.App.DBClient.WithContext(r.Context()),
mr,
g.App.KubernetesClient,
g.App.KubernetesMetricsClient,
payload.Status,
g.App.Config,
room,
Expand Down Expand Up @@ -252,7 +251,6 @@ func (g *RoomStatusHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
g.App.DBClient.WithContext(r.Context()),
mr,
g.App.KubernetesClient,
g.App.KubernetesMetricsClient,
payload.Status,
g.App.Config,
room,
Expand Down
16 changes: 8 additions & 8 deletions autoscaler/autoscaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ var _ = Describe("AutoScaler", func() {
Occupied: 8,
Terminating: 0,
}
testing.CreatePod(clientset, "1.0", "0", schedulerName, containerName)
testing.CreatePod(clientset, "1.0", "0", schedulerName, "", containerName)
})

It("should return delta", func() {
Expand All @@ -169,7 +169,7 @@ var _ = Describe("AutoScaler", func() {
},
},
)
fakeMetricsClient := testing.CreatePodsMetricsList(containerMetrics, 1, schedulerName)
fakeMetricsClient := testing.CreatePodsMetricsList(containerMetrics, []string{schedulerName}, schedulerName)
autoScaler := NewAutoScaler(schedulerName, clientset, fakeMetricsClient)

delta := autoScaler.Delta(trigger, roomCount)
Expand All @@ -188,7 +188,7 @@ var _ = Describe("AutoScaler", func() {
},
},
)
fakeMetricsClient = testing.CreatePodsMetricsList(containerMetrics, 1, schedulerName)
fakeMetricsClient = testing.CreatePodsMetricsList(containerMetrics, []string{schedulerName}, schedulerName)
autoScaler = NewAutoScaler(schedulerName, clientset, fakeMetricsClient)

delta = autoScaler.Delta(trigger, roomCount)
Expand All @@ -208,7 +208,7 @@ var _ = Describe("AutoScaler", func() {
},
},
)
fakeMetricsClient := testing.CreatePodsMetricsList(containerMetrics, 1, schedulerName)
fakeMetricsClient := testing.CreatePodsMetricsList(containerMetrics, []string{schedulerName}, schedulerName)
autoScaler := NewAutoScaler(schedulerName, clientset, fakeMetricsClient)

usagePercentage := autoScaler.CurrentUtilization(trigger, roomCount)
Expand All @@ -233,7 +233,7 @@ var _ = Describe("AutoScaler", func() {
Terminating: 0,
}

testing.CreatePod(clientset, "0", "1Gi", schedulerName, containerName)
testing.CreatePod(clientset, "0", "1Gi", schedulerName, "", containerName)
})

It("should return delta", func() {
Expand All @@ -250,7 +250,7 @@ var _ = Describe("AutoScaler", func() {
},
},
)
fakeMetricsClient := testing.CreatePodsMetricsList(containerMetrics, 1, schedulerName)
fakeMetricsClient := testing.CreatePodsMetricsList(containerMetrics, []string{schedulerName}, schedulerName)
autoScaler := NewAutoScaler(schedulerName, clientset, fakeMetricsClient)

delta := autoScaler.Delta(trigger, roomCount)
Expand All @@ -269,7 +269,7 @@ var _ = Describe("AutoScaler", func() {
},
},
)
fakeMetricsClient = testing.CreatePodsMetricsList(containerMetrics, 1, schedulerName)
fakeMetricsClient = testing.CreatePodsMetricsList(containerMetrics, []string{schedulerName}, schedulerName)
autoScaler = NewAutoScaler(schedulerName, clientset, fakeMetricsClient)

delta = autoScaler.Delta(trigger, roomCount)
Expand All @@ -289,7 +289,7 @@ var _ = Describe("AutoScaler", func() {
},
},
)
fakeMetricsClient := testing.CreatePodsMetricsList(containerMetrics, 1, schedulerName)
fakeMetricsClient := testing.CreatePodsMetricsList(containerMetrics, []string{schedulerName}, schedulerName)
autoScaler := NewAutoScaler(schedulerName, clientset, fakeMetricsClient)

usagePercentage := autoScaler.CurrentUtilization(trigger, roomCount)
Expand Down
3 changes: 0 additions & 3 deletions controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
yaml "gopkg.in/yaml.v2"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
metricsClient "k8s.io/metrics/pkg/client/clientset_generated/clientset"

"github.com/sirupsen/logrus"
"github.com/spf13/viper"
Expand Down Expand Up @@ -1201,7 +1200,6 @@ func SetRoomStatus(
db pginterfaces.DB,
mr *models.MixedMetricsReporter,
clientset kubernetes.Interface,
metricsClientset metricsClient.Interface,
status string,
config *viper.Viper,
room *models.Room,
Expand All @@ -1218,7 +1216,6 @@ func SetRoomStatus(
roomsCountByStatus, err := room.SetStatus(
redisClient,
db,
metricsClientset,
mr,
status,
cachedScheduler.Scheduler,
Expand Down
7 changes: 0 additions & 7 deletions controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/client-go/kubernetes/fake"
metricsFake "k8s.io/metrics/pkg/client/clientset_generated/clientset/fake"
)

const (
Expand Down Expand Up @@ -290,7 +289,6 @@ autoscaling:
var _ = Describe("Controller", func() {
var (
clientset *fake.Clientset
metricsClientset *metricsFake.Clientset
configYaml1 models.ConfigYAML
opManager *models.OperationManager
roomManager models.RoomManager
Expand All @@ -307,7 +305,6 @@ var _ = Describe("Controller", func() {

BeforeEach(func() {
clientset = fake.NewSimpleClientset()
metricsClientset = metricsFake.NewSimpleClientset()
err := yaml.Unmarshal([]byte(yaml1), &configYaml1)
Expect(err).NotTo(HaveOccurred())

Expand Down Expand Up @@ -6373,7 +6370,6 @@ containers:
mockDb,
mr,
clientset,
metricsClientset,
status,
config,
room,
Expand Down Expand Up @@ -6469,7 +6465,6 @@ containers:
mockDb,
mr,
clientset,
metricsClientset,
status,
config,
room,
Expand Down Expand Up @@ -6525,7 +6520,6 @@ containers:
mockDb,
mr,
clientset,
metricsClientset,
status,
config,
room,
Expand All @@ -6550,7 +6544,6 @@ containers:
mockDb,
mr,
clientset,
metricsClientset,
status,
config,
room,
Expand Down
28 changes: 17 additions & 11 deletions models/models_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package models_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/spf13/viper"

"testing"

Expand All @@ -26,20 +27,23 @@ import (
"github.com/topfreegames/maestro/models"
"github.com/topfreegames/maestro/reporters"
"k8s.io/client-go/kubernetes/fake"
metricsFake "k8s.io/metrics/pkg/client/clientset_generated/clientset/fake"
)

var (
mockCtrl *gomock.Controller
mockDb *pgmocks.MockDB
mockRedisClient *redismocks.MockRedisClient
mockClientset *fake.Clientset
mockPipeline *redismocks.MockPipeliner
mockPortChooser *mocks.MockPortChooser
mmr *models.MixedMetricsReporter
mr *reportersmocks.MockReporter
singleton *reporters.Reporters
hook *test.Hook
logger *logrus.Logger
mockCtrl *gomock.Controller
mockDb *pgmocks.MockDB
metricsClientset *metricsFake.Clientset
mockRedisClient *redismocks.MockRedisClient
config *viper.Viper
mockClientset *fake.Clientset
mockPipeline *redismocks.MockPipeliner
mockPortChooser *mocks.MockPortChooser
mmr *models.MixedMetricsReporter
mr *reportersmocks.MockReporter
singleton *reporters.Reporters
hook *test.Hook
logger *logrus.Logger
)

func TestModels(t *testing.T) {
Expand All @@ -53,6 +57,8 @@ var _ = BeforeEach(func() {
logger.Level = logrus.DebugLevel
mockDb = pgmocks.NewMockDB(mockCtrl)
mockRedisClient = redismocks.NewMockRedisClient(mockCtrl)
config, _ = mtesting.GetDefaultConfig()
metricsClientset = metricsFake.NewSimpleClientset()
mockClientset = fake.NewSimpleClientset()
mockPipeline = redismocks.NewMockPipeliner(mockCtrl)

Expand Down
69 changes: 7 additions & 62 deletions models/room.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ package models
import (
e "errors"
"fmt"
"math"
"strconv"
"strings"
"time"
Expand All @@ -20,10 +19,7 @@ import (
"github.com/topfreegames/extensions/redis/interfaces"
"github.com/topfreegames/maestro/reporters"
reportersConstants "github.com/topfreegames/maestro/reporters/constants"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/metrics/pkg/apis/metrics/v1beta1"
metricsClient "k8s.io/metrics/pkg/client/clientset_generated/clientset"
)

const (
Expand Down Expand Up @@ -58,6 +54,12 @@ type RoomPort struct {
Port int32 `json:"port"`
}

// RoomUsage struct
type RoomUsage struct {
Name string `json:"name"`
Usage float64 `json:"usage"`
}

// NewRoom is the room constructor
func NewRoom(id, schedulerName string) *Room {
return &Room{
Expand Down Expand Up @@ -112,7 +114,6 @@ return 'OK'
func (r *Room) SetStatus(
redisClient interfaces.RedisClient,
db pginterfaces.DB,
metricsClientset metricsClient.Interface,
mr *MixedMetricsReporter,
status string,
scheduler *Scheduler,
Expand All @@ -139,10 +140,6 @@ func (r *Room) SetStatus(
pipe.ZRem(lastStatusChangedKey, r.ID)
}

mr.WithSegment("debug", func() error {
r.addUtilizationMetricsToRedis(metricsClientset, pipe, scheduler, mr)
return nil
})
// remove from other statuses to be safe
for _, st := range allStatus {
if st != status {
Expand All @@ -153,58 +150,6 @@ func (r *Room) SetStatus(
return r.addStatusToRedisPipeAndExec(redisClient, db, mr, pipe, returnRoomsCount, scheduler)
}

func (r *Room) addUtilizationMetricsToRedis(
metricsClientset metricsClient.Interface,
pipe redis.Pipeliner,
scheduler *Scheduler,
mr *MixedMetricsReporter,
) {
fmt.Println("[DEBUG] entered addUtilizationMetricsToRedis")
sp := scheduler.GetAutoScalingPolicy()
metricsMap := map[AutoScalingPolicyType]bool{}
metricsTriggers := append(sp.Up.MetricsTrigger, sp.Down.MetricsTrigger...)
for _, trigger := range metricsTriggers {
if ResourcePolicyType(trigger.Type) {
metricsMap[trigger.Type] = true
}
}

if len(metricsMap) == 0 {
fmt.Println("[DEBUG] no metrics enabled, exit")
return
}

fmt.Println("[DEBUG] retrieving metrics")
requests := scheduler.GetResourcesRequests()
var pmetrics *v1beta1.PodMetrics
err := mr.WithSegment(SegmentMetrics, func() error {
var err error
pmetrics, err = metricsClientset.Metrics().PodMetricses(r.SchedulerName).Get(r.ID, metav1.GetOptions{})
return err
})
if err != nil && !strings.Contains(err.Error(), "not found") {
return
}

for metric := range metricsMap {
usage := int64(0)
if err != nil {
// add rooms with unavailable metrics to the end of the sorted set
usage = math.MaxInt64
} else {
for _, container := range pmetrics.Containers {
usage += GetResourceUsage(container.Usage, metric)
}
reportUsage(scheduler.Game, scheduler.Name, string(metric), requests[metric], usage)
}

pipe.ZAdd(
GetRoomMetricsRedisKey(r.SchedulerName, string(metric)),
redis.Z{Member: r.ID, Score: float64(usage)},
)
}
}

func (r *Room) addStatusToRedisPipeAndExec(
redisClient interfaces.RedisClient,
db pginterfaces.DB,
Expand Down Expand Up @@ -311,7 +256,7 @@ func reportUsage(game, scheduler, metric string, requests, usage int64) error {
reportersConstants.TagGame: game,
reportersConstants.TagScheduler: scheduler,
reportersConstants.TagMetric: metric,
"gauge": gauge,
"gauge": gauge,
})
}

Expand Down
Loading

0 comments on commit ac7ffc3

Please sign in to comment.