Skip to content

Commit

Permalink
Merge pull request #1772 from rancher/vendor-amtool
Browse files Browse the repository at this point in the history
"Vendor" amtool CLI into opni command
  • Loading branch information
alexandreLamarre committed Oct 12, 2023
2 parents 63977ae + d772efc commit 72e8e62
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 96 deletions.
12 changes: 12 additions & 0 deletions internal/alerting/amtool/amtool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package amtool

import (
"os"

"github.com/prometheus/alertmanager/cli"
)

func Execute(args []string) {
os.Args = append([]string{"amtool"}, args...)
cli.Execute()
}
1 change: 1 addition & 0 deletions magefiles/targets/build_opni.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func (Build) OpniMinimal(ctx context.Context) error {
})
}

// Builds the opni release CLI binary, requires version as input
func (Build) OpniReleaseCLI(ctx context.Context, fileSuffix string) error {
mg.CtxDeps(ctx, Build.Archives)

Expand Down
109 changes: 13 additions & 96 deletions pkg/opni/commands/alerting.go
Original file line number Diff line number Diff line change
@@ -1,121 +1,38 @@
//go:build !minimal && !cli
//go:build !minimal

package commands

import (
"context"
"flag"
"fmt"
"os"
"path"
"strings"
"time"

alertmanager_internal "github.com/rancher/opni/internal/alerting/alertmanager"
"github.com/rancher/opni/internal/alerting/syncer"
"github.com/rancher/opni/pkg/alerting/shared"
alertingv1 "github.com/rancher/opni/pkg/apis/alerting/v1"
"github.com/rancher/opni/pkg/tracing"
"github.com/rancher/opni/internal/alerting/amtool"
"github.com/spf13/cobra"
)

const syncerPrefix = "syncer"

func BuildAlertingComponents() *cobra.Command {
func BuildAlertingCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "alerting-server",
Short: "Run one of the alerting components",
Use: "alerting",
Short: "Interact with alerting plugin APIs",
}
cmd.AddCommand(BuildAlertingSyncer())
cmd.AddCommand(BuildAlertManager())

return cmd
}

func addSyncerPrefix(input string) string {
return fmt.Sprintf("%s.%s", syncerPrefix, input)
}
cmd.AddCommand(BuildAmtoolCmd())

func hasSyncerPrefix(input string) bool {
return strings.HasPrefix(input, fmt.Sprintf("--%s", syncerPrefix))
}

func BuildAlertingSyncer() *cobra.Command {
var syncerGatewayJoinAddress string
var syncerAlertManagerConfigFilePath string
var syncerListenAddress string
var syncerAlertManagerAddress string
var syncerPprofPort int64
var syncerProfileBlockRate int64
cmd := &cobra.Command{
Use: "syncer",
Short: "Run the side-car Alertmanager alerting syncer server",
Long: "Note: this command is only intended to be run as a side-car container to the Alertmanager server.",
DisableFlagParsing: false,
Run: func(cmd *cobra.Command, args []string) {
tracing.Configure("alerting-syncer")
flag.CommandLine = flag.NewFlagSet("syncer", flag.ExitOnError)

serverConfig := &alertingv1.SyncerConfig{
GatewayJoinAddress: syncerGatewayJoinAddress,
AlertmanagerConfigPath: syncerAlertManagerConfigFilePath,
ListenAddress: syncerListenAddress,
AlertmanagerAddress: syncerAlertManagerAddress,
HookListenAddress: path.Join(syncerListenAddress, shared.AlertingDefaultHookName),
PprofPort: syncerPprofPort,
ProfileBlockRate: syncerProfileBlockRate,
}

if err := serverConfig.Validate(); err != nil {
lg.Fatal(err)
}
lg.Debug("syncer gateway join address" + syncerGatewayJoinAddress)
err := syncer.Main(cmd.Context(), serverConfig)
if err != nil {
lg.Error(err)
}
},
Args: cobra.NoArgs,
}
cmd.Flags().StringVar(&syncerAlertManagerConfigFilePath, addSyncerPrefix("alertmanager.config.file"), shared.ConfigMountPath, "the alertmanager config file to sync to")
cmd.Flags().StringVar(&syncerListenAddress, addSyncerPrefix("listen.address"), ":8080", "the address to listen on")
cmd.Flags().StringVar(&syncerAlertManagerAddress, addSyncerPrefix("alertmanager.address"), "localhost:9093", "the address of the remote alertmanager instance to sync")
cmd.Flags().Int64Var(&syncerPprofPort, addSyncerPrefix("pprof.port"), 0, "the port to listen on for pprof")
cmd.Flags().Int64Var(&syncerProfileBlockRate, addSyncerPrefix("profile.block-rate"), 0, "the rate at which to profile blocking operations")
cmd.Flags().StringVar(&syncerGatewayJoinAddress, addSyncerPrefix("gateway.join.address"), "", "the address of the gateway to join")
ConfigureManagementCommand(cmd)
return cmd
}

func BuildAlertManager() *cobra.Command {
func BuildAmtoolCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "alertmanager",
Short: "Run the embedded Alertmanager server",
Use: "amtool",
Short: "vendored prometheus/alertmanager amtool",
DisableFlagParsing: true,
Run: func(cmd *cobra.Command, args []string) {
tracing.Configure("alertmanager")
flag.CommandLine = flag.NewFlagSet("alertmanager", flag.ExitOnError)
alertmanager_internal.Main(append([]string{"alertmanager"}, args...))
flag.CommandLine = flag.NewFlagSet("amtool", flag.ExitOnError)
amtool.Execute(args)
},
}
return cmd
}

