44 "context"
55 "flag"
66 "fmt"
7-
87 "net/http"
98 "net/url"
109 "os"
@@ -21,7 +20,6 @@ import (
2120 amconfig "github.com/prometheus/alertmanager/config"
2221 "github.com/prometheus/client_golang/prometheus"
2322 "github.com/prometheus/client_golang/prometheus/promauto"
24- tsdb_errors "github.com/prometheus/prometheus/tsdb/errors"
2523 "github.com/weaveworks/common/httpgrpc"
2624 "github.com/weaveworks/common/httpgrpc/server"
2725 "github.com/weaveworks/common/user"
@@ -469,10 +467,6 @@ func (h *handlerForGRPCServer) ServeHTTP(w http.ResponseWriter, req *http.Reques
469467}
470468
471469func (am * MultitenantAlertmanager ) starting (ctx context.Context ) (err error ) {
472- err = am .migrateStateFilesToPerTenantDirectories ()
473- if err != nil {
474- return err
475- }
476470
477471 defer func () {
478472 if err == nil || am .subservices == nil {
@@ -546,119 +540,6 @@ func (am *MultitenantAlertmanager) starting(ctx context.Context) (err error) {
546540 return nil
547541}
548542
549- // migrateStateFilesToPerTenantDirectories migrates any existing configuration from old place to new hierarchy.
550- // TODO: Remove in Cortex 1.11.
551- func (am * MultitenantAlertmanager ) migrateStateFilesToPerTenantDirectories () error {
552- migrate := func (from , to string ) error {
553- level .Info (am .logger ).Log ("msg" , "migrating alertmanager state" , "from" , from , "to" , to )
554- err := os .Rename (from , to )
555- return errors .Wrapf (err , "failed to migrate alertmanager state from %v to %v" , from , to )
556- }
557-
558- st , err := am .getObsoleteFilesPerUser ()
559- if err != nil {
560- return errors .Wrap (err , "failed to migrate alertmanager state files" )
561- }
562-
563- for userID , files := range st {
564- tenantDir := am .getTenantDirectory (userID )
565- err := os .MkdirAll (tenantDir , 0777 )
566- if err != nil {
567- return errors .Wrapf (err , "failed to create per-tenant directory %v" , tenantDir )
568- }
569-
570- errs := tsdb_errors .NewMulti ()
571-
572- if files .notificationLogSnapshot != "" {
573- errs .Add (migrate (files .notificationLogSnapshot , filepath .Join (tenantDir , notificationLogSnapshot )))
574- }
575-
576- if files .silencesSnapshot != "" {
577- errs .Add (migrate (files .silencesSnapshot , filepath .Join (tenantDir , silencesSnapshot )))
578- }
579-
580- if files .templatesDir != "" {
581- errs .Add (migrate (files .templatesDir , filepath .Join (tenantDir , templatesDir )))
582- }
583-
584- if err := errs .Err (); err != nil {
585- return err
586- }
587- }
588- return nil
589- }
590-
591- type obsoleteStateFiles struct {
592- notificationLogSnapshot string
593- silencesSnapshot string
594- templatesDir string
595- }
596-
597- // getObsoleteFilesPerUser returns per-user set of files that should be migrated from old structure to new structure.
598- func (am * MultitenantAlertmanager ) getObsoleteFilesPerUser () (map [string ]obsoleteStateFiles , error ) {
599- files , err := os .ReadDir (am .cfg .DataDir )
600- if err != nil {
601- return nil , errors .Wrapf (err , "failed to list dir %v" , am .cfg .DataDir )
602- }
603-
604- // old names
605- const (
606- notificationLogPrefix = "nflog:"
607- silencesPrefix = "silences:"
608- templates = "templates"
609- )
610-
611- result := map [string ]obsoleteStateFiles {}
612-
613- for _ , f := range files {
614- fullPath := filepath .Join (am .cfg .DataDir , f .Name ())
615-
616- if f .IsDir () {
617- // Process templates dir.
618- if f .Name () != templates {
619- // Ignore other files -- those are likely per tenant directories.
620- continue
621- }
622-
623- templateDirs , err := os .ReadDir (fullPath )
624- if err != nil {
625- return nil , errors .Wrapf (err , "failed to list dir %v" , fullPath )
626- }
627-
628- // Previously templates directory contained per-tenant subdirectory.
629- for _ , d := range templateDirs {
630- if d .IsDir () {
631- v := result [d .Name ()]
632- v .templatesDir = filepath .Join (fullPath , d .Name ())
633- result [d .Name ()] = v
634- } else {
635- level .Warn (am .logger ).Log ("msg" , "ignoring unknown local file while migrating local alertmanager state files" , "file" , filepath .Join (fullPath , d .Name ()))
636- }
637- }
638- continue
639- }
640-
641- switch {
642- case strings .HasPrefix (f .Name (), notificationLogPrefix ):
643- userID := strings .TrimPrefix (f .Name (), notificationLogPrefix )
644- v := result [userID ]
645- v .notificationLogSnapshot = fullPath
646- result [userID ] = v
647-
648- case strings .HasPrefix (f .Name (), silencesPrefix ):
649- userID := strings .TrimPrefix (f .Name (), silencesPrefix )
650- v := result [userID ]
651- v .silencesSnapshot = fullPath
652- result [userID ] = v
653-
654- default :
655- level .Warn (am .logger ).Log ("msg" , "ignoring unknown local data file while migrating local alertmanager state files" , "file" , fullPath )
656- }
657- }
658-
659- return result , nil
660- }
661-
662543func (am * MultitenantAlertmanager ) run (ctx context.Context ) error {
663544 tick := time .NewTicker (am .cfg .PollInterval )
664545 defer tick .Stop ()
0 commit comments