Skip to content

Commit

Permalink
cleanup pointer, use ptr.Toto obtain a pointer, ptr.Deref to derefere…
Browse files Browse the repository at this point in the history
…nce a pointer.

Signed-off-by: Lan Liang <gcslyp@gmail.com>
  • Loading branch information
liangyuanpeng committed Mar 19, 2024
1 parent 909d005 commit b7b583a
Show file tree
Hide file tree
Showing 26 changed files with 139 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

"k8s.io/apimachinery/pkg/runtime"
"k8s.io/klog/v2"
"k8s.io/utils/pointer"
"k8s.io/utils/ptr"

workloadv1alpha1 "github.com/karmada-io/karmada/examples/customresourceinterpreter/apis/workload/v1alpha1"
configv1alpha1 "github.com/karmada-io/karmada/pkg/apis/config/v1alpha1"
Expand Down Expand Up @@ -142,9 +142,9 @@ func (e *workloadInterpreter) responseWithExploreAggregateStatus(workload *workl
}

func (e *workloadInterpreter) responseWithExploreInterpretHealth(workload *workloadv1alpha1.Workload) interpreter.Response {
healthy := pointer.Bool(false)
healthy := ptr.To[bool](false)
if workload.Status.ReadyReplicas == *workload.Spec.Replicas {
healthy = pointer.Bool(true)
healthy = ptr.To[bool](true)
}

res := interpreter.Succeeded("")
Expand Down
6 changes: 3 additions & 3 deletions pkg/apis/policy/v1alpha1/propagation_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package v1alpha1
import (
"testing"

"k8s.io/utils/pointer"
"k8s.io/utils/ptr"
)

func TestPropagationPolicy_ExplicitPriority(t *testing.T) {
Expand All @@ -34,7 +34,7 @@ func TestPropagationPolicy_ExplicitPriority(t *testing.T) {
},
{
name: "expected to be declared priority in pp",
declaredPriority: pointer.Int32(20),
declaredPriority: ptr.To[int32](20),
expectedPriority: 20,
},
}
Expand Down Expand Up @@ -62,7 +62,7 @@ func TestClusterPropagationPolicy_ExplicitPriority(t *testing.T) {
},
{
name: "expected to be declared priority in cpp",
declaredPriority: pointer.Int32(20),
declaredPriority: ptr.To[int32](20),
expectedPriority: 20,
},
}
Expand Down
16 changes: 8 additions & 8 deletions pkg/apis/work/v1alpha2/binding_types_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"reflect"
"testing"

"k8s.io/utils/pointer"
"k8s.io/utils/ptr"
)

