Skip to content

Commit

Permalink
add error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Dakota Long committed Jul 19, 2024
1 parent 7e4cb75 commit 9ea38fa
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
13 changes: 8 additions & 5 deletions cmd/alerts/silence/add_silence.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func AddSilence(cmd *addSilenceCmd) {
}

}
func AddAllSilence(clusterID, duration, comment, username, clustername string, kubeconfig *rest.Config, clientset *kubernetes.Clientset) {
func AddAllSilence(clusterID, duration, comment, username, clustername string, kubeconfig *rest.Config, clientset *kubernetes.Clientset) error {
alerts := fetchAllAlerts(kubeconfig, clientset)
for _, alert := range alerts {
addCmd := []string{
Expand All @@ -91,13 +91,15 @@ func AddAllSilence(clusterID, duration, comment, username, clustername string, k
output, err := utils.ExecInAlertManagerPod(kubeconfig, clientset, addCmd)
if err != nil {
log.Fatal("Exiting the program")
return
return fmt.Errorf("Failed to exec in AlertManager pod: %w", err)
}

formattedOutput := strings.Replace(output, "\n", "", -1)

fmt.Printf("Alert %s has been silenced with id \"%s\" for a duration of %s by user \"%s\" \n", alert.Labels.Alertname, formattedOutput, duration, username)
}

return nil
}

func fetchAllAlerts(kubeconfig *rest.Config, clientset *kubernetes.Clientset) []utils.Alert {
Expand All @@ -117,7 +119,7 @@ func fetchAllAlerts(kubeconfig *rest.Config, clientset *kubernetes.Clientset) []
return fetchedAlerts
}

func AddAlertNameSilence(alertID []string, duration, comment, username string, kubeconfig *rest.Config, clientset *kubernetes.Clientset) {
func AddAlertNameSilence(alertID []string, duration, comment, username string, kubeconfig *rest.Config, clientset *kubernetes.Clientset) error {
for _, alertname := range alertID {
addCmd := []string{
"amtool",
Expand All @@ -131,14 +133,15 @@ func AddAlertNameSilence(alertID []string, duration, comment, username string, k

output, err := utils.ExecInAlertManagerPod(kubeconfig, clientset, addCmd)
if err != nil {
log.Fatal("Exiting the program")
return
return fmt.Errorf("Failed to exec in AlertManager pod: %w", err)
}

formattedOutput := strings.Replace(output, "\n", "", -1)

fmt.Printf("Alert %s has been silenced with id \"%s\" for duration of %s by user \"%s\" \n", alertname, formattedOutput, duration, username)
}

return nil
}

// Get User name and clustername
Expand Down
15 changes: 12 additions & 3 deletions cmd/alerts/silence/silence_org.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ func AddOrgSilence(cmd *AddOrgSilenceCmd) {
subscriptions, err := orgutils.SearchSubscriptions(organizationID, orgutils.StatusActive)
if err != nil {
log.Fatal(err)
} else if len(subscriptions) == 0 {
log.Fatal("No subscriptions found with that organization ID")
}

//Start ocm connection
Expand Down Expand Up @@ -82,13 +84,20 @@ func AddOrgSilence(cmd *AddOrgSilenceCmd) {

_, kubeconfig, clientset, err := common.GetKubeConfigAndClient(clusterID)
if err != nil {
log.Fatal(err)
log.Print(err)
continue //Skip if cluster is not in supported state
}

if all {
AddAllSilence(clusterID, duration, comment, username, clustername, kubeconfig, clientset)
err := AddAllSilence(clusterID, duration, comment, username, clustername, kubeconfig, clientset)
if err != nil {
log.Print(err)
}
} else if len(alertID) > 0 {
AddAlertNameSilence(alertID, duration, comment, username, kubeconfig, clientset)
err := AddAlertNameSilence(alertID, duration, comment, username, kubeconfig, clientset)
if err != nil {
log.Print(err)
}
} else {
fmt.Println("No valid option specified. Use --all or --alertname.")
}
Expand Down

0 comments on commit 9ea38fa

Please sign in to comment.