diff --git a/go.mod b/go.mod index 433c6ae4..2b797af3 100644 --- a/go.mod +++ b/go.mod @@ -13,6 +13,7 @@ exclude ( require ( github.com/google/go-cmp v0.5.0 + github.com/hashicorp/go-multierror v1.1.0 github.com/hashicorp/go-version v1.2.1 github.com/hashicorp/nomad v0.12.5-0.20201029140339-d6255129a300 github.com/hashicorp/nomad/api v0.0.0-20201028165800-38e23b62a770 diff --git a/nomad/data_source_scheduler_config.go b/nomad/data_source_scheduler_config.go new file mode 100644 index 00000000..2c0993f1 --- /dev/null +++ b/nomad/data_source_scheduler_config.go @@ -0,0 +1,53 @@ +package nomad + +import ( + "fmt" + + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" + "github.com/hashicorp/terraform-provider-nomad/nomad/helper" +) + +func dataSourceSchedulerConfig() *schema.Resource { + return &schema.Resource{ + Read: dataSourceSchedulerConfigRead, + + Schema: map[string]*schema.Schema{ + "scheduler_algorithm": { + Description: "Specifies whether scheduler binpacks or spreads allocations on available nodes.", + Type: schema.TypeString, + Computed: true, + }, + "preemption_config": { + Description: "Options to enable preemption for various schedulers.", + Computed: true, + Type: schema.TypeMap, + Elem: &schema.Schema{Type: schema.TypeBool}, + }, + }, + } +} + +func dataSourceSchedulerConfigRead(d *schema.ResourceData, meta interface{}) error { + + client := meta.(ProviderConfig).client + + schedCfg, _, err := client.Operator().SchedulerGetConfiguration(nil) + if err != nil { + return fmt.Errorf("failed to query scheduler config: %v", err) + } + + // Set a unique ID, as we have nothing else to go on. + d.SetId(resource.UniqueId()) + + premptMap := map[string]bool{ + "batch_scheduler_enabled": schedCfg.SchedulerConfig.PreemptionConfig.BatchSchedulerEnabled, + "service_scheduler_enabled": schedCfg.SchedulerConfig.PreemptionConfig.ServiceSchedulerEnabled, + "system_scheduler_enabled": schedCfg.SchedulerConfig.PreemptionConfig.SystemSchedulerEnabled, + } + + sw := helper.NewStateWriter(d) + sw.Set("scheduler_algorithm", schedCfg.SchedulerConfig.SchedulerAlgorithm) + sw.Set("preemption_config", premptMap) + return sw.Error() +} diff --git a/nomad/data_source_scheduler_config_test.go b/nomad/data_source_scheduler_config_test.go new file mode 100644 index 00000000..51d8d494 --- /dev/null +++ b/nomad/data_source_scheduler_config_test.go @@ -0,0 +1,55 @@ +package nomad + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" +) + +func TestAccDataSourceSchedulerConfig_basic(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testProviders, + CheckDestroy: testFinalConfiguration, + Steps: []resource.TestStep{ + { + Config: testAccNomadSchedulerConfigSpread, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr( + "nomad_scheduler_config.config", + "scheduler_algorithm", + "spread", + ), + resource.TestCheckResourceAttr( + "nomad_scheduler_config.config", + "preemption_config.batch_scheduler_enabled", + "true", + ), + resource.TestCheckResourceAttr( + "nomad_scheduler_config.config", + "preemption_config.service_scheduler_enabled", + "true", + ), + resource.TestCheckResourceAttr( + "nomad_scheduler_config.config", + "preemption_config.system_scheduler_enabled", + "true", + ), + ), + }, + }, + }) +} + +const testAccNomadDataSourceSchedulerConfigS = ` +resource "nomad_scheduler_config" "config" { + scheduler_algorithm = "spread" + preemption_config = { + system_scheduler_enabled = true + batch_scheduler_enabled = true + service_scheduler_enabled = true + } +} + +data "nomad_scheduler_config" "config" {} +` diff --git a/nomad/provider.go b/nomad/provider.go index 481c5a37..cdabf147 100644 --- a/nomad/provider.go +++ b/nomad/provider.go @@ -79,6 +79,7 @@ func Provider() terraform.ResourceProvider { "nomad_plugins": dataSourcePlugins(), "nomad_scaling_policies": dataSourceScalingPolicies(), "nomad_scaling_policy": dataSourceScalingPolicy(), + "nomad_scheduler_config": dataSourceSchedulerConfig(), "nomad_regions": dataSourceRegions(), "nomad_volumes": dataSourceVolumes(), }, diff --git a/website/docs/d/scheduler_config.html.md b/website/docs/d/scheduler_config.html.md new file mode 100644 index 00000000..b4c6394b --- /dev/null +++ b/website/docs/d/scheduler_config.html.md @@ -0,0 +1,24 @@ +--- +layout: "nomad" +page_title: "Nomad: nomad_scheduler_config" +sidebar_current: "docs-nomad-datasource-scheduler-config" +description: |- + Retrieve the cluster's scheduler configuration. +--- + +# nomad_scheduler_config + +Retrieve the cluster's [scheduler configuration](https://www.nomadproject.io/api-docs/operator#sample-response-3). + +## Example Usage + +```hcl +data "nomad_scheduler_config" "global" {} +``` + +## Attribute Reference + +The following attributes are exported: + +* `scheduler_algorithm` `(string)` - Specifies whether scheduler binpacks or spreads allocations on available nodes. +* `preemption_config` `(map[string]bool)` - Options to enable preemption for various schedulers. diff --git a/website/nomad.erb b/website/nomad.erb index ac1af4cf..516cd026 100644 --- a/website/nomad.erb +++ b/website/nomad.erb @@ -58,6 +58,9 @@