Skip to content
This repository has been archived by the owner on Oct 22, 2021. It is now read-only.

Commit

Permalink
Merge pull request #295 from SUSE/alexh-handle-generic-annotations-fo…
Browse files Browse the repository at this point in the history
…r-roles

Add annotations to various kube objects
  • Loading branch information
mook-as authored Nov 9, 2017
2 parents 12ea3e3 + bffb7e7 commit afc2831
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 5 deletions.
10 changes: 9 additions & 1 deletion kube/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,16 @@ func NewJob(role *model.Role, settings ExportSettings) (helm.Node, error) {
// Job objects become a regular feature in kube 1.6
apiVersion = fmt.Sprintf("{{ if %s -}} batch/v1 {{- else -}} %s {{- end }}", minKubeVersion(1, 6), apiVersion)
}

metadata := helm.NewMapping()
metadata.Add("name", role.Name)
if role.Run.ObjectAnnotations != nil {
metadata.Add("annotations", *role.Run.ObjectAnnotations)
}
metadata.Sort()

job := newTypeMeta(apiVersion, "Job", helm.Comment(role.GetLongDescription()))
job.Add("metadata", helm.NewMapping("name", role.Name))
job.Add("metadata", metadata)
job.Add("spec", helm.NewMapping("template", podTemplate))

return job.Sort(), nil
Expand Down
48 changes: 44 additions & 4 deletions kube/job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import (
yaml "gopkg.in/yaml.v2"
)

func jobTestLoadRole(assert *assert.Assertions, roleName string) *model.Role {
func jobTestLoadRole(assert *assert.Assertions, roleName, manifestName string) *model.Role {
workDir, err := os.Getwd()
assert.NoError(err)

manifestPath := filepath.Join(workDir, "../test-assets/role-manifests/jobs.yml")
manifestPath := filepath.Join(workDir, "../test-assets/role-manifests", manifestName)
releasePath := filepath.Join(workDir, "../test-assets/tor-boshrelease")
releasePathBoshCache := filepath.Join(releasePath, "bosh-cache")

Expand All @@ -42,7 +42,7 @@ func jobTestLoadRole(assert *assert.Assertions, roleName string) *model.Role {

func TestJobPreFlight(t *testing.T) {
assert := assert.New(t)
role := jobTestLoadRole(assert, "pre-role")
role := jobTestLoadRole(assert, "pre-role", "jobs.yml")
if role == nil {
return
}
Expand Down Expand Up @@ -88,7 +88,7 @@ func TestJobPreFlight(t *testing.T) {

func TestJobPostFlight(t *testing.T) {
assert := assert.New(t)
role := jobTestLoadRole(assert, "post-role")
role := jobTestLoadRole(assert, "post-role", "jobs.yml")
if role == nil {
return
}
Expand Down Expand Up @@ -130,3 +130,43 @@ func TestJobPostFlight(t *testing.T) {
}
testhelpers.IsYAMLSubset(assert, expected, actual)
}

func TestJobWithAnnotations(t *testing.T) {
assert := assert.New(t)

role := jobTestLoadRole(assert, "role", "job-with-annotation.yml")
if role == nil {
return
}

job, err := NewJob(role, ExportSettings{
Opinions: model.NewEmptyOpinions(),
})
if !assert.NoError(err, "Failed to create job from role pre-role") {
return
}
assert.NotNil(job)

yamlConfig := &bytes.Buffer{}
if err := helm.NewEncoder(yamlConfig).Encode(job); !assert.NoError(err) {
return
}

var expected, actual interface{}
if !assert.NoError(yaml.Unmarshal(yamlConfig.Bytes(), &actual)) {
return
}
expectedYAML := strings.Replace(`---
apiVersion: extensions/v1beta1
kind: Job
metadata:
name: role
annotations:
helm.sh/hook: post-install
`, "\t", " ", -1)
if !assert.NoError(yaml.Unmarshal([]byte(expectedYAML), &expected)) {
return
}

testhelpers.IsYAMLSubset(assert, expected, actual)
}
1 change: 1 addition & 0 deletions model/roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ type RoleRun struct {
ServiceAccount string `yaml:"service-account,omitempty"`
Affinity interface{} `yaml:"affinity,omitempty"`
Environment []string `yaml:"env"`
ObjectAnnotations *map[string]string `yaml:"object-annotations,omitempty"`
}

// RoleRunScaling describes how a role should scale out at runtime
Expand Down
12 changes: 12 additions & 0 deletions test-assets/role-manifests/job-with-annotation.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
roles:
- name: role
type: bosh-task
jobs:
- name: new_hostname
release_name: tor
run:
flight-stage: post-flight
memory: 128
object-annotations:
"helm.sh/hook": post-install

0 comments on commit afc2831

Please sign in to comment.