Skip to content
This repository has been archived by the owner on Mar 18, 2021. It is now read-only.

Commit

Permalink
Merge pull request #56 from acornies/bug/datacenters
Browse files Browse the repository at this point in the history
Change datacenter behaviour:
  • Loading branch information
nicholasjackson authored Oct 27, 2018
2 parents 114f923 + 3c8b8f4 commit 1937e19
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 16 deletions.
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"request": "launch",
"mode": "auto",
"remotePath": "",
"port": 8080,
"host": "0.0.0.0",
"program": "${workspaceFolder}",
"env": {},
Expand All @@ -21,7 +20,8 @@
"-statsd_addr", "192.168.50.2:9125",
"-basic_auth_secret_path", "provisioning/secrets",
"-enable_basic_auth=false",
"-vault_default_policy", "openfaas"
"-vault_default_policy", "openfaas",
"-port=8081"
],
"showLog": true
}
Expand Down
15 changes: 8 additions & 7 deletions handlers/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func createJob(r requests.CreateFunctionRequest, providerConfig types.ProviderCo
job := api.NewServiceJob(jobname, jobname, "global", 1)

job.Meta = createAnnotations(r)
job.Datacenters = createDataCenters(r)
job.Datacenters = createDataCenters(r, providerConfig.Datacenter)
job.Update = createUpdateStrategy()

// add constraints
Expand Down Expand Up @@ -222,20 +222,21 @@ func createLimits(r requests.CreateFunctionRequest) (taskMemory, taskCPU int) {
return taskMemory, taskCPU
}

func createDataCenters(r requests.CreateFunctionRequest) []string {
if r.Labels != nil && (*r.Labels)["datacenters"] != "" {
lbls := (*r.Labels)["datacenters"]
func createDataCenters(r requests.CreateFunctionRequest, defaultDC string) []string {
if r.Constraints != nil && len(r.Constraints) > 0 {
dcs := []string{}

for _, dc := range strings.Split(lbls, ",") {
dcs = append(dcs, strings.Trim(dc, " "))
for _, constr := range r.Constraints {
if strings.Contains(constr, "datacenter") {
dcs = append(dcs, strings.Trim(strings.Split(constr, "==")[1], " "))
}
}

return dcs
}

// default datacenter
return []string{"dc1"}
return []string{defaultDC}
}

func createEnvVars(r requests.CreateFunctionRequest) map[string]string {
Expand Down
6 changes: 3 additions & 3 deletions handlers/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func setupDeploy(body string) (http.HandlerFunc, *httptest.ResponseRecorder, *ht

logger := hclog.Default()

return MakeDeploy(mockJob, fntypes.ProviderConfig{VaultDefaultPolicy: "openfaas", VaultSecretPathPrefix: "secret/openfaas"}, logger, mockStats),
return MakeDeploy(mockJob, fntypes.ProviderConfig{VaultDefaultPolicy: "openfaas", VaultSecretPathPrefix: "secret/openfaas", Datacenter: "dc1"}, logger, mockStats),
httptest.NewRecorder(),
httptest.NewRequest("GET", "/system/functions", bytes.NewReader([]byte(body)))
}
Expand Down Expand Up @@ -80,7 +80,7 @@ func TestHandlerRegistersWithFunctionProcess(t *testing.T) {

func TestHandlesDataCentreLabelWithSingleDC(t *testing.T) {
fr := createRequest()
(*fr.Labels)["datacenters"] = "test"
fr.Constraints = []string{"datacenter == test"}

h, rw, r := setupDeploy(fr.String())

Expand All @@ -95,7 +95,7 @@ func TestHandlesDataCentreLabelWithSingleDC(t *testing.T) {

func TestHandlesDataCentreLabelWithMultipleDC(t *testing.T) {
fr := createRequest()
(*fr.Labels)["datacenters"] = "test1,test2"
fr.Constraints = []string{"datacenter == test1", "datacenter == test2"}

h, rw, r := setupDeploy(fr.String())

Expand Down
9 changes: 8 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var (
nodeURI = flag.String("node_addr", "localhost", "URI of the current Nomad node, this address is used for reporting and logging")
nomadAddr = flag.String("nomad_addr", "localhost:4646", "Address for Nomad API endpoint")
consulAddr = flag.String("consul_addr", "http://localhost:8500", "Address for Consul API endpoint")
consulACL = flag.String("consul_acl", "", "ACL token for Consul API, only required if ACL are enabled in Consul")
consulACL = flag.String("consul_acl", "", "ACL token for Consul API, only required if ACL are enabled in Consul")
nomadRegion = flag.String("nomad_region", "global", "Default region to schedule functions in")
enableBasicAuth = flag.Bool("enable_basic_auth", false, "Flag for enabling basic authentication on gateway endpoints")
basicAuthSecretPath = flag.String("basic_auth_secret_path", "/secrets", "The directory path to the basic auth secret file")
Expand Down Expand Up @@ -149,9 +149,16 @@ func main() {

func createFaaSHandlers(nomadClient *api.Client, consulResolver *consul.Resolver, stats *statsd.Client, logger hclog.Logger) *types.FaaSHandlers {

datacenter, err := nomadClient.Agent().Datacenter()
if err != nil {
datacenter = "dc1"
}
logger.Info("Datacenter from agent:" + datacenter)

providerConfig := &fntypes.ProviderConfig{
VaultDefaultPolicy: *vaultDefaultPolicy,
VaultSecretPathPrefix: *vaultSecretPathPrefix,
Datacenter: datacenter,
}

return &types.FaaSHandlers{
Expand Down
2 changes: 1 addition & 1 deletion nomad_job_files/faas.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ EOH
}

config {
image = "openfaas/gateway:0.8.12"
image = "openfaas/gateway:0.9.3"

port_map {
http = 8080
Expand Down
2 changes: 1 addition & 1 deletion provisioning/saltstack/salt/nomad/files/faas.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ job "faas-nomadd" {
destination = "secrets/gateway.env"
// point the functions_provider_url to the host running vagrant (assuming a go process listening on 0.0.0.0), always .1 on last octet
data = <<EOH
functions_provider_url="http://{{ host_address }}:8080/"
functions_provider_url="http://{{ host_address }}:8081/"
{% raw -%}
{{ range service "prometheus" }}
faas_prometheus_host="{{ .Address }}"
Expand Down
2 changes: 1 addition & 1 deletion types/provider_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package types
type ProviderConfig struct {
VaultDefaultPolicy string
VaultSecretPathPrefix string
ConsulACLToken string
Datacenter string
}

0 comments on commit 1937e19

Please sign in to comment.