diff --git a/.travis.yml b/.travis.yml index c386468..e091d5e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,3 @@ -sudo: required - -services: -- docker - language: go go: - "1.10" @@ -40,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) 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 diff --git a/pkg/inject/hook.go b/pkg/inject/hook.go index 82750a5..0581213 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" @@ -36,20 +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 `json:"cpu"` + Memory string `json:"memory"` +} + +// InitContainerResources resources for init container +type InitContainerResources struct { + 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"` + 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. @@ -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 9d05c52..0df9ece 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" @@ -239,6 +240,24 @@ 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(10, resource.Milli).String(), + Memory: resource.NewScaledQuantity(10, resource.Mega).String(), + }, + Limits: InitContainerResourcesList{ + CPU: resource.NewScaledQuantity(50, resource.Milli).String(), + Memory: resource.NewScaledQuantity(50, resource.Mega).String(), + }, + }, } configBytes, err := yaml.Marshal(config) @@ -298,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":{}, + "resources":{"limits":{"cpu":"50m","memory":"50M"},"requests":{"cpu":"10m","memory":"10M"}}, "volumeMounts":[{"name":"config-volume","mountPath":"/config"}] } }, diff --git a/pkg/inject/inject.go b/pkg/inject/inject.go index f9646ea..6914a1c 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.MustParse(config.Resources.Requests.CPU), + "memory": resource.MustParse(config.Resources.Requests.Memory), + }, + Limits: corev1.ResourceList{ + "cpu": resource.MustParse(config.Resources.Limits.CPU), + "memory": resource.MustParse(config.Resources.Limits.Memory), + }, + }, }, }, VolumeMounts: []corev1.VolumeMount{volumeMount},