Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

esgateway pggateway: Changes to parsed nodes and resolvers to PG gateway HAProxy And Make resolvers configurable #4486

Merged
merged 11 commits into from
Dec 22, 2020
17 changes: 16 additions & 1 deletion api/config/esgateway/config_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ func DefaultConfigRequest() *ConfigRequest {
// resolvers once we are a bit more confident in the nginx
// module we are using to do dynamic resolution.
//
// c.V1.Sys.Ngx.Main.Resolvers = getSystemResolvers()

c.V1.Sys.Ngx.Main.Resolvers.EnableSystemNameservers = w.Bool(false)
kalroy marked this conversation as resolved.
Show resolved Hide resolved
c.V1.Sys.Ngx.Main.Resolvers.Nameservers = nil
kalroy marked this conversation as resolved.
Show resolved Hide resolved
c.V1.Sys.Ngx.Events.WorkerConnections = w.Int32(1024)

c.V1.Sys.Ngx.Http.ServerNamesHashBucketSize = w.Int32(128)
Expand Down Expand Up @@ -89,6 +90,20 @@ func (c *ConfigRequest) Validate() error {
// to start the service.
func (c *ConfigRequest) PrepareSystemConfig(creds *ac.TLSCredentials) (ac.PreparedSystemConfig, error) {
c.V1.Sys.Tls = creds

enableSystemNameServer := c.V1.Sys.Ngx.Main.Resolvers.EnableSystemNameservers.GetValue()
kalroy marked this conversation as resolved.
Show resolved Hide resolved

if ((c.V1.Sys.Ngx.Main.Resolvers.Nameservers == nil) || (len(c.V1.Sys.Ngx.Main.Resolvers.Nameservers) == 0)) && enableSystemNameServer {
c.V1.Sys.Ngx.Main.Resolvers.NameserversString = getSystemResolvers()
} else {
if (c.V1.Sys.Ngx.Main.Resolvers.Nameservers != nil) && (len(c.V1.Sys.Ngx.Main.Resolvers.Nameservers) > 0) {
ns := make([]string, 0, len(c.V1.Sys.Ngx.Main.Resolvers.Nameservers))
for _, n := range c.V1.Sys.Ngx.Main.Resolvers.Nameservers {
ns = append(ns, n.GetValue())
kalroy marked this conversation as resolved.
Show resolved Hide resolved
}
c.V1.Sys.Ngx.Main.Resolvers.NameserversString = w.String(strings.Join(ns, " "))
}
}
return c.V1.Sys, nil
}

Expand Down
516 changes: 307 additions & 209 deletions api/config/esgateway/config_request.pb.go

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion api/config/esgateway/config_request.proto
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@ message ConfigRequest {
message Main {
google.protobuf.Int32Value worker_processes = 1;
google.protobuf.Int32Value max_fails = 2;
google.protobuf.StringValue resolvers = 3;
Resolvers resolvers = 3;

message Resolvers {
google.protobuf.BoolValue enable_system_nameservers = 1;
repeated google.protobuf.StringValue nameservers = 2;
google.protobuf.StringValue nameservers_string = 3;
}
}

message Events {
Expand Down
9 changes: 9 additions & 0 deletions api/config/esgateway/config_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

wrappers "github.com/golang/protobuf/ptypes/wrappers"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

ac "github.com/chef/automate/api/config/shared"
w "github.com/chef/automate/api/config/shared/wrappers"
Expand Down Expand Up @@ -267,4 +268,12 @@ func TestExternalElasticsearch(t *testing.T) {
assert.Equal(t, "443", c.GetV1().GetSys().GetExternal().GetParsedEndpoints()[0].GetPort().GetValue())
assert.Equal(t, false, c.GetV1().GetSys().GetExternal().GetParsedEndpoints()[0].GetIsDomain().GetValue())
})

t.Run("setting platformconfig for nameservers", func(t *testing.T) {
c := DefaultConfigRequest()
c.V1.Sys.Ngx.Main.Resolvers.Nameservers = []*wrappers.StringValue{w.String("111.11.11.11:50")}
c.PrepareSystemConfig(&ac.TLSCredentials{})

require.Equal(t, c.V1.Sys.Ngx.Main.Resolvers.NameserversString.GetValue(), "111.11.11.11:50", "does not match with the nameserver passed")
})
}
61 changes: 61 additions & 0 deletions api/config/pg_gateway/config_request.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
package pg_gateway

import (
"fmt"
"net"

ac "github.com/chef/automate/api/config/shared"
w "github.com/chef/automate/api/config/shared/wrappers"
"github.com/chef/automate/lib/config"
wrappers "github.com/golang/protobuf/ptypes/wrappers"
)

const (
defaultResolverPort = 52
defaultServerPort = "5432"
)

// NewConfigRequest returns a new instance of ConfigRequest with zero values.
Expand All @@ -14,6 +24,7 @@ func NewConfigRequest() *ConfigRequest {
Service: &ConfigRequest_V1_System_Service{},
Log: &ac.Log{},
Timeouts: &ConfigRequest_V1_System_Timeouts{},
Resolvers: &ConfigRequest_V1_System_Resolvers{},
},
},
}
Expand All @@ -28,6 +39,30 @@ func DefaultConfigRequest() *ConfigRequest {

c.V1.Sys.Timeouts.Connect = w.Int32(5)
c.V1.Sys.Timeouts.Idle = w.Int32(43200)
c.V1.Sys.Resolvers.EnableSystemNameservers = w.Bool(false)
c.V1.Sys.Resolvers.NameServers = nil

if externalPG := c.GetV1().GetSys().Service.GetExternalPostgresql(); externalPG.GetEnable().GetValue() {

nodes := externalPG.GetNodes()

if len(nodes) > 0 {
endpoints := make([]*ConfigRequest_V1_System_Endpoint, 0, len(nodes))

for _, node := range nodes {
host, port, err := net.SplitHostPort(node.GetValue())
if err != nil {
port = defaultServerPort
}

n := &ConfigRequest_V1_System_Endpoint{Address: w.String(host), Port: w.String(port)}
n.IsDomain = w.Bool(!isIPAddress(host))
endpoints = append(endpoints, n)
}

c.V1.Sys.Service.ParsedNodes = endpoints
}
}
return c
}

Expand All @@ -50,5 +85,31 @@ func (c *ConfigRequest) SetGlobalConfig(g *ac.GlobalConfig) {
// to start the service.
func (c *ConfigRequest) PrepareSystemConfig(creds *ac.TLSCredentials) (ac.PreparedSystemConfig, error) {
c.V1.Sys.Tls = creds

enableSystemNameServer := c.V1.Sys.Resolvers.EnableSystemNameservers.GetValue()

if ((c.V1.Sys.Resolvers.NameServers == nil) || (len(c.V1.Sys.Resolvers.NameServers) == 0)) && enableSystemNameServer {
c.V1.Sys.Resolvers.NameServers = getSystemResolvers()
}

return c.V1.Sys, nil
}

func getSystemResolvers() []*wrappers.StringValue {
ns := config.GetSystemResolvers()
resolvers := make([]*wrappers.StringValue, 0, len(ns))

for _, n := range ns {
_, _, err := net.SplitHostPort(n)
if err == nil {
resolvers = append(resolvers, w.String(n))
} else {
resolvers = append(resolvers, w.String(fmt.Sprintf("%s:%s", n, defaultServerPort)))
}
}
return resolvers
}

func isIPAddress(addr string) bool {
return net.ParseIP(addr) != nil
}
Loading