Browse Source

Move definition of cluster.Container to resource

So that we can (shortly) use the same definition of containers whether
we get them the cluster or from a manifest, move the Container type to
the `resource`. This also updates the type so that images are parsed
when constructed, i.e., in one place.
Michael Bridgen 1 year ago
parent
commit
85da259def

+ 4
- 11
cluster/cluster.go View File

@@ -4,6 +4,7 @@ import (
4 4
 	"errors"
5 5
 
6 6
 	"github.com/weaveworks/flux"
7
+	"github.com/weaveworks/flux/resource"
7 8
 	"github.com/weaveworks/flux/ssh"
8 9
 )
9 10
 
@@ -37,26 +38,18 @@ type Controller struct {
37 38
 	Containers ContainersOrExcuse
38 39
 }
39 40
 
40
-// A Container represents a container specification in a pod. The Name
41
-// identifies it within the pod, and the Image says which image it's
42
-// configured to run.
43
-type Container struct {
44
-	Name  string
45
-	Image string
46
-}
47
-
48 41
 // Sometimes we care if we can't find the containers for a service,
49 42
 // sometimes we just want the information we can get.
50 43
 type ContainersOrExcuse struct {
51 44
 	Excuse     string
52
-	Containers []Container
45
+	Containers []resource.Container
53 46
 }
54 47
 
