Browse code

Add labels to templates

csrwng authored on 2015/02/12 05:35:31
Showing 7 changed files
... ...
@@ -372,7 +372,7 @@ func (d *TemplateDescriber) DescribeParameters(params []templateapi.Parameter, o
372 372
 	}
373 373
 }
374 374
 
375
-func (d *TemplateDescriber) DescribeObjects(objects []runtime.Object, out *tabwriter.Writer) {
375
+func (d *TemplateDescriber) DescribeObjects(objects []runtime.Object, labels map[string]string, out *tabwriter.Writer) {
376 376
 	formatString(out, "Objects", " ")
377 377
 
378 378
 	indent := "    "
... ...
@@ -395,6 +395,10 @@ func (d *TemplateDescriber) DescribeObjects(objects []runtime.Object, out *tabwr
395 395
 		}
396 396
 		formatAnnotations(out, meta, indent)
397 397
 	}
398
+	if len(labels) > 0 {
399
+		out.Write([]byte("\n"))
400
+		formatString(out, indent+"Common Labels", formatLabels(labels))
401
+	}
398 402
 }
399 403
 
400 404
 func (d *TemplateDescriber) Describe(namespace, name string) (string, error) {
... ...
@@ -410,7 +414,7 @@ func (d *TemplateDescriber) Describe(namespace, name string) (string, error) {
410 410
 		out.Flush()
411 411
 		d.DescribeParameters(template.Parameters, out)
412 412
 		out.Write([]byte("\n"))
413
-		d.DescribeObjects(template.Objects, out)
413
+		d.DescribeObjects(template.Objects, template.ObjectLabels, out)
414 414
 		return nil
415 415
 	})
416 416
 }
... ...
@@ -16,6 +16,10 @@ type Template struct {
16 16
 
17 17
 	// Required: A list of resources to create
18 18
 	Objects []runtime.Object
19
+
20
+	// Optional: ObjectLabels is a set of labels that are applied to every
21
+	// object during the Template to Config transformation
22
+	ObjectLabels map[string]string
19 23
 }
20 24
 
21 25
 // TemplateList is a list of Template objects.
... ...
@@ -14,12 +14,21 @@ func init() {
14 14
 			if err := s.Convert(&in.Objects, &out.Items, 0); err != nil {
15 15
 				return err
16 16
 			}
17
-			return s.DefaultConvert(in, out, conversion.IgnoreMissingFields)
17
+			//FIXME: DefaultConvert should not overwrite the Labels field on the
18
+			//       the base object. This is likely a bug in the DefaultConvert
19
+			//       code. For now, it is called before converting the labels.
20
+			if err := s.DefaultConvert(in, out, conversion.IgnoreMissingFields); err != nil {
21
+				return err
22
+			}
23
+			return s.Convert(&in.ObjectLabels, &out.Labels, 0)
18 24
 		},
19 25
 		func(in *Template, out *newer.Template, s conversion.Scope) error {
20 26
 			if err := s.Convert(&in.Items, &out.Objects, 0); err != nil {
21 27
 				return err
22 28
 			}
29
+			if err := s.Convert(&in.Labels, &out.ObjectLabels, 0); err != nil {
30
+				return err
31
+			}
23 32
 			return s.DefaultConvert(in, out, conversion.IgnoreMissingFields)
24 33
 		},
25 34
 	)
... ...
@@ -17,6 +17,10 @@ type Template struct {
17 17
 	// Optional: Parameters is an array of Parameters used during the
18 18
 	// Template to Config transformation.
19 19
 	Parameters []Parameter `json:"parameters,omitempty"`
20
+
21
+	// Optional: Labels is a set of labels that are applied to every
22
+	// object during the Template to Config transformation
23
+	Labels map[string]string `json:"labels,omitempty"`
20 24
 }
21 25
 
22 26
 // TemplateList is a list of Template objects.
... ...
@@ -48,5 +48,6 @@ func validateTemplateBody(template *api.Template) (errs errors.ValidationErrorLi
48 48
 		paramErr := ValidateParameter(&template.Parameters[i])
49 49
 		errs = append(errs, paramErr.PrefixIndex(i).Prefix("parameters")...)
50 50
 	}
51
+	errs = append(errs, validation.ValidateLabels(template.ObjectLabels, "labels")...)
51 52
 	return
52 53
 }
... ...
@@ -96,10 +96,13 @@ func (s *REST) Create(ctx kapi.Context, obj runtime.Object) (runtime.Object, err
96 96
 		glog.V(1).Infof(utilerr.NewAggregate(err).Error())
97 97
 	}
98 98
 
99
-	if err := template.AddConfigLabels(cfg, labels.Set{"template": tpl.Name}); len(err) > 0 {
100
-		// TODO: We don't report the processing errors to users as there is no
101
-		// good way how to do it for just some items.
102
-		glog.V(1).Infof(utilerr.NewAggregate(err).Error())
99
+	if tpl.ObjectLabels != nil {
100
+		objectLabels := labels.Set(tpl.ObjectLabels)
101
+		if err := template.AddConfigLabels(cfg, objectLabels); len(err) > 0 {
102
+			// TODO: We don't report the processing errors to users as there is no
103
+			// good way how to do it for just some items.
104
+			glog.V(1).Infof(utilerr.NewAggregate(err).Error())
105
+		}
103 106
 	}
104 107
 	return cfg, nil
105 108
 }
... ...
@@ -4,6 +4,7 @@ import (
4 4
 	"testing"
5 5
 
6 6
 	kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
7
+	"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
7 8
 
8 9
 	config "github.com/openshift/origin/pkg/config/api"
9 10
 	template "github.com/openshift/origin/pkg/template/api"
... ...
@@ -32,3 +33,49 @@ func TestNewRESTDefaultsName(t *testing.T) {
32 32
 		t.Fatalf("unexpected return object: %#v", obj)
33 33
 	}
34 34
 }
35
+
36
+func TestNewRESTTemplateLabels(t *testing.T) {
37
+	testLabels := map[string]string{
38
+		"label1": "value1",
39
+		"label2": "value2",
40
+	}
41
+	storage := NewREST()
42
+	obj, err := storage.Create(nil, &template.Template{
43
+		ObjectMeta: kapi.ObjectMeta{
44
+			Name: "test",
45
+		},
46
+		Objects: []runtime.Object{
47
+			&kapi.Service{
48
+				ObjectMeta: kapi.ObjectMeta{
49
+					Name: "test-service",
50
+				},
51
+				Spec: kapi.ServiceSpec{
52
+					Port:            80,
53
+					Protocol:        kapi.ProtocolTCP,
54
+					SessionAffinity: kapi.AffinityTypeNone,
55
+				},
56
+			},
57
+		},
58
+		ObjectLabels: testLabels,
59
+	})
60
+	if err != nil {
61
+		t.Fatalf("unexpected error: %v", err)
62
+	}
63
+	config, ok := obj.(*config.Config)
64
+	if !ok {
65
+		t.Fatalf("unexpected return object: %#v", obj)
66
+	}
67
+	svc, ok := config.Items[0].(*kapi.Service)
68
+	if !ok {
69
+		t.Fatalf("Unexpected object in config: %#v", svc)
70
+	}
71
+	for k, v := range testLabels {
72
+		value, ok := svc.Labels[k]
73
+		if !ok {
74
+			t.Fatalf("Missing output label: %s", k)
75
+		}
76
+		if value != v {
77
+			t.Fatalf("Unexpected label value: %s", value)
78
+		}
79
+	}
80
+}