func NewUUID() types.UID { uuidLock.Lock() defer uuidLock.Unlock() result := uuid.NewUUID() // The UUID package is naive and can generate identical UUIDs if the // time interval is quick enough. // The UUID uses 100 ns increments so it's short enough to actively // wait for a new value. for uuid.Equal(lastUUID, result) == true { result = uuid.NewUUID() } lastUUID = result return types.UID(result.String()) }
func TestAPIObjectMeta(t *testing.T) { j := &api.Pod{ TypeMeta: unversioned.TypeMeta{APIVersion: "/a", Kind: "b"}, ObjectMeta: api.ObjectMeta{ Namespace: "bar", Name: "foo", GenerateName: "prefix", UID: "uid", ResourceVersion: "1", SelfLink: "some/place/only/we/know", Labels: map[string]string{"foo": "bar"}, Annotations: map[string]string{"x": "y"}, }, } var _ meta.Object = &j.ObjectMeta var _ meta.ObjectMetaAccessor = j accessor, err := meta.Accessor(j) if err != nil { t.Fatalf("unexpected error: %v", err) } if accessor != meta.Object(&j.ObjectMeta) { t.Fatalf("should have returned the same pointer: %#v %#v", accessor, j) } if e, a := "bar", accessor.GetNamespace(); e != a { t.Errorf("expected %v, got %v", e, a) } if e, a := "foo", accessor.GetName(); e != a { t.Errorf("expected %v, got %v", e, a) } if e, a := "prefix", accessor.GetGenerateName(); e != a { t.Errorf("expected %v, got %v", e, a) } if e, a := "uid", string(accessor.GetUID()); e != a { t.Errorf("expected %v, got %v", e, a) } if e, a := "1", accessor.GetResourceVersion(); e != a { t.Errorf("expected %v, got %v", e, a) } if e, a := "some/place/only/we/know", accessor.GetSelfLink(); e != a { t.Errorf("expected %v, got %v", e, a) } typeAccessor, err := meta.TypeAccessor(j) if err != nil { t.Fatalf("unexpected error: %v", err) } if e, a := "a", typeAccessor.GetAPIVersion(); e != a { t.Errorf("expected %v, got %v", e, a) } if e, a := "b", typeAccessor.GetKind(); e != a { t.Errorf("expected %v, got %v", e, a) } accessor.SetNamespace("baz") accessor.SetName("bar") accessor.SetGenerateName("generate") accessor.SetUID("other") typeAccessor.SetAPIVersion("c") typeAccessor.SetKind("d") accessor.SetResourceVersion("2") accessor.SetSelfLink("google.com") // Prove that accessor changes the original object. if e, a := "baz", j.Namespace; e != a { t.Errorf("expected %v, got %v", e, a) } if e, a := "bar", j.Name; e != a { t.Errorf("expected %v, got %v", e, a) } if e, a := "generate", j.GenerateName; e != a { t.Errorf("expected %v, got %v", e, a) } if e, a := types.UID("other"), j.UID; e != a { t.Errorf("expected %v, got %v", e, a) } if e, a := "c", j.APIVersion; e != a { t.Errorf("expected %v, got %v", e, a) } if e, a := "d", j.Kind; e != a { t.Errorf("expected %v, got %v", e, a) } if e, a := "2", j.ResourceVersion; e != a { t.Errorf("expected %v, got %v", e, a) } if e, a := "google.com", j.SelfLink; e != a { t.Errorf("expected %v, got %v", e, a) } typeAccessor.SetAPIVersion("d") typeAccessor.SetKind("e") if e, a := "d", j.APIVersion; e != a { t.Errorf("expected %v, got %v", e, a) } if e, a := "e", j.Kind; e != a { t.Errorf("expected %v, got %v", e, a) } }
func podRef(uid string) *api.ObjectReference { ref := api.ObjectReference{UID: types.UID(uid)} return &ref }