Commit f52e7ef4 authored by Kenneth Owens's avatar Kenneth Owens

Update the DaemonSet controller to use the apps/v1 API

parent fd1527a9
...@@ -21,10 +21,31 @@ limitations under the License. ...@@ -21,10 +21,31 @@ limitations under the License.
package app package app
import ( import (
"fmt"
"k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/kubernetes/pkg/controller/daemon"
"k8s.io/kubernetes/pkg/controller/statefulset" "k8s.io/kubernetes/pkg/controller/statefulset"
) )
func startDaemonSetController(ctx ControllerContext) (bool, error) {
if !ctx.AvailableResources[schema.GroupVersionResource{Group: "apps", Version: "v1", Resource: "daemonsets"}] {
return false, nil
}
dsc, err := daemon.NewDaemonSetsController(
ctx.InformerFactory.Apps().V1().DaemonSets(),
ctx.InformerFactory.Apps().V1().ControllerRevisions(),
ctx.InformerFactory.Core().V1().Pods(),
ctx.InformerFactory.Core().V1().Nodes(),
ctx.ClientBuilder.ClientOrDie("daemon-set-controller"),
)
if err != nil {
return true, fmt.Errorf("error creating DaemonSets controller: %v", err)
}
go dsc.Run(int(ctx.ComponentConfig.ConcurrentDaemonSetSyncs), ctx.Stop)
return true, nil
}
func startStatefulSetController(ctx ControllerContext) (bool, error) { func startStatefulSetController(ctx ControllerContext) (bool, error) {
if !ctx.AvailableResources[schema.GroupVersionResource{Group: "apps", Version: "v1beta1", Resource: "statefulsets"}] { if !ctx.AvailableResources[schema.GroupVersionResource{Group: "apps", Version: "v1beta1", Resource: "statefulsets"}] {
return false, nil return false, nil
......
...@@ -24,29 +24,10 @@ import ( ...@@ -24,29 +24,10 @@ import (
"fmt" "fmt"
"k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/kubernetes/pkg/controller/daemon"
"k8s.io/kubernetes/pkg/controller/deployment" "k8s.io/kubernetes/pkg/controller/deployment"
"k8s.io/kubernetes/pkg/controller/replicaset" "k8s.io/kubernetes/pkg/controller/replicaset"
) )
func startDaemonSetController(ctx ControllerContext) (bool, error) {
if !ctx.AvailableResources[schema.GroupVersionResource{Group: "extensions", Version: "v1beta1", Resource: "daemonsets"}] {
return false, nil
}
dsc, err := daemon.NewDaemonSetsController(
ctx.InformerFactory.Extensions().V1beta1().DaemonSets(),
ctx.InformerFactory.Apps().V1beta1().ControllerRevisions(),
ctx.InformerFactory.Core().V1().Pods(),
ctx.InformerFactory.Core().V1().Nodes(),
ctx.ClientBuilder.ClientOrDie("daemon-set-controller"),
)
if err != nil {
return true, fmt.Errorf("error creating DaemonSets controller: %v", err)
}
go dsc.Run(int(ctx.ComponentConfig.ConcurrentDaemonSetSyncs), ctx.Stop)
return true, nil
}
func startDeploymentController(ctx ControllerContext) (bool, error) { func startDeploymentController(ctx ControllerContext) (bool, error) {
if !ctx.AvailableResources[schema.GroupVersionResource{Group: "extensions", Version: "v1beta1", Resource: "deployments"}] { if !ctx.AvailableResources[schema.GroupVersionResource{Group: "extensions", Version: "v1beta1", Resource: "deployments"}] {
return false, nil return false, nil
......
...@@ -21,7 +21,7 @@ import ( ...@@ -21,7 +21,7 @@ import (
"sync" "sync"
"github.com/golang/glog" "github.com/golang/glog"
appsv1beta1 "k8s.io/api/apps/v1beta1" apps "k8s.io/api/apps/v1"
"k8s.io/api/core/v1" "k8s.io/api/core/v1"
extensions "k8s.io/api/extensions/v1beta1" extensions "k8s.io/api/extensions/v1beta1"
"k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/errors"
...@@ -436,18 +436,18 @@ func NewControllerRevisionControllerRefManager( ...@@ -436,18 +436,18 @@ func NewControllerRevisionControllerRefManager(
// If the error is nil, either the reconciliation succeeded, or no // If the error is nil, either the reconciliation succeeded, or no
// reconciliation was necessary. The list of ControllerRevisions that you now own is // reconciliation was necessary. The list of ControllerRevisions that you now own is
// returned. // returned.
func (m *ControllerRevisionControllerRefManager) ClaimControllerRevisions(histories []*appsv1beta1.ControllerRevision) ([]*appsv1beta1.ControllerRevision, error) { func (m *ControllerRevisionControllerRefManager) ClaimControllerRevisions(histories []*apps.ControllerRevision) ([]*apps.ControllerRevision, error) {
var claimed []*appsv1beta1.ControllerRevision var claimed []*apps.ControllerRevision
var errlist []error var errlist []error
match := func(obj metav1.Object) bool { match := func(obj metav1.Object) bool {
return m.Selector.Matches(labels.Set(obj.GetLabels())) return m.Selector.Matches(labels.Set(obj.GetLabels()))
} }
adopt := func(obj metav1.Object) error { adopt := func(obj metav1.Object) error {
return m.AdoptControllerRevision(obj.(*appsv1beta1.ControllerRevision)) return m.AdoptControllerRevision(obj.(*apps.ControllerRevision))
} }
release := func(obj metav1.Object) error { release := func(obj metav1.Object) error {
return m.ReleaseControllerRevision(obj.(*appsv1beta1.ControllerRevision)) return m.ReleaseControllerRevision(obj.(*apps.ControllerRevision))
} }
for _, h := range histories { for _, h := range histories {
...@@ -465,7 +465,7 @@ func (m *ControllerRevisionControllerRefManager) ClaimControllerRevisions(histor ...@@ -465,7 +465,7 @@ func (m *ControllerRevisionControllerRefManager) ClaimControllerRevisions(histor
// AdoptControllerRevision sends a patch to take control of the ControllerRevision. It returns the error if // AdoptControllerRevision sends a patch to take control of the ControllerRevision. It returns the error if
// the patching fails. // the patching fails.
func (m *ControllerRevisionControllerRefManager) AdoptControllerRevision(history *appsv1beta1.ControllerRevision) error { func (m *ControllerRevisionControllerRefManager) AdoptControllerRevision(history *apps.ControllerRevision) error {
if err := m.CanAdopt(); err != nil { if err := m.CanAdopt(); err != nil {
return fmt.Errorf("can't adopt ControllerRevision %v/%v (%v): %v", history.Namespace, history.Name, history.UID, err) return fmt.Errorf("can't adopt ControllerRevision %v/%v (%v): %v", history.Namespace, history.Name, history.UID, err)
} }
...@@ -480,7 +480,7 @@ func (m *ControllerRevisionControllerRefManager) AdoptControllerRevision(history ...@@ -480,7 +480,7 @@ func (m *ControllerRevisionControllerRefManager) AdoptControllerRevision(history
// ReleaseControllerRevision sends a patch to free the ControllerRevision from the control of its controller. // ReleaseControllerRevision sends a patch to free the ControllerRevision from the control of its controller.
// It returns the error if the patching fails. 404 and 422 errors are ignored. // It returns the error if the patching fails. 404 and 422 errors are ignored.
func (m *ControllerRevisionControllerRefManager) ReleaseControllerRevision(history *appsv1beta1.ControllerRevision) error { func (m *ControllerRevisionControllerRefManager) ReleaseControllerRevision(history *apps.ControllerRevision) error {
glog.V(2).Infof("patching ControllerRevision %s_%s to remove its controllerRef to %s/%s:%s", glog.V(2).Infof("patching ControllerRevision %s_%s to remove its controllerRef to %s/%s:%s",
history.Namespace, history.Name, m.controllerKind.GroupVersion(), m.controllerKind.Kind, m.Controller.GetName()) history.Namespace, history.Name, m.controllerKind.GroupVersion(), m.controllerKind.Kind, m.Controller.GetName())
deleteOwnerRefPatch := fmt.Sprintf(`{"metadata":{"ownerReferences":[{"$patch":"delete","uid":"%s"}],"uid":"%s"}}`, m.Controller.GetUID(), history.UID) deleteOwnerRefPatch := fmt.Sprintf(`{"metadata":{"ownerReferences":[{"$patch":"delete","uid":"%s"}],"uid":"%s"}}`, m.Controller.GetUID(), history.UID)
......
...@@ -19,8 +19,8 @@ package daemon ...@@ -19,8 +19,8 @@ package daemon
import ( import (
"testing" "testing"
apps "k8s.io/api/apps/v1"
"k8s.io/api/core/v1" "k8s.io/api/core/v1"
extensions "k8s.io/api/extensions/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr" "k8s.io/apimachinery/pkg/util/intstr"
) )
...@@ -38,10 +38,9 @@ func TestDaemonSetUpdatesPods(t *testing.T) { ...@@ -38,10 +38,9 @@ func TestDaemonSetUpdatesPods(t *testing.T) {
markPodsReady(podControl.podStore) markPodsReady(podControl.podStore)
ds.Spec.Template.Spec.Containers[0].Image = "foo2/bar2" ds.Spec.Template.Spec.Containers[0].Image = "foo2/bar2"
ds.Spec.UpdateStrategy.Type = extensions.RollingUpdateDaemonSetStrategyType ds.Spec.UpdateStrategy.Type = apps.RollingUpdateDaemonSetStrategyType
intStr := intstr.FromInt(maxUnavailable) intStr := intstr.FromInt(maxUnavailable)
ds.Spec.UpdateStrategy.RollingUpdate = &extensions.RollingUpdateDaemonSet{MaxUnavailable: &intStr} ds.Spec.UpdateStrategy.RollingUpdate = &apps.RollingUpdateDaemonSet{MaxUnavailable: &intStr}
ds.Spec.TemplateGeneration++
manager.dsStore.Update(ds) manager.dsStore.Update(ds)
clearExpectations(t, manager, ds, podControl) clearExpectations(t, manager, ds, podControl)
...@@ -80,10 +79,9 @@ func TestDaemonSetUpdatesWhenNewPosIsNotReady(t *testing.T) { ...@@ -80,10 +79,9 @@ func TestDaemonSetUpdatesWhenNewPosIsNotReady(t *testing.T) {
markPodsReady(podControl.podStore) markPodsReady(podControl.podStore)
ds.Spec.Template.Spec.Containers[0].Image = "foo2/bar2" ds.Spec.Template.Spec.Containers[0].Image = "foo2/bar2"
ds.Spec.UpdateStrategy.Type = extensions.RollingUpdateDaemonSetStrategyType ds.Spec.UpdateStrategy.Type = apps.RollingUpdateDaemonSetStrategyType
intStr := intstr.FromInt(maxUnavailable) intStr := intstr.FromInt(maxUnavailable)
ds.Spec.UpdateStrategy.RollingUpdate = &extensions.RollingUpdateDaemonSet{MaxUnavailable: &intStr} ds.Spec.UpdateStrategy.RollingUpdate = &apps.RollingUpdateDaemonSet{MaxUnavailable: &intStr}
ds.Spec.TemplateGeneration++
manager.dsStore.Update(ds) manager.dsStore.Update(ds)
// new pods are not ready numUnavailable == maxUnavailable // new pods are not ready numUnavailable == maxUnavailable
...@@ -109,10 +107,9 @@ func TestDaemonSetUpdatesAllOldPodsNotReady(t *testing.T) { ...@@ -109,10 +107,9 @@ func TestDaemonSetUpdatesAllOldPodsNotReady(t *testing.T) {
syncAndValidateDaemonSets(t, manager, ds, podControl, 5, 0, 0) syncAndValidateDaemonSets(t, manager, ds, podControl, 5, 0, 0)
ds.Spec.Template.Spec.Containers[0].Image = "foo2/bar2" ds.Spec.Template.Spec.Containers[0].Image = "foo2/bar2"
ds.Spec.UpdateStrategy.Type = extensions.RollingUpdateDaemonSetStrategyType ds.Spec.UpdateStrategy.Type = apps.RollingUpdateDaemonSetStrategyType
intStr := intstr.FromInt(maxUnavailable) intStr := intstr.FromInt(maxUnavailable)
ds.Spec.UpdateStrategy.RollingUpdate = &extensions.RollingUpdateDaemonSet{MaxUnavailable: &intStr} ds.Spec.UpdateStrategy.RollingUpdate = &apps.RollingUpdateDaemonSet{MaxUnavailable: &intStr}
ds.Spec.TemplateGeneration++
manager.dsStore.Update(ds) manager.dsStore.Update(ds)
// all old pods are unavailable so should be removed // all old pods are unavailable so should be removed
...@@ -137,9 +134,9 @@ func TestDaemonSetUpdatesNoTemplateChanged(t *testing.T) { ...@@ -137,9 +134,9 @@ func TestDaemonSetUpdatesNoTemplateChanged(t *testing.T) {
manager.dsStore.Add(ds) manager.dsStore.Add(ds)
syncAndValidateDaemonSets(t, manager, ds, podControl, 5, 0, 0) syncAndValidateDaemonSets(t, manager, ds, podControl, 5, 0, 0)
ds.Spec.UpdateStrategy.Type = extensions.RollingUpdateDaemonSetStrategyType ds.Spec.UpdateStrategy.Type = apps.RollingUpdateDaemonSetStrategyType
intStr := intstr.FromInt(maxUnavailable) intStr := intstr.FromInt(maxUnavailable)
ds.Spec.UpdateStrategy.RollingUpdate = &extensions.RollingUpdateDaemonSet{MaxUnavailable: &intStr} ds.Spec.UpdateStrategy.RollingUpdate = &apps.RollingUpdateDaemonSet{MaxUnavailable: &intStr}
manager.dsStore.Update(ds) manager.dsStore.Update(ds)
// template is not changed no pod should be removed // template is not changed no pod should be removed
...@@ -152,7 +149,7 @@ func TestGetUnavailableNumbers(t *testing.T) { ...@@ -152,7 +149,7 @@ func TestGetUnavailableNumbers(t *testing.T) {
cases := []struct { cases := []struct {
name string name string
Manager *daemonSetsController Manager *daemonSetsController
ds *extensions.DaemonSet ds *apps.DaemonSet
nodeToPods map[string][]*v1.Pod nodeToPods map[string][]*v1.Pod
maxUnavailable int maxUnavailable int
numUnavailable int numUnavailable int
...@@ -167,10 +164,10 @@ func TestGetUnavailableNumbers(t *testing.T) { ...@@ -167,10 +164,10 @@ func TestGetUnavailableNumbers(t *testing.T) {
} }
return manager return manager
}(), }(),
ds: func() *extensions.DaemonSet { ds: func() *apps.DaemonSet {
ds := newDaemonSet("x") ds := newDaemonSet("x")
intStr := intstr.FromInt(0) intStr := intstr.FromInt(0)
ds.Spec.UpdateStrategy.RollingUpdate = &extensions.RollingUpdateDaemonSet{MaxUnavailable: &intStr} ds.Spec.UpdateStrategy.RollingUpdate = &apps.RollingUpdateDaemonSet{MaxUnavailable: &intStr}
return ds return ds
}(), }(),
nodeToPods: make(map[string][]*v1.Pod), nodeToPods: make(map[string][]*v1.Pod),
...@@ -187,10 +184,10 @@ func TestGetUnavailableNumbers(t *testing.T) { ...@@ -187,10 +184,10 @@ func TestGetUnavailableNumbers(t *testing.T) {
addNodes(manager.nodeStore, 0, 2, nil) addNodes(manager.nodeStore, 0, 2, nil)
return manager return manager
}(), }(),
ds: func() *extensions.DaemonSet { ds: func() *apps.DaemonSet {
ds := newDaemonSet("x") ds := newDaemonSet("x")
intStr := intstr.FromInt(1) intStr := intstr.FromInt(1)
ds.Spec.UpdateStrategy.RollingUpdate = &extensions.RollingUpdateDaemonSet{MaxUnavailable: &intStr} ds.Spec.UpdateStrategy.RollingUpdate = &apps.RollingUpdateDaemonSet{MaxUnavailable: &intStr}
return ds return ds
}(), }(),
nodeToPods: func() map[string][]*v1.Pod { nodeToPods: func() map[string][]*v1.Pod {
...@@ -216,10 +213,10 @@ func TestGetUnavailableNumbers(t *testing.T) { ...@@ -216,10 +213,10 @@ func TestGetUnavailableNumbers(t *testing.T) {
addNodes(manager.nodeStore, 0, 2, nil) addNodes(manager.nodeStore, 0, 2, nil)
return manager return manager
}(), }(),
ds: func() *extensions.DaemonSet { ds: func() *apps.DaemonSet {
ds := newDaemonSet("x") ds := newDaemonSet("x")
intStr := intstr.FromInt(0) intStr := intstr.FromInt(0)
ds.Spec.UpdateStrategy.RollingUpdate = &extensions.RollingUpdateDaemonSet{MaxUnavailable: &intStr} ds.Spec.UpdateStrategy.RollingUpdate = &apps.RollingUpdateDaemonSet{MaxUnavailable: &intStr}
return ds return ds
}(), }(),
nodeToPods: func() map[string][]*v1.Pod { nodeToPods: func() map[string][]*v1.Pod {
...@@ -242,10 +239,10 @@ func TestGetUnavailableNumbers(t *testing.T) { ...@@ -242,10 +239,10 @@ func TestGetUnavailableNumbers(t *testing.T) {
addNodes(manager.nodeStore, 0, 2, nil) addNodes(manager.nodeStore, 0, 2, nil)
return manager return manager
}(), }(),
ds: func() *extensions.DaemonSet { ds: func() *apps.DaemonSet {
ds := newDaemonSet("x") ds := newDaemonSet("x")
intStr := intstr.FromString("50%") intStr := intstr.FromString("50%")
ds.Spec.UpdateStrategy.RollingUpdate = &extensions.RollingUpdateDaemonSet{MaxUnavailable: &intStr} ds.Spec.UpdateStrategy.RollingUpdate = &apps.RollingUpdateDaemonSet{MaxUnavailable: &intStr}
return ds return ds
}(), }(),
nodeToPods: func() map[string][]*v1.Pod { nodeToPods: func() map[string][]*v1.Pod {
...@@ -271,10 +268,10 @@ func TestGetUnavailableNumbers(t *testing.T) { ...@@ -271,10 +268,10 @@ func TestGetUnavailableNumbers(t *testing.T) {
addNodes(manager.nodeStore, 0, 2, nil) addNodes(manager.nodeStore, 0, 2, nil)
return manager return manager
}(), }(),
ds: func() *extensions.DaemonSet { ds: func() *apps.DaemonSet {
ds := newDaemonSet("x") ds := newDaemonSet("x")
intStr := intstr.FromString("50%") intStr := intstr.FromString("50%")
ds.Spec.UpdateStrategy.RollingUpdate = &extensions.RollingUpdateDaemonSet{MaxUnavailable: &intStr} ds.Spec.UpdateStrategy.RollingUpdate = &apps.RollingUpdateDaemonSet{MaxUnavailable: &intStr}
return ds return ds
}(), }(),
nodeToPods: func() map[string][]*v1.Pod { nodeToPods: func() map[string][]*v1.Pod {
......
...@@ -18,7 +18,9 @@ package util ...@@ -18,7 +18,9 @@ package util
import ( import (
"fmt" "fmt"
"strconv"
apps "k8s.io/api/apps/v1"
"k8s.io/api/core/v1" "k8s.io/api/core/v1"
extensions "k8s.io/api/extensions/v1beta1" extensions "k8s.io/api/extensions/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
...@@ -28,13 +30,28 @@ import ( ...@@ -28,13 +30,28 @@ import (
"k8s.io/kubernetes/pkg/features" "k8s.io/kubernetes/pkg/features"
kubelettypes "k8s.io/kubernetes/pkg/kubelet/types" kubelettypes "k8s.io/kubernetes/pkg/kubelet/types"
"k8s.io/kubernetes/pkg/scheduler/algorithm" "k8s.io/kubernetes/pkg/scheduler/algorithm"
labelsutil "k8s.io/kubernetes/pkg/util/labels"
) )
// GetTemplateGeneration gets the template generation associated with a v1.DaemonSet by extracting it from the
// deprecated annotation. If no annotation is found nil is returned. If the annotation is found and fails to parse
// nil is returned with an error. If the generation can be parsed from the annotation, a pointer to the parsed int64
// value is returned.
func GetTemplateGeneration(ds *apps.DaemonSet) (*int64, error) {
annotation, found := ds.Annotations[apps.DeprecatedTemplateGeneration]
if !found {
return nil, nil
}
generation, err := strconv.ParseInt(annotation, 10, 64)
if err != nil {
return nil, err
}
return &generation, nil
}
// CreatePodTemplate returns copy of provided template with additional // CreatePodTemplate returns copy of provided template with additional
// label which contains templateGeneration (for backward compatibility), // label which contains templateGeneration (for backward compatibility),
// hash of provided template and sets default daemon tolerations. // hash of provided template and sets default daemon tolerations.
func CreatePodTemplate(template v1.PodTemplateSpec, generation int64, hash string) v1.PodTemplateSpec { func CreatePodTemplate(template v1.PodTemplateSpec, generation *int64, hash string) v1.PodTemplateSpec {
newTemplate := *template.DeepCopy() newTemplate := *template.DeepCopy()
// DaemonSet pods shouldn't be deleted by NodeController in case of node problems. // DaemonSet pods shouldn't be deleted by NodeController in case of node problems.
// Add infinite toleration for taint notReady:NoExecute here // Add infinite toleration for taint notReady:NoExecute here
...@@ -81,12 +98,12 @@ func CreatePodTemplate(template v1.PodTemplateSpec, generation int64, hash strin ...@@ -81,12 +98,12 @@ func CreatePodTemplate(template v1.PodTemplateSpec, generation int64, hash strin
}) })
} }
templateGenerationStr := fmt.Sprint(generation) if newTemplate.ObjectMeta.Labels == nil {
newTemplate.ObjectMeta.Labels = labelsutil.CloneAndAddLabel( newTemplate.ObjectMeta.Labels = make(map[string]string)
template.ObjectMeta.Labels, }
extensions.DaemonSetTemplateGenerationKey, if generation != nil {
templateGenerationStr, newTemplate.ObjectMeta.Labels[extensions.DaemonSetTemplateGenerationKey] = fmt.Sprint(*generation)
) }
// TODO: do we need to validate if the DaemonSet is RollingUpdate or not? // TODO: do we need to validate if the DaemonSet is RollingUpdate or not?
if len(hash) > 0 { if len(hash) > 0 {
newTemplate.ObjectMeta.Labels[extensions.DefaultDaemonSetUniqueLabelKey] = hash newTemplate.ObjectMeta.Labels[extensions.DefaultDaemonSetUniqueLabelKey] = hash
...@@ -94,10 +111,11 @@ func CreatePodTemplate(template v1.PodTemplateSpec, generation int64, hash strin ...@@ -94,10 +111,11 @@ func CreatePodTemplate(template v1.PodTemplateSpec, generation int64, hash strin
return newTemplate return newTemplate
} }
// IsPodUpdated checks if pod contains label value that either matches templateGeneration or hash // IsPodUpdate checks if pod contains label value that either matches templateGeneration or hash
func IsPodUpdated(dsTemplateGeneration int64, pod *v1.Pod, hash string) bool { func IsPodUpdated(pod *v1.Pod, hash string, dsTemplateGeneration *int64) bool {
// Compare with hash to see if the pod is updated, need to maintain backward compatibility of templateGeneration // Compare with hash to see if the pod is updated, need to maintain backward compatibility of templateGeneration
templateMatches := pod.Labels[extensions.DaemonSetTemplateGenerationKey] == fmt.Sprint(dsTemplateGeneration) templateMatches := dsTemplateGeneration != nil &&
pod.Labels[extensions.DaemonSetTemplateGenerationKey] == fmt.Sprint(dsTemplateGeneration)
hashMatches := len(hash) > 0 && pod.Labels[extensions.DefaultDaemonSetUniqueLabelKey] == hash hashMatches := len(hash) > 0 && pod.Labels[extensions.DefaultDaemonSetUniqueLabelKey] == hash
return hashMatches || templateMatches return hashMatches || templateMatches
} }
......
...@@ -47,13 +47,14 @@ func newPod(podName string, nodeName string, label map[string]string) *v1.Pod { ...@@ -47,13 +47,14 @@ func newPod(podName string, nodeName string, label map[string]string) *v1.Pod {
} }
func TestIsPodUpdated(t *testing.T) { func TestIsPodUpdated(t *testing.T) {
templateGeneration := int64(12345) templateGeneration := int64Ptr(12345)
badGeneration := int64Ptr(12345)
hash := "55555" hash := "55555"
labels := map[string]string{extensions.DaemonSetTemplateGenerationKey: fmt.Sprint(templateGeneration), extensions.DefaultDaemonSetUniqueLabelKey: hash} labels := map[string]string{extensions.DaemonSetTemplateGenerationKey: fmt.Sprint(templateGeneration), extensions.DefaultDaemonSetUniqueLabelKey: hash}
labelsNoHash := map[string]string{extensions.DaemonSetTemplateGenerationKey: fmt.Sprint(templateGeneration)} labelsNoHash := map[string]string{extensions.DaemonSetTemplateGenerationKey: fmt.Sprint(templateGeneration)}
tests := []struct { tests := []struct {
test string test string
templateGeneration int64 templateGeneration *int64
pod *v1.Pod pod *v1.Pod
hash string hash string
isUpdated bool isUpdated bool
...@@ -95,14 +96,14 @@ func TestIsPodUpdated(t *testing.T) { ...@@ -95,14 +96,14 @@ func TestIsPodUpdated(t *testing.T) {
}, },
{ {
"templateGeneration doesn't match, hash does", "templateGeneration doesn't match, hash does",
templateGeneration + 1, badGeneration,
newPod("pod1", "node1", labels), newPod("pod1", "node1", labels),
hash, hash,
true, true,
}, },
{ {
"templateGeneration and hash don't match", "templateGeneration and hash don't match",
templateGeneration + 1, badGeneration,
newPod("pod1", "node1", labels), newPod("pod1", "node1", labels),
hash + "123", hash + "123",
false, false,
...@@ -130,7 +131,7 @@ func TestIsPodUpdated(t *testing.T) { ...@@ -130,7 +131,7 @@ func TestIsPodUpdated(t *testing.T) {
}, },
} }
for _, test := range tests { for _, test := range tests {
updated := IsPodUpdated(test.templateGeneration, test.pod, test.hash) updated := IsPodUpdated(test.pod, test.hash, test.templateGeneration)
if updated != test.isUpdated { if updated != test.isUpdated {
t.Errorf("%s: IsPodUpdated returned wrong value. Expected %t, got %t", test.test, test.isUpdated, updated) t.Errorf("%s: IsPodUpdated returned wrong value. Expected %t, got %t", test.test, test.isUpdated, updated)
} }
...@@ -139,19 +140,19 @@ func TestIsPodUpdated(t *testing.T) { ...@@ -139,19 +140,19 @@ func TestIsPodUpdated(t *testing.T) {
func TestCreatePodTemplate(t *testing.T) { func TestCreatePodTemplate(t *testing.T) {
tests := []struct { tests := []struct {
templateGeneration int64 templateGeneration *int64
hash string hash string
expectUniqueLabel bool expectUniqueLabel bool
}{ }{
{int64(1), "", false}, {int64Ptr(1), "", false},
{int64(2), "3242341807", true}, {int64Ptr(2), "3242341807", true},
} }
for _, test := range tests { for _, test := range tests {
podTemplateSpec := v1.PodTemplateSpec{} podTemplateSpec := v1.PodTemplateSpec{}
newPodTemplate := CreatePodTemplate(podTemplateSpec, test.templateGeneration, test.hash) newPodTemplate := CreatePodTemplate(podTemplateSpec, test.templateGeneration, test.hash)
val, exists := newPodTemplate.ObjectMeta.Labels[extensions.DaemonSetTemplateGenerationKey] val, exists := newPodTemplate.ObjectMeta.Labels[extensions.DaemonSetTemplateGenerationKey]
if !exists || val != fmt.Sprint(test.templateGeneration) { if !exists || val != fmt.Sprint(*test.templateGeneration) {
t.Errorf("Expected podTemplateSpec to have generation label value: %d, got: %s", test.templateGeneration, val) t.Errorf("Expected podTemplateSpec to have generation label value: %d, got: %s", *test.templateGeneration, val)
} }
val, exists = newPodTemplate.ObjectMeta.Labels[extensions.DefaultDaemonSetUniqueLabelKey] val, exists = newPodTemplate.ObjectMeta.Labels[extensions.DefaultDaemonSetUniqueLabelKey]
if test.expectUniqueLabel && (!exists || val != test.hash) { if test.expectUniqueLabel && (!exists || val != test.hash) {
...@@ -162,3 +163,8 @@ func TestCreatePodTemplate(t *testing.T) { ...@@ -162,3 +163,8 @@ func TestCreatePodTemplate(t *testing.T) {
} }
} }
} }
func int64Ptr(i int) *int64 {
li := int64(i)
return &li
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment