Skip to content

Commit

Permalink
Add new osdctl command to allow silencing alert(s) across a specific org
Browse files Browse the repository at this point in the history
  • Loading branch information
Dakota Long committed Jul 15, 2024
1 parent 167a090 commit d4d4db4
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 6 deletions.
1 change: 1 addition & 0 deletions cmd/alerts/silence/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func NewCmdSilence() *cobra.Command {
silenceCmd.AddCommand(NewCmdAddSilence())
silenceCmd.AddCommand(NewCmdClearSilence())
silenceCmd.AddCommand(NewCmdListSilence())
silenceCmd.AddCommand(NewCmdAddOrgSilence())

return silenceCmd
}
96 changes: 96 additions & 0 deletions cmd/alerts/silence/silence_org.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package silence

import (
"fmt"
"log"

"github.com/openshift/osdctl/cmd/common"
orgutils "github.com/openshift/osdctl/cmd/org"
ocmutils "github.com/openshift/osdctl/pkg/utils"
"github.com/spf13/cobra"
)

type AddOrgSilenceCmd struct {
organization string
alertID []string
duration string
comment string
all bool
}

func NewCmdAddOrgSilence() *cobra.Command {
AddOrgSilenceCmd := &AddOrgSilenceCmd{}
cmd := &cobra.Command{
Use: "org <org-id> [--all --duration --comment | --alertname --duration --comment]",
Short: "Add new silence for alert for org",
Long: `add new silence for specfic or all alerts with comment and duration of alert for an organization`,
Args: cobra.ExactArgs(1),
DisableAutoGenTag: true,
Run: func(cmd *cobra.Command, args []string) {
AddOrgSilenceCmd.organization = args[0]
AddOrgSilence(AddOrgSilenceCmd)
},
}

cmd.Flags().StringSliceVar(&AddOrgSilenceCmd.alertID, "alertname", []string{}, "alertname (comma-separated)")
cmd.Flags().StringVarP(&AddOrgSilenceCmd.comment, "comment", "c", "", "add comment about silence")
cmd.Flags().StringVarP(&AddOrgSilenceCmd.duration, "duration", "d", "15d", "add duration for silence") //default duration set to 15 days
cmd.Flags().BoolVarP(&AddOrgSilenceCmd.all, "all", "a", false, "add silences for all alert")

return cmd
}

// Add alert silences to organization's clusters
func AddOrgSilence(cmd *AddOrgSilenceCmd) {
alertID := cmd.alertID
comment := cmd.comment
duration := cmd.duration
all := cmd.all
organizationID := cmd.organization

//Retrieve v1.Subscriptions list
subscriptions, err := orgutils.SearchSubscriptions(organizationID, orgutils.StatusActive)
if err != nil {
log.Fatal(err)
}

//Start ocm connection
connection, err := ocmutils.CreateConnection()
if err != nil {
log.Fatal(err)
}

//Retrieve organization
organization, err := ocmutils.GetOrganization(connection, subscriptions[0].ClusterID())
if err != nil {
log.Fatal(err)
}

log.Printf("Are you sure you want silence alerts for %d clusters for this organization: %s", len(subscriptions), organization.Name())
ocmutils.ConfirmPrompt()

for _, subscription := range subscriptions {
clusterID := subscription.ClusterID()
if len(clusterID) == 0 {
log.Print("Cluster ID invalid, skipping: %s", clusterID)
continue //Skip invalid clusters
} else {
log.Print("Silencing alert(s) on cluster: %s", clusterID)
}

username, clustername := GetUserAndClusterInfo(clusterID)

_, kubeconfig, clientset, err := common.GetKubeConfigAndClient(clusterID)
if err != nil {
log.Fatal(err)
}

if all {
AddAllSilence(clusterID, duration, comment, username, clustername, kubeconfig, clientset)
} else if len(alertID) > 0 {
AddAlertNameSilence(alertID, duration, comment, username, kubeconfig, clientset)
} else {
fmt.Println("No valid option specified. Use --all or --alertname.")
}
}
}
5 changes: 3 additions & 2 deletions cmd/org/clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package org

import (
"fmt"
accountsv1 "github.com/openshift-online/ocm-sdk-go/accountsmgmt/v1"
"os"

accountsv1 "github.com/openshift-online/ocm-sdk-go/accountsmgmt/v1"

"github.com/aws/aws-sdk-go-v2/service/organizations"
"github.com/openshift/osdctl/pkg/printer"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -41,7 +42,7 @@ osdctl org clusters --aws-profile my-aws-profile --aws-account-id 123456789

status := ""
if !allClustersFlag {
status = statusActive
status = StatusActive
}

clusters, err := SearchSubscriptions(orgId, status)
Expand Down
5 changes: 3 additions & 2 deletions cmd/org/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package org
import (
"encoding/json"
"fmt"
"os"

accountsv1 "github.com/openshift-online/ocm-sdk-go/accountsmgmt/v1"
"github.com/openshift/osdctl/pkg/utils"
"os"

"github.com/openshift-online/ocm-cli/pkg/dump"
sdk "github.com/openshift-online/ocm-sdk-go"
Expand All @@ -20,7 +21,7 @@ const (
accountsAPIPath = "/api/accounts_mgmt/v1/accounts"
currentAccountApiPath = "/api/accounts_mgmt/v1/current_account"

statusActive = "Active"
StatusActive = "Active"
)

var (
Expand Down
4 changes: 2 additions & 2 deletions cmd/org/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"errors"
"fmt"
v1 "github.com/openshift-online/ocm-sdk-go/servicelogs/v1"
"os"
"strconv"
"sync"
Expand All @@ -15,6 +14,7 @@ import (
"github.com/andygrunwald/go-jira"
sdk "github.com/openshift-online/ocm-sdk-go"
cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
v1 "github.com/openshift-online/ocm-sdk-go/servicelogs/v1"
"github.com/openshift/osdctl/cmd/servicelog"
"github.com/openshift/osdctl/pkg/printer"
pdProvider "github.com/openshift/osdctl/pkg/provider/pagerduty"
Expand Down Expand Up @@ -179,7 +179,7 @@ func getPlanDisplayText(plan string) string {
}

func Context(orgId string) ([]ClusterInfo, error) {
clusterSubscriptions, err := SearchAllSubscriptionsByOrg(orgId, statusActive, true)
clusterSubscriptions, err := SearchAllSubscriptionsByOrg(orgId, StatusActive, true)
if err != nil {
return nil, fmt.Errorf("failed to fetch cluster subscriptions for org with ID %s: %w", orgId, err)
}
Expand Down

0 comments on commit d4d4db4

Please sign in to comment.