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

fix : add alerts migration script #306

Open
wants to merge 1 commit into
base: release-v0.18
Choose a base branch
from
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
86 changes: 86 additions & 0 deletions client/cmd/job/alert_migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package job

import (
"strings"

"github.com/goto/salt/log"
"github.com/spf13/afero"
"github.com/spf13/cobra"

"github.com/goto/optimus/client/cmd/internal/logger"
"github.com/goto/optimus/client/local/specio"
"github.com/goto/optimus/config"
)

type migrateCommand struct {
logger log.Logger

configFilePath string
clientConfig *config.ClientConfig
}

func NewMigrateCommand() *cobra.Command {
migrate := &migrateCommand{
logger: logger.NewClientLogger(),
}

cmd := &cobra.Command{
Use: "migrate",
RunE: migrate.RunE,
PreRunE: migrate.PreRunE,
}

cmd.Flags().StringVarP(&migrate.configFilePath, "config", "c", config.EmptyPath, "File path for client configuration")
return cmd
}

func (v *migrateCommand) PreRunE(_ *cobra.Command, _ []string) error {
conf, err := config.LoadClientConfig(v.configFilePath)
if err != nil {
return err
}
v.clientConfig = conf
return nil
}

func (v *migrateCommand) RunE(_ *cobra.Command, _ []string) error {
return v.migrate()

Check failure on line 48 in client/cmd/job/alert_migrate.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed with `-extra` (gofumpt)
}

func (v *migrateCommand) migrate() error {
jobSpecReadWriter, err := specio.NewJobSpecReadWriter(afero.NewOsFs())
if err != nil {
return err
}

for _, namespace := range v.clientConfig.Namespaces {
jobs, err := jobSpecReadWriter.ReadAll(namespace.Job.Path)
if err != nil {
v.logger.Error(err.Error())
continue
}
for _, job := range jobs {
isModified := false
for i, on := range job.Behavior.Notify {
pagerduty := false
for _, ch := range on.Channels {
if strings.Contains(ch, "pagerduty://") {
pagerduty = true
break
}
}
if pagerduty {
job.Behavior.Notify[i].Severity = "CRITICAL"
v.logger.Info("Modified job %s", job.Path)
isModified = true
}
}
if isModified {
jobSpecReadWriter.Write(job.Path, job)
}
}
}

return nil
}
1 change: 1 addition & 0 deletions client/cmd/job/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func NewJobCommand() *cobra.Command {
NewDeleteCommand(),
NewDeployCommand(),
NewPlanCommand(),
NewMigrateCommand(),
)
return cmd
}
Loading