Skip to content

Commit

Permalink
Added dry run flag to agentctl config-sync cmd (#68)
Browse files Browse the repository at this point in the history
* Added dry run flag to agentctl config-sync cmd

* Simplify test, replace print with logger
  • Loading branch information
hoenn authored May 12, 2020
1 parent 2bdb842 commit 0eff751
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
4 changes: 3 additions & 1 deletion cmd/agentctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func main() {
func configSyncCmd() *cobra.Command {
var (
agentAddr string
dryRun bool
)

cmd := &cobra.Command{
Expand All @@ -55,7 +56,7 @@ source-of-truth directory.`,

logger := log.NewLogfmtLogger(log.NewSyncWriter(os.Stdout))

err := agentctl.ConfigSync(logger, cli.PrometheusClient, directory)
err := agentctl.ConfigSync(logger, cli.PrometheusClient, directory, dryRun)
if err != nil {
level.Error(logger).Log("msg", "failed to sync config", "err", err)
os.Exit(1)
Expand All @@ -64,6 +65,7 @@ source-of-truth directory.`,
}

cmd.Flags().StringVarP(&agentAddr, "addr", "a", "http://localhost:12345", "address of the agent to connect to")
cmd.Flags().BoolVarP(&dryRun, "dry-run", "d", false, "use the dry run option to validate config files without attempting to upload")
must(cmd.MarkFlagRequired("addr"))
return cmd
}
Expand Down
17 changes: 11 additions & 6 deletions pkg/agentctl/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,22 @@ import (
// ConfigSync will completely overwrite the set of active configs
// present in the provided PrometheusClient - configs present in the
// API but not in the directory will be deleted.
func ConfigSync(logger log.Logger, cli client.PrometheusClient, dir string) error {
func ConfigSync(logger log.Logger, cli client.PrometheusClient, dir string, dryRun bool) error {
if logger == nil {
logger = log.NewNopLogger()
}

ctx := context.Background()
existing, err := cli.ListConfigs(ctx)
if err != nil {
return fmt.Errorf("could not list configs: %w", err)
}

cfgs, err := ConfigsFromDirectory(dir)
if err != nil {
return err
}

if dryRun {
level.Info(logger).Log("msg", "config files validated successfully")
return nil
}

uploaded := make(map[string]struct{}, len(cfgs))
var hadErrors bool

Expand All @@ -54,6 +54,11 @@ func ConfigSync(logger log.Logger, cli client.PrometheusClient, dir string) erro
uploaded[cfg.Name] = struct{}{}
}

existing, err := cli.ListConfigs(ctx)
if err != nil {
return fmt.Errorf("could not list configs: %w", err)
}

// Delete configs from the existing API list that we didn't upload.
for _, existing := range existing.Configs {
if _, existsLocally := uploaded[existing]; !existsLocally {
Expand Down
26 changes: 24 additions & 2 deletions pkg/agentctl/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestConfigSync_EmptyStore(t *testing.T) {
return nil
}

err := ConfigSync(nil, cli, "./testdata")
err := ConfigSync(nil, cli, "./testdata", false)
require.NoError(t, err)

expect := []string{
Expand Down Expand Up @@ -53,7 +53,7 @@ func TestConfigSync_PrepopulatedStore(t *testing.T) {
return nil
}

err := ConfigSync(nil, cli, "./testdata")
err := ConfigSync(nil, cli, "./testdata", false)
require.NoError(t, err)

expectUpdated := []string{
Expand All @@ -71,6 +71,28 @@ func TestConfigSync_PrepopulatedStore(t *testing.T) {
require.Equal(t, expectDeleted, deletedConfigs)
}

func TestConfigSync_DryRun(t *testing.T) {
cli := &mockFuncPromClient{}
cli.ListConfigsFunc = func(_ context.Context) (*configapi.ListConfigurationsResponse, error) {
return &configapi.ListConfigurationsResponse{
Configs: []string{"delete-a", "agent-1", "delete-b", "delete-c"},
}, nil
}

cli.PutConfigurationFunc = func(_ context.Context, name string, _ *instance.Config) error {
t.FailNow()
return nil
}

cli.DeleteConfigurationFunc = func(_ context.Context, name string) error {
t.FailNow()
return nil
}

err := ConfigSync(nil, cli, "./testdata", true)
require.NoError(t, err)
}

type mockFuncPromClient struct {
InstancesFunc func(ctx context.Context) ([]string, error)
ListConfigsFunc func(ctx context.Context) (*configapi.ListConfigurationsResponse, error)
Expand Down

0 comments on commit 0eff751

Please sign in to comment.