github.com/projecteru2/core@v0.0.0-20240321043226-06bcc1c23f58/types/options.go (about) 1 package types 2 3 import ( 4 "fmt" 5 "io" 6 7 resourcetypes "github.com/projecteru2/core/resource/types" 8 ) 9 10 // TODO should validate options 11 12 // DeployOptions is options for deploying 13 type DeployOptions struct { 14 Resources resourcetypes.Resources 15 Name string // Name of application 16 Entrypoint *Entrypoint // entrypoint 17 Podname string // Name of pod to deploy 18 NodeFilter *NodeFilter // filter of nodenames, using includes or not using excludes 19 Image string // Name of image to deploy 20 ExtraArgs string // Extra arguments to append to command 21 Count int // How many workloads needed, e.g. 4 22 Env []string // Env for workload 23 DNS []string // DNS for workload 24 ExtraHosts []string // Extra hosts for workload 25 Networks map[string]string // Network names and specified IPs 26 User string // User for workload 27 Debug bool // debug mode, use syslog as log driver 28 OpenStdin bool // OpenStdin for workload 29 Labels map[string]string // Labels for workloads 30 DeployStrategy string // Deploy strategy 31 Files []LinuxFile // For additional file data 32 NodesLimit int // Limit nodes count 33 ProcessIdent string // ProcessIdent ident this deploy 34 IgnoreHook bool // IgnoreHook ignore hook process 35 AfterCreate []string // AfterCreate support run cmds after create 36 RawArgs RawArgs // RawArgs for raw args processing 37 Lambda bool // indicate is lambda workload or not 38 IgnorePull bool // ignore pull image 39 } 40 41 // Processing tracks workloads count yet finished 42 type Processing struct { 43 Appname string 44 Entryname string 45 Nodename string 46 Ident string 47 } 48 49 // GetProcessing . 50 func (o DeployOptions) GetProcessing(nodename string) *Processing { 51 return &Processing{ 52 Appname: o.Name, 53 Entryname: o.Entrypoint.Name, 54 Nodename: nodename, 55 Ident: o.ProcessIdent, 56 } 57 } 58 59 // Validate checks options 60 func (o *DeployOptions) Validate() error { 61 if o.Name == "" { 62 return ErrEmptyAppName 63 } 64 if o.Podname == "" { 65 return ErrEmptyPodName 66 } 67 if o.Image == "" { 68 return ErrEmptyImage 69 } 70 if o.Count == 0 { 71 return ErrEmptyCount 72 } 73 return o.Entrypoint.Validate() 74 } 75 76 // CopyOptions for multiple workload files copy 77 type CopyOptions struct { 78 Targets map[string][]string 79 } 80 81 // Validate checks options 82 func (o *CopyOptions) Validate() error { 83 if len(o.Targets) == 0 { 84 return ErrNoFilesToCopy 85 } 86 return nil 87 } 88 89 // LinuxFile is used for copy file 90 type LinuxFile struct { 91 Content []byte 92 Filename string 93 UID int 94 GID int 95 Mode int64 96 } 97 98 // Clone returns a copy of content bytes 99 func (f LinuxFile) Clone() LinuxFile { 100 c := make([]byte, len(f.Content)) 101 copy(c, f.Content) 102 return LinuxFile{ 103 Content: c, 104 Filename: f.Filename, 105 UID: f.UID, 106 GID: f.GID, 107 Mode: f.Mode, 108 } 109 } 110 111 // String for %+v 112 func (f LinuxFile) String() string { 113 return fmt.Sprintf("file %+v:%+v:%+v:%#o, len: %+v", f.Filename, f.UID, f.GID, f.Mode, len(f.Content)) 114 } 115 116 // LitterDump for litter.Sdump 117 func (f LinuxFile) LitterDump(w io.Writer) { 118 fmt.Fprintf(w, `{Content:{%d bytes},Filename:%s,UID:%d,GID:%d,Mode:%#o"}`, len(f.Content), f.Filename, f.UID, f.GID, f.Mode) 119 } 120 121 // SendOptions for send files to multiple workload 122 type SendOptions struct { 123 IDs []string 124 Files []LinuxFile 125 } 126 127 // Validate checks options 128 func (o *SendOptions) Validate() error { 129 if len(o.IDs) == 0 { 130 return ErrNoWorkloadIDs 131 } 132 if len(o.Files) == 0 { 133 return ErrNoFilesToSend 134 } 135 for i, file := range o.Files { 136 if file.UID == 0 && file.GID == 0 && file.Mode == 0 { 137 // we see it as requiring "default perm" 138 o.Files[i].Mode = 0755 139 } 140 } 141 return nil 142 } 143 144 // ListWorkloadsOptions for list workloads 145 type ListWorkloadsOptions struct { 146 Appname string 147 Entrypoint string 148 Nodename string 149 Limit int64 150 Labels map[string]string 151 } 152 153 // ReplaceOptions for replace workload 154 type ReplaceOptions struct { 155 DeployOptions 156 NetworkInherit bool 157 FilterLabels map[string]string 158 Copy map[string]string 159 IDs []string 160 } 161 162 // Validate doesn't check image here 163 // because in cluster/calcium//helper.go, pullImage will check this 164 // to keep the original behavior, no check here. 165 func (o *ReplaceOptions) Validate() error { 166 if o.DeployOptions.Name == "" { 167 return ErrEmptyAppName 168 } 169 return o.DeployOptions.Entrypoint.Validate() 170 } 171 172 // Normalize checks count 173 func (o *ReplaceOptions) Normalize() { 174 if o.Count == 0 { 175 o.Count = 1 176 } 177 } 178 179 // ListNodesOptions for list nodes 180 type ListNodesOptions struct { 181 Podname string 182 Labels map[string]string 183 All bool 184 CallInfo bool 185 } 186 187 // AddNodeOptions for adding node 188 type AddNodeOptions struct { 189 Nodename string 190 Endpoint string 191 Podname string 192 Ca string 193 Cert string 194 Key string 195 Labels map[string]string 196 Resources resourcetypes.Resources 197 Test bool 198 } 199 200 // Validate checks options 201 func (o *AddNodeOptions) Validate() error { 202 if o.Nodename == "" { 203 return ErrEmptyNodeName 204 } 205 if o.Podname == "" { 206 return ErrEmptyPodName 207 } 208 if o.Endpoint == "" { 209 return ErrInvaildNodeEndpoint 210 } 211 return nil 212 } 213 214 // SetNodeOptions for node set 215 type SetNodeOptions struct { 216 Nodename string 217 Endpoint string 218 WorkloadsDown bool 219 Resources resourcetypes.Resources 220 Delta bool 221 Labels map[string]string 222 Bypass TriOptions 223 Ca string 224 Cert string 225 Key string 226 } 227 228 // Validate checks options 229 func (o *SetNodeOptions) Validate() error { 230 if o.Nodename == "" { 231 return ErrEmptyNodeName 232 } 233 return nil 234 } 235 236 // ImageOptions wraps options for images 237 // Prune is only used when remove image 238 type ImageOptions struct { 239 Podname string 240 Nodenames []string 241 Images []string 242 Prune bool 243 Filter string 244 } 245 246 // Validate checks the options 247 func (o *ImageOptions) Validate() error { 248 if o.Podname == "" { 249 return ErrEmptyPodName 250 } 251 return nil 252 } 253 254 // ExecuteWorkloadOptions for executing commands in running workload 255 type ExecuteWorkloadOptions struct { 256 WorkloadID string 257 Commands []string 258 Envs []string 259 Workdir string 260 OpenStdin bool 261 ReplCmd []byte 262 } 263 264 // ReallocOptions . 265 type ReallocOptions struct { 266 ID string 267 Resources resourcetypes.Resources 268 } 269 270 // TriOptions . 271 type TriOptions int 272 273 const ( 274 // TriKeep . 275 TriKeep = iota 276 // TriTrue . 277 TriTrue 278 // TriFalse . 279 TriFalse 280 ) 281 282 // ParseTriOption . 283 func ParseTriOption(opt TriOptions, original bool) (res bool) { 284 switch opt { 285 case TriKeep: 286 res = original 287 case TriTrue: 288 res = true 289 case TriFalse: 290 res = false 291 } 292 return 293 } 294 295 // RawArgs . 296 type RawArgs []byte 297 298 // String for %+v 299 func (r RawArgs) String() string { 300 return string(r) 301 } 302 303 // LitterDump from litter.Dumper 304 func (r RawArgs) LitterDump(w io.Writer) { 305 w.Write(r) //nolint:errcheck 306 } 307 308 const SendLargeFileChunkSize = 2 << 10 309 310 // SendLargeFileOptions for LargeFileTransfer 311 type SendLargeFileOptions struct { 312 IDs []string 313 Dst string 314 Size int64 315 Mode int64 316 UID int 317 GID int 318 Chunk []byte 319 } 320 321 // Validate checks options 322 func (o *SendLargeFileOptions) Validate() error { 323 if len(o.IDs) == 0 { 324 return ErrNoWorkloadIDs 325 } 326 if len(o.Chunk) == 0 { 327 return ErrNoFilesToSend 328 } 329 if o.UID == 0 && o.GID == 0 && o.Mode == 0 { 330 // we see it as requiring "default perm" 331 o.Mode = 0755 332 } 333 return nil 334 } 335 336 type RawEngineOptions struct { 337 ID string 338 Op string 339 Params []byte 340 IgnoreLock bool // whether lock the workload 341 } 342 343 func (o *RawEngineOptions) Validate() error { 344 if o.ID == "" { 345 return ErrEmptyWorkloadID 346 } 347 if o.Op == "" { 348 return ErrEmptyRawEngineOp 349 } 350 return nil 351 }