From 253ce4b6fedb8496bb7e59324ce37f649507581d Mon Sep 17 00:00:00 2001 From: Federico Simoncelli Date: Tue, 24 Mar 2015 05:36:23 -0400 Subject: [PATCH] replication: annotate replicated pods with controller name Signed-off-by: Federico Simoncelli --- pkg/controller/replication_controller.go | 16 ++++++++++++++++ pkg/controller/replication_controller_test.go | 4 ++++ 2 files changed, 20 insertions(+) diff --git a/pkg/controller/replication_controller.go b/pkg/controller/replication_controller.go index bdf77eedacf..7c02cc41643 100644 --- a/pkg/controller/replication_controller.go +++ b/pkg/controller/replication_controller.go @@ -17,6 +17,7 @@ limitations under the License. package controller import ( + "encoding/json" "fmt" "sync" "time" @@ -61,6 +62,7 @@ type RealPodControl struct { // Time period of main replication controller sync loop const DefaultSyncPeriod = 5 * time.Second +const CreatedByAnnotation = "kubernetes.io/created-by" func (r RealPodControl) createReplica(namespace string, controller api.ReplicationController) { desiredLabels := make(labels.Set) @@ -72,6 +74,20 @@ func (r RealPodControl) createReplica(namespace string, controller api.Replicati desiredAnnotations[k] = v } + createdByRef, err := api.GetReference(&controller) + if err != nil { + util.HandleError(fmt.Errorf("unable to get controller reference: %v", err)) + return + } + + createdByRefJson, err := json.Marshal(createdByRef) + if err != nil { + util.HandleError(fmt.Errorf("unable to serialize controller reference: %v", err)) + return + } + + desiredAnnotations[CreatedByAnnotation] = string(createdByRefJson) + // use the dash (if the name isn't too long) to make the pod name a bit prettier prefix := fmt.Sprintf("%s-", controller.Name) if ok, _ := validation.ValidatePodName(prefix, true); !ok { diff --git a/pkg/controller/replication_controller_test.go b/pkg/controller/replication_controller_test.go index 8adb7094ba5..55d369eb163 100644 --- a/pkg/controller/replication_controller_test.go +++ b/pkg/controller/replication_controller_test.go @@ -40,6 +40,10 @@ type FakePodControl struct { lock sync.Mutex } +func init() { + api.ForTesting_ReferencesAllowBlankSelfLinks = true +} + func (f *FakePodControl) createReplica(namespace string, spec api.ReplicationController) { f.lock.Lock() defer f.lock.Unlock()