Skip to content

Commit

Permalink
🍱 [patch] Initial resources for mock deployment engine (#100)
Browse files Browse the repository at this point in the history
* 🍱 Initial resources for mock deployment engine

* 🔧 Move check resources to be configurable

* 💡 Add docs

* ♻️ Rewrite ensure staging resources quota

* ✅ Temporary disable failure deploy test
  • Loading branch information
Pohfy123 authored Apr 29, 2021
1 parent 1ad6af6 commit c504664
Show file tree
Hide file tree
Showing 20 changed files with 286 additions and 36 deletions.
18 changes: 18 additions & 0 deletions cmd/samsahai/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
httpSwagger "github.com/swaggo/http-swagger"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
Expand Down Expand Up @@ -178,6 +180,14 @@ func startCtrlCmd() *cobra.Command {
Password: viper.GetString(s2h.VKMSTeamsPassword),
},
},
CheckerResources: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse(viper.GetString(s2h.VKCheckerCPU)),
corev1.ResourceMemory: resource.MustParse(viper.GetString(s2h.VKCheckerMemory)),
},
InitialResourcesQuota: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse(viper.GetString(s2h.VKInitialResourcesQuotaCPU)),
corev1.ResourceMemory: resource.MustParse(viper.GetString(s2h.VKInitialResourcesQuotaMemory)),
},
}

configPath := viper.GetString(s2h.VKS2HConfigPath)
Expand Down Expand Up @@ -286,6 +296,14 @@ func startCtrlCmd() *cobra.Command {
"Waiting duration time to re-check pull request image in the registry.")
cmd.Flags().Int(s2h.VKPullRequestQueueMaxHistoryDays, 7,
"Max stored pull request queue histories in day.")
cmd.Flags().String(s2h.VKCheckerCPU, "100m",
"CPU of cronjobs for sending new component webhook.")
cmd.Flags().String(s2h.VKCheckerMemory, "125Mi",
"Memory of cronjobs for sending new component webhook.")
cmd.Flags().String(s2h.VKInitialResourcesQuotaCPU, "3",
"Required minimum cpu of resources quota which will be used for mock deployment engine.")
cmd.Flags().String(s2h.VKInitialResourcesQuotaMemory, "3Gi",
"Required minimum memory of resources quota which will be used for mock deployment engine.")
return cmd
}

Expand Down
12 changes: 12 additions & 0 deletions config/chart/samsahai/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ fileConfigs:
clusterDomain: "cluster.local"
githubURL: "https://github.com"

# cpu/memory of cronjobs for sending new component webhook
checkerResources:
cpu: 100m
memory: 125Mi

# required minimum cpu/memory of resources quota
# which will be used for mock deployment engine.
# It will be activated for only the Team that have resources quota defined.
initialResourcesQuota:
cpu: '3'
memory: 3Gi

# pullRequest global config
pullRequest:
# how many concurrences of pull request queue running?
Expand Down
2 changes: 1 addition & 1 deletion docs/docs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
// This file was generated by swaggo/swag at
// 2021-04-15 16:18:40.621018 +0700 +07 m=+0.182370689
// 2021-04-28 09:40:41.159975 +0700 +07 m=+0.155719399

package docs

Expand Down
79 changes: 52 additions & 27 deletions internal/config/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
batchv1beta1 "k8s.io/api/batch/v1beta1"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -227,25 +226,20 @@ func (c *controller) GetPriorityQueues(configName string) ([]string, error) {
return config.Status.Used.PriorityQueues, nil
}

