Skip to content

Commit

Permalink
Fix dumb mistake where you couldnt actually select different images f…
Browse files Browse the repository at this point in the history
…or containers (#13)

* Fix dumb mistake where you couldnt actually select different images for containers

* Flip ref check to get rid of n^2 loop
  • Loading branch information
bobbytables authored Feb 12, 2018
1 parent 3a363c1 commit 1374106
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 26 deletions.
2 changes: 1 addition & 1 deletion cmd/k8s-pipeliner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

const (
// Version defines the current version of k8s-pipeliner
Version = "0.0.8"
Version = "0.0.9"
)

func main() {
Expand Down
13 changes: 8 additions & 5 deletions pipeline/builder/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -123,17 +123,20 @@ 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 {
spinContainer := &types.Container{}

// 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{
Expand Down
22 changes: 15 additions & 7 deletions pipeline/builder/kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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",
},
},
})

Expand Down
28 changes: 21 additions & 7 deletions pipeline/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand All @@ -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"`
Expand Down Expand Up @@ -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{}
Expand All @@ -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
}
12 changes: 6 additions & 6 deletions test-pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1374106

Please sign in to comment.