Skip to content

Commit

Permalink
slices of pointers should not be preallocated if value can be nil
Browse files Browse the repository at this point in the history
  • Loading branch information
tgross committed Jul 6, 2021
1 parent 12dca86 commit 96a1580
Showing 1 changed file with 67 additions and 65 deletions.
132 changes: 67 additions & 65 deletions command/agent/job_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -810,10 +810,10 @@ func ApiJobToStructJob(job *api.Job) *structs.Job {
}
}

if l := len(job.Spreads); l != 0 {
j.Spreads = make([]*structs.Spread, l)
for i, apiSpread := range job.Spreads {
j.Spreads[i] = ApiSpreadToStructs(apiSpread)
if len(job.Spreads) > 0 {
j.Spreads = []*structs.Spread{}
for _, apiSpread := range job.Spreads {
j.Spreads = append(j.Spreads, ApiSpreadToStructs(apiSpread))
}
}

Expand Down Expand Up @@ -855,12 +855,12 @@ func ApiJobToStructJob(job *api.Job) *structs.Job {
}
}

if l := len(job.TaskGroups); l != 0 {
j.TaskGroups = make([]*structs.TaskGroup, l)
for i, taskGroup := range job.TaskGroups {
if len(job.TaskGroups) > 0 {
j.TaskGroups = []*structs.TaskGroup{}
for _, taskGroup := range job.TaskGroups {
tg := &structs.TaskGroup{}
ApiTgToStructsTG(j, taskGroup, tg)
j.TaskGroups[i] = tg
j.TaskGroups = append(j.TaskGroups, tg)
}
}

Expand Down Expand Up @@ -922,15 +922,15 @@ func ApiTgToStructsTG(job *structs.Job, taskGroup *api.TaskGroup, tg *structs.Ta
Migrate: *taskGroup.EphemeralDisk.Migrate,
}

if l := len(taskGroup.Spreads); l != 0 {
tg.Spreads = make([]*structs.Spread, l)
for k, spread := range taskGroup.Spreads {
tg.Spreads[k] = ApiSpreadToStructs(spread)
if len(taskGroup.Spreads) > 0 {
tg.Spreads = []*structs.Spread{}
for _, spread := range taskGroup.Spreads {
tg.Spreads = append(tg.Spreads, ApiSpreadToStructs(spread))
}
}

if l := len(taskGroup.Volumes); l != 0 {
tg.Volumes = make(map[string]*structs.VolumeRequest, l)
if len(taskGroup.Volumes) > 0 {
tg.Volumes = map[string]*structs.VolumeRequest{}
for k, v := range taskGroup.Volumes {
if v == nil || (v.Type != structs.VolumeTypeHost && v.Type != structs.VolumeTypeCSI) {
// Ignore volumes we don't understand in this iteration currently.
Expand Down Expand Up @@ -980,9 +980,9 @@ func ApiTgToStructsTG(job *structs.Job, taskGroup *api.TaskGroup, tg *structs.Ta
}
}

if l := len(taskGroup.Tasks); l != 0 {
tg.Tasks = make([]*structs.Task, l)
for l, task := range taskGroup.Tasks {
if len(taskGroup.Tasks) > 0 {
tg.Tasks = []*structs.Task{}
for _, task := range taskGroup.Tasks {
t := &structs.Task{}
ApiTaskToStructsTask(job, tg, task, t)

Expand All @@ -991,7 +991,7 @@ func ApiTgToStructsTG(job *structs.Job, taskGroup *api.TaskGroup, tg *structs.Ta
if t.Vault != nil && t.Vault.Namespace == "" && job.VaultNamespace != "" {
t.Vault.Namespace = job.VaultNamespace
}
tg.Tasks[l] = t
tg.Tasks = append(tg.Tasks, t)
}
}
}
Expand Down Expand Up @@ -1025,25 +1025,25 @@ func ApiTaskToStructsTask(job *structs.Job, group *structs.TaskGroup,
}
}

if l := len(apiTask.VolumeMounts); l != 0 {
structsTask.VolumeMounts = make([]*structs.VolumeMount, l)
for _, mount := range apiTask.VolumeMounts {
if mount != nil {
structsTask.VolumeMounts = append(structsTask.VolumeMounts,
&structs.VolumeMount{
Volume: *mount.Volume,
Destination: *mount.Destination,
ReadOnly: *mount.ReadOnly,
PropagationMode: *mount.PropagationMode,
})
}
structsTask.VolumeMounts = []*structs.VolumeMount{}
for _, mount := range apiTask.VolumeMounts {
if mount != nil && mount.Volume != nil {
structsTask.VolumeMounts = append(structsTask.VolumeMounts,
&structs.VolumeMount{
Volume: *mount.Volume,
Destination: *mount.Destination,
ReadOnly: *mount.ReadOnly,
PropagationMode: *mount.PropagationMode,
})
}
}

if l := len(apiTask.ScalingPolicies); l != 0 {
structsTask.ScalingPolicies = make([]*structs.ScalingPolicy, l)
for i, policy := range apiTask.ScalingPolicies {
structsTask.ScalingPolicies[i] = ApiScalingPolicyToStructs(0, policy).TargetTask(job, group, structsTask)
if len(apiTask.ScalingPolicies) > 0 {
structsTask.ScalingPolicies = []*structs.ScalingPolicy{}
for _, policy := range apiTask.ScalingPolicies {
structsTask.ScalingPolicies = append(
structsTask.ScalingPolicies,
ApiScalingPolicyToStructs(0, policy).TargetTask(job, group, structsTask))
}
}

Expand All @@ -1056,16 +1056,17 @@ func ApiTaskToStructsTask(job *structs.Job, group *structs.TaskGroup,
MaxFileSizeMB: *apiTask.LogConfig.MaxFileSizeMB,
}

if l := len(apiTask.Artifacts); l != 0 {
structsTask.Artifacts = make([]*structs.TaskArtifact, l)
for k, ta := range apiTask.Artifacts {
structsTask.Artifacts[k] = &structs.TaskArtifact{
GetterSource: *ta.GetterSource,
GetterOptions: helper.CopyMapStringString(ta.GetterOptions),
GetterHeaders: helper.CopyMapStringString(ta.GetterHeaders),
GetterMode: *ta.GetterMode,
RelativeDest: *ta.RelativeDest,
}
if len(apiTask.Artifacts) > 0 {
structsTask.Artifacts = []*structs.TaskArtifact{}
for _, ta := range apiTask.Artifacts {
structsTask.Artifacts = append(structsTask.Artifacts,
&structs.TaskArtifact{
GetterSource: *ta.GetterSource,
GetterOptions: helper.CopyMapStringString(ta.GetterOptions),
GetterHeaders: helper.CopyMapStringString(ta.GetterHeaders),
GetterMode: *ta.GetterMode,
RelativeDest: *ta.RelativeDest,
})
}
}

Expand All @@ -1079,22 +1080,23 @@ func ApiTaskToStructsTask(job *structs.Job, group *structs.TaskGroup,
}
}

if l := len(apiTask.Templates); l != 0 {
structsTask.Templates = make([]*structs.Template, l)
for i, template := range apiTask.Templates {
structsTask.Templates[i] = &structs.Template{
SourcePath: *template.SourcePath,
DestPath: *template.DestPath,
EmbeddedTmpl: *template.EmbeddedTmpl,
ChangeMode: *template.ChangeMode,
ChangeSignal: *template.ChangeSignal,
Splay: *template.Splay,
Perms: *template.Perms,
LeftDelim: *template.LeftDelim,
RightDelim: *template.RightDelim,
Envvars: *template.Envvars,
VaultGrace: *template.VaultGrace,
}
if len(apiTask.Templates) > 0 {
structsTask.Templates = []*structs.Template{}
for _, template := range apiTask.Templates {
structsTask.Templates = append(structsTask.Templates,
&structs.Template{
SourcePath: *template.SourcePath,
DestPath: *template.DestPath,
EmbeddedTmpl: *template.EmbeddedTmpl,
ChangeMode: *template.ChangeMode,
ChangeSignal: *template.ChangeSignal,
Splay: *template.Splay,
Perms: *template.Perms,
LeftDelim: *template.LeftDelim,
RightDelim: *template.RightDelim,
Envvars: *template.Envvars,
VaultGrace: *template.VaultGrace,
})
}
}

Expand Down Expand Up @@ -1151,15 +1153,15 @@ func ApiResourcesToStructs(in *api.Resources) *structs.Resources {
out.Networks = ApiNetworkResourceToStructs(in.Networks)
}

if l := len(in.Devices); l != 0 {
out.Devices = make([]*structs.RequestedDevice, l)
for i, d := range in.Devices {
out.Devices[i] = &structs.RequestedDevice{
if len(in.Devices) > 0 {
out.Devices = []*structs.RequestedDevice{}
for _, d := range in.Devices {
out.Devices = append(out.Devices, &structs.RequestedDevice{
Name: d.Name,
Count: *d.Count,
Constraints: ApiConstraintsToStructs(d.Constraints),
Affinities: ApiAffinitiesToStructs(d.Affinities),
}
})
}
}

Expand Down

0 comments on commit 96a1580

Please sign in to comment.