Skip to content

Commit

Permalink
Merge pull request #135 from digitalocean/agandhi/config-updates
Browse files Browse the repository at this point in the history
config: cleanup, add env var support
  • Loading branch information
anitgandhi authored Dec 11, 2024
2 parents 18b16e6 + db7240b commit a4a7514
Show file tree
Hide file tree
Showing 21 changed files with 959 additions and 46 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

## [Unreleased](https://github.com/digitalocean/droplet-agent/tree/HEAD)

## [1.2.9](https://github.com/digitalocean/droplet-agent/tree/1.2.9) (2024-12-11)
### Updated
- config: include version in user agent
- config: simplify backgroundJobInterval
- config: export and use Version const directly
- config: add support for environment variables

### Related PRs
- config: cleanup, add env var support [\#135](https://github.com/digitalocean/droplet-agent/pull/135)

## [1.2.8](https://github.com/digitalocean/droplet-agent/tree/1.2.8) (2024-06-14)
### Updated
- Properly close the response body when requesting metadata service to avoid memory leak
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ endif
internal/config/version.go:
$(print)
@printf "version = v%s" $(VERSION)
@printf "// SPDX-License-Identifier: Apache-2.0\n\npackage config\n\nconst version = \"v%s\"\n" $(VERSION) > $@
@printf "// SPDX-License-Identifier: Apache-2.0\n\npackage config\n\n// Version is the current package version.\nconst Version = \"v%s\"\n" $(VERSION) > $@

.PHONY: target/VERSION internal/config/version.go
target/VERSION:
Expand Down
2 changes: 1 addition & 1 deletion cmd/agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func main() {
log.Info("Launching %s", config.AppFullName)
cfg := config.Init()

log.Info("Config Loaded. Agent Starting (version:%s)", cfg.Version)
log.Info("Config Loaded. Agent Starting (version:%s)", config.Version)

if cfg.DebugMode {
log.EnableDebug()
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.22
require (
github.com/fsnotify/fsnotify v1.7.0
github.com/opencontainers/selinux v1.11.0
github.com/peterbourgon/ff/v3 v3.4.0
go.uber.org/mock v0.4.0
golang.org/x/crypto v0.28.0
golang.org/x/net v0.30.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nos
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/opencontainers/selinux v1.11.0 h1:+5Zbo97w3Lbmb3PeqQtpmTkMwsW5nRI3YaLpt7tQ7oU=
github.com/opencontainers/selinux v1.11.0/go.mod h1:E5dMC3VPuVvVHDYmi78qvhJp8+M586T4DlDRYpFkyec=
github.com/peterbourgon/ff/v3 v3.4.0 h1:QBvM/rizZM1cB0p0lGMdmR7HxZeI/ZrBWB4DqLkMUBc=
github.com/peterbourgon/ff/v3 v3.4.0/go.mod h1:zjJVUhx+twciwfDl0zBcFzl4dW8axCRyXE/eKY9RztQ=
go.uber.org/mock v0.4.0 h1:VcM4ZOtdbR4f6VXfiOpwpVJDL6lCReaZ6mw31wqh7KU=
go.uber.org/mock v0.4.0/go.mod h1:a6FSlNadKUHUa9IP5Vyt1zh4fC7uAwxMutEAscFbkZc=
golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw=
Expand Down
25 changes: 0 additions & 25 deletions internal/config/cliargs.go

This file was deleted.

45 changes: 29 additions & 16 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,50 @@

package config

import "time"
import (
"flag"
"os"
"time"

"github.com/peterbourgon/ff/v3"
)

const (
AppFullName = "DigitalOcean Droplet Agent (code name: DOTTY)"
AppShortName = "Droplet Agent"
AppDebugAddr = "127.0.0.1:304"
)

const (
backgroundJobIntervalSeconds = 120
UserAgent = "Droplet-Agent/" + Version

backgroundJobInterval = 120 * time.Second
)

// Conf contains the configurations needed to run the agent
type Conf struct {
Version string
UseSyslog bool
DebugMode bool
AuthorizedKeysCheckInterval time.Duration
UseSyslog bool
DebugMode bool

CustomSSHDPort int
CustomSSHDCfgFile string
AuthorizedKeysCheckInterval time.Duration
}

// Init initializes the agent's configuration
func Init() *Conf {
cliArgs := parseCLIArgs()
return &Conf{
Version: version,
UseSyslog: cliArgs.useSyslog,
DebugMode: cliArgs.debugMode,
CustomSSHDPort: cliArgs.sshdPort,
CustomSSHDCfgFile: cliArgs.sshdCfgFile,
AuthorizedKeysCheckInterval: backgroundJobIntervalSeconds * time.Second,
cfg := Conf{
AuthorizedKeysCheckInterval: backgroundJobInterval,
}

fs := flag.NewFlagSet("droplet-agent", flag.ExitOnError)

fs.BoolVar(&cfg.UseSyslog, "syslog", false, "Use syslog service for logging")
fs.BoolVar(&cfg.DebugMode, "debug", false, "Turn on debug mode")
fs.IntVar(&cfg.CustomSSHDPort, "sshd_port", 0, "The port sshd is binding to")
fs.StringVar(&cfg.CustomSSHDCfgFile, "sshd_config", "", "The location of sshd_config")

ff.Parse(fs, os.Args[1:],
ff.WithEnvVarPrefix("DROPLET_AGENT"),
)

return &cfg
}
3 changes: 2 additions & 1 deletion internal/config/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

package config

const version = "v1.2.8"
// Version is the current package version.
const Version = "v1.2.9"
3 changes: 2 additions & 1 deletion internal/metadata/updater/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"net/http"

"github.com/digitalocean/droplet-agent/internal/config"
"github.com/digitalocean/droplet-agent/internal/metadata"
)

Expand Down Expand Up @@ -37,7 +38,7 @@ func (u *agentInfoUpdaterImpl) Update(md *metadata.Metadata) error {
return fmt.Errorf("%w:%v", ErrUpdateMetadataFailed, err)
}

req.Header.Set("User-Agent", "Droplet-Agent/1.0.1")
req.Header.Set("User-Agent", config.UserAgent)
req.Header.Set("Content-Type", "application/json; charset=utf-8")
resp, err := u.client.Do(req)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion internal/metadata/updater/updater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http"
"testing"

"github.com/digitalocean/droplet-agent/internal/config"
"github.com/digitalocean/droplet-agent/internal/metadata"
"github.com/digitalocean/droplet-agent/internal/mockutils"
"go.uber.org/mock/gomock"
Expand Down Expand Up @@ -89,7 +90,7 @@ func newRequest(t *testing.T, body []byte) *http.Request {
t.Fatalf("could not create http request: %s", err)
}
req.Header.Set("Content-Type", "application/json; charset=utf-8")
req.Header.Set("User-Agent", "Droplet-Agent/1.0.1")
req.Header.Set("User-Agent", config.UserAgent)

return req
}
14 changes: 14 additions & 0 deletions vendor/github.com/peterbourgon/ff/v3/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a4a7514

Please sign in to comment.