uuid.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package uuid
  14. import (
  15. "sync"
  16. "github.com/pborman/uuid"
  17. "k8s.io/kubernetes/pkg/types"
  18. )
  19. var uuidLock sync.Mutex
  20. var lastUUID uuid.UUID
  21. func NewUUID() types.UID {
  22. uuidLock.Lock()
  23. defer uuidLock.Unlock()
  24. result := uuid.NewUUID()
  25. // The UUID package is naive and can generate identical UUIDs if the
  26. // time interval is quick enough.
  27. // The UUID uses 100 ns increments so it's short enough to actively
  28. // wait for a new value.
  29. for uuid.Equal(lastUUID, result) == true {
  30. result = uuid.NewUUID()
  31. }
  32. lastUUID = result
  33. return types.UID(result.String())
  34. }