diff --git a/cmd/k8s-pipeliner/main.go b/cmd/k8s-pipeliner/main.go index 258a8c1..791498e 100644 --- a/cmd/k8s-pipeliner/main.go +++ b/cmd/k8s-pipeliner/main.go @@ -14,7 +14,7 @@ import ( const ( // Version defines the current version of k8s-pipeliner - Version = "0.0.8" + Version = "0.0.9" ) func main() { diff --git a/pipeline/builder/kubernetes.go b/pipeline/builder/kubernetes.go index 079e174..a0fb805 100644 --- a/pipeline/builder/kubernetes.go +++ b/pipeline/builder/kubernetes.go @@ -72,7 +72,7 @@ func (mp *ManifestParser) ContainersFromScaffold(scaffold config.ContainerScaffo switch t := resource.(type) { case *appsv1.Deployment: - mg.Containers = mp.deploymentContainers(t, scaffold.ImageDescriptionRef()) + mg.Containers = mp.deploymentContainers(t, scaffold) mg.Annotations = t.Annotations mg.Namespace = t.Namespace mg.VolumeSources = mp.volumeSources(t.Spec.Template.Spec.Volumes) @@ -123,7 +123,7 @@ func (mp *ManifestParser) volumeSources(vols []corev1.Volume) []*types.VolumeSou return vs } -func (mp *ManifestParser) deploymentContainers(dep *appsv1.Deployment, ref config.ImageDescriptionRef) []*types.Container { +func (mp *ManifestParser) deploymentContainers(dep *appsv1.Deployment, scaffold config.ContainerScaffold) []*types.Container { var c []*types.Container for _, container := range dep.Spec.Template.Spec.Containers { @@ -131,9 +131,12 @@ func (mp *ManifestParser) deploymentContainers(dep *appsv1.Deployment, ref confi // add the image description first off using the annotations on the container var imageDescription config.ImageDescription - for _, desc := range mp.config.ImageDescriptions { - if desc.Name == ref.Name && ref.ContainerName == container.Name { - imageDescription = desc + if ref := scaffold.ImageDescriptionRef(container.Name); ref != nil { + for _, desc := range mp.config.ImageDescriptions { + if desc.Name == ref.Name && ref.ContainerName == container.Name { + imageDescription = desc + break + } } } spinContainer.ImageDescription = types.ImageDescription{ diff --git a/pipeline/builder/kubernetes_test.go b/pipeline/builder/kubernetes_test.go index 222ae05..2ef0568 100644 --- a/pipeline/builder/kubernetes_test.go +++ b/pipeline/builder/kubernetes_test.go @@ -13,16 +13,22 @@ import ( ) type scaffoldMock struct { - manifest string - imageDescriptionRef config.ImageDescriptionRef + manifest string + imageDescriptionRefs []config.ImageDescriptionRef } func (sm scaffoldMock) Manifest() string { return sm.manifest } -func (sm scaffoldMock) ImageDescriptionRef() config.ImageDescriptionRef { - return sm.imageDescriptionRef +func (sm scaffoldMock) ImageDescriptionRef(containerName string) *config.ImageDescriptionRef { + for _, ref := range sm.imageDescriptionRefs { + if ref.ContainerName == containerName { + return &ref + } + } + + return nil } func TestContainersFromManifests(t *testing.T) { @@ -40,9 +46,11 @@ func TestContainersFromManifests(t *testing.T) { }) group, err := parser.ContainersFromScaffold(scaffoldMock{ manifest: file, - imageDescriptionRef: config.ImageDescriptionRef{ - Name: "test-ref", - ContainerName: "test-container", + imageDescriptionRefs: []config.ImageDescriptionRef{ + { + Name: "test-ref", + ContainerName: "test-container", + }, }, }) diff --git a/pipeline/config/config.go b/pipeline/config/config.go index 47d5400..85f3abf 100644 --- a/pipeline/config/config.go +++ b/pipeline/config/config.go @@ -91,8 +91,8 @@ type Container struct { // RunJobStage is the configuration for a one off job in a spinnaker pipeline type RunJobStage struct { - ManifestFile string `yaml:"manifestFile"` - ImageDescription ImageDescriptionRef `yaml:"imageDescription"` + ManifestFile string `yaml:"manifestFile"` + ImageDescriptions []ImageDescriptionRef `yaml:"imageDescriptions"` Container *Container `yaml:"container"` } @@ -113,8 +113,8 @@ type ImageDescriptionRef struct { // of a group is filled out by the defined manifest file. This means things like commands, env vars, // etc, are all pulled into the group spec for you. type Group struct { - ManifestFile string `yaml:"manifestFile"` - ImageDescription ImageDescriptionRef `yaml:"imageDescription"` + ManifestFile string `yaml:"manifestFile"` + ImageDescriptions []ImageDescriptionRef `yaml:"imageDescriptions"` MaxRemainingASGS int `yaml:"maxRemainingASGS"` ScaleDown bool `yaml:"scaleDown"` @@ -152,7 +152,7 @@ type ContainerOverrides struct { // so you can build multiple types of stages (run job or deploys) type ContainerScaffold interface { Manifest() string - ImageDescriptionRef() ImageDescriptionRef + ImageDescriptionRef(containerName string) *ImageDescriptionRef } var _ ContainerScaffold = Group{} @@ -162,10 +162,24 @@ var _ ContainerScaffold = RunJobStage{} func (g Group) Manifest() string { return g.ManifestFile } // ImageDescriptionRef implements ContainerScaffold -func (g Group) ImageDescriptionRef() ImageDescriptionRef { return g.ImageDescription } +func (g Group) ImageDescriptionRef(containerName string) *ImageDescriptionRef { + return findImageDescription(containerName, g.ImageDescriptions) +} // Manifest implements ContainerScaffold func (rj RunJobStage) Manifest() string { return rj.ManifestFile } // ImageDescriptionRef implements ContainerScaffold -func (rj RunJobStage) ImageDescriptionRef() ImageDescriptionRef { return rj.ImageDescription } +func (rj RunJobStage) ImageDescriptionRef(containerName string) *ImageDescriptionRef { + return findImageDescription(containerName, rj.ImageDescriptions) +} + +func findImageDescription(containerName string, refs []ImageDescriptionRef) *ImageDescriptionRef { + for _, r := range refs { + if r.ContainerName == containerName { + return &r + } + } + + return nil +} diff --git a/test-pipeline.yml b/test-pipeline.yml index 208f04d..0c1bcd9 100644 --- a/test-pipeline.yml +++ b/test-pipeline.yml @@ -19,9 +19,9 @@ stages: refId: "1" runJob: manifestFile: test-deployment.yml - imageDescription: - name: main-image - containerName: example + imageDescriptions: + - name: main-image + containerName: example container: # override default command defined in the manifest command: - bundle @@ -54,9 +54,9 @@ stages: strategy: redblack targetSize: 2 # this is the total amount of replicas for the deployment containerOverrides: {} - imageDescription: - name: main-image - containerName: example + imageDescriptions: + - name: main-image + containerName: example loadBalancers: - "test" - account: int-k8s