func waitForAlertmanagerFile(ctx context.Context, configFile string) bool {
ctxTimeout := 1 * time.Minute
ctxCa, cancel := context.WithTimeout(ctx, ctxTimeout)
defer cancel()
select {
case <-ctxCa.Done():
return false
default:
_, err := os.Stat(configFile)
if err == nil {
return true
}
}
return false
}

func init() {
AddCommandsToGroup(OpniComponents, BuildAlertingComponents())
AddCommandsToGroup(PluginAPIs, BuildAlertingCmd())
}
121 changes: 121 additions & 0 deletions pkg/opni/commands/alerting_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
//go:build !minimal && !cli

package commands

import (
"context"
"flag"
"fmt"
"os"
"path"
"strings"
"time"

alertmanager_internal "github.com/rancher/opni/internal/alerting/alertmanager"
"github.com/rancher/opni/internal/alerting/syncer"
"github.com/rancher/opni/pkg/alerting/shared"
alertingv1 "github.com/rancher/opni/pkg/apis/alerting/v1"
"github.com/rancher/opni/pkg/tracing"
"github.com/spf13/cobra"
)

const syncerPrefix = "syncer"

func BuildAlertingComponents() *cobra.Command {
cmd := &cobra.Command{
Use: "alerting-server",
Short: "Run one of the alerting components",
}
cmd.AddCommand(BuildAlertingSyncer())
cmd.AddCommand(BuildAlertManager())

return cmd
}

func addSyncerPrefix(input string) string {
return fmt.Sprintf("%s.%s", syncerPrefix, input)
}

func hasSyncerPrefix(input string) bool {
return strings.HasPrefix(input, fmt.Sprintf("--%s", syncerPrefix))
}

func BuildAlertingSyncer() *cobra.Command {
var syncerGatewayJoinAddress string
var syncerAlertManagerConfigFilePath string
var syncerListenAddress string
var syncerAlertManagerAddress string
var syncerPprofPort int64
var syncerProfileBlockRate int64
cmd := &cobra.Command{
Use: "syncer",
Short: "Run the side-car Alertmanager alerting syncer server",
Long: "Note: this command is only intended to be run as a side-car container to the Alertmanager server.",
DisableFlagParsing: false,
Run: func(cmd *cobra.Command, args []string) {
tracing.Configure("alerting-syncer")
flag.CommandLine = flag.NewFlagSet("syncer", flag.ExitOnError)

serverConfig := &alertingv1.SyncerConfig{
GatewayJoinAddress: syncerGatewayJoinAddress,
AlertmanagerConfigPath: syncerAlertManagerConfigFilePath,
ListenAddress: syncerListenAddress,
AlertmanagerAddress: syncerAlertManagerAddress,
HookListenAddress: path.Join(syncerListenAddress, shared.AlertingDefaultHookName),
PprofPort: syncerPprofPort,
ProfileBlockRate: syncerProfileBlockRate,
}

if err := serverConfig.Validate(); err != nil {
lg.Fatal(err)
}
lg.Debug("syncer gateway join address" + syncerGatewayJoinAddress)
err := syncer.Main(cmd.Context(), serverConfig)
if err != nil {
lg.Error(err)
}
},
Args: cobra.NoArgs,
}
cmd.Flags().StringVar(&syncerAlertManagerConfigFilePath, addSyncerPrefix("alertmanager.config.file"), shared.ConfigMountPath, "the alertmanager config file to sync to")
cmd.Flags().StringVar(&syncerListenAddress, addSyncerPrefix("listen.address"), ":8080", "the address to listen on")
cmd.Flags().StringVar(&syncerAlertManagerAddress, addSyncerPrefix("alertmanager.address"), "localhost:9093", "the address of the remote alertmanager instance to sync")
cmd.Flags().Int64Var(&syncerPprofPort, addSyncerPrefix("pprof.port"), 0, "the port to listen on for pprof")
cmd.Flags().Int64Var(&syncerProfileBlockRate, addSyncerPrefix("profile.block-rate"), 0, "the rate at which to profile blocking operations")
cmd.Flags().StringVar(&syncerGatewayJoinAddress, addSyncerPrefix("gateway.join.address"), "", "the address of the gateway to join")
return cmd
}

func BuildAlertManager() *cobra.Command {
cmd := &cobra.Command{
Use: "alertmanager",
Short: "Run the embedded Alertmanager server",
DisableFlagParsing: true,
Run: func(cmd *cobra.Command, args []string) {
tracing.Configure("alertmanager")
flag.CommandLine = flag.NewFlagSet("alertmanager", flag.ExitOnError)
alertmanager_internal.Main(append([]string{"alertmanager"}, args...))
},
}
return cmd
}

func waitForAlertmanagerFile(ctx context.Context, configFile string) bool {
ctxTimeout := 1 * time.Minute
ctxCa, cancel := context.WithTimeout(ctx, ctxTimeout)
defer cancel()
select {
case <-ctxCa.Done():
return false
default:
_, err := os.Stat(configFile)
if err == nil {
return true
}
}
return false
}

func init() {
AddCommandsToGroup(OpniComponents, BuildAlertingComponents())
}

0 comments on commit 72e8e62

Please sign in to comment.