Skip to content

Commit 1cd5062

Browse files
committed
generate: Adjust to Spec.Linux being a pointer
Catch up with opencontainers/runtime-spec@6323157 (specs-go/config: Make Linux and Solaris omitempty (again), 2016-06-17, #502). The "does the marshaled JSON look like '{}'?" check is a pretty cheap trick, but it was the easiest way I could think of for "is there anything useful in here?". An alternative approach that conditionally added an &rspec.Linux{} only if needed seemed too tedious. Signed-off-by: W. Trevor King <[email protected]>
1 parent e05b847 commit 1cd5062

1 file changed

Lines changed: 72 additions & 8 deletions

File tree

generate/generate.go

Lines changed: 72 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func New() Generator {
110110
Options: []string{"nosuid", "noexec", "nodev", "ro"},
111111
},
112112
},
113-
Linux: rspec.Linux{
113+
Linux: &rspec.Linux{
114114
Resources: &rspec.Resources{
115115
Devices: []rspec.DeviceCgroup{
116116
{
@@ -181,6 +181,16 @@ func (g Generator) GetSpec() *rspec.Spec {
181181

182182
// Save writes the spec into w.
183183
func (g Generator) Save(w io.Writer) error {
184+
if g.spec.Linux != nil {
185+
buf, err := json.Marshal(spec.Linux)
186+
if err != nil {
187+
return err
188+
}
189+
if string(buf) == "{}" {
190+
spec.Linux = nil
191+
}
192+
}
193+
184194
data, err := json.MarshalIndent(g.spec, "", "\t")
185195
if err != nil {
186196
return err
@@ -334,21 +344,33 @@ func (g Generator) SetProcessSelinuxLabel(label string) {
334344

335345
// SetLinuxCgroupsPath sets g.spec.Linux.CgroupsPath.
336346
func (g Generator) SetLinuxCgroupsPath(path string) {
347+
if g.spec.Linux == nil {
348+
g.spec.Linux = &rspec.Linux{}
349+
}
337350
g.spec.Linux.CgroupsPath = strPtr(path)
338351
}
339352

340353
// SetLinuxMountLabel sets g.spec.Linux.MountLabel.
341354
func (g Generator) SetLinuxMountLabel(label string) {
355+
if g.spec.Linux == nil {
356+
g.spec.Linux = &rspec.Linux{}
357+
}
342358
g.spec.Linux.MountLabel = label
343359
}
344360

345361
// ClearLinuxSysctl clears g.spec.Linux.Sysctl.
346362
func (g Generator) ClearLinuxSysctl() {
363+
if g.spec.Linux == nil {
364+
g.spec.Linux = &rspec.Linux{}
365+
}
347366
g.spec.Linux.Sysctl = make(map[string]string)
348367
}
349368

350369
// AddLinuxSysctl adds a new sysctl config into g.spec.Linux.Sysctl.
351370
func (g Generator) AddLinuxSysctl(s string) error {
371+
if g.spec.Linux == nil {
372+
g.spec.Linux = &rspec.Linux{}
373+
}
352374
if g.spec.Linux.Sysctl == nil {
353375
g.spec.Linux.Sysctl = make(map[string]string)
354376
}
@@ -363,7 +385,7 @@ func (g Generator) AddLinuxSysctl(s string) error {
363385

364386
// RemoveLinuxSysctl removes a sysctl config from g.spec.Linux.Sysctl.
365387
func (g Generator) RemoveLinuxSysctl(key string) {
366-
if g.spec.Linux.Sysctl == nil {
388+
if g.spec.Linux == nil || g.spec.Linux.Sysctl == nil {
367389
return
368390
}
369391
delete(g.spec.Linux.Sysctl, key)
@@ -384,6 +406,9 @@ func (g Generator) SetLinuxSeccompDefault(sdefault string) error {
384406
"SCMP_ACT_ALLOW")
385407
}
386408

409+
if g.spec.Linux == nil {
410+
g.spec.Linux = &rspec.Linux{}
411+
}
387412
if g.spec.Linux.Seccomp == nil {
388413
g.spec.Linux.Seccomp = &rspec.Seccomp{}
389414
}
@@ -418,7 +443,7 @@ func checkSeccompArch(arch string) error {
418443

419444
// ClearLinuxSeccompArch clears g.spec.Linux.Seccomp.Architectures.
420445
func (g Generator) ClearLinuxSeccompArch() {
421-
if g.spec.Linux.Seccomp == nil {
446+
if g.spec.Linux == nil || g.spec.Linux.Seccomp == nil {
422447
return
423448
}
424449

@@ -431,6 +456,9 @@ func (g Generator) AddLinuxSeccompArch(sArch string) error {
431456
return err
432457
}
433458

459+
if g.spec.Linux == nil {
460+
g.spec.Linux = &rspec.Linux{}
461+
}
434462
if g.spec.Linux.Seccomp == nil {
435463
g.spec.Linux.Seccomp = &rspec.Seccomp{}
436464
}
@@ -446,7 +474,7 @@ func (g Generator) RemoveSeccompArch(sArch string) error {
446474
return err
447475
}
448476

449-
if g.spec.Linux.Seccomp == nil {
477+
if g.spec.Linux == nil || g.spec.Linux.Seccomp == nil {
450478
return nil
451479
}
452480

@@ -546,7 +574,7 @@ func parseSeccompSyscall(s string) (rspec.Syscall, error) {
546574

547575
// ClearLinuxSeccompSyscall clears g.spec.Linux.Seccomp.Syscalls.
548576
func (g Generator) ClearLinuxSeccompSyscall() {
549-
if g.spec.Linux.Seccomp == nil {
577+
if g.spec.Linux == nil || g.spec.Linux.Seccomp == nil {
550578
return
551579
}
552580

@@ -560,6 +588,9 @@ func (g Generator) AddLinuxSeccompSyscall(sSyscall string) error {
560588
return err
561589
}
562590

591+
if g.spec.Linux == nil {
592+
g.spec.Linux = &rspec.Linux{}
593+
}
563594
if g.spec.Linux.Seccomp == nil {
564595
g.spec.Linux.Seccomp = &rspec.Seccomp{}
565596
}
@@ -570,6 +601,9 @@ func (g Generator) AddLinuxSeccompSyscall(sSyscall string) error {
570601

571602
// AddLinuxSeccompSyscallAllow adds seccompAllow into g.spec.Linux.Seccomp.Syscalls.
572603
func (g Generator) AddLinuxSeccompSyscallAllow(seccompAllow string) {
604+
if g.spec.Linux == nil {
605+
g.spec.Linux = &rspec.Linux{}
606+
}
573607
if g.spec.Linux.Seccomp == nil {
574608
g.spec.Linux.Seccomp = &rspec.Seccomp{}
575609
}
@@ -583,6 +617,9 @@ func (g Generator) AddLinuxSeccompSyscallAllow(seccompAllow string) {
583617

584618
// AddLinuxSeccompSyscallErrno adds seccompErrno into g.spec.Linux.Seccomp.Syscalls.
585619
func (g Generator) AddLinuxSeccompSyscallErrno(seccompErrno string) {
620+
if g.spec.Linux == nil {
621+
g.spec.Linux = &rspec.Linux{}
622+
}
586623
if g.spec.Linux.Seccomp == nil {
587624
g.spec.Linux.Seccomp = &rspec.Seccomp{}
588625
}
@@ -597,7 +634,7 @@ func (g Generator) AddLinuxSeccompSyscallErrno(seccompErrno string) {
597634
// RemoveSeccompSyscallByName removes all the seccomp syscalls with the given
598635
// name from g.spec.Linux.Seccomp.Syscalls.
599636
func (g Generator) RemoveSeccompSyscallByName(name string) error {
600-
if g.spec.Linux.Seccomp == nil {
637+
if g.spec.Linux == nil || g.spec.Linux.Seccomp == nil {
601638
return nil
602639
}
603640

@@ -614,7 +651,7 @@ func (g Generator) RemoveSeccompSyscallByName(name string) error {
614651
// RemoveSeccompSyscallByAction removes all the seccomp syscalls with the given
615652
// action from g.spec.Linux.Seccomp.Syscalls.
616653
func (g Generator) RemoveSeccompSyscallByAction(action string) error {
617-
if g.spec.Linux.Seccomp == nil {
654+
if g.spec.Linux == nil || g.spec.Linux.Seccomp == nil {
618655
return nil
619656
}
620657

@@ -635,7 +672,7 @@ func (g Generator) RemoveSeccompSyscallByAction(action string) error {
635672
// RemoveSeccompSyscall removes all the seccomp syscalls with the given
636673
// name and action from g.spec.Linux.Seccomp.Syscalls.
637674
func (g Generator) RemoveSeccompSyscall(name string, action string) error {
638-
if g.spec.Linux.Seccomp == nil {
675+
if g.spec.Linux == nil || g.spec.Linux.Seccomp == nil {
639676
return nil
640677
}
641678

@@ -685,6 +722,9 @@ func parseIDMapping(idms string) (rspec.IDMapping, error) {
685722

686723
// ClearLinuxUIDMappings clear g.spec.Linux.UIDMappings.
687724
func (g Generator) ClearLinuxUIDMappings() {
725+
if g.spec.Linux == nil {
726+
return
727+
}
688728
g.spec.Linux.UIDMappings = []rspec.IDMapping{}
689729
}
690730

@@ -695,12 +735,18 @@ func (g Generator) AddLinuxUIDMapping(uidMap string) error {
695735
return err
696736
}
697737

738+
if g.spec.Linux == nil {
739+
g.spec.Linux = &rspec.Linux{}
740+
}
698741
g.spec.Linux.UIDMappings = append(g.spec.Linux.UIDMappings, r)
699742
return nil
700743
}
701744

702745
// ClearLinuxGIDMappings clear g.spec.Linux.GIDMappings.
703746
func (g Generator) ClearLinuxGIDMappings() {
747+
if g.spec.Linux == nil {
748+
return
749+
}
704750
g.spec.Linux.GIDMappings = []rspec.IDMapping{}
705751
}
706752

@@ -711,6 +757,9 @@ func (g Generator) AddLinuxGIDMapping(gidMap string) error {
711757
return err
712758
}
713759

760+
if g.spec.Linux == nil {
761+
g.spec.Linux = &rspec.Linux{}
762+
}
714763
g.spec.Linux.GIDMappings = append(g.spec.Linux.GIDMappings, r)
715764
return nil
716765
}
@@ -728,6 +777,9 @@ func (g Generator) SetLinuxRootPropagation(rp string) error {
728777
default:
729778
return fmt.Errorf("rootfs-propagation must be empty or one of private|rprivate|slave|rslave|shared|rshared")
730779
}
780+
if g.spec.Linux == nil {
781+
g.spec.Linux = &rspec.Linux{}
782+
}
731783
g.spec.Linux.RootfsPropagation = rp
732784
return nil
733785
}
@@ -849,6 +901,9 @@ func (g Generator) SetupPrivileged(privileged bool) {
849901
g.spec.Process.Capabilities = finalCapList
850902
g.spec.Process.SelinuxLabel = ""
851903
g.spec.Process.ApparmorProfile = ""
904+
if g.spec.Linux == nil {
905+
g.spec.Linux = &rspec.Linux{}
906+
}
852907
g.spec.Linux.Seccomp = nil
853908
}
854909
}
@@ -934,6 +989,9 @@ func mapStrToNamespace(ns string, path string) (rspec.Namespace, error) {
934989

935990
// ClearLinuxNamespaces clear g.spec.Linux.Namespaces.
936991
func (g Generator) ClearLinuxNamespaces() {
992+
if g.spec.Linux == nil {
993+
return
994+
}
937995
g.spec.Linux.Namespaces = []rspec.Namespace{}
938996
}
939997

@@ -945,6 +1003,9 @@ func (g Generator) AddOrReplaceLinuxNamespace(ns string, path string) error {
9451003
return err
9461004
}
9471005

1006+
if g.spec.Linux == nil {
1007+
g.spec.Linux = &rspec.Linux{}
1008+
}
9481009
for i, ns := range g.spec.Linux.Namespaces {
9491010
if ns.Type == namespace.Type {
9501011
g.spec.Linux.Namespaces[i] = namespace
@@ -962,6 +1023,9 @@ func (g Generator) RemoveLinuxNamespace(ns string) error {
9621023
return err
9631024
}
9641025

1026+
if g.spec.Linux == nil {
1027+
return nil
1028+
}
9651029
for i, ns := range g.spec.Linux.Namespaces {
9661030
if ns.Type == namespace.Type {
9671031
g.spec.Linux.Namespaces = append(g.spec.Linux.Namespaces[:i], g.spec.Linux.Namespaces[i+1:]...)

0 commit comments

Comments
 (0)