-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
namespaces: allow enabling/disabling allowed drivers per namespace
- Loading branch information
Showing
10 changed files
with
405 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:improvement | ||
namespaces: Allow enabling/disabling allowed drivers per namespace. | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package nomad | ||
|
||
import ( | ||
"github.com/hashicorp/nomad/nomad/structs" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
type jobNamespaceConstraintCheckHook struct { | ||
srv *Server | ||
} | ||
|
||
func (jobNamespaceConstraintCheckHook) Name() string { | ||
return "namespace-constraint-check" | ||
} | ||
|
||
func (c jobNamespaceConstraintCheckHook) Validate(job *structs.Job) (warnings []error, err error) { | ||
// This was validated before and matches the WriteRequest namespace | ||
ns, err := c.srv.State().NamespaceByName(nil, job.Namespace) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if ns == nil { | ||
return nil, errors.Errorf("job %q is in nonexistent namespace %q", job.ID, job.Namespace) | ||
} | ||
|
||
var disallowedDrivers []string | ||
for _, tg := range job.TaskGroups { | ||
for _, t := range tg.Tasks { | ||
if !taskValidateDriver(t, ns) { | ||
disallowedDrivers = append(disallowedDrivers, t.Driver) | ||
} | ||
} | ||
} | ||
if len(disallowedDrivers) > 0 { | ||
if len(disallowedDrivers) == 1 { | ||
return nil, errors.Errorf( | ||
"used task driver %q is not allowed in namespace %q", disallowedDrivers[0], ns.Name) | ||
|
||
} else { | ||
return nil, errors.Errorf( | ||
"used task drivers %q are not allowed in namespace %q", disallowedDrivers, ns.Name) | ||
} | ||
} | ||
return nil, nil | ||
} | ||
|
||
func taskValidateDriver(task *structs.Task, ns *structs.Namespace) bool { | ||
if ns.Capabilities == nil { | ||
return true | ||
} | ||
allow := len(ns.Capabilities.EnabledTaskDrivers) == 0 | ||
for _, d := range ns.Capabilities.EnabledTaskDrivers { | ||
if task.Driver == d { | ||
allow = true | ||
break | ||
} | ||
} | ||
for _, d := range ns.Capabilities.DisabledTaskDrivers { | ||
if task.Driver == d { | ||
allow = false | ||
break | ||
} | ||
} | ||
return allow | ||
} |
Oops, something went wrong.