From 9276395cc2b4602e7937f19b4c9b63e8d2a78bd0 Mon Sep 17 00:00:00 2001 From: "jakub.coufal" Date: Thu, 26 Apr 2018 22:06:41 +0200 Subject: [PATCH 1/5] Removed unnecessary docker service & updated README --- .travis.yml | 5 ----- README.md | 4 ++-- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/.travis.yml b/.travis.yml index c386468..6afce98 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,3 @@ -sudo: required - -services: -- docker - language: go go: - "1.10" diff --git a/README.md b/README.md index c060535..d77d9e6 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ Tool for obtaining configuration from config server ### How to develop * Checkout into your GOROOT directory (e.g. /go/src/github.com/WanderaOrg/scccmd) -* `cd` into the folder and run `dep ensure` +* `cd` into the folder and run `dep ensure --vendor-only` * Tests are started by `go test -v ./...` * Or if you dont want to setup your local go env just use the provided Dockerfile @@ -24,4 +24,4 @@ which in turn downloads configuration in deployment initialization phase. Example k8s [manifest](docs/k8s/bundle.yaml). ### Tool documentation -[docs](docs/config.md) - Generated documentation for the tool \ No newline at end of file +[docs](docs/scccmd.md) - Generated documentation for the tool \ No newline at end of file From 040d1991cd5426c02eb392b360a7de57737e6bdc Mon Sep 17 00:00:00 2001 From: "jakub.coufal" Date: Fri, 1 Jun 2018 18:13:23 +0200 Subject: [PATCH 2/5] Improved travis check --- .travis.yml | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 6afce98..e091d5e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -35,9 +35,15 @@ install: - dep ensure -vendor-only script: -- go test -v ./... -- go vet -v ./... -- golint -set_exit_status $(go list ./...) +- | + go test -v ./... + go vet -v ./... + golint -set_exit_status $(go list ./...) + if [ -n "$(gofmt -s -l $(find . -type f -name '*.go' -not -path "./vendor/*"))" ]; then + echo "Go code is not formatted:" + gofmt -s -d -e $(find . -type f -name '*.go' -not -path "./vendor/*") + exit 1 + fi before_deploy: - PLATFORMS=(darwin/amd64 freebsd/amd64 linux/amd64 windows/amd64) From 43dd3c52140c9f42428140311b6edae2aead5bc5 Mon Sep 17 00:00:00 2001 From: "jakub.coufal" Date: Fri, 1 Jun 2018 18:13:47 +0200 Subject: [PATCH 3/5] Added default resources and limits for init container --- pkg/inject/hook_test.go | 5 +++-- pkg/inject/inject.go | 11 +++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/pkg/inject/hook_test.go b/pkg/inject/hook_test.go index 9d05c52..4fb907d 100644 --- a/pkg/inject/hook_test.go +++ b/pkg/inject/hook_test.go @@ -238,7 +238,8 @@ func createWebhook(t testing.TB) (*Webhook, func()) { } config := &WebhookConfig{ - Policy: InjectionPolicyEnabled, + Policy: InjectionPolicyEnabled, + Default: WebhookConfigDefaults{}, } configBytes, err := yaml.Marshal(config) @@ -298,7 +299,7 @@ func TestRunAndServe(t *testing.T) { "name":"config-init", "image":"wanderadock/scccmd", "args":["get","values","--source","http://config-service.default.svc:8080","--application","c1","--profile","default","--label","master","--destination","config.yaml"], - "resources":{}, + "resources":{"limits":{"cpu":"100m","memory":"50M"},"requests":{"cpu":"100m","memory":"10M"}}, "volumeMounts":[{"name":"config-volume","mountPath":"/config"}] } }, diff --git a/pkg/inject/inject.go b/pkg/inject/inject.go index f9646ea..1656aab 100644 --- a/pkg/inject/inject.go +++ b/pkg/inject/inject.go @@ -5,6 +5,7 @@ import ( "fmt" "k8s.io/api/core/v1" corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "log" "strings" @@ -97,6 +98,16 @@ func injectionData(spec *v1.PodSpec, metadata *metav1.ObjectMeta, config *Webhoo Image: config.ContainerImage, Args: d.imageArgs, VolumeMounts: []corev1.VolumeMount{volumeMount}, + Resources: corev1.ResourceRequirements{ + Requests: corev1.ResourceList{ + "cpu": *resource.NewScaledQuantity(100, resource.Milli), + "memory": *resource.NewScaledQuantity(10, resource.Mega), + }, + Limits: corev1.ResourceList{ + "cpu": *resource.NewScaledQuantity(100, resource.Milli), + "memory": *resource.NewScaledQuantity(50, resource.Mega), + }, + }, }, }, VolumeMounts: []corev1.VolumeMount{volumeMount}, From dea7f0907b4a36ffbfcbc8ddf249a05c1e513247 Mon Sep 17 00:00:00 2001 From: "jakub.coufal" Date: Sun, 3 Jun 2018 21:13:41 +0200 Subject: [PATCH 4/5] Make resources configurable --- pkg/inject/hook.go | 32 ++++++++++++++++++++++++++++---- pkg/inject/hook_test.go | 14 ++++++++++++-- pkg/inject/inject.go | 8 ++++---- 3 files changed, 44 insertions(+), 10 deletions(-) diff --git a/pkg/inject/hook.go b/pkg/inject/hook.go index 82750a5..73a45e4 100644 --- a/pkg/inject/hook.go +++ b/pkg/inject/hook.go @@ -10,6 +10,7 @@ import ( "io/ioutil" "k8s.io/api/admission/v1beta1" 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" "k8s.io/apimachinery/pkg/runtime/serializer" @@ -44,12 +45,25 @@ type WebhookConfigDefaults struct { Source string `yaml:"source"` } +// InitContainerResourcesList resources for init container +type InitContainerResourcesList struct { + CPU string `yaml:"cpu"` + Memory string `yaml:"memory"` +} + +// InitContainerResources resources for init container +type InitContainerResources struct { + Requests InitContainerResourcesList `yaml:"requests"` + Limits InitContainerResourcesList `yaml:"limits"` +} + // WebhookConfig struct representing webhook configuration values. type WebhookConfig struct { - AnnotationPrefix string `yaml:"annotation-prefix"` - Policy InjectionPolicy `yaml:"policy"` - ContainerImage string `yaml:"container-image"` - Default WebhookConfigDefaults `yaml:"default"` + AnnotationPrefix string `yaml:"annotation-prefix"` + Policy InjectionPolicy `yaml:"policy"` + ContainerImage string `yaml:"container-image"` + Default WebhookConfigDefaults `yaml:"default"` + Resources InitContainerResources `yaml:"resources"` } // Webhook implements a mutating webhook for automatic config injection. @@ -98,6 +112,16 @@ func (w *WebhookConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { Profile: "default", Source: "http://config-service.default.svc:8080", }, + Resources: InitContainerResources{ + Requests: InitContainerResourcesList{ + CPU: resource.NewScaledQuantity(100, resource.Milli).String(), + Memory: resource.NewScaledQuantity(10, resource.Mega).String(), + }, + Limits: InitContainerResourcesList{ + CPU: resource.NewScaledQuantity(100, resource.Milli).String(), + Memory: resource.NewScaledQuantity(50, resource.Mega).String(), + }, + }, } if err := unmarshal(&raw); err != nil { return err diff --git a/pkg/inject/hook_test.go b/pkg/inject/hook_test.go index 4fb907d..afe9810 100644 --- a/pkg/inject/hook_test.go +++ b/pkg/inject/hook_test.go @@ -8,6 +8,7 @@ import ( "github.com/WanderaOrg/scccmd/internal" "github.com/WanderaOrg/scccmd/internal/testcerts" "io/ioutil" + "k8s.io/apimachinery/pkg/api/resource" "net/http" "net/http/httptest" "os" @@ -238,8 +239,17 @@ func createWebhook(t testing.TB) (*Webhook, func()) { } config := &WebhookConfig{ - Policy: InjectionPolicyEnabled, - Default: WebhookConfigDefaults{}, + Policy: InjectionPolicyEnabled, + Resources: InitContainerResources{ + Requests: InitContainerResourcesList{ + CPU: resource.NewScaledQuantity(100, resource.Milli).String(), + Memory: resource.NewScaledQuantity(10, resource.Mega).String(), + }, + Limits: InitContainerResourcesList{ + CPU: resource.NewScaledQuantity(100, resource.Milli).String(), + Memory: resource.NewScaledQuantity(50, resource.Mega).String(), + }, + }, } configBytes, err := yaml.Marshal(config) diff --git a/pkg/inject/inject.go b/pkg/inject/inject.go index 1656aab..6914a1c 100644 --- a/pkg/inject/inject.go +++ b/pkg/inject/inject.go @@ -100,12 +100,12 @@ func injectionData(spec *v1.PodSpec, metadata *metav1.ObjectMeta, config *Webhoo VolumeMounts: []corev1.VolumeMount{volumeMount}, Resources: corev1.ResourceRequirements{ Requests: corev1.ResourceList{ - "cpu": *resource.NewScaledQuantity(100, resource.Milli), - "memory": *resource.NewScaledQuantity(10, resource.Mega), + "cpu": resource.MustParse(config.Resources.Requests.CPU), + "memory": resource.MustParse(config.Resources.Requests.Memory), }, Limits: corev1.ResourceList{ - "cpu": *resource.NewScaledQuantity(100, resource.Milli), - "memory": *resource.NewScaledQuantity(50, resource.Mega), + "cpu": resource.MustParse(config.Resources.Limits.CPU), + "memory": resource.MustParse(config.Resources.Limits.Memory), }, }, }, From 531182d7f579505bf26ddcf32a9b6ce05304c013 Mon Sep 17 00:00:00 2001 From: "jakub.coufal" Date: Sun, 3 Jun 2018 21:47:37 +0200 Subject: [PATCH 5/5] Fix of config file loading --- pkg/inject/hook.go | 30 +++++++++++++++--------------- pkg/inject/hook_test.go | 14 +++++++++++--- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/pkg/inject/hook.go b/pkg/inject/hook.go index 73a45e4..0581213 100644 --- a/pkg/inject/hook.go +++ b/pkg/inject/hook.go @@ -37,33 +37,33 @@ const ( // WebhookConfigDefaults configures default init container values. type WebhookConfigDefaults struct { - ContainerName string `yaml:"container-name"` - Label string `yaml:"label"` - Profile string `yaml:"profile"` - VolumeName string `yaml:"volume-name"` - VolumeMount string `yaml:"volume-mount"` - Source string `yaml:"source"` + ContainerName string `json:"container-name"` + Label string `json:"label"` + Profile string `json:"profile"` + VolumeName string `json:"volume-name"` + VolumeMount string `json:"volume-mount"` + Source string `json:"source"` } // InitContainerResourcesList resources for init container type InitContainerResourcesList struct { - CPU string `yaml:"cpu"` - Memory string `yaml:"memory"` + CPU string `json:"cpu"` + Memory string `json:"memory"` } // InitContainerResources resources for init container type InitContainerResources struct { - Requests InitContainerResourcesList `yaml:"requests"` - Limits InitContainerResourcesList `yaml:"limits"` + Requests InitContainerResourcesList `json:"requests"` + Limits InitContainerResourcesList `json:"limits"` } // WebhookConfig struct representing webhook configuration values. type WebhookConfig struct { - AnnotationPrefix string `yaml:"annotation-prefix"` - Policy InjectionPolicy `yaml:"policy"` - ContainerImage string `yaml:"container-image"` - Default WebhookConfigDefaults `yaml:"default"` - Resources InitContainerResources `yaml:"resources"` + AnnotationPrefix string `json:"annotation-prefix"` + Policy InjectionPolicy `json:"policy"` + ContainerImage string `json:"container-image"` + Default WebhookConfigDefaults `json:"default"` + Resources InitContainerResources `json:"resources"` } // Webhook implements a mutating webhook for automatic config injection. diff --git a/pkg/inject/hook_test.go b/pkg/inject/hook_test.go index afe9810..0df9ece 100644 --- a/pkg/inject/hook_test.go +++ b/pkg/inject/hook_test.go @@ -240,13 +240,21 @@ func createWebhook(t testing.TB) (*Webhook, func()) { config := &WebhookConfig{ Policy: InjectionPolicyEnabled, + Default: WebhookConfigDefaults{ + ContainerName: "config-init", + VolumeMount: "/config", + VolumeName: "config-volume", + Label: "master", + Profile: "default", + Source: "http://config-service.default.svc:8080", + }, Resources: InitContainerResources{ Requests: InitContainerResourcesList{ - CPU: resource.NewScaledQuantity(100, resource.Milli).String(), + CPU: resource.NewScaledQuantity(10, resource.Milli).String(), Memory: resource.NewScaledQuantity(10, resource.Mega).String(), }, Limits: InitContainerResourcesList{ - CPU: resource.NewScaledQuantity(100, resource.Milli).String(), + CPU: resource.NewScaledQuantity(50, resource.Milli).String(), Memory: resource.NewScaledQuantity(50, resource.Mega).String(), }, }, @@ -309,7 +317,7 @@ func TestRunAndServe(t *testing.T) { "name":"config-init", "image":"wanderadock/scccmd", "args":["get","values","--source","http://config-service.default.svc:8080","--application","c1","--profile","default","--label","master","--destination","config.yaml"], - "resources":{"limits":{"cpu":"100m","memory":"50M"},"requests":{"cpu":"100m","memory":"10M"}}, + "resources":{"limits":{"cpu":"50m","memory":"50M"},"requests":{"cpu":"10m","memory":"10M"}}, "volumeMounts":[{"name":"config-volume","mountPath":"/config"}] } },