Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Based on #15983: Stores hash of pod manifest in mirror pod's annotation for release 1.0 #16325

Merged
merged 1 commit into from
Oct 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions pkg/kubelet/config/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ func applyDefaults(pod *api.Pod, source string, isFile bool, nodeName string) er
pod.Spec.NodeName = nodeName

pod.ObjectMeta.SelfLink = getSelfLink(pod.Name, pod.Namespace)

if pod.Annotations == nil {
pod.Annotations = make(map[string]string)
}
// The generated UID is the hash of the file.
pod.Annotations[kubelet.ConfigHashAnnotationKey] = string(pod.UID)
return nil
}

Expand Down
9 changes: 5 additions & 4 deletions pkg/kubelet/config/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,11 @@ func TestReadPodsFromFile(t *testing.T) {
},
expected: CreatePodUpdate(kubelet.SET, kubelet.FileSource, &api.Pod{
ObjectMeta: api.ObjectMeta{
Name: "test-" + hostname,
UID: "12345",
Namespace: "mynamespace",
SelfLink: getSelfLink("test-"+hostname, "mynamespace"),
Name: "test-" + hostname,
UID: "12345",
Namespace: "mynamespace",
Annotations: map[string]string{kubelet.ConfigHashAnnotationKey: "12345"},
SelfLink: getSelfLink("test-"+hostname, "mynamespace"),
},
Spec: api.PodSpec{
NodeName: hostname,
Expand Down
30 changes: 15 additions & 15 deletions pkg/kubelet/config/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,11 @@ func TestExtractPodsFromHTTP(t *testing.T) {
kubelet.HTTPSource,
&api.Pod{
ObjectMeta: api.ObjectMeta{
UID: "111",
Name: "foo" + "-" + hostname,
Namespace: "mynamespace",

SelfLink: getSelfLink("foo-"+hostname, "mynamespace"),
UID: "111",
Name: "foo" + "-" + hostname,
Namespace: "mynamespace",
Annotations: map[string]string{kubelet.ConfigHashAnnotationKey: "111"},
SelfLink: getSelfLink("foo-"+hostname, "mynamespace"),
},
Spec: api.PodSpec{
NodeName: hostname,
Expand Down Expand Up @@ -202,11 +202,11 @@ func TestExtractPodsFromHTTP(t *testing.T) {
kubelet.HTTPSource,
&api.Pod{
ObjectMeta: api.ObjectMeta{
UID: "111",
Name: "foo" + "-" + hostname,
Namespace: "default",

SelfLink: getSelfLink("foo-"+hostname, kubelet.NamespaceDefault),
UID: "111",
Name: "foo" + "-" + hostname,
Namespace: "default",
Annotations: map[string]string{kubelet.ConfigHashAnnotationKey: "111"},
SelfLink: getSelfLink("foo-"+hostname, kubelet.NamespaceDefault),
},
Spec: api.PodSpec{
NodeName: hostname,
Expand All @@ -222,11 +222,11 @@ func TestExtractPodsFromHTTP(t *testing.T) {
},
&api.Pod{
ObjectMeta: api.ObjectMeta{
UID: "222",
Name: "bar" + "-" + hostname,
Namespace: "default",

SelfLink: getSelfLink("bar-"+hostname, kubelet.NamespaceDefault),
UID: "222",
Name: "bar" + "-" + hostname,
Namespace: "default",
Annotations: map[string]string{kubelet.ConfigHashAnnotationKey: "222"},
SelfLink: getSelfLink("bar-"+hostname, kubelet.NamespaceDefault),
},
Spec: api.PodSpec{
NodeName: hostname,
Expand Down
30 changes: 22 additions & 8 deletions pkg/kubelet/mirror_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,16 @@ func (mc *basicMirrorClient) CreateMirrorPod(pod *api.Pod) error {
if mc.apiserverClient == nil {
return nil
}
pod.Annotations[ConfigMirrorAnnotationKey] = MirrorType
// Make a copy of the pod.
copyPod := *pod
copyPod.Annotations = make(map[string]string)

_, err := mc.apiserverClient.Pods(pod.Namespace).Create(pod)
for k, v := range pod.Annotations {
copyPod.Annotations[k] = v
}
copyPod.Annotations[ConfigMirrorAnnotationKey] = getPodHash(pod)

_, err := mc.apiserverClient.Pods(copyPod.Namespace).Create(&copyPod)
return err
}

Expand Down Expand Up @@ -81,14 +88,21 @@ func getPodSource(pod *api.Pod) (string, error) {
}

func isStaticPod(pod *api.Pod) bool {
source, err := getPodSource(pod)
source, err := GetPodSource(pod)
return err == nil && source != ApiserverSource
}

func isMirrorPod(pod *api.Pod) bool {
if value, ok := pod.Annotations[ConfigMirrorAnnotationKey]; !ok {
return false
} else {
return value == MirrorType
}
_, ok := pod.Annotations[ConfigMirrorAnnotationKey]
return ok
}

func getHashFromMirrorPod(pod *api.Pod) (string, bool) {
hash, ok := pod.Annotations[ConfigMirrorAnnotationKey]
return hash, ok
}

func getPodHash(pod *api.Pod) string {
// The annotation exists for all static pods.
return pod.Annotations[ConfigHashAnnotationKey]
}
6 changes: 5 additions & 1 deletion pkg/kubelet/pod_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,11 @@ func (pm *basicPodManager) IsMirrorPodOf(mirrorPod, pod *api.Pod) bool {
if pod.Name != mirrorPod.Name || pod.Namespace != mirrorPod.Namespace {
return false
}
return api.Semantic.DeepEqual(&pod.Spec, &mirrorPod.Spec)
hash, ok := getHashFromMirrorPod(mirrorPod)
if !ok {
return false
}
return hash == getPodHash(pod)
}

func podsMapToPods(UIDMap map[types.UID]*api.Pod) []*api.Pod {
Expand Down
11 changes: 11 additions & 0 deletions pkg/kubelet/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
const ConfigSourceAnnotationKey = "kubernetes.io/config.source"
const ConfigMirrorAnnotationKey = "kubernetes.io/config.mirror"
const ConfigFirstSeenAnnotationKey = "kubernetes.io/config.seen"
const ConfigHashAnnotationKey = "kubernetes.io/config.hash"

// PodOperation defines what changes will be made on a pod configuration.
type PodOperation int
Expand Down Expand Up @@ -88,3 +89,13 @@ func GetValidatedSources(sources []string) ([]string, error) {
}
return validated, nil
}

// GetPodSource returns the source of the pod based on the annotation.
func GetPodSource(pod *api.Pod) (string, error) {
if pod.Annotations != nil {
if source, ok := pod.Annotations[ConfigSourceAnnotationKey]; ok {
return source, nil
}
}
return "", fmt.Errorf("cannot get source of pod %q", pod.UID)
}