github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/nomad/structs/volumes.go (about) 1 package structs 2 3 const ( 4 VolumeTypeHost = "host" 5 ) 6 7 const ( 8 VolumeMountPropagationPrivate = "private" 9 VolumeMountPropagationHostToTask = "host-to-task" 10 VolumeMountPropagationBidirectional = "bidirectional" 11 ) 12 13 func MountPropagationModeIsValid(propagationMode string) bool { 14 switch propagationMode { 15 case "", VolumeMountPropagationPrivate, VolumeMountPropagationHostToTask, VolumeMountPropagationBidirectional: 16 return true 17 default: 18 return false 19 } 20 } 21 22 // ClientHostVolumeConfig is used to configure access to host paths on a Nomad Client 23 type ClientHostVolumeConfig struct { 24 Name string `hcl:",key"` 25 Path string `hcl:"path"` 26 ReadOnly bool `hcl:"read_only"` 27 } 28 29 func (p *ClientHostVolumeConfig) Copy() *ClientHostVolumeConfig { 30 if p == nil { 31 return nil 32 } 33 34 c := new(ClientHostVolumeConfig) 35 *c = *p 36 return c 37 } 38 39 func CopyMapStringClientHostVolumeConfig(m map[string]*ClientHostVolumeConfig) map[string]*ClientHostVolumeConfig { 40 if m == nil { 41 return nil 42 } 43 44 nm := make(map[string]*ClientHostVolumeConfig, len(m)) 45 for k, v := range m { 46 nm[k] = v.Copy() 47 } 48 49 return nm 50 } 51 52 func CopySliceClientHostVolumeConfig(s []*ClientHostVolumeConfig) []*ClientHostVolumeConfig { 53 l := len(s) 54 if l == 0 { 55 return nil 56 } 57 58 ns := make([]*ClientHostVolumeConfig, l) 59 for idx, cfg := range s { 60 ns[idx] = cfg.Copy() 61 } 62 63 return ns 64 } 65 66 func HostVolumeSliceMerge(a, b []*ClientHostVolumeConfig) []*ClientHostVolumeConfig { 67 n := make([]*ClientHostVolumeConfig, len(a)) 68 seenKeys := make(map[string]int, len(a)) 69 70 for i, config := range a { 71 n[i] = config.Copy() 72 seenKeys[config.Name] = i 73 } 74 75 for _, config := range b { 76 if fIndex, ok := seenKeys[config.Name]; ok { 77 n[fIndex] = config.Copy() 78 continue 79 } 80 81 n = append(n, config.Copy()) 82 } 83 84 return n 85 } 86 87 // VolumeRequest is a representation of a storage volume that a TaskGroup wishes to use. 88 type VolumeRequest struct { 89 Name string 90 Type string 91 Source string 92 ReadOnly bool 93 MountOptions *CSIMountOptions 94 } 95 96 func (v *VolumeRequest) Copy() *VolumeRequest { 97 if v == nil { 98 return nil 99 } 100 nv := new(VolumeRequest) 101 *nv = *v 102 103 if v.MountOptions != nil { 104 nv.MountOptions = v.MountOptions.Copy() 105 } 106 107 return nv 108 } 109 110 func CopyMapVolumeRequest(s map[string]*VolumeRequest) map[string]*VolumeRequest { 111 if s == nil { 112 return nil 113 } 114 115 l := len(s) 116 c := make(map[string]*VolumeRequest, l) 117 for k, v := range s { 118 c[k] = v.Copy() 119 } 120 return c 121 } 122 123 // VolumeMount represents the relationship between a destination path in a task 124 // and the task group volume that should be mounted there. 125 type VolumeMount struct { 126 Volume string 127 Destination string 128 ReadOnly bool 129 PropagationMode string 130 } 131 132 func (v *VolumeMount) Copy() *VolumeMount { 133 if v == nil { 134 return nil 135 } 136 137 nv := new(VolumeMount) 138 *nv = *v 139 return nv 140 } 141 142 func CopySliceVolumeMount(s []*VolumeMount) []*VolumeMount { 143 l := len(s) 144 if l == 0 { 145 return nil 146 } 147 148 c := make([]*VolumeMount, l) 149 for i, v := range s { 150 c[i] = v.Copy() 151 } 152 return c 153 }