mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-03 01:06:27 +00:00
Merge pull request #2120 from brendandburns/uuid
Add some blocking so that we don't generate identical UUIDs for API objects.
This commit is contained in:
commit
c4aa4d28a6
@ -28,8 +28,6 @@ import (
|
|||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
|
||||||
|
|
||||||
"code.google.com/p/go-uuid/uuid"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// PodLister is anything that knows how to list pods.
|
// PodLister is anything that knows how to list pods.
|
||||||
@ -64,7 +62,7 @@ func (rs *REST) Create(ctx api.Context, obj runtime.Object) (<-chan apiserver.RE
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(controller.Name) == 0 {
|
if len(controller.Name) == 0 {
|
||||||
controller.Name = uuid.NewUUID().String()
|
controller.Name = util.NewUUID().String()
|
||||||
}
|
}
|
||||||
// Pod Manifest ID should be assigned by the pod API
|
// Pod Manifest ID should be assigned by the pod API
|
||||||
controller.DesiredState.PodTemplate.DesiredState.Manifest.ID = ""
|
controller.DesiredState.PodTemplate.DesiredState.Manifest.ID = ""
|
||||||
|
@ -32,7 +32,6 @@ import (
|
|||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
|
||||||
|
|
||||||
"code.google.com/p/go-uuid/uuid"
|
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -93,7 +92,7 @@ func (rs *REST) Create(ctx api.Context, obj runtime.Object) (<-chan apiserver.RE
|
|||||||
if !api.ValidNamespace(ctx, &pod.ObjectMeta) {
|
if !api.ValidNamespace(ctx, &pod.ObjectMeta) {
|
||||||
return nil, errors.NewConflict("pod", pod.Namespace, fmt.Errorf("Pod.Namespace does not match the provided context"))
|
return nil, errors.NewConflict("pod", pod.Namespace, fmt.Errorf("Pod.Namespace does not match the provided context"))
|
||||||
}
|
}
|
||||||
pod.DesiredState.Manifest.UUID = uuid.NewUUID().String()
|
pod.DesiredState.Manifest.UUID = util.NewUUID().String()
|
||||||
if len(pod.Name) == 0 {
|
if len(pod.Name) == 0 {
|
||||||
pod.Name = pod.DesiredState.Manifest.UUID
|
pod.Name = pod.DesiredState.Manifest.UUID
|
||||||
}
|
}
|
||||||
|
46
pkg/util/uuid.go
Normal file
46
pkg/util/uuid.go
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2014 Google Inc. All rights reserved.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package util
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"code.google.com/p/go-uuid/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
type UUID interface {
|
||||||
|
String() string
|
||||||
|
}
|
||||||
|
|
||||||
|
var uuidLock sync.Mutex
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The UUID package is naive and can generate identical UUIDs if the time interval is quick enough.
|
||||||
|
* Block subsequent UUIDs for 200 Nanoseconds, the UUID uses 100 ns increments, we block for 200 to be safe
|
||||||
|
* Blocks in a go routine, so that the caller doesn't have to wait.
|
||||||
|
* TODO: save old unused UUIDs so that no one has to block.
|
||||||
|
*/
|
||||||
|
func NewUUID() UUID {
|
||||||
|
uuidLock.Lock()
|
||||||
|
result := uuid.NewUUID()
|
||||||
|
go func() {
|
||||||
|
time.Sleep(200 * time.Nanosecond)
|
||||||
|
uuidLock.Unlock()
|
||||||
|
}()
|
||||||
|
return result
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user