Browse code

Fixing #24631, inspect output on swarm object types without labels is empty object {}

Signed-off-by: Arash Deshmeh <adeshmeh@ca.ibm.com>

Arash Deshmeh authored on 2017/02/13 17:07:03
Showing 7 changed files
... ...
@@ -17,7 +17,7 @@ type Meta struct {
17 17
 // Annotations represents how to describe an object.
18 18
 type Annotations struct {
19 19
 	Name   string            `json:",omitempty"`
20
-	Labels map[string]string `json:",omitempty"`
20
+	Labels map[string]string `json:"Labels"`
21 21
 }
22 22
 
23 23
 // Driver represents a driver (network, logging).
... ...
@@ -39,8 +39,7 @@ func networkFromGRPC(n *swarmapi.Network) types.Network {
39 39
 		network.UpdatedAt, _ = gogotypes.TimestampFromProto(n.Meta.UpdatedAt)
40 40
 
41 41
 		//Annotations
42
-		network.Spec.Name = n.Spec.Annotations.Name
43
-		network.Spec.Labels = n.Spec.Annotations.Labels
42
+		network.Spec.Annotations = annotationsFromGRPC(n.Spec.Annotations)
44 43
 
45 44
 		//DriverConfiguration
46 45
 		if n.Spec.DriverConfig != nil {
... ...
@@ -30,8 +30,7 @@ func NodeFromGRPC(n swarmapi.Node) types.Node {
30 30
 	node.UpdatedAt, _ = gogotypes.TimestampFromProto(n.Meta.UpdatedAt)
31 31
 
32 32
 	//Annotations
33
-	node.Spec.Name = n.Spec.Annotations.Name
34
-	node.Spec.Labels = n.Spec.Annotations.Labels
33
+	node.Spec.Annotations = annotationsFromGRPC(n.Spec.Annotations)
35 34
 
36 35
 	//Description
37 36
 	if n.Description != nil {
... ...
@@ -11,11 +11,8 @@ func SecretFromGRPC(s *swarmapi.Secret) swarmtypes.Secret {
11 11
 	secret := swarmtypes.Secret{
12 12
 		ID: s.ID,
13 13
 		Spec: swarmtypes.SecretSpec{
14
-			Annotations: swarmtypes.Annotations{
15
-				Name:   s.Spec.Annotations.Name,
16
-				Labels: s.Spec.Annotations.Labels,
17
-			},
18
-			Data: s.Spec.Data,
14
+			Annotations: annotationsFromGRPC(s.Spec.Annotations),
15
+			Data:        s.Spec.Data,
19 16
 		},
20 17
 	}
21 18
 
... ...
@@ -70,11 +70,7 @@ func serviceSpecFromGRPC(spec *swarmapi.ServiceSpec) *types.ServiceSpec {
70 70
 
71 71
 	containerConfig := spec.Task.Runtime.(*swarmapi.TaskSpec_Container).Container
72 72
 	convertedSpec := &types.ServiceSpec{
73
-		Annotations: types.Annotations{
74
-			Name:   spec.Annotations.Name,
75
-			Labels: spec.Annotations.Labels,
76
-		},
77
-
73
+		Annotations: annotationsFromGRPC(spec.Annotations),
78 74
 		TaskTemplate: types.TaskSpec{
79 75
 			ContainerSpec: containerSpecFromGRPC(containerConfig),
80 76
 			Resources:     resourcesFromGRPC(spec.Task.Resources),
... ...
@@ -236,6 +232,19 @@ func ServiceSpecToGRPC(s types.ServiceSpec) (swarmapi.ServiceSpec, error) {
236 236
 	return spec, nil
237 237
 }
238 238
 
239
+func annotationsFromGRPC(ann swarmapi.Annotations) types.Annotations {
240
+	a := types.Annotations{
241
+		Name:   ann.Name,
242
+		Labels: ann.Labels,
243
+	}
244
+
245
+	if a.Labels == nil {
246
+		a.Labels = make(map[string]string)
247
+	}
248
+
249
+	return a
250
+}
251
+
239 252
 func resourcesFromGRPC(res *swarmapi.ResourceRequirements) *types.ResourceRequirements {
240 253
 	var resources *types.ResourceRequirements
241 254
 	if res != nil {
... ...
@@ -56,8 +56,7 @@ func SwarmFromGRPC(c swarmapi.Cluster) types.Swarm {
56 56
 	swarm.UpdatedAt, _ = gogotypes.TimestampFromProto(c.Meta.UpdatedAt)
57 57
 
58 58
 	// Annotations
59
-	swarm.Spec.Name = c.Spec.Annotations.Name
60
-	swarm.Spec.Labels = c.Spec.Annotations.Labels
59
+	swarm.Spec.Annotations = annotationsFromGRPC(c.Spec.Annotations)
61 60
 
62 61
 	return swarm
63 62
 }
... ...
@@ -21,14 +21,11 @@ func TaskFromGRPC(t swarmapi.Task) types.Task {
21 21
 	}
22 22
 
23 23
 	task := types.Task{
24
-		ID: t.ID,
25
-		Annotations: types.Annotations{
26
-			Name:   t.Annotations.Name,
27
-			Labels: t.Annotations.Labels,
28
-		},
29
-		ServiceID: t.ServiceID,
30
-		Slot:      int(t.Slot),
31
-		NodeID:    t.NodeID,
24
+		ID:          t.ID,
25
+		Annotations: annotationsFromGRPC(t.Annotations),
26
+		ServiceID:   t.ServiceID,
27
+		Slot:        int(t.Slot),
28
+		NodeID:      t.NodeID,
32 29
 		Spec: types.TaskSpec{
33 30
 			ContainerSpec: containerSpecFromGRPC(containerConfig),
34 31
 			Resources:     resourcesFromGRPC(t.Spec.Resources),