// GetPullRequestBundleDependencies returns dependencies list of a pull request bundle from configuration
func (c *controller) GetPullRequestBundleDependencies(configName, prBundleName string) ([]string, error) {
// GetPullRequestConfig returns a configuration of staging
func (c *controller) GetStagingConfig(configName string) (*s2hv1.ConfigStaging, error) {
config, err := c.Get(configName)
if err != nil {
logger.Error(err, "cannot get Config", "name", configName)
return []string{}, err
return &s2hv1.ConfigStaging{}, err
}

prDeps := make([]string, 0)
if config.Status.Used.PullRequest != nil {
for _, prBundle := range config.Status.Used.PullRequest.Bundles {
if prBundle.Name == prBundleName {
prDeps = prBundle.Dependencies
break
}
}
stgConfig := config.Status.Used.Staging
if stgConfig == nil {
stgConfig = &s2hv1.ConfigStaging{}
}

return prDeps, nil
return stgConfig, nil
}

// GetPullRequestConfig returns a configuration of pull request
Expand All @@ -264,6 +258,27 @@ func (c *controller) GetPullRequestConfig(configName string) (*s2hv1.ConfigPullR
return prConfig, nil
}

// GetPullRequestBundleDependencies returns dependencies list of a pull request bundle from configuration
func (c *controller) GetPullRequestBundleDependencies(configName, prBundleName string) ([]string, error) {
config, err := c.Get(configName)
if err != nil {
logger.Error(err, "cannot get Config", "name", configName)
return []string{}, err
}

prDeps := make([]string, 0)
if config.Status.Used.PullRequest != nil {
for _, prBundle := range config.Status.Used.PullRequest.Bundles {
if prBundle.Name == prBundleName {
prDeps = prBundle.Dependencies
break
}
}
}

return prDeps, nil
}

// Update updates Config CRD
func (c *controller) Update(config *s2hv1.Config) error {
if err := c.client.Update(context.TODO(), config); err != nil {
Expand Down Expand Up @@ -516,15 +531,10 @@ curl -X POST -k %s/%s -d '{"component": "%s", "team": "%s", "repository": "%s"}'
}

func (c *controller) getCronJobResources() corev1.ResourceRequirements {
resources := c.s2hConfig.CheckerResources
return corev1.ResourceRequirements{
Limits: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("500m"),
corev1.ResourceMemory: resource.MustParse("500Mi"),
},
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("500m"),
corev1.ResourceMemory: resource.MustParse("500Mi"),
},
Limits: resources,
Requests: resources,
}
}

Expand Down Expand Up @@ -585,6 +595,10 @@ func (c *controller) ensureConfigChanged(teamName, namespace string) error {
return err
}

if err := c.ensureStagingQuotaFromDeployEngine(teamName, namespace); err != nil {
return err
}

return nil
}

Expand Down Expand Up @@ -793,24 +807,35 @@ func (c *controller) updateChildrenConfig(config s2hv1.Config) error {
return nil
}

