worker: behaviour: prevent mixed usage of runtime flags
If flags related to runtimes other than the one the operator
has set by CONCOURSE_RUNTIME_TYPE are provided throw an error.
e.g. if CONCOURSE_RUNTIME_TYPE is set to "containerd" throw an
error if any CONCOURSE_GARDEN_ prefixed flags are present.
@@ -8,6 +8,7 @@ import (
"os"
"os/user"
"path/filepath"
+ "strings"
"time"
"code.cloudfoundry.org/lager"
@@ -45,6 +46,10 @@ type ContainerdRuntime struct {
NetworkPool string `long:"network-pool" description:"Network range to use for dynamically allocated container subnets."`
}
+const containerdRuntime = "containerd"
+const guardianRuntime = "guardian"
+const houdiniRuntime = "houdini"
+
func (cmd WorkerCommand) LessenRequirements(prefix string, command *flags.Command) {
// configured as work-dir/volumes
command.FindOptionByLongName(prefix + "baggageclaim-volumes").Required = false
@@ -82,11 +87,11 @@ func (cmd *WorkerCommand) gardenServerRunner(logger lager.Logger) (atc.Worker, i
var runner ifrit.Runner
switch {
- case cmd.Runtime.Type == "houdini":
+ case cmd.Runtime.Type == houdiniRuntime:
runner, err = cmd.houdiniRunner(logger)
- case cmd.Runtime.Type == "containerd":
+ case cmd.Runtime.Type == containerdRuntime:
runner, err = cmd.containerdRunner(logger)
- case cmd.Runtime.Type == "guardian":
+ case cmd.Runtime.Type == guardianRuntime:
runner, err = cmd.guardianRunner(logger)
default:
err = fmt.Errorf("unsupported Runtime :%s", cmd.Runtime.Type)
@@ -190,3 +195,37 @@ func (cmd *WorkerCommand) loadResources(logger lager.Logger) ([]atc.WorkerResour
return types, nil
}
+
+
+func (cmd *WorkerCommand) hasFlags(prefix string) bool {
+ env := os.Environ()
+
+ for _, envVar := range env {
+ if strings.HasPrefix(envVar, prefix) {
+ return true
+ }
+ }
+
+ return false
+}
+
+func (cmd *WorkerCommand) verifyRuntimeFlags() error {
+ switch {
+ case cmd.Runtime.Type == houdiniRuntime:
+ if cmd.hasFlags("CONCOURSE_GUARDIAN_") || cmd.hasFlags("CONCOURSE_CONTAINERD_") {
+ return fmt.Errorf("cannot use CONCOURSE_GUARDIAN_ or CONCOURSE_CONTAINERD_ environment variables with Houdini")
+ }
+ case cmd.Runtime.Type == containerdRuntime:
+ if cmd.hasFlags("CONCOURSE_GUARDIAN_") {
+ return fmt.Errorf("cannot use CONCOURSE_GUARDIAN_ environment variables with Containerd")
+ }
+ case cmd.Runtime.Type == guardianRuntime:
+ if cmd.hasFlags("CONCOURSE_CONTAINERD_") {
+ return fmt.Errorf("cannot use CONCOURSE_CONTAINERD_ environment variables with Guardian")
+ }
+ default:
+ return fmt.Errorf("unsupported Runtime :%s", cmd.Runtime.Type)
+ }
+
+ return nil
+}
\ No newline at end of file
