Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
666a41c2
Commit
666a41c2
authored
Jul 11, 2018
by
Janet Kuo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Safe encode template hash value to make it consistent with resource name
parent
24ee75e2
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
23 additions
and
20 deletions
+23
-20
BUILD
pkg/controller/BUILD
+1
-0
controller_utils.go
pkg/controller/controller_utils.go
+6
-3
BUILD
pkg/controller/daemon/BUILD
+0
-1
daemon_controller_test.go
pkg/controller/daemon/daemon_controller_test.go
+1
-1
update.go
pkg/controller/daemon/update.go
+2
-3
BUILD
pkg/controller/deployment/BUILD
+0
-1
sync.go
pkg/controller/deployment/sync.go
+2
-3
hash_test.go
pkg/controller/deployment/util/hash_test.go
+1
-1
controller_history.go
pkg/controller/history/controller_history.go
+6
-7
deployment_test.go
test/integration/deployment/deployment_test.go
+4
-0
No files found.
pkg/controller/BUILD
View file @
666a41c2
...
@@ -72,6 +72,7 @@ go_library(
...
@@ -72,6 +72,7 @@ go_library(
"//staging/src/k8s.io/apimachinery/pkg/types:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/types:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/clock:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/clock:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/errors:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/errors:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/rand:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/runtime:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/runtime:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/sets:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/sets:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/strategicpatch:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/strategicpatch:go_default_library",
...
...
pkg/controller/controller_utils.go
View file @
666a41c2
...
@@ -33,6 +33,7 @@ import (
...
@@ -33,6 +33,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/clock"
"k8s.io/apimachinery/pkg/util/clock"
"k8s.io/apimachinery/pkg/util/rand"
utilruntime
"k8s.io/apimachinery/pkg/util/runtime"
utilruntime
"k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/strategicpatch"
"k8s.io/apimachinery/pkg/util/strategicpatch"
...
@@ -1033,8 +1034,10 @@ func WaitForCacheSync(controllerName string, stopCh <-chan struct{}, cacheSyncs
...
@@ -1033,8 +1034,10 @@ func WaitForCacheSync(controllerName string, stopCh <-chan struct{}, cacheSyncs
return
true
return
true
}
}
// ComputeHash returns a hash value calculated from pod template and a collisionCount to avoid hash collision
// ComputeHash returns a hash value calculated from pod template and
func
ComputeHash
(
template
*
v1
.
PodTemplateSpec
,
collisionCount
*
int32
)
uint32
{
// a collisionCount to avoid hash collision. The hash will be safe encoded to
// avoid bad words.
func
ComputeHash
(
template
*
v1
.
PodTemplateSpec
,
collisionCount
*
int32
)
string
{
podTemplateSpecHasher
:=
fnv
.
New32a
()
podTemplateSpecHasher
:=
fnv
.
New32a
()
hashutil
.
DeepHashObject
(
podTemplateSpecHasher
,
*
template
)
hashutil
.
DeepHashObject
(
podTemplateSpecHasher
,
*
template
)
...
@@ -1045,5 +1048,5 @@ func ComputeHash(template *v1.PodTemplateSpec, collisionCount *int32) uint32 {
...
@@ -1045,5 +1048,5 @@ func ComputeHash(template *v1.PodTemplateSpec, collisionCount *int32) uint32 {
podTemplateSpecHasher
.
Write
(
collisionCountBytes
)
podTemplateSpecHasher
.
Write
(
collisionCountBytes
)
}
}
return
podTemplateSpecHasher
.
Sum32
(
)
return
rand
.
SafeEncodeString
(
fmt
.
Sprint
(
podTemplateSpecHasher
.
Sum32
())
)
}
}
pkg/controller/daemon/BUILD
View file @
666a41c2
...
@@ -36,7 +36,6 @@ go_library(
...
@@ -36,7 +36,6 @@ go_library(
"//staging/src/k8s.io/apimachinery/pkg/util/errors:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/errors:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/intstr:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/intstr:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/json:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/json:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/rand:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/runtime:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/runtime:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/sets:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/sets:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/wait:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/wait:go_default_library",
...
...
pkg/controller/daemon/daemon_controller_test.go
View file @
666a41c2
...
@@ -167,7 +167,7 @@ func newPod(podName string, nodeName string, label map[string]string, ds *apps.D
...
@@ -167,7 +167,7 @@ func newPod(podName string, nodeName string, label map[string]string, ds *apps.D
var
podSpec
v1
.
PodSpec
var
podSpec
v1
.
PodSpec
// Copy pod spec from DaemonSet template, or use a default one if DaemonSet is nil
// Copy pod spec from DaemonSet template, or use a default one if DaemonSet is nil
if
ds
!=
nil
{
if
ds
!=
nil
{
hash
:=
fmt
.
Sprint
(
controller
.
ComputeHash
(
&
ds
.
Spec
.
Template
,
ds
.
Status
.
CollisionCount
)
)
hash
:=
controller
.
ComputeHash
(
&
ds
.
Spec
.
Template
,
ds
.
Status
.
CollisionCount
)
newLabels
=
labelsutil
.
CloneAndAddLabel
(
label
,
apps
.
DefaultDaemonSetUniqueLabelKey
,
hash
)
newLabels
=
labelsutil
.
CloneAndAddLabel
(
label
,
apps
.
DefaultDaemonSetUniqueLabelKey
,
hash
)
podSpec
=
ds
.
Spec
.
Template
.
Spec
podSpec
=
ds
.
Spec
.
Template
.
Spec
}
else
{
}
else
{
...
...
pkg/controller/daemon/update.go
View file @
666a41c2
...
@@ -31,7 +31,6 @@ import (
...
@@ -31,7 +31,6 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime"
intstrutil
"k8s.io/apimachinery/pkg/util/intstr"
intstrutil
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/util/json"
"k8s.io/apimachinery/pkg/util/json"
"k8s.io/apimachinery/pkg/util/rand"
podutil
"k8s.io/kubernetes/pkg/api/v1/pod"
podutil
"k8s.io/kubernetes/pkg/api/v1/pod"
"k8s.io/kubernetes/pkg/controller"
"k8s.io/kubernetes/pkg/controller"
"k8s.io/kubernetes/pkg/controller/daemon/util"
"k8s.io/kubernetes/pkg/controller/daemon/util"
...
@@ -316,8 +315,8 @@ func (dsc *DaemonSetsController) snapshot(ds *apps.DaemonSet, revision int64) (*
...
@@ -316,8 +315,8 @@ func (dsc *DaemonSetsController) snapshot(ds *apps.DaemonSet, revision int64) (*
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
hash
:=
fmt
.
Sprint
(
controller
.
ComputeHash
(
&
ds
.
Spec
.
Template
,
ds
.
Status
.
CollisionCount
)
)
hash
:=
controller
.
ComputeHash
(
&
ds
.
Spec
.
Template
,
ds
.
Status
.
CollisionCount
)
name
:=
ds
.
Name
+
"-"
+
rand
.
SafeEncodeString
(
hash
)
name
:=
ds
.
Name
+
"-"
+
hash
history
:=
&
apps
.
ControllerRevision
{
history
:=
&
apps
.
ControllerRevision
{
ObjectMeta
:
metav1
.
ObjectMeta
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
name
,
Name
:
name
,
...
...
pkg/controller/deployment/BUILD
View file @
666a41c2
...
@@ -29,7 +29,6 @@ go_library(
...
@@ -29,7 +29,6 @@ go_library(
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/labels:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/labels:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/types:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/types:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/rand:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/runtime:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/runtime:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/wait:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/wait:go_default_library",
"//staging/src/k8s.io/client-go/informers/apps/v1:go_default_library",
"//staging/src/k8s.io/client-go/informers/apps/v1:go_default_library",
...
...
pkg/controller/deployment/sync.go
View file @
666a41c2
...
@@ -27,7 +27,6 @@ import (
...
@@ -27,7 +27,6 @@ import (
"k8s.io/api/core/v1"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/errors"
metav1
"k8s.io/apimachinery/pkg/apis/meta/v1"
metav1
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/rand"
"k8s.io/kubernetes/pkg/controller"
"k8s.io/kubernetes/pkg/controller"
deploymentutil
"k8s.io/kubernetes/pkg/controller/deployment/util"
deploymentutil
"k8s.io/kubernetes/pkg/controller/deployment/util"
labelsutil
"k8s.io/kubernetes/pkg/util/labels"
labelsutil
"k8s.io/kubernetes/pkg/util/labels"
...
@@ -181,7 +180,7 @@ func (dc *DeploymentController) getNewReplicaSet(d *apps.Deployment, rsList, old
...
@@ -181,7 +180,7 @@ func (dc *DeploymentController) getNewReplicaSet(d *apps.Deployment, rsList, old
// new ReplicaSet does not exist, create one.
// new ReplicaSet does not exist, create one.
newRSTemplate
:=
*
d
.
Spec
.
Template
.
DeepCopy
()
newRSTemplate
:=
*
d
.
Spec
.
Template
.
DeepCopy
()
podTemplateSpecHash
:=
fmt
.
Sprintf
(
"%d"
,
controller
.
ComputeHash
(
&
newRSTemplate
,
d
.
Status
.
CollisionCount
)
)
podTemplateSpecHash
:=
controller
.
ComputeHash
(
&
newRSTemplate
,
d
.
Status
.
CollisionCount
)
newRSTemplate
.
Labels
=
labelsutil
.
CloneAndAddLabel
(
d
.
Spec
.
Template
.
Labels
,
apps
.
DefaultDeploymentUniqueLabelKey
,
podTemplateSpecHash
)
newRSTemplate
.
Labels
=
labelsutil
.
CloneAndAddLabel
(
d
.
Spec
.
Template
.
Labels
,
apps
.
DefaultDeploymentUniqueLabelKey
,
podTemplateSpecHash
)
// Add podTemplateHash label to selector.
// Add podTemplateHash label to selector.
newRSSelector
:=
labelsutil
.
CloneSelectorAndAddLabel
(
d
.
Spec
.
Selector
,
apps
.
DefaultDeploymentUniqueLabelKey
,
podTemplateSpecHash
)
newRSSelector
:=
labelsutil
.
CloneSelectorAndAddLabel
(
d
.
Spec
.
Selector
,
apps
.
DefaultDeploymentUniqueLabelKey
,
podTemplateSpecHash
)
...
@@ -190,7 +189,7 @@ func (dc *DeploymentController) getNewReplicaSet(d *apps.Deployment, rsList, old
...
@@ -190,7 +189,7 @@ func (dc *DeploymentController) getNewReplicaSet(d *apps.Deployment, rsList, old
newRS
:=
apps
.
ReplicaSet
{
newRS
:=
apps
.
ReplicaSet
{
ObjectMeta
:
metav1
.
ObjectMeta
{
ObjectMeta
:
metav1
.
ObjectMeta
{
// Make the name deterministic, to ensure idempotence
// Make the name deterministic, to ensure idempotence
Name
:
d
.
Name
+
"-"
+
rand
.
SafeEncodeString
(
podTemplateSpecHash
)
,
Name
:
d
.
Name
+
"-"
+
podTemplateSpecHash
,
Namespace
:
d
.
Namespace
,
Namespace
:
d
.
Namespace
,
OwnerReferences
:
[]
metav1
.
OwnerReference
{
*
metav1
.
NewControllerRef
(
d
,
controllerKind
)},
OwnerReferences
:
[]
metav1
.
OwnerReference
{
*
metav1
.
NewControllerRef
(
d
,
controllerKind
)},
Labels
:
newRSTemplate
.
Labels
,
Labels
:
newRSTemplate
.
Labels
,
...
...
pkg/controller/deployment/util/hash_test.go
View file @
666a41c2
...
@@ -105,7 +105,7 @@ var podSpec string = `
...
@@ -105,7 +105,7 @@ var podSpec string = `
`
`
func
TestPodTemplateSpecHash
(
t
*
testing
.
T
)
{
func
TestPodTemplateSpecHash
(
t
*
testing
.
T
)
{
seenHashes
:=
make
(
map
[
uint32
]
int
)
seenHashes
:=
make
(
map
[
string
]
int
)
for
i
:=
0
;
i
<
1000
;
i
++
{
for
i
:=
0
;
i
<
1000
;
i
++
{
specJson
:=
strings
.
Replace
(
podSpec
,
"@@VERSION@@"
,
strconv
.
Itoa
(
i
),
1
)
specJson
:=
strings
.
Replace
(
podSpec
,
"@@VERSION@@"
,
strconv
.
Itoa
(
i
),
1
)
...
...
pkg/controller/history/controller_history.go
View file @
666a41c2
...
@@ -47,12 +47,12 @@ const ControllerRevisionHashLabel = "controller.kubernetes.io/hash"
...
@@ -47,12 +47,12 @@ const ControllerRevisionHashLabel = "controller.kubernetes.io/hash"
// ControllerRevisionName returns the Name for a ControllerRevision in the form prefix-hash. If the length
// ControllerRevisionName returns the Name for a ControllerRevision in the form prefix-hash. If the length
// of prefix is greater than 223 bytes, it is truncated to allow for a name that is no larger than 253 bytes.
// of prefix is greater than 223 bytes, it is truncated to allow for a name that is no larger than 253 bytes.
func
ControllerRevisionName
(
prefix
string
,
hash
uint32
)
string
{
func
ControllerRevisionName
(
prefix
string
,
hash
string
)
string
{
if
len
(
prefix
)
>
223
{
if
len
(
prefix
)
>
223
{
prefix
=
prefix
[
:
223
]
prefix
=
prefix
[
:
223
]
}
}
return
fmt
.
Sprintf
(
"%s-%s"
,
prefix
,
rand
.
SafeEncodeString
(
strconv
.
FormatInt
(
int64
(
hash
),
10
))
)
return
fmt
.
Sprintf
(
"%s-%s"
,
prefix
,
hash
)
}
}
// NewControllerRevision returns a ControllerRevision with a ControllerRef pointing to parent and indicating that
// NewControllerRevision returns a ControllerRevision with a ControllerRef pointing to parent and indicating that
...
@@ -91,13 +91,13 @@ func NewControllerRevision(parent metav1.Object,
...
@@ -91,13 +91,13 @@ func NewControllerRevision(parent metav1.Object,
}
}
hash
:=
HashControllerRevision
(
cr
,
collisionCount
)
hash
:=
HashControllerRevision
(
cr
,
collisionCount
)
cr
.
Name
=
ControllerRevisionName
(
parent
.
GetName
(),
hash
)
cr
.
Name
=
ControllerRevisionName
(
parent
.
GetName
(),
hash
)
cr
.
Labels
[
ControllerRevisionHashLabel
]
=
strconv
.
FormatInt
(
int64
(
hash
),
10
)
cr
.
Labels
[
ControllerRevisionHashLabel
]
=
hash
return
cr
,
nil
return
cr
,
nil
}
}
// HashControllerRevision hashes the contents of revision's Data using FNV hashing. If probe is not nil, the byte value
// HashControllerRevision hashes the contents of revision's Data using FNV hashing. If probe is not nil, the byte value
// of probe is added written to the hash as well.
// of probe is added written to the hash as well.
The returned hash will be a safe encoded string to avoid bad words.
func
HashControllerRevision
(
revision
*
apps
.
ControllerRevision
,
probe
*
int32
)
uint32
{
func
HashControllerRevision
(
revision
*
apps
.
ControllerRevision
,
probe
*
int32
)
string
{
hf
:=
fnv
.
New32
()
hf
:=
fnv
.
New32
()
if
len
(
revision
.
Data
.
Raw
)
>
0
{
if
len
(
revision
.
Data
.
Raw
)
>
0
{
hf
.
Write
(
revision
.
Data
.
Raw
)
hf
.
Write
(
revision
.
Data
.
Raw
)
...
@@ -108,8 +108,7 @@ func HashControllerRevision(revision *apps.ControllerRevision, probe *int32) uin
...
@@ -108,8 +108,7 @@ func HashControllerRevision(revision *apps.ControllerRevision, probe *int32) uin
if
probe
!=
nil
{
if
probe
!=
nil
{
hf
.
Write
([]
byte
(
strconv
.
FormatInt
(
int64
(
*
probe
),
10
)))
hf
.
Write
([]
byte
(
strconv
.
FormatInt
(
int64
(
*
probe
),
10
)))
}
}
return
hf
.
Sum32
()
return
rand
.
SafeEncodeString
(
fmt
.
Sprint
(
hf
.
Sum32
()))
}
}
// SortControllerRevisions sorts revisions by their Revision.
// SortControllerRevisions sorts revisions by their Revision.
...
...
test/integration/deployment/deployment_test.go
View file @
666a41c2
...
@@ -681,6 +681,10 @@ func checkRSHashLabels(rs *apps.ReplicaSet) (string, error) {
...
@@ -681,6 +681,10 @@ func checkRSHashLabels(rs *apps.ReplicaSet) (string, error) {
return
""
,
fmt
.
Errorf
(
"unexpected replicaset %s missing required pod-template-hash labels"
,
rs
.
Name
)
return
""
,
fmt
.
Errorf
(
"unexpected replicaset %s missing required pod-template-hash labels"
,
rs
.
Name
)
}
}
if
!
strings
.
HasSuffix
(
rs
.
Name
,
hash
)
{
return
""
,
fmt
.
Errorf
(
"unexpected replicaset %s name suffix doesn't match hash %s"
,
rs
.
Name
,
hash
)
}
return
hash
,
nil
return
hash
,
nil
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment