Skip to content

Commit

Permalink
Merge pull request #2159 from hashicorp/b-consul-config
Browse files Browse the repository at this point in the history
Fixed merging consul config
  • Loading branch information
diptanu authored Jan 19, 2017
2 parents 6a431b4 + 6d669fb commit 49e6735
Show file tree
Hide file tree
Showing 13 changed files with 255 additions and 234 deletions.
2 changes: 1 addition & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ func NewClient(cfg *config.Config, consulSyncer *consul.Syncer, logger *log.Logg
c.configLock.RUnlock()

// Setup Consul discovery if enabled
if c.configCopy.ConsulConfig.ClientAutoJoin {
if c.configCopy.ConsulConfig.ClientAutoJoin != nil && *c.configCopy.ConsulConfig.ClientAutoJoin {
go c.consulDiscovery()
if len(c.servers.all()) == 0 {
// No configured servers; trigger discovery manually
Expand Down
7 changes: 4 additions & 3 deletions client/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"time"

"github.com/hashicorp/nomad/helper"
"github.com/hashicorp/nomad/helper/tlsutil"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/hashicorp/nomad/nomad/structs/config"
Expand Down Expand Up @@ -158,9 +159,9 @@ func (c *Config) Copy() *Config {
nc := new(Config)
*nc = *c
nc.Node = nc.Node.Copy()
nc.Servers = structs.CopySliceString(nc.Servers)
nc.Options = structs.CopyMapStringString(nc.Options)
nc.GloballyReservedPorts = structs.CopySliceInt(c.GloballyReservedPorts)
nc.Servers = helper.CopySliceString(nc.Servers)
nc.Options = helper.CopyMapStringString(nc.Options)
nc.GloballyReservedPorts = helper.CopySliceInt(c.GloballyReservedPorts)
nc.ConsulConfig = c.ConsulConfig.Copy()
nc.VaultConfig = c.VaultConfig.Copy()
return nc
Expand Down
4 changes: 2 additions & 2 deletions client/consul_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,10 +420,10 @@ func runnerConfig(config *config.Config, vaultToken string) (*ctconf.Config, err
conf.Token = config.ConsulConfig.Token
set([]string{"consul", "token"})

if config.ConsulConfig.EnableSSL {
if config.ConsulConfig.EnableSSL != nil && *config.ConsulConfig.EnableSSL {
conf.SSL = &ctconf.SSLConfig{
Enabled: true,
Verify: config.ConsulConfig.VerifySSL,
Verify: *config.ConsulConfig.VerifySSL,
Cert: config.ConsulConfig.CertFile,
Key: config.ConsulConfig.KeyFile,
CaCert: config.ConsulConfig.CAFile,
Expand Down
12 changes: 6 additions & 6 deletions command/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func (a *Agent) serverConfig() (*nomad.Config, error) {
conf.HeartbeatGrace = dur
}

if a.config.Consul.AutoAdvertise && a.config.Consul.ServerServiceName == "" {
if *a.config.Consul.AutoAdvertise && a.config.Consul.ServerServiceName == "" {
return nil, fmt.Errorf("server_service_name must be set when auto_advertise is enabled")
}

Expand Down Expand Up @@ -274,7 +274,7 @@ func (a *Agent) clientConfig() (*clientconfig.Config, error) {
conf.Version = fmt.Sprintf("%s%s", a.config.Version, a.config.VersionPrerelease)
conf.Revision = a.config.Revision

if a.config.Consul.AutoAdvertise && a.config.Consul.ClientServiceName == "" {
if *a.config.Consul.AutoAdvertise && a.config.Consul.ClientServiceName == "" {
return nil, fmt.Errorf("client_service_name must be set when auto_advertise is enabled")
}

Expand Down Expand Up @@ -319,15 +319,15 @@ func (a *Agent) setupServer() error {
httpCheckAddr := a.config.normalizedAddrs.HTTP
rpcCheckAddr := a.config.normalizedAddrs.RPC
serfCheckAddr := a.config.normalizedAddrs.Serf
if a.config.Consul.ChecksUseAdvertise {
if *a.config.Consul.ChecksUseAdvertise {
httpCheckAddr = a.config.AdvertiseAddrs.HTTP
rpcCheckAddr = a.config.AdvertiseAddrs.RPC
serfCheckAddr = a.config.AdvertiseAddrs.Serf
}

// Create the Nomad Server services for Consul
// TODO re-introduce HTTP/S checks when Consul 0.7.1 comes out
if a.config.Consul.AutoAdvertise {
if *a.config.Consul.AutoAdvertise {
httpServ := &structs.Service{
Name: a.config.Consul.ServerServiceName,
PortLabel: a.config.AdvertiseAddrs.HTTP,
Expand Down Expand Up @@ -440,14 +440,14 @@ func (a *Agent) setupClient() error {

// Resolve the http check address
httpCheckAddr := a.config.normalizedAddrs.HTTP
if a.config.Consul.ChecksUseAdvertise {
if *a.config.Consul.ChecksUseAdvertise {
httpCheckAddr = a.config.AdvertiseAddrs.HTTP
}

// Create the Nomad Client services for Consul
// TODO think how we can re-introduce HTTP/S checks when Consul 0.7.1 comes
// out
if a.config.Consul.AutoAdvertise {
if *a.config.Consul.AutoAdvertise {
httpServ := &structs.Service{
Name: a.config.Consul.ClientServiceName,
PortLabel: a.config.AdvertiseAddrs.HTTP,
Expand Down
3 changes: 2 additions & 1 deletion command/agent/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"time"

client "github.com/hashicorp/nomad/client/config"
"github.com/hashicorp/nomad/helper"
"github.com/hashicorp/nomad/nomad"
"github.com/hashicorp/nomad/nomad/structs/config"
)
Expand Down Expand Up @@ -452,7 +453,7 @@ func DevConfig() *Config {
conf.DevMode = true
conf.EnableDebug = true
conf.DisableAnonymousSignature = true
conf.Consul.AutoAdvertise = true
conf.Consul.AutoAdvertise = helper.BoolToPtr(true)
if runtime.GOOS == "darwin" {
conf.Client.NetworkInterface = "lo0"
} else if runtime.GOOS == "linux" {
Expand Down
141 changes: 141 additions & 0 deletions helper/funcs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package helper

// boolToPtr returns the pointer to a boolean
func BoolToPtr(b bool) *bool {
return &b
}

// MapStringStringSliceValueSet returns the set of values in a map[string][]string
func MapStringStringSliceValueSet(m map[string][]string) []string {
set := make(map[string]struct{})
for _, slice := range m {
for _, v := range slice {
set[v] = struct{}{}
}
}

flat := make([]string, 0, len(set))
for k := range set {
flat = append(flat, k)
}
return flat
}

func SliceStringToSet(s []string) map[string]struct{} {
m := make(map[string]struct{}, (len(s)+1)/2)
for _, k := range s {
m[k] = struct{}{}
}
return m
}

// SliceStringIsSubset returns whether the smaller set of strings is a subset of
// the larger. If the smaller slice is not a subset, the offending elements are
// returned.
func SliceStringIsSubset(larger, smaller []string) (bool, []string) {
largerSet := make(map[string]struct{}, len(larger))
for _, l := range larger {
largerSet[l] = struct{}{}
}

subset := true
var offending []string
for _, s := range smaller {
if _, ok := largerSet[s]; !ok {
subset = false
offending = append(offending, s)
}
}

return subset, offending
}

func SliceSetDisjoint(first, second []string) (bool, []string) {
contained := make(map[string]struct{}, len(first))
for _, k := range first {
contained[k] = struct{}{}
}

offending := make(map[string]struct{})
for _, k := range second {
if _, ok := contained[k]; ok {
offending[k] = struct{}{}
}
}

if len(offending) == 0 {
return true, nil
}

flattened := make([]string, 0, len(offending))
for k := range offending {
flattened = append(flattened, k)
}
return false, flattened
}

// Helpers for copying generic structures.
func CopyMapStringString(m map[string]string) map[string]string {
l := len(m)
if l == 0 {
return nil
}

c := make(map[string]string, l)
for k, v := range m {
c[k] = v
}
return c
}

func CopyMapStringInt(m map[string]int) map[string]int {
l := len(m)
if l == 0 {
return nil
}

c := make(map[string]int, l)
for k, v := range m {
c[k] = v
}
return c
}

func CopyMapStringFloat64(m map[string]float64) map[string]float64 {
l := len(m)
if l == 0 {
return nil
}

c := make(map[string]float64, l)
for k, v := range m {
c[k] = v
}
return c
}

func CopySliceString(s []string) []string {
l := len(s)
if l == 0 {
return nil
}

c := make([]string, l)
for i, v := range s {
c[i] = v
}
return c
}

func CopySliceInt(s []int) []int {
l := len(s)
if l == 0 {
return nil
}

c := make([]int, l)
for i, v := range s {
c[i] = v
}
return c
}
37 changes: 37 additions & 0 deletions helper/funcs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package helper

import (
"reflect"
"sort"
"testing"
)

func TestSliceStringIsSubset(t *testing.T) {
l := []string{"a", "b", "c"}
s := []string{"d"}

sub, offending := SliceStringIsSubset(l, l[:1])
if !sub || len(offending) != 0 {
t.Fatalf("bad %v %v", sub, offending)
}

sub, offending = SliceStringIsSubset(l, s)
if sub || len(offending) == 0 || offending[0] != "d" {
t.Fatalf("bad %v %v", sub, offending)
}
}

func TestMapStringStringSliceValueSet(t *testing.T) {
m := map[string][]string{
"foo": []string{"1", "2"},
"bar": []string{"3"},
"baz": nil,
}

act := MapStringStringSliceValueSet(m)
exp := []string{"1", "2", "3"}
sort.Strings(act)
if !reflect.DeepEqual(act, exp) {
t.Fatalf("Bad; got %v; want %v", act, exp)
}
}
9 changes: 5 additions & 4 deletions nomad/job_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/hashicorp/go-memdb"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/nomad/client/driver"
"github.com/hashicorp/nomad/helper"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/hashicorp/nomad/nomad/watch"
"github.com/hashicorp/nomad/scheduler"
Expand Down Expand Up @@ -116,7 +117,7 @@ func (j *Job) Register(args *structs.JobRegisterRequest, reply *structs.JobRegis
// If we are given a root token it can access all policies
if !lib.StrContains(allowedPolicies, "root") {
flatPolicies := structs.VaultPoliciesSet(policies)
subset, offending := structs.SliceStringIsSubset(allowedPolicies, flatPolicies)
subset, offending := helper.SliceStringIsSubset(allowedPolicies, flatPolicies)
if !subset {
return fmt.Errorf("Passed Vault Token doesn't allow access to the following policies: %s",
strings.Join(offending, ", "))
Expand Down Expand Up @@ -218,7 +219,7 @@ func setImplicitConstraints(j *structs.Job) {
}

// Flatten the signals
required := structs.MapStringStringSliceValueSet(tgSignals)
required := helper.MapStringStringSliceValueSet(tgSignals)
sigConstraint := getSignalConstraint(required)

found := false
Expand Down Expand Up @@ -899,8 +900,8 @@ func validateDispatchRequest(req *structs.JobDispatchRequest, job *structs.Job)
keys[k] = struct{}{}
}

required := structs.SliceStringToSet(job.Constructor.MetaRequired)
optional := structs.SliceStringToSet(job.Constructor.MetaOptional)
required := helper.SliceStringToSet(job.Constructor.MetaRequired)
optional := helper.SliceStringToSet(job.Constructor.MetaOptional)

// Check the metadata key constraints are met
unpermitted := make(map[string]struct{})
Expand Down
2 changes: 1 addition & 1 deletion nomad/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ func (s *Server) setupBootstrapHandler() error {
// setupConsulSyncer creates Server-mode consul.Syncer which periodically
// executes callbacks on a fixed interval.
func (s *Server) setupConsulSyncer() error {
if s.config.ConsulConfig.ServerAutoJoin {
if s.config.ConsulConfig.ServerAutoJoin != nil && *s.config.ConsulConfig.ServerAutoJoin {
if err := s.setupBootstrapHandler(); err != nil {
return err
}
Expand Down
Loading

0 comments on commit 49e6735

Please sign in to comment.