Skip to content

Commit

Permalink
Merge pull request #14 from WanderaOrg/default-resources
Browse files Browse the repository at this point in the history
Default resources for init container
  • Loading branch information
coufalja authored Jun 4, 2018
2 parents 9fab822 + 531182d commit eb61b33
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 21 deletions.
17 changes: 9 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
sudo: required

services:
- docker

language: go
go:
- "1.10"
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
[docs](docs/scccmd.md) - Generated documentation for the tool
44 changes: 34 additions & 10 deletions pkg/inject/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down
21 changes: 20 additions & 1 deletion pkg/inject/hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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"}]
}
},
Expand Down
11 changes: 11 additions & 0 deletions pkg/inject/inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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},
Expand Down

0 comments on commit eb61b33

Please sign in to comment.