File: capability.go

package info (click to toggle)
docker.io 20.10.24%2Bdfsg1-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 60,824 kB
  • sloc: sh: 5,621; makefile: 593; ansic: 179; python: 162; asm: 7
file content (106 lines) | stat: -rw-r--r-- 2,349 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package flagparser

import (
	"github.com/docker/swarmkit/api"
	"github.com/spf13/cobra"
)

// ParseAddCapability validates capabilities passed on the command line
func ParseAddCapability(cmd *cobra.Command, spec *api.ServiceSpec, flagName string) error {
	flags := cmd.Flags()

	if !flags.Changed(flagName) {
		return nil
	}

	add, err := flags.GetStringSlice(flagName)
	if err != nil {
		return err
	}

	container := spec.Task.GetContainer()
	if container == nil {
		return nil
	}

	// Index adds so we don't have to double loop
	addIndex := make(map[string]bool, len(add))
	for _, v := range add {
		addIndex[v] = true
	}

	// Check if any of the adds are in drop so we can remove them from the drop list.
	var evict []int
	for i, v := range container.CapabilityDrop {
		if addIndex[v] {
			evict = append(evict, i)
		}
	}
	for n, i := range evict {
		container.CapabilityDrop = append(container.CapabilityDrop[:i-n], container.CapabilityDrop[i-n+1:]...)
	}

	// De-dup the list to be added
	for _, v := range container.CapabilityAdd {
		if addIndex[v] {
			delete(addIndex, v)
			continue
		}
	}

	for cap := range addIndex {
		container.CapabilityAdd = append(container.CapabilityAdd, cap)
	}

	return nil
}

// ParseDropCapability validates capabilities passed on the command line
func ParseDropCapability(cmd *cobra.Command, spec *api.ServiceSpec, flagName string) error {
	flags := cmd.Flags()

	if !flags.Changed(flagName) {
		return nil
	}

	drop, err := flags.GetStringSlice(flagName)
	if err != nil {
		return err
	}

	container := spec.Task.GetContainer()
	if container == nil {
		return nil
	}

	// Index removals so we don't have to double loop
	dropIndex := make(map[string]bool, len(drop))
	for _, v := range drop {
		dropIndex[v] = true
	}

	// Check if any of the adds are in add so we can remove them from the add list.
	var evict []int
	for i, v := range container.CapabilityAdd {
		if dropIndex[v] {
			evict = append(evict, i)
		}
	}
	for n, i := range evict {
		container.CapabilityAdd = append(container.CapabilityAdd[:i-n], container.CapabilityAdd[i-n+1:]...)
	}

	// De-dup the list to be dropped
	for _, v := range container.CapabilityDrop {
		if dropIndex[v] {
			delete(dropIndex, v)
			continue
		}
	}

	for cap := range dropIndex {
		container.CapabilityDrop = append(container.CapabilityDrop, cap)
	}

	return nil
}