func (c *controller) ensureTriggerChildrenConfig(name string) error {
func (c *controller) ensureTriggerChildrenConfig(templateName string) error {
ctx := context.TODO()
configs := &s2hv1.ConfigList{}
if err := c.client.List(ctx, configs, &client.ListOptions{}); err != nil {
logger.Error(err, "cannot list Configs ")
logger.Error(err, "cannot list Configs")
return err
}
for _, conf := range configs.Items {
if conf.Spec.Template == name {
conf.Status.SyncTemplate = false
if err := c.updateChildrenConfig(conf); err != nil {
for _, config := range configs.Items {
if config.Spec.Template == templateName {
config.Status.SyncTemplate = false
if err := c.updateChildrenConfig(config); err != nil {
return err
}
}
}
return nil
}

func (c *controller) ensureStagingQuotaFromDeployEngine(configName, namespace string) error {
_, err := c.s2hCtrl.EnsureStagingResourcesQuota(configName, namespace, false)
if err != nil {
logger.Error(err, "cannot ensure staging resources quota",
"team", configName, "namespace", namespace)
return err
}

return nil
}

func ValidateConfigRequiredField(config *s2hv1.Config) error {
if len(config.Status.Used.Components) == 0 || config.Status.Used.Staging == nil {
return errors.ErrConfigurationRequiredField
Expand Down
3 changes: 3 additions & 0 deletions internal/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ type ConfigController interface {
// GetPriorityQueues returns a list of priority queues which defined in Config
GetPriorityQueues(configName string) ([]string, error)

// GetStagingConfig returns a configuration of staging
GetStagingConfig(configName string) (*s2hv1.ConfigStaging, error)

// GetPullRequestConfig returns a configuration of pull request
GetPullRequestConfig(configName string) (*s2hv1.ConfigPullRequest, error)

Expand Down
6 changes: 6 additions & 0 deletions internal/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const (
StagingCtrlName = "s2h-staging-ctrl"
StagingDefaultPort = 8090

ResourcesQuotaSuffix = "-resources"

// Viper keys
VKDebug = "debug"
VKServerHTTPPort = "port"
Expand Down Expand Up @@ -61,6 +63,10 @@ const (
VKPRTriggerMaxRetry = "pr-trigger-max-retry"
VKPRTriggerPollingTime = "pr-trigger-polling-time"
VKPullRequestQueueMaxHistoryDays = "pr-queue-max-history-days"
VKCheckerCPU = "checker-cpu"
VKCheckerMemory = "checker-memory"
VKInitialResourcesQuotaCPU = "initial-resources-quota-cpu"
VKInitialResourcesQuotaMemory = "initial-resources-quota-memory"
)

type ConfigurationJSON struct {
Expand Down
4 changes: 4 additions & 0 deletions internal/reporter/github/reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ func (c *mockConfigCtrl) GetPriorityQueues(configName string) ([]string, error)
return nil, nil
}

func (c *mockConfigCtrl) GetStagingConfig(configName string) (*s2hv1.ConfigStaging, error) {
return nil, nil
}

func (c *mockConfigCtrl) GetPullRequestConfig(configName string) (*s2hv1.ConfigPullRequest, error) {
return nil, nil
}
Expand Down
4 changes: 4 additions & 0 deletions internal/reporter/msteams/reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,10 @@ func (c *mockConfigCtrl) GetPriorityQueues(configName string) ([]string, error)
return nil, nil
}

func (c *mockConfigCtrl) GetStagingConfig(configName string) (*s2hv1.ConfigStaging, error) {
return nil, nil
}

func (c *mockConfigCtrl) GetPullRequestConfig(configName string) (*s2hv1.ConfigPullRequest, error) {
return nil, nil
}
Expand Down
4 changes: 4 additions & 0 deletions internal/reporter/rest/reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,10 @@ func (c *mockConfigCtrl) GetPriorityQueues(configName string) ([]string, error)
return nil, nil
}

func (c *mockConfigCtrl) GetStagingConfig(configName string) (*s2hv1.ConfigStaging, error) {
return nil, nil
}

func (c *mockConfigCtrl) GetPullRequestConfig(configName string) (*s2hv1.ConfigPullRequest, error) {
return nil, nil
}
Expand Down
4 changes: 4 additions & 0 deletions internal/reporter/shell/reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,10 @@ func (c *mockConfigCtrl) GetPriorityQueues(configName string) ([]string, error)
return nil, nil
}

func (c *mockConfigCtrl) GetStagingConfig(configName string) (*s2hv1.ConfigStaging, error) {
return nil, nil
}

func (c *mockConfigCtrl) GetPullRequestConfig(configName string) (*s2hv1.ConfigPullRequest, error) {
return nil, nil
}
Expand Down
4 changes: 4 additions & 0 deletions internal/reporter/slack/reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,10 @@ func (c *mockConfigCtrl) GetPriorityQueues(configName string) ([]string, error)
return nil, nil
}

func (c *mockConfigCtrl) GetStagingConfig(configName string) (*s2hv1.ConfigStaging, error) {
return nil, nil
}

func (c *mockConfigCtrl) GetPullRequestConfig(configName string) (*s2hv1.ConfigPullRequest, error) {
return nil, nil
}
Expand Down
14 changes: 13 additions & 1 deletion internal/samsahai.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"net/http"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

s2hv1 "github.com/agoda-com/samsahai/api/v1"
Expand Down Expand Up @@ -81,6 +82,14 @@ type SamsahaiConfig struct {
s2hv1.CommandAndArgs
} `json:"postNamespaceCreation,omitempty" yaml:"postNamespaceCreation,omitempty"`

// CheckerResources defines cpu/memory of cronjobs for sending new component webhook
CheckerResources corev1.ResourceList `json:"checkerResources,omitempty" yaml:"checkerResources,omitempty"`

// InitialResourcesQuota defines required minimum cpu/memory of resources quota
// which will be used for mock deployment engine.
// It will be activated for only the Team that have resources quota defined.
InitialResourcesQuota corev1.ResourceList `json:"initialResourcesQuota,omitempty" yaml:"initialResourcesQuota,omitempty"`

// StagingEnvs defines environment variables of staging controller
StagingEnvs map[string]string `json:"stagingEnvs,omitempty" yaml:"stagingEnvs,omitempty"`

Expand Down Expand Up @@ -159,9 +168,12 @@ type SamsahaiController interface {
// GetActivePromotionDeployEngine returns samsahai deploy engine
GetActivePromotionDeployEngine(teamName, ns string) DeployEngine

// EnsureTeamTemplateChanged updates team if template changed
// EnsureTeamTemplateChanged updates team if template changed
EnsureTeamTemplateChanged(teamComp *s2hv1.Team) error

// EnsureStagingResourcesQuota ensures staging resources quota applied correctly
EnsureStagingResourcesQuota(teamName, namespace string, dryRun bool) (corev1.ResourceList, error)

// LoadTeamSecret loads team secret from main namespace
LoadTeamSecret(teamComp *s2hv1.Team) error

Expand Down
4 changes: 4 additions & 0 deletions internal/samsahai/checker/samsahai/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,10 @@ func (c *mockConfigCtrl) GetPriorityQueues(configName string) ([]string, error)
return nil, nil
}

func (c *mockConfigCtrl) GetStagingConfig(configName string) (*s2hv1.ConfigStaging, error) {
return nil, nil
}

func (c *mockConfigCtrl) GetPullRequestConfig(configName string) (*s2hv1.ConfigPullRequest, error) {
return nil, nil
}
Expand Down
Loading

0 comments on commit c504664

Please sign in to comment.