From 23b7ae6041ce406d6fe454c51ebbc4c651c6e991 Mon Sep 17 00:00:00 2001 From: SataQiu Date: Tue, 16 Jul 2019 21:06:01 +0800 Subject: [PATCH] make taint.ToString() consistent with the reverse parsing logic --- pkg/apis/core/taint.go | 8 +++++++- pkg/apis/core/taint_test.go | 13 +++++++++++++ staging/src/k8s.io/api/core/v1/taint.go | 8 +++++++- staging/src/k8s.io/api/core/v1/taint_test.go | 13 +++++++++++++ 4 files changed, 40 insertions(+), 2 deletions(-) diff --git a/pkg/apis/core/taint.go b/pkg/apis/core/taint.go index ae1feb74d7e..4dea6ad6147 100644 --- a/pkg/apis/core/taint.go +++ b/pkg/apis/core/taint.go @@ -27,8 +27,14 @@ func (t *Taint) MatchTaint(taintToMatch Taint) bool { return t.Key == taintToMatch.Key && t.Effect == taintToMatch.Effect } -// taint.ToString() converts taint struct to string in format key=value:effect or key:effect. +// taint.ToString() converts taint struct to string in format '=:', '=:', ':', or ''. func (t *Taint) ToString() string { + if len(t.Effect) == 0 { + if len(t.Value) == 0 { + return fmt.Sprintf("%v", t.Key) + } + return fmt.Sprintf("%v=%v:", t.Key, t.Value) + } if len(t.Value) == 0 { return fmt.Sprintf("%v:%v", t.Key, t.Effect) } diff --git a/pkg/apis/core/taint_test.go b/pkg/apis/core/taint_test.go index baa9404e084..dc2ed25175f 100644 --- a/pkg/apis/core/taint_test.go +++ b/pkg/apis/core/taint_test.go @@ -38,6 +38,19 @@ func TestTaintToString(t *testing.T) { }, expectedString: "foo:NoSchedule", }, + { + taint: &Taint{ + Key: "foo", + }, + expectedString: "foo", + }, + { + taint: &Taint{ + Key: "foo", + Value: "bar", + }, + expectedString: "foo=bar:", + }, } for i, tc := range testCases { diff --git a/staging/src/k8s.io/api/core/v1/taint.go b/staging/src/k8s.io/api/core/v1/taint.go index 7b606a30904..db71bd2fd0b 100644 --- a/staging/src/k8s.io/api/core/v1/taint.go +++ b/staging/src/k8s.io/api/core/v1/taint.go @@ -24,8 +24,14 @@ func (t *Taint) MatchTaint(taintToMatch *Taint) bool { return t.Key == taintToMatch.Key && t.Effect == taintToMatch.Effect } -// taint.ToString() converts taint struct to string in format key=value:effect or key:effect. +// taint.ToString() converts taint struct to string in format '=:', '=:', ':', or ''. func (t *Taint) ToString() string { + if len(t.Effect) == 0 { + if len(t.Value) == 0 { + return fmt.Sprintf("%v", t.Key) + } + return fmt.Sprintf("%v=%v:", t.Key, t.Value) + } if len(t.Value) == 0 { return fmt.Sprintf("%v:%v", t.Key, t.Effect) } diff --git a/staging/src/k8s.io/api/core/v1/taint_test.go b/staging/src/k8s.io/api/core/v1/taint_test.go index 22c7f9c4fe5..6a417182984 100644 --- a/staging/src/k8s.io/api/core/v1/taint_test.go +++ b/staging/src/k8s.io/api/core/v1/taint_test.go @@ -40,6 +40,19 @@ func TestTaintToString(t *testing.T) { }, expectedString: "foo:NoSchedule", }, + { + taint: &Taint{ + Key: "foo", + }, + expectedString: "foo", + }, + { + taint: &Taint{ + Key: "foo", + Value: "bar", + }, + expectedString: "foo=bar:", + }, } for i, tc := range testCases {