func TestResourceBindingSpec_TargetContains(t *testing.T) {
Expand Down Expand Up @@ -181,7 +181,7 @@ func TestResourceBindingSpec_GracefulEvictCluster(t *testing.T) {
GracefulEvictionTasks: []GracefulEvictionTask{
{
FromCluster: "m1",
Replicas: pointer.Int32(1),
Replicas: ptr.To[int32](1),
Reason: EvictionReasonTaintUntolerated,
Message: "graceful eviction",
Producer: EvictionProducerTaintManager,
Expand All @@ -205,7 +205,7 @@ func TestResourceBindingSpec_GracefulEvictCluster(t *testing.T) {
GracefulEvictionTasks: []GracefulEvictionTask{
{
FromCluster: "m2",
Replicas: pointer.Int32(2),
Replicas: ptr.To[int32](2),
Reason: EvictionReasonTaintUntolerated,
Message: "graceful eviction",
Producer: EvictionProducerTaintManager,
Expand All @@ -229,7 +229,7 @@ func TestResourceBindingSpec_GracefulEvictCluster(t *testing.T) {
GracefulEvictionTasks: []GracefulEvictionTask{
{
FromCluster: "m3",
Replicas: pointer.Int32(3),
Replicas: ptr.To[int32](3),
Reason: EvictionReasonTaintUntolerated,
Message: "graceful eviction",
Producer: EvictionProducerTaintManager,
Expand Down Expand Up @@ -257,7 +257,7 @@ func TestResourceBindingSpec_GracefulEvictCluster(t *testing.T) {
},
{
FromCluster: "m3",
Replicas: pointer.Int32(3),
Replicas: ptr.To[int32](3),
Reason: EvictionReasonTaintUntolerated,
Message: "graceful eviction",
Producer: EvictionProducerTaintManager,
Expand All @@ -277,7 +277,7 @@ func TestResourceBindingSpec_GracefulEvictCluster(t *testing.T) {
GracefulEvictionTasks: []GracefulEvictionTask{
{
FromCluster: "m1",
Replicas: pointer.Int32(1),
Replicas: ptr.To[int32](1),
Reason: EvictionReasonTaintUntolerated,
Message: "graceful eviction v1",
Producer: EvictionProducerTaintManager,
Expand All @@ -286,7 +286,7 @@ func TestResourceBindingSpec_GracefulEvictCluster(t *testing.T) {
},
EvictEvent: GracefulEvictionTask{
FromCluster: "m1",
Replicas: pointer.Int32(1),
Replicas: ptr.To[int32](1),
Reason: EvictionReasonTaintUntolerated,
Message: "graceful eviction v2",
Producer: EvictionProducerTaintManager,
Expand All @@ -296,7 +296,7 @@ func TestResourceBindingSpec_GracefulEvictCluster(t *testing.T) {
GracefulEvictionTasks: []GracefulEvictionTask{
{
FromCluster: "m1",
Replicas: pointer.Int32(1),
Replicas: ptr.To[int32](1),
Reason: EvictionReasonTaintUntolerated,
Message: "graceful eviction v1",
Producer: EvictionProducerTaintManager,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/tools/record"
"k8s.io/klog/v2"
"k8s.io/utils/pointer"
"k8s.io/utils/ptr"
controllerruntime "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -165,7 +165,7 @@ func (c *CRBApplicationFailoverController) evictBinding(binding *workv1alpha2.Cl
}
case policyv1alpha1.Never:
if features.FeatureGate.Enabled(features.GracefulEviction) {
binding.Spec.GracefulEvictCluster(cluster, workv1alpha2.NewTaskOptions(workv1alpha2.WithProducer(CRBApplicationFailoverControllerName), workv1alpha2.WithReason(workv1alpha2.EvictionReasonApplicationFailure), workv1alpha2.WithSuppressDeletion(pointer.Bool(true))))
binding.Spec.GracefulEvictCluster(cluster, workv1alpha2.NewTaskOptions(workv1alpha2.WithProducer(CRBApplicationFailoverControllerName), workv1alpha2.WithReason(workv1alpha2.EvictionReasonApplicationFailure), workv1alpha2.WithSuppressDeletion(ptr.To[bool](true))))
} else {
err := fmt.Errorf("GracefulEviction featureGate must be enabled when purgeMode is %s", policyv1alpha1.Never)
klog.Error(err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/tools/record"
"k8s.io/klog/v2"
"k8s.io/utils/pointer"
"k8s.io/utils/ptr"
controllerruntime "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -167,7 +167,7 @@ func (c *RBApplicationFailoverController) evictBinding(binding *workv1alpha2.Res
case policyv1alpha1.Never:
if features.FeatureGate.Enabled(features.GracefulEviction) {
binding.Spec.GracefulEvictCluster(cluster, workv1alpha2.NewTaskOptions(workv1alpha2.WithProducer(RBApplicationFailoverControllerName),
workv1alpha2.WithReason(workv1alpha2.EvictionReasonApplicationFailure), workv1alpha2.WithSuppressDeletion(pointer.Bool(true))))
workv1alpha2.WithReason(workv1alpha2.EvictionReasonApplicationFailure), workv1alpha2.WithSuppressDeletion(ptr.To[bool](true))))
} else {
err := fmt.Errorf("GracefulEviction featureGate must be enabled when purgeMode is %s", policyv1alpha1.Never)
klog.Error(err)
Expand Down
20 changes: 10 additions & 10 deletions pkg/controllers/gracefuleviction/evictiontask_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"time"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/pointer"
"k8s.io/utils/ptr"

workv1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2"
)
Expand Down Expand Up @@ -143,7 +143,7 @@ func Test_assessSingleTask(t *testing.T) {
args: args{
task: workv1alpha2.GracefulEvictionTask{
FromCluster: "member1",
GracePeriodSeconds: pointer.Int32(30),
GracePeriodSeconds: ptr.To[int32](30),
CreationTimestamp: &metav1.Time{Time: timeNow.Add(time.Minute * -1)},
},
opt: assessmentOption{
Expand All @@ -163,7 +163,7 @@ func Test_assessSingleTask(t *testing.T) {
args: args{
task: workv1alpha2.GracefulEvictionTask{
FromCluster: "member1",
GracePeriodSeconds: pointer.Int32(120),
GracePeriodSeconds: ptr.To[int32](120),
CreationTimestamp: &metav1.Time{Time: timeNow.Add(time.Minute * -1)},
},
opt: assessmentOption{
Expand All @@ -178,7 +178,7 @@ func Test_assessSingleTask(t *testing.T) {
},
want: &workv1alpha2.GracefulEvictionTask{
FromCluster: "member1",
GracePeriodSeconds: pointer.Int32(120),
GracePeriodSeconds: ptr.To[int32](120),
CreationTimestamp: &metav1.Time{Time: timeNow.Add(time.Minute * -1)},
},
},
Expand All @@ -187,7 +187,7 @@ func Test_assessSingleTask(t *testing.T) {
args: args{
task: workv1alpha2.GracefulEvictionTask{
FromCluster: "member1",
SuppressDeletion: pointer.Bool(true),
SuppressDeletion: ptr.To[bool](true),
CreationTimestamp: &metav1.Time{Time: timeNow.Add(time.Minute * -1)},
},
opt: assessmentOption{
Expand All @@ -202,7 +202,7 @@ func Test_assessSingleTask(t *testing.T) {
},
want: &workv1alpha2.GracefulEvictionTask{
FromCluster: "member1",
SuppressDeletion: pointer.Bool(true),
SuppressDeletion: ptr.To[bool](true),
CreationTimestamp: &metav1.Time{Time: timeNow.Add(time.Minute * -1)},
},
},
Expand All @@ -211,7 +211,7 @@ func Test_assessSingleTask(t *testing.T) {
args: args{
task: workv1alpha2.GracefulEvictionTask{
FromCluster: "member1",
SuppressDeletion: pointer.Bool(false),
SuppressDeletion: ptr.To[bool](false),
CreationTimestamp: &metav1.Time{Time: timeNow.Add(time.Minute * -1)},
},
opt: assessmentOption{
Expand Down Expand Up @@ -594,7 +594,7 @@ func Test_nextRetry(t *testing.T) {
{
FromCluster: "member1",
CreationTimestamp: &metav1.Time{Time: timeNow.Add(time.Minute * -60)},
SuppressDeletion: pointer.Bool(true),
SuppressDeletion: ptr.To[bool](true),
},
{
FromCluster: "member2",
Expand All @@ -613,12 +613,12 @@ func Test_nextRetry(t *testing.T) {
{
FromCluster: "member1",
CreationTimestamp: &metav1.Time{Time: timeNow.Add(time.Minute * -60)},
SuppressDeletion: pointer.Bool(true),
SuppressDeletion: ptr.To[bool](true),
},
{
FromCluster: "member2",
CreationTimestamp: &metav1.Time{Time: timeNow.Add(time.Minute * -5)},
SuppressDeletion: pointer.Bool(true),
SuppressDeletion: ptr.To[bool](true),
},
},
timeout: timeout,
Expand Down
14 changes: 7 additions & 7 deletions pkg/karmadactl/cmdinit/kubernetes/deployments.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/utils/pointer"
"k8s.io/utils/ptr"

"github.com/karmada-io/karmada/pkg/karmadactl/cmdinit/options"
globaloptions "github.com/karmada-io/karmada/pkg/karmadactl/options"
Expand Down Expand Up @@ -182,7 +182,7 @@ func (i *CommandInitOption) makeKarmadaAPIServerDeployment() *appsv1.Deployment
},
},
},
AutomountServiceAccountToken: pointer.Bool(false),
AutomountServiceAccountToken: ptr.To[bool](false),
Containers: []corev1.Container{
{
Name: karmadaAPIServerDeploymentAndServiceName,
Expand Down Expand Up @@ -296,7 +296,7 @@ func (i *CommandInitOption) makeKarmadaKubeControllerManagerDeployment() *appsv1
},
},
},
AutomountServiceAccountToken: pointer.Bool(false),
AutomountServiceAccountToken: ptr.To[bool](false),
Containers: []corev1.Container{
{
Name: kubeControllerManagerClusterRoleAndDeploymentAndServiceName,
Expand Down Expand Up @@ -483,7 +483,7 @@ func (i *CommandInitOption) makeKarmadaSchedulerDeployment() *appsv1.Deployment
Operator: corev1.TolerationOpExists,
},
},
AutomountServiceAccountToken: pointer.Bool(false),
AutomountServiceAccountToken: ptr.To[bool](false),
}

// PodTemplateSpec
Expand Down Expand Up @@ -604,7 +604,7 @@ func (i *CommandInitOption) makeKarmadaControllerManagerDeployment() *appsv1.Dep
Operator: corev1.TolerationOpExists,
},
},
AutomountServiceAccountToken: pointer.Bool(false),
AutomountServiceAccountToken: ptr.To[bool](false),
}

// PodTemplateSpec
Expand Down Expand Up @@ -674,7 +674,7 @@ func (i *CommandInitOption) makeKarmadaWebhookDeployment() *appsv1.Deployment {
},
},
},
AutomountServiceAccountToken: pointer.Bool(false),
AutomountServiceAccountToken: ptr.To[bool](false),
Containers: []corev1.Container{
{
Name: webhookDeploymentAndServiceAccountAndServiceName,
Expand Down Expand Up @@ -844,7 +844,7 @@ func (i *CommandInitOption) makeKarmadaAggregatedAPIServerDeployment() *appsv1.D
},
},
},
AutomountServiceAccountToken: pointer.Bool(false),
AutomountServiceAccountToken: ptr.To[bool](false),
Containers: []corev1.Container{
{
Name: karmadaAggregatedAPIServerDeploymentAndServiceName,
Expand Down
4 changes: 2 additions & 2 deletions pkg/karmadactl/cmdinit/kubernetes/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/component-base/cli/flag"
"k8s.io/utils/pointer"
"k8s.io/utils/ptr"

"github.com/karmada-io/karmada/pkg/karmadactl/cmdinit/options"
"github.com/karmada-io/karmada/pkg/karmadactl/cmdinit/utils"
Expand Down Expand Up @@ -255,7 +255,7 @@ func (i *CommandInitOption) makeETCDStatefulSet() *appsv1.StatefulSet {
},
},
},
AutomountServiceAccountToken: pointer.Bool(false),
AutomountServiceAccountToken: ptr.To[bool](false),
Containers: []corev1.Container{
{
Name: etcdStatefulSetAndServiceName,
Expand Down
4 changes: 2 additions & 2 deletions pkg/karmadactl/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import (
cmdutil "k8s.io/kubectl/pkg/cmd/util"
"k8s.io/kubectl/pkg/util/interrupt"
"k8s.io/kubectl/pkg/util/templates"
utilpointer "k8s.io/utils/pointer"
"k8s.io/utils/ptr"

karmadaclientset "github.com/karmada-io/karmada/pkg/generated/clientset/versioned"
"github.com/karmada-io/karmada/pkg/karmadactl/options"
Expand Down Expand Up @@ -627,7 +627,7 @@ func (g *CommandGetOptions) watch(watchObjs []WatchObj) error {

info := infos[0]
mapping := info.ResourceMapping()
outputObjects := utilpointer.Bool(!g.WatchOnly)
outputObjects := ptr.To[bool](!g.WatchOnly)

printer, err := g.ToPrinter(mapping, outputObjects, g.AllNamespaces, false)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/utils/pointer"
"k8s.io/utils/ptr"

configv1alpha1 "github.com/karmada-io/karmada/pkg/apis/config/v1alpha1"
workv1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2"
Expand Down Expand Up @@ -57,7 +57,7 @@ func TestGetReplicas(t *testing.T) {
Name: "bar",
},
Spec: appsv1.DeploymentSpec{
Replicas: pointer.Int32(1),
Replicas: ptr.To[int32](1),
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
NodeSelector: map[string]string{"foo": "foo1"},
Expand Down Expand Up @@ -108,7 +108,7 @@ end`,
Name: "bar",
},
Spec: appsv1.DeploymentSpec{
Replicas: pointer.Int32(1),
Replicas: ptr.To[int32](1),
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
NodeSelector: map[string]string{"foo": "foo1"},
Expand Down Expand Up @@ -641,7 +641,7 @@ func Test_decodeValue(t *testing.T) {
{
name: "int pointer",
args: args{
value: pointer.Int(1),
value: ptr.To[int](1),
},
want: lua.LNumber(1),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ package spreadconstraint
import (
"fmt"

"k8s.io/utils/ptr"

clusterv1alpha1 "github.com/karmada-io/karmada/pkg/apis/cluster/v1alpha1"
policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1"
"k8s.io/utils/pointer"
)

func selectBestClustersByRegion(spreadConstraintMap map[policyv1alpha1.SpreadFieldValue]policyv1alpha1.SpreadConstraint,
Expand Down Expand Up @@ -56,7 +57,7 @@ func selectBestClustersByRegion(spreadConstraintMap map[policyv1alpha1.SpreadFie
if restCnt > 0 {
sortClusters(candidateClusters, func(i *ClusterDetailInfo, j *ClusterDetailInfo) *bool {
if i.AvailableReplicas != j.AvailableReplicas {
return pointer.Bool(i.AvailableReplicas > j.AvailableReplicas)
return ptr.To[bool](i.AvailableReplicas > j.AvailableReplicas)
}
return nil
})
Expand Down
Loading

0 comments on commit b7b583a

Please sign in to comment.