55
-func (s Controller) ContainersOrNil() []Container {
48
+func (s Controller) ContainersOrNil() []resource.Container {
56 49
 	return s.Containers.Containers
57 50
 }
58 51
 
59
-func (s Controller) ContainersOrError() ([]Container, error) {
52
+func (s Controller) ContainersOrError() ([]resource.Container, error) {
60 53
 	var err error
61 54
 	if s.Containers.Excuse != "" {
62 55
 		err = errors.New(s.Containers.Excuse)

+ 12
- 3
cluster/kubernetes/resourcekinds.go View File

@@ -11,6 +11,8 @@ import (
11 11
 
12 12
 	"github.com/weaveworks/flux"
13 13
 	"github.com/weaveworks/flux/cluster"
14
+	"github.com/weaveworks/flux/image"
15
+	"github.com/weaveworks/flux/resource"
14 16
 )
15 17
 
16 18
 /////////////////////////////////////////////////////////////////////////////
@@ -42,15 +44,22 @@ type podController struct {
42 44
 }
43 45
 
44 46
 func (pc podController) toClusterController(resourceID flux.ResourceID) cluster.Controller {
45
-	var clusterContainers []cluster.Container
47
+	var clusterContainers []resource.Container
48
+	var excuse string
46 49
 	for _, container := range pc.podTemplate.Spec.Containers {
47
-		clusterContainers = append(clusterContainers, cluster.Container{Name: container.Name, Image: container.Image})
50
+		ref, err := image.ParseRef(container.Image)
51
+		if err != nil {
52
+			clusterContainers = nil
53
+			excuse = err.Error()
54
+			break
55
+		}
56
+		clusterContainers = append(clusterContainers, resource.Container{Name: container.Name, Image: ref})
48 57
 	}
49 58
 
50 59
 	return cluster.Controller{
51 60
 		ID:         resourceID,
52 61
 		Status:     pc.status,
53
-		Containers: cluster.ContainersOrExcuse{Containers: clusterContainers},
62
+		Containers: cluster.ContainersOrExcuse{Containers: clusterContainers, Excuse: excuse},
54 63
 	}
55 64
 }
56 65
 

+ 5
- 6
daemon/daemon.go View File

@@ -24,6 +24,7 @@ import (
24 24
 	"github.com/weaveworks/flux/policy"
25 25
 	"github.com/weaveworks/flux/registry"
26 26
 	"github.com/weaveworks/flux/release"
27
+	"github.com/weaveworks/flux/resource"
27 28
 	"github.com/weaveworks/flux/update"
28 29
 )
29 30
 
@@ -520,14 +521,13 @@ func (d *Daemon) LogEvent(ev event.Event) error {
520 521
 
521 522
 // vvv helpers vvv
522 523
 
523
-func containers2containers(cs []cluster.Container) []v6.Container {
524
+func containers2containers(cs []resource.Container) []v6.Container {
524 525
 	res := make([]v6.Container, len(cs))
525 526
 	for i, c := range cs {
526
-		id, _ := image.ParseRef(c.Image)
527 527
 		res[i] = v6.Container{
528 528
 			Name: c.Name,
529 529
 			Current: image.Info{
530
-				ID: id,
530
+				ID: c.Image,
531 531
 			},
532 532
 		}
533 533
 	}
@@ -536,8 +536,7 @@ func containers2containers(cs []cluster.Container) []v6.Container {
536 536
 
537 537
 func containersWithAvailable(service cluster.Controller, images update.ImageMap) (res []v6.Container) {
538 538
 	for _, c := range service.ContainersOrNil() {
539
-		im, _ := image.ParseRef(c.Image)
540
-		available := images.Available(im.Name)
539
+		available := images.Available(c.Image.Name)
541 540
 		availableErr := ""
542 541
 		if available == nil {
543 542
 			availableErr = registry.ErrNoImageData.Error()
@@ -545,7 +544,7 @@ func containersWithAvailable(service cluster.Controller, images update.ImageMap)
545 544
 		res = append(res, v6.Container{
546 545
 			Name: c.Name,
547 546
 			Current: image.Info{
548
-				ID: im,
547
+				ID: c.Image,
549 548
 			},
550 549
 			Available:      available,
551 550
 			AvailableError: availableErr,

+ 14
- 7
daemon/daemon_test.go View File

@@ -344,8 +344,15 @@ func TestDaemon_JobStatusWithNoCache(t *testing.T) {
344 344
 }
345 345
 
346 346
 func makeImageInfo(ref string, t time.Time) image.Info {
347
-	r, _ := image.ParseRef(ref)
348
-	return image.Info{ID: r, CreatedAt: t}
347
+	return image.Info{ID: mustParseImageRef(ref), CreatedAt: t}
348
+}
349
+
350
+func mustParseImageRef(ref string) image.Ref {
351
+	r, err := image.ParseRef(ref)
352
+	if err != nil {
353
+		panic(err)
354
+	}
355
+	return r
349 356
 }
350 357
 
351 358
 func mockDaemon(t *testing.T) (*Daemon, func(), func(), *cluster.Mock, *mockEventWriter) {
@@ -354,10 +361,10 @@ func mockDaemon(t *testing.T) (*Daemon, func(), func(), *cluster.Mock, *mockEven
354 361
 	singleService := cluster.Controller{
355 362
 		ID: flux.MustParseResourceID(svc),
356 363
 		Containers: cluster.ContainersOrExcuse{
357
-			Containers: []cluster.Container{
364
+			Containers: []resource.Container{
358 365
 				{
359 366
 					Name:  container,
360
-					Image: currentHelloImage,
367
+					Image: mustParseImageRef(currentHelloImage),
361 368
 				},
362 369
 			},
363 370
 		},
@@ -367,10 +374,10 @@ func mockDaemon(t *testing.T) (*Daemon, func(), func(), *cluster.Mock, *mockEven
367 374
 		cluster.Controller{
368 375
 			ID: flux.MakeResourceID("another", "deployment", "service"),
369 376
 			Containers: cluster.ContainersOrExcuse{
370
-				Containers: []cluster.Container{
377
+				Containers: []resource.Container{
371 378
 					{
372
-						Name:  "it doesn't matter",
373
-						Image: "another/service:latest",
379
+						Name:  "it-doesn't-matter",
380
+						Image: mustParseImageRef("another/service:latest"),
374 381
 					},
375 382
 				},
376 383
 			},

+ 1
- 2
daemon/images.go View File

@@ -9,7 +9,6 @@ import (
9 9
 
10 10
 	"github.com/weaveworks/flux"
11 11
 	"github.com/weaveworks/flux/git"
12
-	"github.com/weaveworks/flux/image"
13 12
 	"github.com/weaveworks/flux/policy"
14 13
 	"github.com/weaveworks/flux/update"
15 14
 )
@@ -46,7 +45,7 @@ func (d *Daemon) pollForNewImages(logger log.Logger) {
46 45
 		for _, container := range service.ContainersOrNil() {
47 46
 			logger := log.With(logger, "service", service.ID, "container", container.Name, "currentimage", container.Image)
48 47
 
49
-			currentImageID, err := image.ParseRef(container.Image)
48
+			currentImageID := container.Image
50 49
 			if err != nil {
51 50
 				logger.Log("error", err)
52 51
 				continue

+ 17
- 12
release/releaser_test.go View File

@@ -15,6 +15,7 @@ import (
15 15
 	"github.com/weaveworks/flux/git/gittest"
16 16
 	"github.com/weaveworks/flux/image"
17 17
 	registryMock "github.com/weaveworks/flux/registry/mock"
18
+	"github.com/weaveworks/flux/resource"
18 19
 	"github.com/weaveworks/flux/update"
19 20
 )
20 21
 
@@ -32,20 +33,24 @@ var (
32 33
 	hwSvc         = cluster.Controller{
33 34
 		ID: hwSvcID,
34 35
 		Containers: cluster.ContainersOrExcuse{
35
-			Containers: []cluster.Container{
36
-				cluster.Container{
36
+			Containers: []resource.Container{
37
+				{
37 38
 					Name:  helloContainer,
38
-					Image: oldImage,
39
+					Image: oldRef,
39 40
 				},
40
-				cluster.Container{
41
+				{
41 42
 					Name:  sidecarContainer,
42
-					Image: sidecarImage,
43
+					Image: sidecarRef,
43 44
 				},
44 45
 			},
45 46
 		},
46 47
 	}
47 48
 
48
-	oldLockedImg     = "quay.io/weaveworks/locked-service:1"
49
+	testServiceRef, _ = image.ParseRef("quay.io/weaveworks/test-service:1")
50
+
51
+	oldLockedImg    = "quay.io/weaveworks/locked-service:1"
52
+	oldLockedRef, _ = image.ParseRef(oldLockedImg)
53
+
49 54
 	newLockedImg     = "quay.io/weaveworks/locked-service:2"
50 55
 	newLockedID, _   = image.ParseRef(newLockedImg)
51 56
 	lockedSvcID, _   = flux.ParseResourceID("default:deployment/locked-service")
@@ -53,10 +58,10 @@ var (
53 58
 	lockedSvc        = cluster.Controller{
54 59
 		ID: lockedSvcID,
55 60
 		Containers: cluster.ContainersOrExcuse{
56
-			Containers: []cluster.Container{
57
-				cluster.Container{
61
+			Containers: []resource.Container{
62
+				{
58 63
 					Name:  "locked-service",
59
-					Image: oldLockedImg,
64
+					Image: oldLockedRef,
60 65
 				},
61 66
 			},
62 67
 		},
@@ -65,10 +70,10 @@ var (
65 70
 	testSvc = cluster.Controller{
66 71
 		ID: flux.MustParseResourceID("default:deployment/test-service"),
67 72
 		Containers: cluster.ContainersOrExcuse{
68
-			Containers: []cluster.Container{
69
-				cluster.Container{
73
+			Containers: []resource.Container{
74
+				{
70 75
 					Name:  "test-service",
71
-					Image: "quay.io/weaveworks/test-service:1",
76
+					Image: testServiceRef,
72 77
 				},
73 78
 			},
74 79
 		},

+ 6
- 0
resource/resource.go View File

@@ -2,6 +2,7 @@ package resource
2 2
 
3 3
 import (
4 4
 	"github.com/weaveworks/flux"
5
+	"github.com/weaveworks/flux/image"
5 6
 	"github.com/weaveworks/flux/policy"
6 7
 )
7 8
 
@@ -12,3 +13,8 @@ type Resource interface {
12 13
 	Source() string              // where did this come from (informational)
13 14
 	Bytes() []byte               // the definition, for sending to cluster.Sync
14 15
 }
16
+
17
+type Container struct {
18
+	Name  string
19
+	Image image.Ref
20
+}

+ 4
- 8
update/automated.go View File

@@ -6,8 +6,8 @@ import (
6 6
 
7 7
 	"github.com/go-kit/kit/log"
8 8
 	"github.com/weaveworks/flux"
9
-	"github.com/weaveworks/flux/cluster"
10 9
 	"github.com/weaveworks/flux/image"
10
+	"github.com/weaveworks/flux/resource"
11 11
 )
12 12
 
13 13
 type Automated struct {
@@ -16,11 +16,11 @@ type Automated struct {
16 16
 
17 17
 type Change struct {
18 18
 	ServiceID flux.ResourceID
19
-	Container cluster.Container
19
+	Container resource.Container
20 20
 	ImageID   image.Ref
21 21
 }
22 22
 
23
-func (a *Automated) Add(service flux.ResourceID, container cluster.Container, image image.Ref) {
23
+func (a *Automated) Add(service flux.ResourceID, container resource.Container, image image.Ref) {
24 24
 	a.Changes = append(a.Changes, Change{service, container, image})
25 25
 }
26 26
 
@@ -100,11 +100,7 @@ func (a *Automated) calculateImageUpdates(rc ReleaseContext, candidates []*Contr
100 100
 		changes := serviceMap[u.ResourceID]
101 101
 		containerUpdates := []ContainerUpdate{}
102 102
 		for _, container := range containers {
103
-			currentImageID, err := image.ParseRef(container.Image)
104
-			if err != nil {
105
-				return nil, err
106
-			}
107
-
103
+			currentImageID := container.Image
108 104
 			for _, change := range changes {
109 105
 				if change.Container.Name != container.Name {
110 106
 					continue

+ 1
- 3
update/filter.go View File

@@ -31,9 +31,7 @@ func (f *SpecificImageFilter) Filter(u ControllerUpdate) ControllerResult {
31 31
 	}
32 32
 	// For each container in update
33 33
 	for _, c := range u.Controller.Containers.Containers {
34
-		cID, _ := image.ParseRef(c.Image)
35
-		// If container image == image in update
36
-		if cID.CanonicalName() == f.Img.CanonicalName() {
34
+		if c.Image.CanonicalName() == f.Img.CanonicalName() {
37 35
 			// We want to update this
38 36
 			return ControllerResult{}
39 37
 		}

+ 1
- 6
update/images.go View File

@@ -73,12 +73,7 @@ func CollectAvailableImages(reg registry.Registry, services []cluster.Controller
73 73
 	images := infoMap{}
74 74
 	for _, service := range services {
75 75
 		for _, container := range service.ContainersOrNil() {
76
-			id, err := image.ParseRef(container.Image)
77
-			if err != nil {
78
-				// container is running an invalid image id? what?
79
-				return ImageMap{}, err
80
-			}
81
-			images[id.CanonicalName()] = nil
76
+			images[container.Image.CanonicalName()] = nil
82 77
 		}
83 78
 	}
84 79
 	for name := range images {

+ 1
- 6
update/release.go View File

@@ -229,12 +229,7 @@ func (s ReleaseSpec) calculateImageUpdates(rc ReleaseContext, candidates []*Cont
229 229
 		var containerUpdates []ContainerUpdate
230 230
 
231 231
 		for _, container := range containers {
232
-			currentImageID, err := image.ParseRef(container.Image)
233
-			if err != nil {
234
-				// We may hope never to find a malformed image ID, but
235
-				// anything is possible.
236
-				return nil, err
237
-			}
232
+			currentImageID := container.Image
238 233
 
239 234
 			latestImage, ok := images.LatestImage(currentImageID.Name, "*")
240 235
 			if !ok {

Loading…
Cancel
Save