Skip to content

Commit 1b824d5

Browse files
committed
feat: allow configuring workload identities for sidecar_task
1 parent 2d63abd commit 1b824d5

File tree

8 files changed

+55
-0
lines changed

8 files changed

+55
-0
lines changed

.changelog/25877.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:improvement
2+
connect: allow configuring identities for sidecar_task
3+
```

api/consul.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ type SidecarTask struct {
116116
ShutdownDelay *time.Duration `mapstructure:"shutdown_delay" hcl:"shutdown_delay,optional"`
117117
KillSignal string `mapstructure:"kill_signal" hcl:"kill_signal,optional"`
118118
VolumeMounts []*VolumeMount `hcl:"volume_mount,block"`
119+
Identities []*WorkloadIdentity `hcl:"identity,block"`
119120
}
120121

121122
func (st *SidecarTask) Canonicalize() {

command/agent/job_endpoint.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2117,6 +2117,19 @@ func apiConnectSidecarTaskToStructs(in *api.SidecarTask) *structs.SidecarTask {
21172117
return nil
21182118
}
21192119

2120+
var identities []*structs.WorkloadIdentity
2121+
2122+
if ids := in.Identities; len(ids) > 0 {
2123+
identities = make([]*structs.WorkloadIdentity, 0, len(ids))
2124+
for _, id := range ids {
2125+
if id == nil {
2126+
continue
2127+
}
2128+
2129+
identities = append(identities, apiWorkloadIdentityToStructs(id))
2130+
}
2131+
}
2132+
21202133
return &structs.SidecarTask{
21212134
Name: in.Name,
21222135
Driver: in.Driver,
@@ -2130,6 +2143,7 @@ func apiConnectSidecarTaskToStructs(in *api.SidecarTask) *structs.SidecarTask {
21302143
KillTimeout: in.KillTimeout,
21312144
LogConfig: apiLogConfigToStructs(in.LogConfig),
21322145
VolumeMounts: apiVolumeMountsToStructs(in.VolumeMounts),
2146+
Identities: identities,
21332147
}
21342148
}
21352149

nomad/structs/diff.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1703,6 +1703,11 @@ func sidecarTaskDiff(old, new *SidecarTask, contextual bool) *ObjectDiff {
17031703
diff.Objects = append(diff.Objects, rDiff)
17041704
}
17051705

1706+
// identities diff
1707+
if idDiffs := idSliceDiffs(old.Identities, new.Identities, contextual); idDiffs != nil {
1708+
diff.Objects = append(diff.Objects, idDiffs...)
1709+
}
1710+
17061711
// LogConfig diff
17071712
lDiff := primitiveObjectDiff(old.LogConfig, new.LogConfig, nil, "LogConfig", contextual)
17081713
if lDiff != nil {

nomad/structs/services.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,6 +1433,9 @@ type SidecarTask struct {
14331433
// VolumeMounts is a list of Volume name <-> mount configurations that will be
14341434
// attached to this task.
14351435
VolumeMounts []*VolumeMount
1436+
1437+
// Identities is a list of Workload Identies to attach to this task
1438+
Identities []*WorkloadIdentity
14361439
}
14371440

14381441
func (t *SidecarTask) Equal(o *SidecarTask) bool {
@@ -1490,6 +1493,11 @@ func (t *SidecarTask) Equal(o *SidecarTask) bool {
14901493
return false
14911494
}
14921495

1496+
if !slices.EqualFunc(t.Identities, o.Identities,
1497+
func(tID, oID *WorkloadIdentity) bool { return tID.Equal(oID) }) {
1498+
return false
1499+
}
1500+
14931501
return true
14941502
}
14951503

@@ -1521,6 +1529,8 @@ func (t *SidecarTask) Copy() *SidecarTask {
15211529

15221530
nt.VolumeMounts = CopySliceVolumeMount(t.VolumeMounts)
15231531

1532+
nt.Identities = CopySliceWorkloadIdentity(t.Identities)
1533+
15241534
return nt
15251535
}
15261536

@@ -1597,6 +1607,10 @@ func (t *SidecarTask) MergeIntoTask(task *Task) {
15971607
if t.VolumeMounts != nil {
15981608
task.VolumeMounts = t.VolumeMounts
15991609
}
1610+
1611+
if t.Identities != nil {
1612+
task.Identities = t.Identities
1613+
}
16001614
}
16011615

16021616
// ConsulProxy represents a Consul Connect sidecar proxy jobspec block.

nomad/structs/workload_id.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,3 +573,16 @@ func (w *WIHandle) Equal(o WIHandle) bool {
573573
w.WorkloadIdentifier == o.WorkloadIdentifier &&
574574
w.WorkloadType == o.WorkloadType
575575
}
576+
577+
func CopySliceWorkloadIdentity(s []*WorkloadIdentity) []*WorkloadIdentity {
578+
l := len(s)
579+
if l == 0 {
580+
return nil
581+
}
582+
583+
c := make([]*WorkloadIdentity, l)
584+
for i, v := range s {
585+
c[i] = v.Copy()
586+
}
587+
return c
588+
}

website/content/docs/job-specification/identity.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ description: |-
1212
['job', 'group', 'service', 'identity'],
1313
['job', 'group', 'task', 'identity'],
1414
['job', 'group', 'task', 'service', 'identity'],
15+
['job', 'group', 'service', 'connect', 'sidecar_task', 'identity'],
1516
]}
1617
/>
1718

website/content/docs/job-specification/sidecar_task.mdx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ meta.connect.sidecar_image = custom/envoy-${NOMAD_envoy_version}:latest
156156
- `volume_mount` <code>([VolumeMount][]: nil)</code> - Specifies where a group
157157
volume should be mounted.
158158

159+
- `identity` <code>([Identity][]: nil)</code> - Expose [Workload Identity][] to
160+
the task.
161+
159162
## Examples
160163

161164
The following example configures resources for the sidecar task and other configuration.
@@ -181,6 +184,7 @@ The following example configures resources for the sidecar task and other config
181184
[group]: /nomad/docs/job-specification/group 'Nomad group Job Specification'
182185
[interpolation]: /nomad/docs/runtime/interpolation 'Nomad interpolation'
183186
[job]: /nomad/docs/job-specification/job 'Nomad job Job Specification'
187+
[Identity]: /nomad/docs/job-specification/identity 'Nomad identity Job Specification'
184188
[logs]: /nomad/docs/job-specification/logs 'Nomad logs Job Specification'
185189
[resources]: /nomad/docs/job-specification/resources 'Nomad resources Job Specification'
186190
[sidecar_service]: /nomad/docs/job-specification/sidecar_service 'Nomad sidecar service Specification'

0 commit comments

Comments
 (0)