Browse Source

Include specs from resources in release calc'n

This commit adds an interface `resource.Workload`, and implements it
for the "pod controller" resources. This makes it possible to
calculate based on the images used in the resources, rather than those
used in the cluster (without implementing exactly that, yet).
Michael Bridgen 1 year ago
parent
commit
621a49210a

+ 17
- 0
cluster/kubernetes/resource/cronjob.go View File

@@ -1,5 +1,22 @@
1 1
 package resource
2 2
 
3
+import (
4
+	"github.com/weaveworks/flux/resource"
5
+)
6
+
3 7
 type CronJob struct {
4 8
 	baseObject
9
+	Spec CronJobSpec
10
+}
11
+
12
+type CronJobSpec struct {
13
+	JobTemplate struct {
14
+		Spec struct {
15
+			Template PodTemplate
16
+		}
17
+	}
18
+}
19
+
20
+func (c CronJob) Containers() []resource.Container {
21
+	return c.Spec.JobTemplate.Spec.Template.Containers()
5 22
 }

+ 8
- 0
cluster/kubernetes/resource/daemonset.go View File

@@ -1,5 +1,9 @@
1 1
 package resource
2 2
 
3
+import (
4
+	"github.com/weaveworks/flux/resource"
5
+)
6
+
3 7
 type DaemonSet struct {
4 8
 	baseObject
5 9
 	Spec DaemonSetSpec
@@ -8,3 +12,7 @@ type DaemonSet struct {
8 12
 type DaemonSetSpec struct {
9 13
 	Template PodTemplate
10 14
 }
15
+
16
+func (ds DaemonSet) Containers() []resource.Container {
17
+	return ds.Spec.Template.Containers()
18
+}

+ 8
- 0
cluster/kubernetes/resource/deployment.go View File

@@ -1,5 +1,9 @@
1 1
 package resource
2 2
 
3
+import (
4
+	"github.com/weaveworks/flux/resource"
5
+)
6
+
3 7
 type Deployment struct {
4 8
 	baseObject
5 9
 	Spec DeploymentSpec
@@ -9,3 +13,7 @@ type DeploymentSpec struct {
9 13
 	Replicas int
10 14
 	Template PodTemplate
11 15
 }
16
+
17
+func (d Deployment) Containers() []resource.Container {
18
+	return d.Spec.Template.Containers()
19
+}

+ 15
- 0
cluster/kubernetes/resource/spec.go View File

@@ -1,5 +1,10 @@
1 1
 package resource
2 2
 
3
+import (
4
+	"github.com/weaveworks/flux/image"
5
+	"github.com/weaveworks/flux/resource"
6
+)
7
+
3 8
 // Types that daemonsets, deployments, and other things have in
4 9
 // common.
5 10
 
@@ -13,6 +18,16 @@ type PodTemplate struct {
13 18
 	Spec     PodSpec
14 19
 }
15 20
 
21
+func (t PodTemplate) Containers() []resource.Container {
22
+	var result []resource.Container
23
+	for _, c := range t.Spec.Containers {
24
+		// FIXME(michael): account for possible errors here
25
+		im, _ := image.ParseRef(c.Image)
26
+		result = append(result, resource.Container{Name: c.Name, Image: im})
27
+	}
28
+	return result
29
+}
30
+
16 31
 type PodSpec struct {
17 32
 	ImagePullSecrets []struct{ Name string }
18 33
 	Volumes          []Volume

+ 8
- 0
cluster/kubernetes/resource/statefulset.go View File

@@ -1,5 +1,9 @@
1 1
 package resource
2 2
 
3
+import (
4
+	"github.com/weaveworks/flux/resource"
5
+)
6
+
3 7
 type StatefulSet struct {
4 8
 	baseObject
5 9
 	Spec StatefulSetSpec
@@ -9,3 +13,7 @@ type StatefulSetSpec struct {
9 13
 	Replicas int
10 14
 	Template PodTemplate
11 15
 }
16
+
17
+func (ss StatefulSet) Containers() []resource.Container {
18
+	return ss.Spec.Template.Containers()
19
+}

+ 1
- 0
cluster/manifests.go View File

@@ -16,6 +16,7 @@ import (
16 16
 type Manifests interface {
17 17
 	// Given a directory with manifest files, find which files define
18 18
 	// which services.
19
+	// FIXME(michael): remove when redundant
19 20
 	FindDefinedServices(path string) (map[flux.ResourceID][]string, error)
20 21
 	// Update the definitions in a manifests bytes according to the
21 22
 	// spec given.

+ 12
- 17
release/context.go View File

@@ -4,13 +4,14 @@ import (
4 4
 	"fmt"
5 5
 	"io/ioutil"
6 6
 	"os"
7
-	"strings"
7
+	"path/filepath"
8 8
 
9 9
 	"github.com/weaveworks/flux"
10 10
 	"github.com/weaveworks/flux/cluster"
11 11
 	"github.com/weaveworks/flux/git"
12 12
 	"github.com/weaveworks/flux/policy"
13 13
 	"github.com/weaveworks/flux/registry"
14
+	"github.com/weaveworks/flux/resource"
14 15
 	"github.com/weaveworks/flux/update"
15 16
 )
16 17
 
@@ -63,7 +64,7 @@ func (rc *ReleaseContext) WriteUpdates(updates []*update.ControllerUpdate) error
63 64
 func (rc *ReleaseContext) SelectServices(results update.Result, prefilters, postfilters []update.ControllerFilter) ([]*update.ControllerUpdate, error) {
64 65
 
65 66
 	// Start with all the controllers that are defined in the repo.
66
-	allDefined, err := rc.FindDefinedServices()
67
+	allDefined, err := rc.WorkloadsForUpdate()
67 68
 	if err != nil {
68 69
 		return nil, err
69 70
 	}
@@ -118,27 +119,21 @@ func (rc *ReleaseContext) SelectServices(results update.Result, prefilters, post
118 119
 	return filteredUpdates, nil
119 120
 }
120 121
 
121
-func (rc *ReleaseContext) FindDefinedServices() (map[flux.ResourceID]*update.ControllerUpdate, error) {
122
-	services, err := rc.manifests.FindDefinedServices(rc.repo.ManifestDir())
122
+func (rc *ReleaseContext) WorkloadsForUpdate() (map[flux.ResourceID]*update.ControllerUpdate, error) {
123
+	resources, err := rc.manifests.LoadManifests(rc.repo.Dir(), rc.repo.ManifestDir())
123 124
 	if err != nil {
124 125
 		return nil, err
125 126
 	}
126 127
 
127 128
 	var defined = map[flux.ResourceID]*update.ControllerUpdate{}
128
-	for id, paths := range services {
129
-		switch len(paths) {
130
-		case 1:
131
-			def, err := ioutil.ReadFile(paths[0])
132
-			if err != nil {
133
-				return nil, err
134
-			}
135
-			defined[id] = &update.ControllerUpdate{
136
-				ResourceID:    id,
137
-				ManifestPath:  paths[0],
138
-				ManifestBytes: def,
129
+	for _, res := range resources {
130
+		if wl, ok := res.(resource.Workload); ok {
131
+			defined[res.ResourceID()] = &update.ControllerUpdate{
132
+				ResourceID:    res.ResourceID(),
133
+				Resource:      wl,
134
+				ManifestPath:  filepath.Join(rc.repo.Dir(), res.Source()),
135
+				ManifestBytes: res.Bytes(),
139 136
 			}
140
-		default:
141
-			return nil, fmt.Errorf("multiple resource files found for service %s: %s", id, strings.Join(paths, ", "))
142 137
 		}
143 138
 	}
144 139
 	return defined, nil

+ 5
- 0
resource/resource.go View File

@@ -18,3 +18,8 @@ type Container struct {
18 18
 	Name  string
19 19
 	Image image.Ref
20 20
 }
21
+
22
+type Workload interface {
23
+	Resource
24
+	Containers() []Container
25
+}

+ 2
- 0
update/service.go View File

@@ -3,11 +3,13 @@ package update
3 3
 import (
4 4
 	"github.com/weaveworks/flux"
5 5
 	"github.com/weaveworks/flux/cluster"
6
+	"github.com/weaveworks/flux/resource"
6 7
 )
7 8
 
8 9
 type ControllerUpdate struct {
9 10
 	ResourceID    flux.ResourceID
10 11
 	Controller    cluster.Controller
12
+	Resource      resource.Workload
11 13
 	ManifestPath  string
12 14
 	ManifestBytes []byte
13 15
 	Updates       []ContainerUpdate

Loading…
Cancel
Save