Skip to content

Commit

Permalink
DS: added v1beta2/defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
foxish committed Jul 25, 2017
1 parent f221d44 commit e3a22d8
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 0 deletions.
35 changes: 35 additions & 0 deletions pkg/apis/apps/v1beta2/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,41 @@ func addDefaultingFuncs(scheme *runtime.Scheme) error {
return RegisterDefaults(scheme)
}

func SetDefaults_DaemonSet(obj *appsv1beta2.DaemonSet) {
labels := obj.Spec.Template.Labels

// TODO: support templates defined elsewhere when we support them in the API
if labels != nil {
if obj.Spec.Selector == nil {
obj.Spec.Selector = &metav1.LabelSelector{
MatchLabels: labels,
}
}
if len(obj.Labels) == 0 {
obj.Labels = labels
}
}
updateStrategy := &obj.Spec.UpdateStrategy
if updateStrategy.Type == "" {
updateStrategy.Type = appsv1beta2.OnDeleteDaemonSetStrategyType
}
if updateStrategy.Type == appsv1beta2.RollingUpdateDaemonSetStrategyType {
if updateStrategy.RollingUpdate == nil {
rollingUpdate := appsv1beta2.RollingUpdateDaemonSet{}
updateStrategy.RollingUpdate = &rollingUpdate
}
if updateStrategy.RollingUpdate.MaxUnavailable == nil {
// Set default MaxUnavailable as 1 by default.
maxUnavailable := intstr.FromInt(1)
updateStrategy.RollingUpdate.MaxUnavailable = &maxUnavailable
}
}
if obj.Spec.RevisionHistoryLimit == nil {
obj.Spec.RevisionHistoryLimit = new(int32)
*obj.Spec.RevisionHistoryLimit = 10
}
}

func SetDefaults_StatefulSet(obj *appsv1beta2.StatefulSet) {
if len(obj.Spec.PodManagementPolicy) == 0 {
obj.Spec.PodManagementPolicy = appsv1beta2.OrderedReadyPodManagement
Expand Down
123 changes: 123 additions & 0 deletions pkg/apis/apps/v1beta2/defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,129 @@ import (
. "k8s.io/kubernetes/pkg/apis/apps/v1beta2"
)

func TestSetDefaultDaemonSetSpec(t *testing.T) {
defaultLabels := map[string]string{"foo": "bar"}
period := int64(v1.DefaultTerminationGracePeriodSeconds)
defaultTemplate := v1.PodTemplateSpec{
Spec: v1.PodSpec{
DNSPolicy: v1.DNSClusterFirst,
RestartPolicy: v1.RestartPolicyAlways,
SecurityContext: &v1.PodSecurityContext{},
TerminationGracePeriodSeconds: &period,
SchedulerName: api.DefaultSchedulerName,
},
ObjectMeta: metav1.ObjectMeta{
Labels: defaultLabels,
},
}
templateNoLabel := v1.PodTemplateSpec{
Spec: v1.PodSpec{
DNSPolicy: v1.DNSClusterFirst,
RestartPolicy: v1.RestartPolicyAlways,
SecurityContext: &v1.PodSecurityContext{},
TerminationGracePeriodSeconds: &period,
SchedulerName: api.DefaultSchedulerName,
},
}
tests := []struct {
original *appsv1beta2.DaemonSet
expected *appsv1beta2.DaemonSet
}{
{ // Labels change/defaulting test.
original: &appsv1beta2.DaemonSet{
Spec: appsv1beta2.DaemonSetSpec{
Template: defaultTemplate,
},
},
expected: &appsv1beta2.DaemonSet{
ObjectMeta: metav1.ObjectMeta{
Labels: defaultLabels,
},
Spec: appsv1beta2.DaemonSetSpec{
Selector: &metav1.LabelSelector{
MatchLabels: defaultLabels,
},
Template: defaultTemplate,
UpdateStrategy: appsv1beta2.DaemonSetUpdateStrategy{
Type: appsv1beta2.OnDeleteDaemonSetStrategyType,
},
RevisionHistoryLimit: newInt32(10),
},
},
},
{ // Labels change/defaulting test.
original: &appsv1beta2.DaemonSet{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"bar": "foo",
},
},
Spec: appsv1beta2.DaemonSetSpec{
Template: defaultTemplate,
RevisionHistoryLimit: newInt32(1),
},
},
expected: &appsv1beta2.DaemonSet{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"bar": "foo",
},
},
Spec: appsv1beta2.DaemonSetSpec{
Selector: &metav1.LabelSelector{
MatchLabels: defaultLabels,
},
Template: defaultTemplate,
UpdateStrategy: appsv1beta2.DaemonSetUpdateStrategy{
Type: appsv1beta2.OnDeleteDaemonSetStrategyType,
},
RevisionHistoryLimit: newInt32(1),
},
},
},
{ // Update strategy.
original: &appsv1beta2.DaemonSet{},
expected: &appsv1beta2.DaemonSet{
Spec: appsv1beta2.DaemonSetSpec{
Template: templateNoLabel,
UpdateStrategy: appsv1beta2.DaemonSetUpdateStrategy{
Type: appsv1beta2.OnDeleteDaemonSetStrategyType,
},
RevisionHistoryLimit: newInt32(10),
},
},
},
{ // Custom unique label key.
original: &appsv1beta2.DaemonSet{
Spec: appsv1beta2.DaemonSetSpec{},
},
expected: &appsv1beta2.DaemonSet{
Spec: appsv1beta2.DaemonSetSpec{
Template: templateNoLabel,
UpdateStrategy: appsv1beta2.DaemonSetUpdateStrategy{
Type: appsv1beta2.OnDeleteDaemonSetStrategyType,
},
RevisionHistoryLimit: newInt32(10),
},
},
},
}

for i, test := range tests {
original := test.original
expected := test.expected
obj2 := roundTrip(t, runtime.Object(original))
got, ok := obj2.(*appsv1beta2.DaemonSet)
if !ok {
t.Errorf("(%d) unexpected object: %v", i, got)
t.FailNow()
}
if !apiequality.Semantic.DeepEqual(got.Spec, expected.Spec) {
t.Errorf("(%d) got different than expected\ngot:\n\t%+v\nexpected:\n\t%+v", i, got.Spec, expected.Spec)
}
}
}

func TestSetDefaultDeployment(t *testing.T) {
defaultIntOrString := intstr.FromString("25%")
differentIntOrString := intstr.FromInt(5)
Expand Down

0 comments on commit e3a22d8

Please sign in to comment.