Skip to content

Commit

Permalink
Replace original pointer methods with ptr libs (#635)
Browse files Browse the repository at this point in the history
Signed-off-by: Yuki Iwai <yuki.iwai.tz@gmail.com>
  • Loading branch information
tenzen-y committed Apr 17, 2024
1 parent a1453cf commit 4d5156d
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 158 deletions.
17 changes: 5 additions & 12 deletions pkg/apis/kubeflow/v2beta1/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package v2beta1

import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/utils/ptr"
)

func addDefaultingFuncs(scheme *runtime.Scheme) error {
Expand All @@ -31,7 +32,7 @@ func setDefaultsTypeLauncher(spec *ReplicaSpec) {
spec.RestartPolicy = DefaultLauncherRestartPolicy
}
if spec.Replicas == nil {
spec.Replicas = newInt32(1)
spec.Replicas = ptr.To[int32](1)
}
}

Expand All @@ -44,13 +45,13 @@ func setDefaultsTypeWorker(spec *ReplicaSpec) {
spec.RestartPolicy = DefaultRestartPolicy
}
if spec.Replicas == nil {
spec.Replicas = newInt32(0)
spec.Replicas = ptr.To[int32](0)
}
}

func setDefaultsRunPolicy(policy *RunPolicy) {
if policy.CleanPodPolicy == nil {
policy.CleanPodPolicy = NewCleanPodPolicy(CleanPodPolicyNone)
policy.CleanPodPolicy = ptr.To(CleanPodPolicyNone)
}
// The remaining fields are passed as-is to the k8s Job API, which does its
// own defaulting.
Expand All @@ -59,7 +60,7 @@ func setDefaultsRunPolicy(policy *RunPolicy) {
func SetDefaults_MPIJob(mpiJob *MPIJob) {
setDefaultsRunPolicy(&mpiJob.Spec.RunPolicy)
if mpiJob.Spec.SlotsPerWorker == nil {
mpiJob.Spec.SlotsPerWorker = newInt32(1)
mpiJob.Spec.SlotsPerWorker = ptr.To[int32](1)
}
if mpiJob.Spec.SSHAuthMountPath == "" {
mpiJob.Spec.SSHAuthMountPath = "/root/.ssh"
Expand All @@ -77,11 +78,3 @@ func SetDefaults_MPIJob(mpiJob *MPIJob) {
// set default to Worker
setDefaultsTypeWorker(mpiJob.Spec.MPIReplicaSpecs[MPIReplicaTypeWorker])
}

func newInt32(v int32) *int32 {
return &v
}

func NewCleanPodPolicy(policy CleanPodPolicy) *CleanPodPolicy {
return &policy
}
61 changes: 29 additions & 32 deletions pkg/apis/kubeflow/v2beta1/default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
"k8s.io/utils/ptr"
)

func TestSetDefaults_MPIJob(t *testing.T) {
Expand All @@ -28,9 +29,9 @@ func TestSetDefaults_MPIJob(t *testing.T) {
"base defaults": {
want: MPIJob{
Spec: MPIJobSpec{
SlotsPerWorker: newInt32(1),
SlotsPerWorker: ptr.To[int32](1),
RunPolicy: RunPolicy{
CleanPodPolicy: NewCleanPodPolicy(CleanPodPolicyNone),
CleanPodPolicy: ptr.To(CleanPodPolicyNone),
},
SSHAuthMountPath: "/root/.ssh",
MPIImplementation: MPIImplementationOpenMPI,
Expand All @@ -41,12 +42,12 @@ func TestSetDefaults_MPIJob(t *testing.T) {
"base defaults overridden (intel)": {
job: MPIJob{
Spec: MPIJobSpec{
SlotsPerWorker: newInt32(10),
SlotsPerWorker: ptr.To[int32](10),
RunPolicy: RunPolicy{
CleanPodPolicy: NewCleanPodPolicy(CleanPodPolicyRunning),
TTLSecondsAfterFinished: newInt32(2),
ActiveDeadlineSeconds: newInt64(3),
BackoffLimit: newInt32(4),
CleanPodPolicy: ptr.To(CleanPodPolicyRunning),
TTLSecondsAfterFinished: ptr.To[int32](2),
ActiveDeadlineSeconds: ptr.To[int64](3),
BackoffLimit: ptr.To[int32](4),
},
SSHAuthMountPath: "/home/mpiuser/.ssh",
MPIImplementation: MPIImplementationIntel,
Expand All @@ -55,12 +56,12 @@ func TestSetDefaults_MPIJob(t *testing.T) {
},
want: MPIJob{
Spec: MPIJobSpec{
SlotsPerWorker: newInt32(10),
SlotsPerWorker: ptr.To[int32](10),
RunPolicy: RunPolicy{
CleanPodPolicy: NewCleanPodPolicy(CleanPodPolicyRunning),
TTLSecondsAfterFinished: newInt32(2),
ActiveDeadlineSeconds: newInt64(3),
BackoffLimit: newInt32(4),
CleanPodPolicy: ptr.To(CleanPodPolicyRunning),
TTLSecondsAfterFinished: ptr.To[int32](2),
ActiveDeadlineSeconds: ptr.To[int64](3),
BackoffLimit: ptr.To[int32](4),
},
SSHAuthMountPath: "/home/mpiuser/.ssh",
MPIImplementation: MPIImplementationIntel,
Expand All @@ -71,12 +72,12 @@ func TestSetDefaults_MPIJob(t *testing.T) {
"base defaults overridden (mpich)": {
job: MPIJob{
Spec: MPIJobSpec{
SlotsPerWorker: newInt32(10),
SlotsPerWorker: ptr.To[int32](10),
RunPolicy: RunPolicy{
CleanPodPolicy: NewCleanPodPolicy(CleanPodPolicyRunning),
TTLSecondsAfterFinished: newInt32(2),
ActiveDeadlineSeconds: newInt64(3),
BackoffLimit: newInt32(4),
CleanPodPolicy: ptr.To(CleanPodPolicyRunning),
TTLSecondsAfterFinished: ptr.To[int32](2),
ActiveDeadlineSeconds: ptr.To[int64](3),
BackoffLimit: ptr.To[int32](4),
},
SSHAuthMountPath: "/home/mpiuser/.ssh",
MPIImplementation: MPIImplementationMPICH,
Expand All @@ -85,12 +86,12 @@ func TestSetDefaults_MPIJob(t *testing.T) {
},
want: MPIJob{
Spec: MPIJobSpec{
SlotsPerWorker: newInt32(10),
SlotsPerWorker: ptr.To[int32](10),
RunPolicy: RunPolicy{
CleanPodPolicy: NewCleanPodPolicy(CleanPodPolicyRunning),
TTLSecondsAfterFinished: newInt32(2),
ActiveDeadlineSeconds: newInt64(3),
BackoffLimit: newInt32(4),
CleanPodPolicy: ptr.To(CleanPodPolicyRunning),
TTLSecondsAfterFinished: ptr.To[int32](2),
ActiveDeadlineSeconds: ptr.To[int64](3),
BackoffLimit: ptr.To[int32](4),
},
SSHAuthMountPath: "/home/mpiuser/.ssh",
MPIImplementation: MPIImplementationMPICH,
Expand All @@ -108,16 +109,16 @@ func TestSetDefaults_MPIJob(t *testing.T) {
},
want: MPIJob{
Spec: MPIJobSpec{
SlotsPerWorker: newInt32(1),
SlotsPerWorker: ptr.To[int32](1),
RunPolicy: RunPolicy{
CleanPodPolicy: NewCleanPodPolicy(CleanPodPolicyNone),
CleanPodPolicy: ptr.To(CleanPodPolicyNone),
},
SSHAuthMountPath: "/root/.ssh",
MPIImplementation: MPIImplementationOpenMPI,
LauncherCreationPolicy: "AtStartup",
MPIReplicaSpecs: map[MPIReplicaType]*ReplicaSpec{
MPIReplicaTypeLauncher: {
Replicas: newInt32(1),
Replicas: ptr.To[int32](1),
RestartPolicy: DefaultLauncherRestartPolicy,
},
},
Expand All @@ -134,16 +135,16 @@ func TestSetDefaults_MPIJob(t *testing.T) {
},
want: MPIJob{
Spec: MPIJobSpec{
SlotsPerWorker: newInt32(1),
SlotsPerWorker: ptr.To[int32](1),
RunPolicy: RunPolicy{
CleanPodPolicy: NewCleanPodPolicy(CleanPodPolicyNone),
CleanPodPolicy: ptr.To(CleanPodPolicyNone),
},
SSHAuthMountPath: "/root/.ssh",
MPIImplementation: MPIImplementationOpenMPI,
LauncherCreationPolicy: "AtStartup",
MPIReplicaSpecs: map[MPIReplicaType]*ReplicaSpec{
MPIReplicaTypeWorker: {
Replicas: newInt32(0),
Replicas: ptr.To[int32](0),
RestartPolicy: DefaultRestartPolicy,
},
},
Expand All @@ -161,7 +162,3 @@ func TestSetDefaults_MPIJob(t *testing.T) {
})
}
}

func newInt64(v int64) *int64 {
return &v
}
Loading

0 comments on commit 4d5156d

Please sign in to comment.