Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions internal/app/diun.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
kubernetesPrd "github.com/crazy-max/diun/v4/internal/provider/kubernetes"
nomadPrd "github.com/crazy-max/diun/v4/internal/provider/nomad"
swarmPrd "github.com/crazy-max/diun/v4/internal/provider/swarm"
"github.com/crazy-max/diun/v4/pkg/utl"
"github.com/crazy-max/gohealthchecks"
"github.com/dromara/carbon/v2"
"github.com/panjf2000/ants/v2"
Expand All @@ -30,10 +31,11 @@ type Diun struct {
meta model.Meta
cfg *config.Config

db *db.Client
grpc *grpc.Client
hc *gohealthchecks.Client
notif *notif.Client
db *db.Client
grpc *grpc.Client
hc *gohealthchecks.Client
hcUUID string
notif *notif.Client

cron *cron.Cron
jobID cron.EntryID
Expand Down Expand Up @@ -77,6 +79,10 @@ func New(meta model.Meta, cfg *config.Config, grpcAuthority string) (*Diun, erro
return nil, errors.Wrap(err, "cannot parse Healthchecks base URL")
}
}
diun.hcUUID, err = utl.GetSecret(cfg.Watch.Healthchecks.UUID, cfg.Watch.Healthchecks.UUIDFile)
if err != nil {
return nil, errors.Wrap(err, "cannot retrieve Healthchecks UUID")
}
diun.hc = gohealthchecks.NewClient(&gohealthchecks.ClientOptions{
BaseURL: hcBaseURL,
})
Expand Down
6 changes: 3 additions & 3 deletions internal/app/hc.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (di *Diun) HealthchecksStart() {
}

if err := di.hc.Start(context.Background(), gohealthchecks.PingingOptions{
UUID: di.cfg.Watch.Healthchecks.UUID,
UUID: di.hcUUID,
}); err != nil {
log.Error().Err(err).Msgf("Cannot send Healthchecks start event")
}
Expand All @@ -40,7 +40,7 @@ func (di *Diun) HealthchecksSuccess(entries *model.NotifEntries) {
}

if err := di.hc.Success(context.Background(), gohealthchecks.PingingOptions{
UUID: di.cfg.Watch.Healthchecks.UUID,
UUID: di.hcUUID,
Logs: logsBuf.String(),
}); err != nil {
log.Error().Err(err).Msgf("Cannot send Healthchecks success event")
Expand All @@ -53,7 +53,7 @@ func (di *Diun) HealthchecksFail(logs string) {
}

if err := di.hc.Fail(context.Background(), gohealthchecks.PingingOptions{
UUID: di.cfg.Watch.Healthchecks.UUID,
UUID: di.hcUUID,
Logs: logs,
}); err != nil {
log.Error().Err(err).Msgf("Cannot send Healthchecks fail event")
Expand Down
2 changes: 1 addition & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (cfg *Config) validate() error {
}
}

if cfg.Watch.Healthchecks != nil && len(cfg.Watch.Healthchecks.UUID) == 0 {
if cfg.Watch.Healthchecks != nil && len(cfg.Watch.Healthchecks.UUID) == 0 && len(cfg.Watch.Healthchecks.UUIDFile) == 0 {
return errors.New("healthchecks UUID is required")
}

Expand Down
55 changes: 55 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,32 @@ func TestLoadFile(t *testing.T) {
cfg: "./fixtures/config.err.hc.yml",
wantErr: true,
},
{
name: "Success with healthchecks uuidFile",
cfg: "./fixtures/config.hc.uuidfile.yml",
wantData: &Config{
Db: (&model.Db{}).GetDefaults(),
Watch: &model.Watch{
Workers: 10,
Jitter: utl.NewDuration(30 * time.Second),
FirstCheckNotif: utl.NewFalse(),
RunOnStartup: utl.NewTrue(),
CompareDigest: utl.NewTrue(),
Healthchecks: &model.Healthchecks{
BaseURL: "https://hc-ping.com/",
UUIDFile: "./fixtures/run_secrets_uuid",
},
},
Defaults: (&model.Defaults{}).GetDefaults(),
Providers: &model.Providers{
Docker: &model.PrdDocker{
TLSVerify: utl.NewTrue(),
WatchByDefault: utl.NewFalse(),
WatchStopped: utl.NewFalse(),
},
},
},
},
{
name: "Fail on no provider",
cfg: "./fixtures/config.err.provider.yml",
Expand Down Expand Up @@ -290,6 +316,35 @@ func TestLoadEnv(t *testing.T) {
expected: nil,
wantErr: true,
},
{
desc: "healthchecks with UUIDFILE env var",
environ: []string{
"DIUN_WATCH_HEALTHCHECKS_UUIDFILE=./fixtures/run_secrets_uuid",
"DIUN_PROVIDERS_DOCKER=true",
},
expected: &Config{
Db: (&model.Db{}).GetDefaults(),
Watch: &model.Watch{
Workers: 10,
Jitter: utl.NewDuration(30 * time.Second),
FirstCheckNotif: utl.NewFalse(),
RunOnStartup: utl.NewTrue(),
CompareDigest: utl.NewTrue(),
Healthchecks: &model.Healthchecks{
UUIDFile: "./fixtures/run_secrets_uuid",
},
},
Defaults: (&model.Defaults{}).GetDefaults(),
Providers: &model.Providers{
Docker: &model.PrdDocker{
TLSVerify: utl.NewTrue(),
WatchByDefault: utl.NewFalse(),
WatchStopped: utl.NewFalse(),
},
},
},
wantErr: false,
},
{
desc: "docker provider",
environ: []string{
Expand Down
7 changes: 7 additions & 0 deletions internal/config/fixtures/config.hc.uuidfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
watch:
healthchecks:
baseURL: https://hc-ping.com/
uuidFile: ./fixtures/run_secrets_uuid

providers:
docker: {}
1 change: 1 addition & 0 deletions internal/config/fixtures/run_secrets_uuid
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5bf66975-d4c7-4bf5-bcc8-b8d8a82ea278
5 changes: 3 additions & 2 deletions internal/model/watch_healthchecks.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package model

// Healthchecks holds data necessary for Healthchecks configuration
type Healthchecks struct {
BaseURL string `yaml:"baseURL,omitempty" json:"baseURL,omitempty"`
UUID string `yaml:"uuid,omitempty" json:"uuid,omitempty" validate:"required"`
BaseURL string `yaml:"baseURL,omitempty" json:"baseURL,omitempty"`
UUID string `yaml:"uuid,omitempty" json:"uuid,omitempty" validate:"omitempty"`
UUIDFile string `yaml:"uuidFile,omitempty" json:"uuidFile,omitempty" validate:"omitempty,file"`
}

// GetDefaults gets the default values
Expand Down