Skip to content

Commit b914a43

Browse files
Code mantainance (#647)
* func: remove consul * func: remove loop * func: remove duplicated call to status * func: remove consul from configuration and tests * Update agent/agent_test.go Co-authored-by: Luiz Aoqui <[email protected]> * fix: move the error check from the monitor up, to avoid infinite loops * style: add comment * func: simplify the calls to plugins * fix: update test * style: remove unused code * style: wrap get statu on metrics * Update policy/handler.go Co-authored-by: Luiz Aoqui <[email protected]> * Update policy/handler.go Co-authored-by: Luiz Aoqui <[email protected]> * Update policy/manager.go Co-authored-by: Luiz Aoqui <[email protected]> * Update policyeval/base_worker.go Co-authored-by: Luiz Aoqui <[email protected]> * Update policyeval/base_worker.go Co-authored-by: Luiz Aoqui <[email protected]> * style: document channels * style: remove unsued target status from e valuation --------- Co-authored-by: Luiz Aoqui <[email protected]>
1 parent 516536a commit b914a43

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+171
-505
lines changed

.gitignore

+4-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,7 @@ plugins/test/noop-*/noop-*
1818
# demo configs
1919
example.nomad.hcl
2020
spec.hcl
21-
config.hcl
21+
config.hcl
22+
23+
# IDE config files
24+
.vscode/

agent/agent.go

-34
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"syscall"
1212

1313
metrics "github.com/armon/go-metrics"
14-
consulapi "github.com/hashicorp/consul/api"
1514
"github.com/hashicorp/go-hclog"
1615
"github.com/hashicorp/nomad-autoscaler/agent/config"
1716
"github.com/hashicorp/nomad-autoscaler/plugins/manager"
@@ -29,7 +28,6 @@ type Agent struct {
2928
config *config.Agent
3029
configPaths []string
3130
nomadClient *api.Client
32-
consulClient *consulapi.Client
3331
pluginManager *manager.PluginManager
3432
policySources map[policy.SourceName]policy.Source
3533
policyManager *policy.Manager
@@ -63,11 +61,6 @@ func (a *Agent) Run() error {
6361
return err
6462
}
6563

66-
// Generate the Consul client
67-
if err := a.generateConsulClient(); err != nil {
68-
return err
69-
}
70-
7164
// launch plugins
7265
if err := a.setupPlugins(); err != nil {
7366
return fmt.Errorf("failed to setup plugins: %v", err)
@@ -199,28 +192,6 @@ func (a *Agent) generateNomadClient() error {
199192
return nil
200193
}
201194

202-
// generateConsulClient creates a Consul client for use within the agent.
203-
func (a *Agent) generateConsulClient() error {
204-
if a.config.Consul == nil {
205-
a.consulClient = nil
206-
return nil
207-
}
208-
209-
cfg, err := a.config.Consul.MergeWithDefault()
210-
if err != nil {
211-
return fmt.Errorf("error generating Consul client config: %v", err)
212-
}
213-
214-
// Generate the Consul client.
215-
client, err := consulapi.NewClient(cfg)
216-
if err != nil {
217-
return fmt.Errorf("failed to instantiate Consul client: %v", err)
218-
}
219-
a.consulClient = client
220-
221-
return nil
222-
}
223-
224195
// reload triggers the reload of sub-routines based on the operator sending a
225196
// SIGHUP signal to the agent.
226197
func (a *Agent) reload() {
@@ -243,11 +214,6 @@ func (a *Agent) reload() {
243214
os.Exit(1)
244215
}
245216

246-
if err := a.generateConsulClient(); err != nil {
247-
a.logger.Error("failed to reload Autoscaler configuration", "error", err)
248-
os.Exit(1)
249-
}
250-
251217
a.logger.Debug("reloading policy sources")
252218
// Set new Nomad client in the Nomad policy source.
253219
ps, ok := a.policySources[policy.SourceNameNomad]

agent/agent_test.go

+1-25
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@ import (
77
"errors"
88
"testing"
99

10-
"github.com/hashicorp/nomad-autoscaler/agent/config"
1110
"github.com/hashicorp/nomad/api"
1211
"github.com/stretchr/testify/assert"
13-
"github.com/stretchr/testify/require"
1412
)
1513

1614
func TestAgent_generateNomadClient(t *testing.T) {
@@ -33,7 +31,7 @@ func TestAgent_generateNomadClient(t *testing.T) {
3331
},
3432
},
3533
expectedOutputEr: errors.New(`failed to instantiate Nomad client: invalid address ' ': parse "\t": net/url: invalid control character in URL`),
36-
name: "invalid input Nomad address", //nolint
34+
name: "invalid input Nomad address",
3735
},
3836
}
3937

@@ -45,25 +43,3 @@ func TestAgent_generateNomadClient(t *testing.T) {
4543
}
4644
}
4745
}
48-
49-
func TestAgent_generateConsulClient(t *testing.T) {
50-
require := require.New(t)
51-
agent := Agent{
52-
config: &config.Agent{
53-
Consul: nil,
54-
},
55-
}
56-
57-
// nil config => no consul client
58-
require.NoError(agent.generateConsulClient())
59-
require.Nil(agent.consulClient, "client should be nil because config was nil")
60-
61-
// empty config => default consul client
62-
agent.config.Consul = &config.Consul{}
63-
require.NoError(agent.generateConsulClient())
64-
require.NotNil(agent.consulClient)
65-
66-
// explicit config overrides defaults, can throw errors
67-
agent.config.Consul.Addr = "bad://1234"
68-
require.EqualError(agent.generateConsulClient(), "failed to instantiate Consul client: Unknown protocol scheme: bad")
69-
}

