Skip to content

Commit

Permalink
This commit does two things in pkg package:
Browse files Browse the repository at this point in the history
1. Remove unused ptr functions.
2. Replace ptr functions with k8s.io/utils/pointer
  • Loading branch information
WanLinghao committed Apr 9, 2019
1 parent 4a1da48 commit d0138ca
Show file tree
Hide file tree
Showing 20 changed files with 75 additions and 97 deletions.
1 change: 1 addition & 0 deletions pkg/apis/auditregistration/validation/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ go_test(
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/validation/field:go_default_library",
"//vendor/github.com/stretchr/testify/require:go_default_library",
"//vendor/k8s.io/utils/pointer:go_default_library",
],
)

Expand Down
35 changes: 17 additions & 18 deletions pkg/apis/auditregistration/validation/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/kubernetes/pkg/apis/auditregistration"
utilpointer "k8s.io/utils/pointer"
)

func TestValidateAuditSink(t *testing.T) {
Expand Down Expand Up @@ -133,8 +134,6 @@ func TestValidatePolicy(t *testing.T) {
}
}

func strPtr(s string) *string { return &s }

func TestValidateWebhookConfiguration(t *testing.T) {
tests := []struct {
name string
Expand All @@ -157,7 +156,7 @@ func TestValidateWebhookConfiguration(t *testing.T) {
Name: "n",
Port: 443,
},
URL: strPtr("example.com/k8s/webhook"),
URL: utilpointer.StringPtr("example.com/k8s/webhook"),
},
},
expectedError: `webhook.clientConfig: Required value: exactly one of url or service is required`,
Expand All @@ -166,7 +165,7 @@ func TestValidateWebhookConfiguration(t *testing.T) {
name: "blank URL",
config: auditregistration.Webhook{
ClientConfig: auditregistration.WebhookClientConfig{
URL: strPtr(""),
URL: utilpointer.StringPtr(""),
},
},
expectedError: `webhook.clientConfig.url: Invalid value: "": host must be provided`,
Expand All @@ -175,7 +174,7 @@ func TestValidateWebhookConfiguration(t *testing.T) {
name: "missing host",
config: auditregistration.Webhook{
ClientConfig: auditregistration.WebhookClientConfig{
URL: strPtr("https:///fancy/webhook"),
URL: utilpointer.StringPtr("https:///fancy/webhook"),
},
},
expectedError: `host must be provided`,
Expand All @@ -184,7 +183,7 @@ func TestValidateWebhookConfiguration(t *testing.T) {
name: "fragment",
config: auditregistration.Webhook{
ClientConfig: auditregistration.WebhookClientConfig{
URL: strPtr("https://example.com/#bookmark"),
URL: utilpointer.StringPtr("https://example.com/#bookmark"),
},
},
expectedError: `"bookmark": fragments are not permitted`,
Expand All @@ -193,7 +192,7 @@ func TestValidateWebhookConfiguration(t *testing.T) {
name: "query",
config: auditregistration.Webhook{
ClientConfig: auditregistration.WebhookClientConfig{
URL: strPtr("https://example.com?arg=value"),
URL: utilpointer.StringPtr("https://example.com?arg=value"),
},
},
expectedError: `"arg=value": query parameters are not permitted`,
Expand All @@ -202,7 +201,7 @@ func TestValidateWebhookConfiguration(t *testing.T) {
name: "user",
config: auditregistration.Webhook{
ClientConfig: auditregistration.WebhookClientConfig{
URL: strPtr("https://[email protected]/"),
URL: utilpointer.StringPtr("https://[email protected]/"),
},
},
expectedError: `"harry.potter": user information is not permitted`,
Expand All @@ -211,7 +210,7 @@ func TestValidateWebhookConfiguration(t *testing.T) {
name: "just totally wrong",
config: auditregistration.Webhook{
ClientConfig: auditregistration.WebhookClientConfig{
URL: strPtr("arg#backwards=thisis?html.index/port:host//:https"),
URL: utilpointer.StringPtr("arg#backwards=thisis?html.index/port:host//:https"),
},
},
expectedError: `host must be provided`,
Expand All @@ -223,7 +222,7 @@ func TestValidateWebhookConfiguration(t *testing.T) {
Service: &auditregistration.ServiceReference{
Namespace: "ns",
Name: "n",
Path: strPtr("foo/"),
Path: utilpointer.StringPtr("foo/"),
Port: 443,
},
},
Expand All @@ -237,7 +236,7 @@ func TestValidateWebhookConfiguration(t *testing.T) {
Service: &auditregistration.ServiceReference{
Namespace: "ns",
Name: "n",
Path: strPtr("foo/"),
Path: utilpointer.StringPtr("foo/"),
Port: 65536,
},
},
Expand All @@ -251,7 +250,7 @@ func TestValidateWebhookConfiguration(t *testing.T) {
Service: &auditregistration.ServiceReference{
Namespace: "ns",
Name: "n",
Path: strPtr("foo/"),
Path: utilpointer.StringPtr("foo/"),
Port: 0,
},
},
Expand All @@ -265,7 +264,7 @@ func TestValidateWebhookConfiguration(t *testing.T) {
Service: &auditregistration.ServiceReference{
Namespace: "ns",
Name: "n",
Path: strPtr("/"),
Path: utilpointer.StringPtr("/"),
Port: 443,
},
},
Expand All @@ -279,7 +278,7 @@ func TestValidateWebhookConfiguration(t *testing.T) {
Service: &auditregistration.ServiceReference{
Namespace: "ns",
Name: "n",
Path: strPtr("/foo"),
Path: utilpointer.StringPtr("/foo"),
Port: 443,
},
},
Expand All @@ -293,7 +292,7 @@ func TestValidateWebhookConfiguration(t *testing.T) {
Service: &auditregistration.ServiceReference{
Namespace: "ns",
Name: "n",
Path: strPtr("//"),
Path: utilpointer.StringPtr("//"),
Port: 443,
},
},
Expand All @@ -307,7 +306,7 @@ func TestValidateWebhookConfiguration(t *testing.T) {
Service: &auditregistration.ServiceReference{
Namespace: "ns",
Name: "n",
Path: strPtr("/foo//bar/"),
Path: utilpointer.StringPtr("/foo//bar/"),
Port: 443,
},
},
Expand All @@ -320,7 +319,7 @@ func TestValidateWebhookConfiguration(t *testing.T) {
Service: &auditregistration.ServiceReference{
Namespace: "ns",
Name: "n",
Path: strPtr("/foo/bar//"),
Path: utilpointer.StringPtr("/foo/bar//"),
Port: 443,
},
},
Expand All @@ -334,7 +333,7 @@ func TestValidateWebhookConfiguration(t *testing.T) {
Service: &auditregistration.ServiceReference{
Namespace: "ns",
Name: "n",
Path: strPtr("/apis/foo.bar/v1alpha1/--bad"),
Path: utilpointer.StringPtr("/apis/foo.bar/v1alpha1/--bad"),
Port: 443,
},
},
Expand Down
14 changes: 5 additions & 9 deletions pkg/apis/core/validation/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12560,7 +12560,7 @@ func TestValidateSecurityContext(t *testing.T) {
runAsUser := int64(1)
fullValidSC := func() *core.SecurityContext {
return &core.SecurityContext{
Privileged: boolPtr(false),
Privileged: utilpointer.BoolPtr(false),
Capabilities: &core.Capabilities{
Add: []core.Capability{"foo"},
Drop: []core.Capability{"bar"},
Expand Down Expand Up @@ -12605,19 +12605,19 @@ func TestValidateSecurityContext(t *testing.T) {
}

privRequestWithGlobalDeny := fullValidSC()
privRequestWithGlobalDeny.Privileged = boolPtr(true)
privRequestWithGlobalDeny.Privileged = utilpointer.BoolPtr(true)

negativeRunAsUser := fullValidSC()
negativeUser := int64(-1)
negativeRunAsUser.RunAsUser = &negativeUser

privWithoutEscalation := fullValidSC()
privWithoutEscalation.Privileged = boolPtr(true)
privWithoutEscalation.AllowPrivilegeEscalation = boolPtr(false)
privWithoutEscalation.Privileged = utilpointer.BoolPtr(true)
privWithoutEscalation.AllowPrivilegeEscalation = utilpointer.BoolPtr(false)

capSysAdminWithoutEscalation := fullValidSC()
capSysAdminWithoutEscalation.Capabilities.Add = []core.Capability{"CAP_SYS_ADMIN"}
capSysAdminWithoutEscalation.AllowPrivilegeEscalation = boolPtr(false)
capSysAdminWithoutEscalation.AllowPrivilegeEscalation = utilpointer.BoolPtr(false)

errorCases := map[string]struct {
sc *core.SecurityContext
Expand Down Expand Up @@ -13145,7 +13145,3 @@ func TestValidateOrSetClientIPAffinityConfig(t *testing.T) {
}
}
}

func boolPtr(b bool) *bool {
return &b
}
1 change: 1 addition & 0 deletions pkg/controller/cronjob/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ go_test(
"//staging/src/k8s.io/apimachinery/pkg/types:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/sets:go_default_library",
"//staging/src/k8s.io/client-go/tools/record:go_default_library",
"//vendor/k8s.io/utils/pointer:go_default_library",
],
)

Expand Down
11 changes: 5 additions & 6 deletions pkg/controller/cronjob/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ import (
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
utilpointer "k8s.io/utils/pointer"
)

func boolptr(b bool) *bool { return &b }

func TestGetJobFromTemplate(t *testing.T) {
// getJobFromTemplate() needs to take the job template and copy the labels and annotations
// and other fields, and add a created-by reference.
Expand Down Expand Up @@ -134,7 +133,7 @@ func TestGetParentUIDFromJob(t *testing.T) {
{
Kind: "CronJob",
UID: types.UID("5ef034e0-1890-11e6-8935-42010af0003e"),
Controller: boolptr(true),
Controller: utilpointer.BoolPtr(true),
},
})

Expand All @@ -158,19 +157,19 @@ func TestGroupJobsByParent(t *testing.T) {
ownerReference1 := metav1.OwnerReference{
Kind: "CronJob",
UID: uid1,
Controller: boolptr(true),
Controller: utilpointer.BoolPtr(true),
}

ownerReference2 := metav1.OwnerReference{
Kind: "CronJob",
UID: uid2,
Controller: boolptr(true),
Controller: utilpointer.BoolPtr(true),
}

ownerReference3 := metav1.OwnerReference{
Kind: "CronJob",
UID: uid3,
Controller: boolptr(true),
Controller: utilpointer.BoolPtr(true),
}

{
Expand Down
1 change: 1 addition & 0 deletions pkg/controller/daemon/util/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,6 @@ go_test(
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//staging/src/k8s.io/apiserver/pkg/util/feature:go_default_library",
"//staging/src/k8s.io/apiserver/pkg/util/feature/testing:go_default_library",
"//vendor/k8s.io/utils/pointer:go_default_library",
],
)
14 changes: 5 additions & 9 deletions pkg/controller/daemon/util/daemonset_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
utilfeaturetesting "k8s.io/apiserver/pkg/util/feature/testing"
"k8s.io/kubernetes/pkg/features"
schedulerapi "k8s.io/kubernetes/pkg/scheduler/api"
utilpointer "k8s.io/utils/pointer"
)

func newPod(podName string, nodeName string, label map[string]string) *v1.Pod {
Expand All @@ -51,8 +52,8 @@ func newPod(podName string, nodeName string, label map[string]string) *v1.Pod {
}

func TestIsPodUpdated(t *testing.T) {
templateGeneration := int64Ptr(12345)
badGeneration := int64Ptr(12345)
templateGeneration := utilpointer.Int64Ptr(12345)
badGeneration := utilpointer.Int64Ptr(12345)
hash := "55555"
labels := map[string]string{extensions.DaemonSetTemplateGenerationKey: fmt.Sprint(templateGeneration), extensions.DefaultDaemonSetUniqueLabelKey: hash}
labelsNoHash := map[string]string{extensions.DaemonSetTemplateGenerationKey: fmt.Sprint(templateGeneration)}
Expand Down Expand Up @@ -148,8 +149,8 @@ func TestCreatePodTemplate(t *testing.T) {
hash string
expectUniqueLabel bool
}{
{int64Ptr(1), "", false},
{int64Ptr(2), "3242341807", true},
{utilpointer.Int64Ptr(1), "", false},
{utilpointer.Int64Ptr(2), "3242341807", true},
}
for _, test := range tests {
podTemplateSpec := v1.PodTemplateSpec{}
Expand All @@ -168,11 +169,6 @@ func TestCreatePodTemplate(t *testing.T) {
}
}

func int64Ptr(i int) *int64 {
li := int64(i)
return &li
}

func TestReplaceDaemonSetPodNodeNameNodeAffinity(t *testing.T) {
tests := []struct {
affinity *v1.Affinity
Expand Down
1 change: 1 addition & 0 deletions pkg/controller/ttlafterfinished/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ go_test(
"//staging/src/k8s.io/api/batch/v1:go_default_library",
"//staging/src/k8s.io/api/core/v1:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/k8s.io/utils/pointer:go_default_library",
],
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
batch "k8s.io/api/batch/v1"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilpointer "k8s.io/utils/pointer"
)

func newJob(completionTime, failedTime metav1.Time, ttl *int32) *batch.Job {
Expand Down Expand Up @@ -74,10 +75,6 @@ func durationPointer(n int) *time.Duration {
return &s
}

func int32Ptr(n int32) *int32 {
return &n
}

func TestTimeLeft(t *testing.T) {
now := metav1.Now()

Expand All @@ -93,7 +90,7 @@ func TestTimeLeft(t *testing.T) {
}{
{
name: "Error case: Job unfinished",
ttl: int32Ptr(100),
ttl: utilpointer.Int32Ptr(100),
since: &now.Time,
expectErr: true,
expectErrStr: "should not be cleaned up",
Expand All @@ -108,21 +105,21 @@ func TestTimeLeft(t *testing.T) {
{
name: "Job completed now, 0s TTL",
completionTime: now,
ttl: int32Ptr(0),
ttl: utilpointer.Int32Ptr(0),
since: &now.Time,
expectedTimeLeft: durationPointer(0),
},
{
name: "Job completed now, 10s TTL",
completionTime: now,
ttl: int32Ptr(10),
ttl: utilpointer.Int32Ptr(10),
since: &now.Time,
expectedTimeLeft: durationPointer(10),
},
{
name: "Job completed 10s ago, 15s TTL",
completionTime: metav1.NewTime(now.Add(-10 * time.Second)),
ttl: int32Ptr(15),
ttl: utilpointer.Int32Ptr(15),
since: &now.Time,
expectedTimeLeft: durationPointer(5),
},
Expand All @@ -136,21 +133,21 @@ func TestTimeLeft(t *testing.T) {
{
name: "Job failed now, 0s TTL",
failedTime: now,
ttl: int32Ptr(0),
ttl: utilpointer.Int32Ptr(0),
since: &now.Time,
expectedTimeLeft: durationPointer(0),
},
{
name: "Job failed now, 10s TTL",
failedTime: now,
ttl: int32Ptr(10),
ttl: utilpointer.Int32Ptr(10),
since: &now.Time,
expectedTimeLeft: durationPointer(10),
},
{
name: "Job failed 10s ago, 15s TTL",
failedTime: metav1.NewTime(now.Add(-10 * time.Second)),
ttl: int32Ptr(15),
ttl: utilpointer.Int32Ptr(15),
since: &now.Time,
expectedTimeLeft: durationPointer(5),
},
Expand Down
Loading

0 comments on commit d0138ca

Please sign in to comment.