github.com/docker/compose-on-kubernetes@v0.5.0/internal/test/builders/stack.go (about) 1 package builders 2 3 import ( 4 "time" 5 6 "github.com/docker/compose-on-kubernetes/api/compose/latest" 7 corev1 "k8s.io/api/core/v1" 8 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 9 ) 10 11 // Stack is a builder to create an Stack 12 func Stack(name string, builders ...func(*latest.Stack)) *latest.Stack { 13 stack := &latest.Stack{ 14 ObjectMeta: metav1.ObjectMeta{ 15 Name: name, 16 }, 17 Spec: &latest.StackSpec{}, 18 } 19 20 for _, builder := range builders { 21 builder(stack) 22 } 23 24 return stack 25 } 26 27 // WithNamespace set the namespace of the stack 28 func WithNamespace(namespace string) func(*latest.Stack) { 29 return func(s *latest.Stack) { 30 s.Namespace = namespace 31 } 32 } 33 34 // WithSecretConfig add a SecretConfig to to stack 35 func WithSecretConfig(name string, builders ...func(*latest.SecretConfig)) func(*latest.Stack) { 36 return func(s *latest.Stack) { 37 secret := &latest.SecretConfig{ 38 Name: name, 39 } 40 41 for _, builder := range builders { 42 builder(secret) 43 } 44 45 if s.Spec.Secrets == nil { 46 s.Spec.Secrets = map[string]latest.SecretConfig{} 47 } 48 s.Spec.Secrets[name] = *secret 49 } 50 } 51 52 // SecretFile specifies the path of a secret 53 func SecretFile(path string) func(*latest.SecretConfig) { 54 return func(s *latest.SecretConfig) { 55 s.File = path 56 } 57 } 58 59 // WithConfigObjConfig add a ConfigConfig to to stack 60 func WithConfigObjConfig(name string, builders ...func(*latest.ConfigObjConfig)) func(*latest.Stack) { 61 return func(s *latest.Stack) { 62 secret := &latest.ConfigObjConfig{ 63 Name: name, 64 } 65 66 for _, builder := range builders { 67 builder(secret) 68 } 69 70 if s.Spec.Configs == nil { 71 s.Spec.Configs = map[string]latest.ConfigObjConfig{} 72 } 73 s.Spec.Configs[name] = *secret 74 } 75 } 76 77 // ConfigFile specifies the path of a config 78 func ConfigFile(path string) func(*latest.ConfigObjConfig) { 79 return func(c *latest.ConfigObjConfig) { 80 c.File = path 81 } 82 } 83 84 // ConfigExternal specifies that the config is external 85 func ConfigExternal(c *latest.ConfigObjConfig) { 86 c.External = latest.External{ 87 External: true, 88 } 89 } 90 91 // WithService adds a ServiceConifg to the stack 92 func WithService(name string, builders ...func(*latest.ServiceConfig)) func(*latest.Stack) { 93 return func(s *latest.Stack) { 94 service := &latest.ServiceConfig{ 95 Name: name, 96 Image: "busybox:latest", 97 } 98 99 for _, builder := range builders { 100 builder(service) 101 } 102 103 if s.Spec.Services == nil { 104 s.Spec.Services = []latest.ServiceConfig{} 105 } 106 s.Spec.Services = append(s.Spec.Services, *service) 107 } 108 } 109 110 // Image specfies the image of the service 111 func Image(reference string) func(*latest.ServiceConfig) { 112 return func(c *latest.ServiceConfig) { 113 c.Image = reference 114 } 115 } 116 117 // PullSecret specifies the name of the pull secret used for this service 118 func PullSecret(name string) func(*latest.ServiceConfig) { 119 return func(c *latest.ServiceConfig) { 120 c.PullSecret = name 121 } 122 } 123 124 // PullPolicy specifies the pull policy used for this service 125 func PullPolicy(policy string) func(*latest.ServiceConfig) { 126 return func(c *latest.ServiceConfig) { 127 c.PullPolicy = policy 128 } 129 } 130 131 // StopGracePeriod specifies the stop-grace-period duration of a service 132 func StopGracePeriod(duration time.Duration) func(*latest.ServiceConfig) { 133 return func(c *latest.ServiceConfig) { 134 c.StopGracePeriod = &duration 135 } 136 } 137 138 // User specifies the user of a service 139 func User(user int64) func(*latest.ServiceConfig) { 140 return func(c *latest.ServiceConfig) { 141 c.User = &user 142 } 143 } 144 145 // WithTmpFS adds a path to the tmpfs of a service 146 func WithTmpFS(path string) func(*latest.ServiceConfig) { 147 return func(c *latest.ServiceConfig) { 148 if c.Tmpfs == nil { 149 c.Tmpfs = []string{} 150 } 151 c.Tmpfs = append(c.Tmpfs, path) 152 } 153 } 154 155 // WithLabel adds a label to a service 156 func WithLabel(key, value string) func(*latest.ServiceConfig) { 157 return func(c *latest.ServiceConfig) { 158 if c.Labels == nil { 159 c.Labels = map[string]string{} 160 } 161 c.Labels[key] = value 162 } 163 } 164 165 // IPC sets the ipc mode of the service 166 func IPC(mode string) func(*latest.ServiceConfig) { 167 return func(c *latest.ServiceConfig) { 168 c.Ipc = mode 169 } 170 } 171 172 // PID sets the pid mode of the service 173 func PID(mode string) func(*latest.ServiceConfig) { 174 return func(c *latest.ServiceConfig) { 175 c.Pid = mode 176 } 177 } 178 179 // Hostname sets the hostname of the service 180 func Hostname(hostname string) func(*latest.ServiceConfig) { 181 return func(c *latest.ServiceConfig) { 182 c.Hostname = hostname 183 } 184 } 185 186 // WithExtraHost adds an extra host to the service 187 func WithExtraHost(host string) func(*latest.ServiceConfig) { 188 return func(c *latest.ServiceConfig) { 189 if c.ExtraHosts == nil { 190 c.ExtraHosts = []string{} 191 } 192 c.ExtraHosts = append(c.ExtraHosts, host) 193 } 194 } 195 196 // WithVolume adds a volume to the service 197 func WithVolume(builders ...func(*latest.ServiceVolumeConfig)) func(*latest.ServiceConfig) { 198 return func(c *latest.ServiceConfig) { 199 if c.Volumes == nil { 200 c.Volumes = []latest.ServiceVolumeConfig{} 201 } 202 203 volume := &latest.ServiceVolumeConfig{} 204 205 for _, builder := range builders { 206 builder(volume) 207 } 208 209 c.Volumes = append(c.Volumes, *volume) 210 } 211 } 212 213 // Source sets the volume source 214 func Source(source string) func(*latest.ServiceVolumeConfig) { 215 return func(v *latest.ServiceVolumeConfig) { 216 v.Source = source 217 } 218 } 219 220 // Target sets the volume target 221 func Target(target string) func(*latest.ServiceVolumeConfig) { 222 return func(v *latest.ServiceVolumeConfig) { 223 v.Target = target 224 } 225 } 226 227 // Volume sets the volume type to volume 228 func Volume(v *latest.ServiceVolumeConfig) { 229 v.Type = "volume" 230 } 231 232 // Mount sets the volume type to mount 233 func Mount(v *latest.ServiceVolumeConfig) { 234 v.Type = "mount" 235 } 236 237 // VolumeReadOnly sets the volume to readonly 238 func VolumeReadOnly(v *latest.ServiceVolumeConfig) { 239 v.ReadOnly = true 240 } 241 242 // Healthcheck sets the healthcheck config of the service 243 func Healthcheck(builders ...func(*latest.HealthCheckConfig)) func(*latest.ServiceConfig) { 244 return func(c *latest.ServiceConfig) { 245 healthcheck := &latest.HealthCheckConfig{} 246 247 for _, builder := range builders { 248 builder(healthcheck) 249 } 250 251 c.HealthCheck = healthcheck 252 } 253 } 254 255 // Test sets the test commands of the healthcheck 256 func Test(cmd ...string) func(*latest.HealthCheckConfig) { 257 return func(h *latest.HealthCheckConfig) { 258 h.Test = cmd 259 } 260 } 261 262 // Interval sets the interval duration of the healthcheck 263 func Interval(duration time.Duration) func(*latest.HealthCheckConfig) { 264 return func(h *latest.HealthCheckConfig) { 265 h.Interval = &duration 266 } 267 } 268 269 // Timeout sets the timeout duration of the healthcheck 270 func Timeout(duration time.Duration) func(*latest.HealthCheckConfig) { 271 return func(h *latest.HealthCheckConfig) { 272 h.Timeout = &duration 273 } 274 } 275 276 // Retries sets the number of retries of the healthcheck 277 func Retries(retries uint64) func(*latest.HealthCheckConfig) { 278 return func(h *latest.HealthCheckConfig) { 279 h.Retries = &retries 280 } 281 } 282 283 // WithSecret adds a secret to the service 284 func WithSecret(builders ...func(*latest.ServiceSecretConfig)) func(*latest.ServiceConfig) { 285 return func(c *latest.ServiceConfig) { 286 if c.Secrets == nil { 287 c.Secrets = []latest.ServiceSecretConfig{} 288 } 289 secret := &latest.ServiceSecretConfig{} 290 for _, builder := range builders { 291 builder(secret) 292 } 293 c.Secrets = append(c.Secrets, *secret) 294 } 295 } 296 297 // SecretSource sets the source of the secret 298 func SecretSource(source string) func(*latest.ServiceSecretConfig) { 299 return func(s *latest.ServiceSecretConfig) { 300 s.Source = source 301 } 302 } 303 304 // SecretTarget sets the target of the secret 305 func SecretTarget(target string) func(*latest.ServiceSecretConfig) { 306 return func(s *latest.ServiceSecretConfig) { 307 s.Target = target 308 } 309 } 310 311 // WithConfig adds a config to the service 312 func WithConfig(builders ...func(*latest.ServiceConfigObjConfig)) func(*latest.ServiceConfig) { 313 return func(c *latest.ServiceConfig) { 314 if c.Configs == nil { 315 c.Configs = []latest.ServiceConfigObjConfig{} 316 } 317 config := &latest.ServiceConfigObjConfig{} 318 for _, builder := range builders { 319 builder(config) 320 } 321 c.Configs = append(c.Configs, *config) 322 } 323 } 324 325 // ConfigSource sets the source of the config 326 func ConfigSource(source string) func(*latest.ServiceConfigObjConfig) { 327 return func(c *latest.ServiceConfigObjConfig) { 328 c.Source = source 329 } 330 } 331 332 // ConfigTarget sets the target of the config 333 func ConfigTarget(target string) func(*latest.ServiceConfigObjConfig) { 334 return func(c *latest.ServiceConfigObjConfig) { 335 c.Target = target 336 } 337 } 338 339 // ConfigUID sets the uid of the config 340 func ConfigUID(uid string) func(*latest.ServiceConfigObjConfig) { 341 return func(c *latest.ServiceConfigObjConfig) { 342 c.UID = uid 343 } 344 } 345 346 // ConfigGID sets the gid of the config 347 func ConfigGID(gid string) func(*latest.ServiceConfigObjConfig) { 348 return func(c *latest.ServiceConfigObjConfig) { 349 c.GID = gid 350 } 351 } 352 353 // ConfigMode sets the mode of the config 354 func ConfigMode(mode uint32) func(*latest.ServiceConfigObjConfig) { 355 return func(c *latest.ServiceConfigObjConfig) { 356 c.Mode = &mode 357 } 358 } 359 360 // Deploy sets the deploy config of the service 361 func Deploy(builders ...func(*latest.DeployConfig)) func(*latest.ServiceConfig) { 362 return func(c *latest.ServiceConfig) { 363 deploy := &latest.DeployConfig{} 364 365 for _, builder := range builders { 366 builder(deploy) 367 } 368 369 c.Deploy = *deploy 370 } 371 } 372 373 // Resources sets the resources of the deploy config 374 func Resources(builders ...func(*latest.Resources)) func(*latest.DeployConfig) { 375 return func(d *latest.DeployConfig) { 376 resources := &latest.Resources{} 377 378 for _, builder := range builders { 379 builder(resources) 380 } 381 382 d.Resources = *resources 383 } 384 } 385 386 // Limits sets the limits of the resources 387 func Limits(builders ...func(*latest.Resource)) func(*latest.Resources) { 388 return func(r *latest.Resources) { 389 limits := &latest.Resource{} 390 391 for _, builder := range builders { 392 builder(limits) 393 } 394 395 r.Limits = limits 396 } 397 } 398 399 // Reservations sets the reservations of the resources 400 func Reservations(builders ...func(*latest.Resource)) func(*latest.Resources) { 401 return func(r *latest.Resources) { 402 reservations := &latest.Resource{} 403 404 for _, builder := range builders { 405 builder(reservations) 406 } 407 408 r.Reservations = reservations 409 } 410 } 411 412 // CPUs sets the cup of the resource 413 func CPUs(cpus string) func(*latest.Resource) { 414 return func(r *latest.Resource) { 415 r.NanoCPUs = cpus 416 } 417 } 418 419 // Memory sets the memory of the resource 420 func Memory(memory int64) func(*latest.Resource) { 421 return func(r *latest.Resource) { 422 r.MemoryBytes = memory 423 } 424 } 425 426 // Update sets the update config of a deploy config 427 func Update(builders ...func(*latest.UpdateConfig)) func(*latest.DeployConfig) { 428 return func(d *latest.DeployConfig) { 429 update := &latest.UpdateConfig{} 430 431 for _, builder := range builders { 432 builder(update) 433 } 434 435 d.UpdateConfig = update 436 } 437 } 438 439 // Parallelism sets the parallelism of the update config 440 func Parallelism(parallelism uint64) func(*latest.UpdateConfig) { 441 return func(u *latest.UpdateConfig) { 442 u.Parallelism = ¶llelism 443 } 444 } 445 446 // ModeGlobal sets the deploy mode to global 447 func ModeGlobal(d *latest.DeployConfig) { 448 d.Mode = "global" 449 } 450 451 // Replicas sets the number of replicas of a deploy config 452 func Replicas(replicas uint64) func(*latest.DeployConfig) { 453 return func(d *latest.DeployConfig) { 454 d.Replicas = &replicas 455 } 456 } 457 458 // WithDeployLabel adds a label to the deploy config 459 func WithDeployLabel(key, value string) func(*latest.DeployConfig) { 460 return func(d *latest.DeployConfig) { 461 if d.Labels == nil { 462 d.Labels = map[string]string{} 463 } 464 d.Labels[key] = value 465 } 466 } 467 468 // RestartPolicy sets the restart policy of the deploy config 469 func RestartPolicy(builders ...func(*latest.RestartPolicy)) func(*latest.DeployConfig) { 470 return func(d *latest.DeployConfig) { 471 rp := &latest.RestartPolicy{} 472 473 for _, builder := range builders { 474 builder(rp) 475 } 476 477 d.RestartPolicy = rp 478 } 479 } 480 481 // OnFailure sets the restart policy to on-failure 482 func OnFailure(r *latest.RestartPolicy) { 483 r.Condition = "on-failure" 484 } 485 486 // Placement sets the placement of the deploy config 487 func Placement(builders ...func(*latest.Placement)) func(*latest.DeployConfig) { 488 return func(d *latest.DeployConfig) { 489 placement := &latest.Placement{} 490 491 for _, builder := range builders { 492 builder(placement) 493 } 494 495 d.Placement = *placement 496 } 497 } 498 499 // Constraints sets the constraints to the placement 500 func Constraints(builders ...func(*latest.Constraints)) func(*latest.Placement) { 501 return func(p *latest.Placement) { 502 constraints := &latest.Constraints{} 503 for _, builder := range builders { 504 builder(constraints) 505 } 506 p.Constraints = constraints 507 } 508 } 509 510 // OperatingSystem set the operating system constraint 511 func OperatingSystem(value, operator string) func(*latest.Constraints) { 512 return func(c *latest.Constraints) { 513 c.OperatingSystem = &latest.Constraint{ 514 Operator: operator, 515 Value: value, 516 } 517 } 518 } 519 520 // Architecture set the operating system constraint 521 func Architecture(value, operator string) func(*latest.Constraints) { 522 return func(c *latest.Constraints) { 523 c.Architecture = &latest.Constraint{ 524 Operator: operator, 525 Value: value, 526 } 527 } 528 } 529 530 // ConstraintHostname set the operating system constraint 531 func ConstraintHostname(value, operator string) func(*latest.Constraints) { 532 return func(c *latest.Constraints) { 533 c.Hostname = &latest.Constraint{ 534 Operator: operator, 535 Value: value, 536 } 537 } 538 } 539 540 // WithMatchLabel adds the labels constraint to the constraint 541 func WithMatchLabel(key, value, operator string) func(*latest.Constraints) { 542 return func(c *latest.Constraints) { 543 if c.MatchLabels == nil { 544 c.MatchLabels = map[string]latest.Constraint{} 545 } 546 c.MatchLabels[key] = latest.Constraint{ 547 Operator: operator, 548 Value: value, 549 } 550 } 551 } 552 553 // WithCapAdd add a cap add to the service 554 func WithCapAdd(caps ...string) func(*latest.ServiceConfig) { 555 return func(c *latest.ServiceConfig) { 556 if c.CapAdd == nil { 557 c.CapAdd = []string{} 558 } 559 c.CapAdd = append(c.CapAdd, caps...) 560 } 561 } 562 563 // WithCapDrop adds a cap drop to the service 564 func WithCapDrop(caps ...string) func(*latest.ServiceConfig) { 565 return func(c *latest.ServiceConfig) { 566 if c.CapDrop == nil { 567 c.CapDrop = []string{} 568 } 569 c.CapDrop = append(c.CapDrop, caps...) 570 } 571 } 572 573 // WithEnvironment adds an environment variable to the service 574 func WithEnvironment(key string, value *string) func(*latest.ServiceConfig) { 575 return func(c *latest.ServiceConfig) { 576 if c.Environment == nil { 577 c.Environment = map[string]*string{} 578 } 579 c.Environment[key] = value 580 } 581 } 582 583 // ReadOnly sets the service to read only 584 func ReadOnly(c *latest.ServiceConfig) { 585 c.ReadOnly = true 586 } 587 588 // Privileged sets the service to privileged 589 func Privileged(c *latest.ServiceConfig) { 590 c.Privileged = true 591 } 592 593 // Entrypoint sets the entrypoint of the service 594 func Entrypoint(s ...string) func(*latest.ServiceConfig) { 595 return func(c *latest.ServiceConfig) { 596 c.Entrypoint = s 597 } 598 } 599 600 // Command sets the command of the service 601 func Command(s ...string) func(*latest.ServiceConfig) { 602 return func(c *latest.ServiceConfig) { 603 c.Command = s 604 } 605 } 606 607 // WorkingDir sets the service's working folder 608 func WorkingDir(w string) func(*latest.ServiceConfig) { 609 return func(c *latest.ServiceConfig) { 610 c.WorkingDir = w 611 } 612 } 613 614 // WithPort adds a port config to the service 615 func WithPort(target uint32, builders ...func(*latest.ServicePortConfig)) func(*latest.ServiceConfig) { 616 return func(c *latest.ServiceConfig) { 617 if c.Ports == nil { 618 c.Ports = []latest.ServicePortConfig{} 619 } 620 port := &latest.ServicePortConfig{ 621 Target: target, 622 Protocol: "tcp", 623 } 624 625 for _, builder := range builders { 626 builder(port) 627 } 628 629 c.Ports = append(c.Ports, *port) 630 } 631 } 632 633 // WithInternalPort adds an internal port declaration 634 func WithInternalPort(port int32, protocol corev1.Protocol) func(*latest.ServiceConfig) { 635 return func(c *latest.ServiceConfig) { 636 c.InternalPorts = append(c.InternalPorts, latest.InternalPort{ 637 Port: port, 638 Protocol: protocol, 639 }) 640 } 641 } 642 643 // Published sets the published port 644 func Published(published uint32) func(*latest.ServicePortConfig) { 645 return func(c *latest.ServicePortConfig) { 646 c.Published = published 647 } 648 } 649 650 // ProtocolUDP set's the port's protocol 651 func ProtocolUDP(c *latest.ServicePortConfig) { 652 c.Protocol = "udp" 653 } 654 655 // Tty sets the service's tty to true 656 func Tty(s *latest.ServiceConfig) { 657 s.Tty = true 658 } 659 660 // StdinOpen sets the service's stdin opne to true 661 func StdinOpen(s *latest.ServiceConfig) { 662 s.StdinOpen = true 663 }