agent/config/config.go

+1-172
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,11 @@ package config
66
import (
77
"errors"
88
"fmt"
9-
"net/http"
109
"os"
1110
"path/filepath"
1211
"sort"
13-
"strings"
1412
"time"
1513

16-
consulapi "github.com/hashicorp/consul/api"
1714
"github.com/hashicorp/go-multierror"
1815
"github.com/hashicorp/hcl/v2/hclsimple"
1916
"github.com/hashicorp/nomad-autoscaler/plugins"
@@ -66,9 +63,6 @@ type Agent struct {
6663
// Telemetry is the configuration used to setup metrics collection.
6764
Telemetry *Telemetry `hcl:"telemetry,block"`
6865

69-
// Consul is used to configure a consul API client
70-
Consul *Consul `hcl:"consul,block"`
71-
7266
APMs []*Plugin `hcl:"apm,block"`
7367
Targets []*Plugin `hcl:"target,block"`
7468
Strategies []*Plugin `hcl:"strategy,block"`
@@ -160,166 +154,6 @@ type Nomad struct {
160154
SkipVerify bool `hcl:"skip_verify,optional"`
161155
}
162156

163-
// Consul contains the configuration information necessary to
164-
// communicate with a Consul server.
165-
type Consul struct {
166-
// Addr is the HTTP endpoint address of the local Consul agent
167-
//
168-
// Uses Consul's default and env var.
169-
Addr string `hcl:"address"`
170-
171-
// TimeoutHCL is used by Consul HTTP Client
172-
TimeoutHCL string `hcl:"timeout" json:"-"`
173-
174-
// Token is used to provide a per-request ACL token. This options overrides
175-
// the agent's default token
176-
Token string `hcl:"token"`
177-
178-
// Auth is the information to use for http access to Consul agent
179-
Auth string `hcl:"auth"`
180-
181-
// EnableSSL sets the transport scheme to talk to the Consul agent as https
182-
//
183-
// Uses Consul's default and env var.
184-
EnableSSL *bool `hcl:"ssl"`
185-
186-
// VerifySSL enables or disables SSL verification when the transport scheme
187-
// for the consul api client is https
188-
//
189-
// Uses Consul's default and env var.
190-
VerifySSL *bool `hcl:"verify_ssl"`
191-
192-
// CAFile is the path to the ca certificate used for Consul communication.
193-
//
194-
// Uses Consul's default and env var.
195-
CAFile string `hcl:"ca_file"`
196-
197-
// CertFile is the path to the certificate for Consul communication
198-
CertFile string `hcl:"cert_file"`
199-
200-
// KeyFile is the path to the private key for Consul communication
201-
KeyFile string `hcl:"key_file"`
202-
203-
// Namespace sets the Consul namespace used for all calls against the
204-
// Consul API. If this is unset, then we don't specify a consul namespace.
205-
Namespace string `hcl:"namespace"`
206-
207-
// Datacenter sets the Consul datacenter used for all calls against the
208-
// Consul API. If this is unset, then we don't specify a consul datacenter.
209-
Datacenter string `hcl:"datacenter"`
210-
}
211-
212-
func (c *Consul) MergeWithDefault() (*consulapi.Config, error) {
213-
if c == nil {
214-
return nil, nil
215-
}
216-
217-
cfg := consulapi.DefaultConfig()
218-
219-
if c.Addr != "" {
220-
cfg.Address = c.Addr
221-
}
222-
if c.Token != "" {
223-
cfg.Token = c.Token
224-
}
225-
if c.TimeoutHCL != "" {
226-
d, err := time.ParseDuration(c.TimeoutHCL)
227-
if err != nil {
228-
return nil, fmt.Errorf("consul->timeout can't parse time duration %s", c.TimeoutHCL)
229-
}
230-
// Create a custom Client to set the timeout
231-
if cfg.HttpClient == nil {
232-
cfg.HttpClient = &http.Client{}
233-
}
234-
cfg.HttpClient.Timeout = d
235-
cfg.HttpClient.Transport = cfg.Transport
236-
}
237-
if c.Auth != "" {
238-
var username, password string
239-
if strings.Contains(c.Auth, ":") {
240-
split := strings.SplitN(c.Auth, ":", 2)
241-
username = split[0]
242-
password = split[1]
243-
} else {
244-
username = c.Auth
245-
}
246-
247-
cfg.HttpAuth = &consulapi.HttpBasicAuth{
248-
Username: username,
249-
Password: password,
250-
}
251-
}
252-
if c.EnableSSL != nil && *c.EnableSSL {
253-
cfg.Scheme = "https"
254-
cfg.TLSConfig = consulapi.TLSConfig{
255-
Address: cfg.Address,
256-
CAFile: c.CAFile,
257-
CertFile: c.CertFile,
258-
KeyFile: c.KeyFile,
259-
}
260-
if c.VerifySSL != nil {
261-
cfg.TLSConfig.InsecureSkipVerify = !*c.VerifySSL
262-
}
263-
tlsConfig, err := consulapi.SetupTLSConfig(&cfg.TLSConfig)
264-
if err != nil {
265-
return nil, err
266-
}
267-
cfg.Transport.TLSClientConfig = tlsConfig
268-
}
269-
if c.Namespace != "" {
270-
cfg.Namespace = c.Namespace
271-
}
272-
if c.Datacenter != "" {
273-
cfg.Datacenter = c.Datacenter
274-
}
275-
276-
return cfg, nil
277-
}
278-
279-
func (c *Consul) merge(b *Consul) *Consul {
280-
if c == nil {
281-
return b
282-
}
283-
284-
result := *c
285-
286-
if b.Addr != "" {
287-
result.Addr = b.Addr
288-
}
289-
if b.TimeoutHCL != "" {
290-
result.TimeoutHCL = b.TimeoutHCL
291-
}
292-
if b.Datacenter != "" {
293-
result.Datacenter = b.Datacenter
294-
}
295-
if b.Namespace != "" {
296-
result.Namespace = b.Namespace
297-
}
298-
if b.Token != "" {
299-
result.Token = b.Token
300-
}
301-
if b.Auth != "" {
302-
result.Auth = b.Auth
303-
}
304-
if b.CAFile != "" {
305-
result.CAFile = b.CAFile
306-
}
307-
if b.CertFile != "" {
308-
result.CertFile = b.CertFile
309-
}
310-
if b.KeyFile != "" {
311-
result.KeyFile = b.KeyFile
312-
}
313-
if b.EnableSSL != nil {
314-
result.EnableSSL = b.EnableSSL
315-
}
316-
if b.VerifySSL != nil {
317-
result.VerifySSL = b.VerifySSL
318-
}
319-
320-
return &result
321-
}
322-
323157
// Telemetry holds the user specified configuration for metrics collection.
324158
type Telemetry struct {
325159

@@ -557,8 +391,7 @@ func Default() (*Agent, error) {
557391
BindAddress: defaultHTTPBindAddress,
558392
BindPort: defaultHTTPBindPort,
559393
},
560-
Consul: nil,
561-
Nomad: &Nomad{},
394+
Nomad: &Nomad{},
562395
Telemetry: &Telemetry{
563396
CollectionInterval: defaultTelemetryCollectionInterval,
564397
},
@@ -623,10 +456,6 @@ func (a *Agent) Merge(b *Agent) *Agent {
623456
result.Nomad = result.Nomad.merge(b.Nomad)
624457
}
625458

626-
if b.Consul != nil {
627-
result.Consul = result.Consul.merge(b.Consul)
628-
}
629-
630459
if b.Telemetry != nil {
631460
result.Telemetry = result.Telemetry.merge(b.Telemetry)
632461
}

agent/config/config_test.go

-3
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ func TestAgent_Merge(t *testing.T) {
9494
HTTP: &HTTP{
9595
BindPort: 4646,
9696
},
97-
Consul: &Consul{},
9897
Nomad: &Nomad{
9998
Address: "https://nomad-new.systems:4646",
10099
Region: "moon-base-1",
@@ -194,7 +193,6 @@ func TestAgent_Merge(t *testing.T) {
194193
BindAddress: "scaler.nomad",
195194
BindPort: 4646,
196195
},
197-
Consul: &Consul{},
198196
Nomad: &Nomad{
199197
Address: "https://nomad-new.systems:4646",
200198
Region: "moon-base-1",
@@ -314,7 +312,6 @@ func TestAgent_Merge(t *testing.T) {
314312
assert.Equal(t, expectedResult.LogJson, actualResult.LogJson)
315313
assert.Equal(t, expectedResult.LogLevel, actualResult.LogLevel)
316314
assert.Equal(t, expectedResult.Nomad, actualResult.Nomad)
317-
assert.Equal(t, expectedResult.Consul, actualResult.Consul)
318315
assert.Equal(t, expectedResult.PluginDir, actualResult.PluginDir)
319316
assert.Equal(t, expectedResult.Policy, actualResult.Policy)
320317
assert.Equal(t, expectedResult.PolicyEval, actualResult.PolicyEval)

0 commit comments

Comments
 (0)