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

@@ -49,7 +49,7 @@ import (
"k8s.io/kubernetes/pkg/controller"
"k8s.io/kubernetes/pkg/controller/deployment/util"
"k8s.io/kubernetes/pkg/controller/testutil"
"k8s.io/utils/pointer"
"k8s.io/utils/ptr"
)
var (
@@ -57,7 +57,7 @@ var (
noTimestamp = metav1.Time{}
)
func rs(name string, replicas int, selector map[string]string, timestamp metav1.Time) *apps.ReplicaSet {
func rs(name string, replicas int32, selector map[string]string, timestamp metav1.Time) *apps.ReplicaSet {
return &apps.ReplicaSet{
ObjectMeta: metav1.ObjectMeta{
Name: name,
@@ -65,22 +65,22 @@ func rs(name string, replicas int, selector map[string]string, timestamp metav1.
Namespace: metav1.NamespaceDefault,
},
Spec: apps.ReplicaSetSpec{
Replicas: pointer.Int32(int32(replicas)),
Replicas: ptr.To(replicas),
Selector: &metav1.LabelSelector{MatchLabels: selector},
Template: v1.PodTemplateSpec{},
},
}
}
func newRSWithStatus(name string, specReplicas, statusReplicas int, selector map[string]string) *apps.ReplicaSet {
func newRSWithStatus(name string, specReplicas, statusReplicas int32, selector map[string]string) *apps.ReplicaSet {
rs := rs(name, specReplicas, selector, noTimestamp)
rs.Status = apps.ReplicaSetStatus{
Replicas: int32(statusReplicas),
Replicas: statusReplicas,
}
return rs
}
func newDeployment(name string, replicas int, revisionHistoryLimit *int32, maxSurge, maxUnavailable *intstr.IntOrString, selector map[string]string) *apps.Deployment {
func newDeployment(name string, replicas int32, revisionHistoryLimit *int32, maxSurge, maxUnavailable *intstr.IntOrString, selector map[string]string) *apps.Deployment {
d := apps.Deployment{
TypeMeta: metav1.TypeMeta{APIVersion: "apps/v1", Kind: "Deployment"},
ObjectMeta: metav1.ObjectMeta{
@@ -93,11 +93,11 @@ func newDeployment(name string, replicas int, revisionHistoryLimit *int32, maxSu
Strategy: apps.DeploymentStrategy{
Type: apps.RollingUpdateDeploymentStrategyType,
RollingUpdate: &apps.RollingUpdateDeployment{
MaxUnavailable: func() *intstr.IntOrString { i := intstr.FromInt32(0); return &i }(),
MaxSurge: func() *intstr.IntOrString { i := intstr.FromInt32(0); return &i }(),
MaxUnavailable: ptr.To(intstr.FromInt32(0)),
MaxSurge: ptr.To(intstr.FromInt32(0)),
},
},
Replicas: pointer.Int32(int32(replicas)),
Replicas: ptr.To(replicas),
Selector: &metav1.LabelSelector{MatchLabels: selector},
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
@@ -123,7 +123,7 @@ func newDeployment(name string, replicas int, revisionHistoryLimit *int32, maxSu
return &d
}
func newReplicaSet(d *apps.Deployment, name string, replicas int) *apps.ReplicaSet {
func newReplicaSet(d *apps.Deployment, name string, replicas int32) *apps.ReplicaSet {
return &apps.ReplicaSet{
TypeMeta: metav1.TypeMeta{Kind: "ReplicaSet"},
ObjectMeta: metav1.ObjectMeta{
@@ -135,7 +135,7 @@ func newReplicaSet(d *apps.Deployment, name string, replicas int) *apps.ReplicaS
},
Spec: apps.ReplicaSetSpec{
Selector: d.Spec.Selector,
Replicas: pointer.Int32(int32(replicas)),
Replicas: ptr.To(replicas),
Template: d.Spec.Template,
},
}

View File

@@ -59,11 +59,11 @@ func currentDeployment(pds *int32, replicas, statusReplicas, updatedReplicas, av
}
// helper to create RS with given availableReplicas
func newRSWithAvailable(name string, specReplicas, statusReplicas, availableReplicas int) *apps.ReplicaSet {
func newRSWithAvailable(name string, specReplicas, statusReplicas, availableReplicas int32) *apps.ReplicaSet {
rs := rs(name, specReplicas, nil, metav1.Time{})
rs.Status = apps.ReplicaSetStatus{
Replicas: int32(statusReplicas),
AvailableReplicas: int32(availableReplicas),
Replicas: statusReplicas,
AvailableReplicas: availableReplicas,
}
return rs
}

View File

@@ -33,11 +33,11 @@ import (
func TestScaleDownOldReplicaSets(t *testing.T) {
tests := []struct {
oldRSSizes []int
oldRSSizes []int32
d *apps.Deployment
}{
{
oldRSSizes: []int{3},
oldRSSizes: []int32{3},
d: newDeployment("foo", 3, nil, nil, nil, map[string]string{"foo": "bar"}),
},
}

View File

@@ -30,12 +30,12 @@ import (
func TestDeploymentController_reconcileNewReplicaSet(t *testing.T) {
tests := []struct {
deploymentReplicas int
deploymentReplicas int32
maxSurge intstr.IntOrString
oldReplicas int
newReplicas int
oldReplicas int32
newReplicas int32
scaleExpected bool
expectedNewReplicas int
expectedNewReplicas int32
}{
{
// Should not scale up.
@@ -115,7 +115,7 @@ func TestDeploymentController_reconcileNewReplicaSet(t *testing.T) {
continue
}
updated := fake.Actions()[0].(core.UpdateAction).GetObject().(*apps.ReplicaSet)
if e, a := test.expectedNewReplicas, int(*(updated.Spec.Replicas)); e != a {
if e, a := test.expectedNewReplicas, *(updated.Spec.Replicas); e != a {
t.Errorf("expected update to %d replicas, got %d", e, a)
}
}
@@ -123,14 +123,14 @@ func TestDeploymentController_reconcileNewReplicaSet(t *testing.T) {
func TestDeploymentController_reconcileOldReplicaSets(t *testing.T) {
tests := []struct {
deploymentReplicas int
deploymentReplicas int32
maxUnavailable intstr.IntOrString
oldReplicas int
newReplicas int
oldReplicas int32
newReplicas int32
readyPodsFromOldRS int
readyPodsFromNewRS int
scaleExpected bool
expectedOldReplicas int
expectedOldReplicas int32
}{
{
deploymentReplicas: 10,
@@ -220,7 +220,7 @@ func TestDeploymentController_reconcileOldReplicaSets(t *testing.T) {
func TestDeploymentController_cleanupUnhealthyReplicas(t *testing.T) {
tests := []struct {
oldReplicas int
oldReplicas int32
readyPods int
unHealthyPods int
maxCleanupCount int
@@ -285,12 +285,12 @@ func TestDeploymentController_cleanupUnhealthyReplicas(t *testing.T) {
func TestDeploymentController_scaleDownOldReplicaSetsForRollingUpdate(t *testing.T) {
tests := []struct {
deploymentReplicas int
deploymentReplicas int32
maxUnavailable intstr.IntOrString
readyPods int
oldReplicas int
oldReplicas int32
scaleExpected bool
expectedOldReplicas int
expectedOldReplicas int32
}{
{
deploymentReplicas: 10,
@@ -379,7 +379,7 @@ func TestDeploymentController_scaleDownOldReplicaSetsForRollingUpdate(t *testing
continue
}
updated := updateAction.GetObject().(*apps.ReplicaSet)
if e, a := test.expectedOldReplicas, int(*(updated.Spec.Replicas)); e != a {
if e, a := test.expectedOldReplicas, *(updated.Spec.Replicas); e != a {
t.Errorf("expected update to %d replicas, got %d", e, a)
}
}

View File

@@ -32,19 +32,15 @@ import (
"k8s.io/klog/v2/ktesting"
"k8s.io/kubernetes/pkg/controller"
deploymentutil "k8s.io/kubernetes/pkg/controller/deployment/util"
"k8s.io/utils/ptr"
)
func intOrStrP(val int) *intstr.IntOrString {
intOrStr := intstr.FromInt32(int32(val))
return &intOrStr
}
func TestScale(t *testing.T) {
newTimestamp := metav1.Date(2016, 5, 20, 2, 0, 0, 0, time.UTC)
oldTimestamp := metav1.Date(2016, 5, 20, 1, 0, 0, 0, time.UTC)
olderTimestamp := metav1.Date(2016, 5, 20, 0, 0, 0, 0, time.UTC)
var updatedTemplate = func(replicas int) *apps.Deployment {
var updatedTemplate = func(replicas int32) *apps.Deployment {
d := newDeployment("foo", replicas, nil, nil, nil, map[string]string{"foo": "bar"})
d.Spec.Template.Labels["another"] = "label"
return d
@@ -221,8 +217,8 @@ func TestScale(t *testing.T) {
},
{
name: "deployment with surge pods",
deployment: newDeployment("foo", 20, nil, intOrStrP(2), nil, nil),
oldDeployment: newDeployment("foo", 10, nil, intOrStrP(2), nil, nil),
deployment: newDeployment("foo", 20, nil, ptr.To(intstr.FromInt32(2)), nil, nil),
oldDeployment: newDeployment("foo", 10, nil, ptr.To(intstr.FromInt32(2)), nil, nil),
newRS: rs("foo-v2", 6, nil, newTimestamp),
oldRSs: []*apps.ReplicaSet{rs("foo-v1", 6, nil, oldTimestamp)},
@@ -232,8 +228,8 @@ func TestScale(t *testing.T) {
},
{
name: "change both surge and size",
deployment: newDeployment("foo", 50, nil, intOrStrP(6), nil, nil),
oldDeployment: newDeployment("foo", 10, nil, intOrStrP(3), nil, nil),
deployment: newDeployment("foo", 50, nil, ptr.To(intstr.FromInt32(6)), nil, nil),
oldDeployment: newDeployment("foo", 10, nil, ptr.To(intstr.FromInt32(3)), nil, nil),
newRS: rs("foo-v2", 5, nil, newTimestamp),
oldRSs: []*apps.ReplicaSet{rs("foo-v1", 8, nil, oldTimestamp)},
@@ -254,8 +250,8 @@ func TestScale(t *testing.T) {
},
{
name: "saturated but broken new replica set does not affect old pods",
deployment: newDeployment("foo", 2, nil, intOrStrP(1), intOrStrP(1), nil),
oldDeployment: newDeployment("foo", 2, nil, intOrStrP(1), intOrStrP(1), nil),
deployment: newDeployment("foo", 2, nil, ptr.To(intstr.FromInt32(1)), ptr.To(intstr.FromInt32(1)), nil),
oldDeployment: newDeployment("foo", 2, nil, ptr.To(intstr.FromInt32(1)), ptr.To(intstr.FromInt32(1)), nil),
newRS: func() *apps.ReplicaSet {
rs := rs("foo-v2", 2, nil, newTimestamp)
@@ -458,7 +454,7 @@ func TestDeploymentController_cleanupDeploymentOrder(t *testing.T) {
now := metav1.Now()
duration := time.Minute
newRSWithRevisionAndCreationTimestamp := func(name string, replicas int, selector map[string]string, timestamp time.Time, revision string) *apps.ReplicaSet {
newRSWithRevisionAndCreationTimestamp := func(name string, replicas int32, selector map[string]string, timestamp time.Time, revision string) *apps.ReplicaSet {
rs := rs(name, replicas, selector, metav1.NewTime(timestamp))
if revision != "" {
rs.Annotations = map[string]string{

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,
},