Use ptr.To to retrieve intstr addresses

This uses the generic ptr.To in k8s.io/utils to replace functions and
code constructs which only serve to return pointers to intstr
values. Other uses of the deprecated pointer package are updated in
modified files.

Signed-off-by: Stephen Kitt <skitt@redhat.com>
This commit is contained in:
Stephen Kitt
2023-08-17 12:12:12 +02:00
parent 9068bec08e
commit aa89e6dc97
85 changed files with 499 additions and 641 deletions

View File

@@ -36,7 +36,7 @@ import (
"k8s.io/client-go/kubernetes/fake"
"k8s.io/klog/v2/ktesting"
"k8s.io/kubernetes/pkg/controller"
"k8s.io/utils/pointer"
"k8s.io/utils/ptr"
)
func newDControllerRef(d *apps.Deployment) *metav1.OwnerReference {
@@ -394,32 +394,32 @@ func TestResolveFenceposts(t *testing.T) {
expectError bool
}{
{
maxSurge: newString("0%"),
maxUnavailable: newString("0%"),
maxSurge: ptr.To("0%"),
maxUnavailable: ptr.To("0%"),
desired: 0,
expectSurge: 0,
expectUnavailable: 1,
expectError: false,
},
{
maxSurge: newString("39%"),
maxUnavailable: newString("39%"),
maxSurge: ptr.To("39%"),
maxUnavailable: ptr.To("39%"),
desired: 10,
expectSurge: 4,
expectUnavailable: 3,
expectError: false,
},
{
maxSurge: newString("oops"),
maxUnavailable: newString("39%"),
maxSurge: ptr.To("oops"),
maxUnavailable: ptr.To("39%"),
desired: 10,
expectSurge: 0,
expectUnavailable: 0,
expectError: true,
},
{
maxSurge: newString("55%"),
maxUnavailable: newString("urg"),
maxSurge: ptr.To("55%"),
maxUnavailable: ptr.To("urg"),
desired: 10,
expectSurge: 0,
expectUnavailable: 0,
@@ -427,14 +427,14 @@ func TestResolveFenceposts(t *testing.T) {
},
{
maxSurge: nil,
maxUnavailable: newString("39%"),
maxUnavailable: ptr.To("39%"),
desired: 10,
expectSurge: 0,
expectUnavailable: 3,
expectError: false,
},
{
maxSurge: newString("39%"),
maxSurge: ptr.To("39%"),
maxUnavailable: nil,
desired: 10,
expectSurge: 4,
@@ -455,12 +455,10 @@ func TestResolveFenceposts(t *testing.T) {
t.Run(fmt.Sprintf("%d", num), func(t *testing.T) {
var maxSurge, maxUnavail *intstr.IntOrString
if test.maxSurge != nil {
surge := intstr.FromString(*test.maxSurge)
maxSurge = &surge
maxSurge = ptr.To(intstr.FromString(*test.maxSurge))
}
if test.maxUnavailable != nil {
unavail := intstr.FromString(*test.maxUnavailable)
maxUnavail = &unavail
maxUnavail = ptr.To(intstr.FromString(*test.maxUnavailable))
}
surge, unavail, err := ResolveFenceposts(maxSurge, maxUnavail, test.desired)
if err != nil && !test.expectError {
@@ -476,17 +474,13 @@ func TestResolveFenceposts(t *testing.T) {
}
}
func newString(s string) *string {
return &s
}
func TestNewRSNewReplicas(t *testing.T) {
tests := []struct {
Name string
strategyType apps.DeploymentStrategyType
depReplicas int32
newRSReplicas int32
maxSurge int
maxSurge int32
expected int32
}{
{
@@ -515,14 +509,8 @@ func TestNewRSNewReplicas(t *testing.T) {
*(newDeployment.Spec.Replicas) = test.depReplicas
newDeployment.Spec.Strategy = apps.DeploymentStrategy{Type: test.strategyType}
newDeployment.Spec.Strategy.RollingUpdate = &apps.RollingUpdateDeployment{
MaxUnavailable: func(i int) *intstr.IntOrString {
x := intstr.FromInt32(int32(i))
return &x
}(1),
MaxSurge: func(i int) *intstr.IntOrString {
x := intstr.FromInt32(int32(i))
return &x
}(test.maxSurge),
MaxUnavailable: ptr.To(intstr.FromInt32(1)),
MaxSurge: ptr.To(intstr.FromInt32(test.maxSurge)),
}
*(newRC.Spec.Replicas) = test.newRSReplicas
rs, err := NewRSNewReplicas(&newDeployment, []*apps.ReplicaSet{&rs5}, &newRC)
@@ -705,8 +693,8 @@ func TestDeploymentComplete(t *testing.T) {
Replicas: &desired,
Strategy: apps.DeploymentStrategy{
RollingUpdate: &apps.RollingUpdateDeployment{
MaxUnavailable: func(i int) *intstr.IntOrString { x := intstr.FromInt32(int32(i)); return &x }(int(maxUnavailable)),
MaxSurge: func(i int) *intstr.IntOrString { x := intstr.FromInt32(int32(i)); return &x }(int(maxSurge)),
MaxUnavailable: ptr.To(intstr.FromInt32(maxUnavailable)),
MaxSurge: ptr.To(intstr.FromInt32(maxSurge)),
},
Type: apps.RollingUpdateDeploymentStrategyType,
},
@@ -960,7 +948,7 @@ func TestMaxUnavailable(t *testing.T) {
Replicas: func(i int32) *int32 { return &i }(replicas),
Strategy: apps.DeploymentStrategy{
RollingUpdate: &apps.RollingUpdateDeployment{
MaxSurge: func(i int) *intstr.IntOrString { x := intstr.FromInt32(int32(i)); return &x }(int(1)),
MaxSurge: ptr.To(intstr.FromInt32(1)),
MaxUnavailable: &maxUnavailable,
},
Type: apps.RollingUpdateDeploymentStrategyType,
@@ -1255,11 +1243,11 @@ func TestGetDeploymentsForReplicaSet(t *testing.T) {
}
func TestMinAvailable(t *testing.T) {
maxSurge := func(i int) *intstr.IntOrString { x := intstr.FromInt32(int32(i)); return &x }(int(1))
maxSurge := ptr.To(intstr.FromInt32(1))
deployment := func(replicas int32, maxUnavailable intstr.IntOrString) *apps.Deployment {
return &apps.Deployment{
Spec: apps.DeploymentSpec{
Replicas: pointer.Int32(replicas),
Replicas: ptr.To(replicas),
Strategy: apps.DeploymentStrategy{
RollingUpdate: &apps.RollingUpdateDeployment{
MaxSurge: maxSurge,
@@ -1299,7 +1287,7 @@ func TestMinAvailable(t *testing.T) {
name: "minAvailable with Recreate deployment strategy",
deployment: &apps.Deployment{
Spec: apps.DeploymentSpec{
Replicas: pointer.Int32(10),
Replicas: ptr.To[int32](10),
Strategy: apps.DeploymentStrategy{
Type: apps.RecreateDeploymentStrategyType,
},