mirror of https://github.com/fluxcd/flux2.git
Add alert commands
parent
f7971a871a
commit
6ea84906ac
@ -0,0 +1,196 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2020 The Flux CD contributors.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"github.com/fluxcd/pkg/apis/meta"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
corev1 "k8s.io/api/core/v1"
|
||||||
|
"k8s.io/apimachinery/pkg/api/errors"
|
||||||
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
|
"k8s.io/apimachinery/pkg/types"
|
||||||
|
"k8s.io/apimachinery/pkg/util/wait"
|
||||||
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||||
|
|
||||||
|
notificationv1 "github.com/fluxcd/notification-controller/api/v1beta1"
|
||||||
|
)
|
||||||
|
|
||||||
|
var createAlertCmd = &cobra.Command{
|
||||||
|
Use: "alert [name]",
|
||||||
|
Aliases: []string{},
|
||||||
|
Short: "Create or update a Alert resource",
|
||||||
|
Long: "The create alert command generates a Alert resource.",
|
||||||
|
Example: ` # Create an Alert for kustomization events
|
||||||
|
gotk create alert \
|
||||||
|
--event-severity info \
|
||||||
|
--event-source kustomization/gotk-system \
|
||||||
|
--provider-ref slack \
|
||||||
|
gotk-system
|
||||||
|
`,
|
||||||
|
RunE: createAlertCmdRun,
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
aProviderRef string
|
||||||
|
aEventSeverity string
|
||||||
|
aEventSources []string
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
createAlertCmd.Flags().StringVar(&aProviderRef, "provider-ref", "", "reference to provider")
|
||||||
|
createAlertCmd.Flags().StringVar(&aEventSeverity, "event-severity", "", "severity of events to send alerts for")
|
||||||
|
createAlertCmd.Flags().StringArrayVar(&aEventSources, "event-source", []string{}, "sources that should generate alerts (<kind>/<name>)")
|
||||||
|
createCmd.AddCommand(createAlertCmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
func createAlertCmdRun(cmd *cobra.Command, args []string) error {
|
||||||
|
if len(args) < 1 {
|
||||||
|
return fmt.Errorf("alert name is required")
|
||||||
|
}
|
||||||
|
name := args[0]
|
||||||
|
|
||||||
|
if aProviderRef == "" {
|
||||||
|
return fmt.Errorf("provider ref is required")
|
||||||
|
}
|
||||||
|
|
||||||
|
eventSources := []notificationv1.CrossNamespaceObjectReference{}
|
||||||
|
for _, eventSource := range aEventSources {
|
||||||
|
kind, name := utils.parseObjectKindName(eventSource)
|
||||||
|
if kind == "" {
|
||||||
|
return fmt.Errorf("invalid event source '%s', must be in format <kind>/<name>", eventSource)
|
||||||
|
}
|
||||||
|
|
||||||
|
eventSources = append(eventSources, notificationv1.CrossNamespaceObjectReference{
|
||||||
|
Kind: kind,
|
||||||
|
Name: name,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(eventSources) == 0 {
|
||||||
|
return fmt.Errorf("atleast one event source is required")
|
||||||
|
}
|
||||||
|
|
||||||
|
sourceLabels, err := parseLabels()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if !export {
|
||||||
|
logger.Generatef("generating alert")
|
||||||
|
}
|
||||||
|
|
||||||
|
alert := notificationv1.Alert{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: name,
|
||||||
|
Namespace: namespace,
|
||||||
|
Labels: sourceLabels,
|
||||||
|
},
|
||||||
|
Spec: notificationv1.AlertSpec{
|
||||||
|
ProviderRef: corev1.LocalObjectReference{
|
||||||
|
Name: aProviderRef,
|
||||||
|
},
|
||||||
|
EventSeverity: aEventSeverity,
|
||||||
|
EventSources: eventSources,
|
||||||
|
Suspend: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
if export {
|
||||||
|
return exportAlert(alert)
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
kubeClient, err := utils.kubeClient(kubeconfig)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Actionf("applying alert")
|
||||||
|
if err := upsertAlert(ctx, kubeClient, alert); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Waitingf("waiting for reconciliation")
|
||||||
|
if err := wait.PollImmediate(pollInterval, timeout,
|
||||||
|
isAlertReady(ctx, kubeClient, name, namespace)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Successf("alert %s is ready", name)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func upsertAlert(ctx context.Context, kubeClient client.Client, alert notificationv1.Alert) error {
|
||||||
|
namespacedName := types.NamespacedName{
|
||||||
|
Namespace: alert.GetNamespace(),
|
||||||
|
Name: alert.GetName(),
|
||||||
|
}
|
||||||
|
|
||||||
|
var existing notificationv1.Alert
|
||||||
|
err := kubeClient.Get(ctx, namespacedName, &existing)
|
||||||
|
if err != nil {
|
||||||
|
if errors.IsNotFound(err) {
|
||||||
|
if err := kubeClient.Create(ctx, &alert); err != nil {
|
||||||
|
return err
|
||||||
|
} else {
|
||||||
|
logger.Successf("alert created")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
existing.Labels = alert.Labels
|
||||||
|
existing.Spec = alert.Spec
|
||||||
|
if err := kubeClient.Update(ctx, &existing); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Successf("alert updated")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func isAlertReady(ctx context.Context, kubeClient client.Client, name, namespace string) wait.ConditionFunc {
|
||||||
|
return func() (bool, error) {
|
||||||
|
var alert notificationv1.Alert
|
||||||
|
namespacedName := types.NamespacedName{
|
||||||
|
Namespace: namespace,
|
||||||
|
Name: name,
|
||||||
|
}
|
||||||
|
|
||||||
|
err := kubeClient.Get(ctx, namespacedName, &alert)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if c := meta.GetCondition(alert.Status.Conditions, meta.ReadyCondition); c != nil {
|
||||||
|
switch c.Status {
|
||||||
|
case corev1.ConditionTrue:
|
||||||
|
return true, nil
|
||||||
|
case corev1.ConditionFalse:
|
||||||
|
return false, fmt.Errorf(c.Message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,88 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2020 The Flux CD contributors.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/manifoldco/promptui"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
"k8s.io/apimachinery/pkg/types"
|
||||||
|
|
||||||
|
notificationv1 "github.com/fluxcd/notification-controller/api/v1beta1"
|
||||||
|
)
|
||||||
|
|
||||||
|
var deleteAlertCmd = &cobra.Command{
|
||||||
|
Use: "alert [name]",
|
||||||
|
Aliases: []string{},
|
||||||
|
Short: "Delete a Alert resource",
|
||||||
|
Long: "The delete alert command removes the given Alert from the cluster.",
|
||||||
|
Example: ` # Delete an Alert and the Kubernetes resources created by it
|
||||||
|
gotk delete alert main
|
||||||
|
`,
|
||||||
|
RunE: deleteAlertCmdRun,
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
deleteCmd.AddCommand(deleteAlertCmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
func deleteAlertCmdRun(cmd *cobra.Command, args []string) error {
|
||||||
|
if len(args) < 1 {
|
||||||
|
return fmt.Errorf("alert name is required")
|
||||||
|
}
|
||||||
|
name := args[0]
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
kubeClient, err := utils.kubeClient(kubeconfig)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
namespacedName := types.NamespacedName{
|
||||||
|
Namespace: namespace,
|
||||||
|
Name: name,
|
||||||
|
}
|
||||||
|
|
||||||
|
var alert notificationv1.Alert
|
||||||
|
err = kubeClient.Get(ctx, namespacedName, &alert)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if !deleteSilent {
|
||||||
|
prompt := promptui.Prompt{
|
||||||
|
Label: "Are you sure you want to delete this Alert",
|
||||||
|
IsConfirm: true,
|
||||||
|
}
|
||||||
|
if _, err := prompt.Run(); err != nil {
|
||||||
|
return fmt.Errorf("aborting")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Actionf("deleting alert %s in %s namespace", name, namespace)
|
||||||
|
err = kubeClient.Delete(ctx, &alert)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
logger.Successf("alert deleted")
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
@ -0,0 +1,120 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2020 The Flux CD contributors.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
|
"k8s.io/apimachinery/pkg/types"
|
||||||
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||||
|
"sigs.k8s.io/yaml"
|
||||||
|
|
||||||
|
notificationv1 "github.com/fluxcd/notification-controller/api/v1beta1"
|
||||||
|
)
|
||||||
|
|
||||||
|
var exportAlertCmd = &cobra.Command{
|
||||||
|
Use: "alert [name]",
|
||||||
|
Aliases: []string{},
|
||||||
|
Short: "Export Alert resources in YAML format",
|
||||||
|
Long: "The export alert command exports one or all Alert resources in YAML format.",
|
||||||
|
Example: ` # Export all Alert resources
|
||||||
|
gotk export alert --all > kustomizations.yaml
|
||||||
|
|
||||||
|
# Export a Alert
|
||||||
|
gotk export alert main > main.yaml
|
||||||
|
`,
|
||||||
|
RunE: exportAlertCmdRun,
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
exportCmd.AddCommand(exportAlertCmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
func exportAlertCmdRun(cmd *cobra.Command, args []string) error {
|
||||||
|
if !exportAll && len(args) < 1 {
|
||||||
|
return fmt.Errorf("name is required")
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
kubeClient, err := utils.kubeClient(kubeconfig)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if exportAll {
|
||||||
|
var list notificationv1.AlertList
|
||||||
|
err = kubeClient.List(ctx, &list, client.InNamespace(namespace))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(list.Items) == 0 {
|
||||||
|
logger.Failuref("no alerts found in %s namespace", namespace)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, alert := range list.Items {
|
||||||
|
if err := exportAlert(alert); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
name := args[0]
|
||||||
|
namespacedName := types.NamespacedName{
|
||||||
|
Namespace: namespace,
|
||||||
|
Name: name,
|
||||||
|
}
|
||||||
|
var alert notificationv1.Alert
|
||||||
|
err = kubeClient.Get(ctx, namespacedName, &alert)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return exportAlert(alert)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func exportAlert(alert notificationv1.Alert) error {
|
||||||
|
gvk := notificationv1.GroupVersion.WithKind("Alert")
|
||||||
|
export := notificationv1.Alert{
|
||||||
|
TypeMeta: metav1.TypeMeta{
|
||||||
|
Kind: gvk.Kind,
|
||||||
|
APIVersion: gvk.GroupVersion().String(),
|
||||||
|
},
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: alert.Name,
|
||||||
|
Namespace: alert.Namespace,
|
||||||
|
Labels: alert.Labels,
|
||||||
|
Annotations: alert.Annotations,
|
||||||
|
},
|
||||||
|
Spec: alert.Spec,
|
||||||
|
}
|
||||||
|
|
||||||
|
data, err := yaml.Marshal(export)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("---")
|
||||||
|
fmt.Println(resourceToString(data))
|
||||||
|
return nil
|
||||||
|
}
|
@ -0,0 +1,87 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2020 The Flux CD contributors.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
corev1 "k8s.io/api/core/v1"
|
||||||
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||||
|
|
||||||
|
notificationv1 "github.com/fluxcd/notification-controller/api/v1beta1"
|
||||||
|
"github.com/fluxcd/pkg/apis/meta"
|
||||||
|
)
|
||||||
|
|
||||||
|
var getAlertCmd = &cobra.Command{
|
||||||
|
Use: "alert",
|
||||||
|
Aliases: []string{},
|
||||||
|
Short: "Get Alert statuses",
|
||||||
|
Long: "The get alert command prints the statuses of the resources.",
|
||||||
|
Example: ` # List all Alerts and their status
|
||||||
|
gotk get alert
|
||||||
|
`,
|
||||||
|
RunE: getAlertCmdRun,
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
getCmd.AddCommand(getAlertCmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getAlertCmdRun(cmd *cobra.Command, args []string) error {
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
kubeClient, err := utils.kubeClient(kubeconfig)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var list notificationv1.AlertList
|
||||||
|
err = kubeClient.List(ctx, &list, client.InNamespace(namespace))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(list.Items) == 0 {
|
||||||
|
logger.Failuref("no alerts found in %s namespace", namespace)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, alert := range list.Items {
|
||||||
|
if alert.Spec.Suspend {
|
||||||
|
logger.Successf("%s is suspended", alert.GetName())
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
isInitialized := false
|
||||||
|
if c := meta.GetCondition(alert.Status.Conditions, meta.ReadyCondition); c != nil {
|
||||||
|
switch c.Status {
|
||||||
|
case corev1.ConditionTrue:
|
||||||
|
logger.Successf("%s is ready", alert.GetName())
|
||||||
|
case corev1.ConditionUnknown:
|
||||||
|
logger.Successf("%s reconciling", alert.GetName())
|
||||||
|
default:
|
||||||
|
logger.Failuref("%s %s", alert.GetName(), c.Message)
|
||||||
|
}
|
||||||
|
isInitialized = true
|
||||||
|
}
|
||||||
|
if !isInitialized {
|
||||||
|
logger.Failuref("%s is not ready", alert.GetName())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
@ -0,0 +1,93 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2020 The Flux CD contributors.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"github.com/fluxcd/pkg/apis/meta"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
"k8s.io/apimachinery/pkg/types"
|
||||||
|
"k8s.io/apimachinery/pkg/util/wait"
|
||||||
|
|
||||||
|
notificationv1 "github.com/fluxcd/notification-controller/api/v1beta1"
|
||||||
|
)
|
||||||
|
|
||||||
|
var reconcileAlertCmd = &cobra.Command{
|
||||||
|
Use: "alert [name]",
|
||||||
|
Short: "Reconcile an Alert",
|
||||||
|
Long: `The reconcile alert command triggers a reconciliation of an Alert resource and waits for it to finish.`,
|
||||||
|
Example: ` # Trigger a reconciliation for an existing alert
|
||||||
|
gotk reconcile alert main
|
||||||
|
`,
|
||||||
|
RunE: reconcileAlertCmdRun,
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
reconcileCmd.AddCommand(reconcileAlertCmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
func reconcileAlertCmdRun(cmd *cobra.Command, args []string) error {
|
||||||
|
if len(args) < 1 {
|
||||||
|
return fmt.Errorf("alert name is required")
|
||||||
|
}
|
||||||
|
name := args[0]
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
kubeClient, err := utils.kubeClient(kubeconfig)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
namespacedName := types.NamespacedName{
|
||||||
|
Namespace: namespace,
|
||||||
|
Name: name,
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Actionf("annotating alert %s in %s namespace", name, namespace)
|
||||||
|
var alert notificationv1.Alert
|
||||||
|
err = kubeClient.Get(ctx, namespacedName, &alert)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if alert.Annotations == nil {
|
||||||
|
alert.Annotations = map[string]string{
|
||||||
|
meta.ReconcileAtAnnotation: time.Now().Format(time.RFC3339Nano),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
alert.Annotations[meta.ReconcileAtAnnotation] = time.Now().Format(time.RFC3339Nano)
|
||||||
|
}
|
||||||
|
if err := kubeClient.Update(ctx, &alert); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
logger.Successf("alert annotated")
|
||||||
|
|
||||||
|
logger.Waitingf("waiting for reconciliation")
|
||||||
|
if err := wait.PollImmediate(pollInterval, timeout,
|
||||||
|
isAlertReady(ctx, kubeClient, name, namespace)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Successf("alert reconciliation completed")
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
@ -0,0 +1,117 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2020 The Flux CD contributors.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"github.com/fluxcd/pkg/apis/meta"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
corev1 "k8s.io/api/core/v1"
|
||||||
|
"k8s.io/apimachinery/pkg/types"
|
||||||
|
"k8s.io/apimachinery/pkg/util/wait"
|
||||||
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||||
|
|
||||||
|
notificationv1 "github.com/fluxcd/notification-controller/api/v1beta1"
|
||||||
|
)
|
||||||
|
|
||||||
|
var resumeAlertCmd = &cobra.Command{
|
||||||
|
Use: "alert [name]",
|
||||||
|
Aliases: []string{},
|
||||||
|
Short: "Resume a suspended Alert",
|
||||||
|
Long: `The resume command marks a previously suspended Alert resource for reconciliation and waits for it to
|
||||||
|
finish the apply.`,
|
||||||
|
Example: ` # Resume reconciliation for an existing Alert
|
||||||
|
gotk resume alert main
|
||||||
|
`,
|
||||||
|
RunE: resumeAlertCmdRun,
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
resumeCmd.AddCommand(resumeAlertCmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
func resumeAlertCmdRun(cmd *cobra.Command, args []string) error {
|
||||||
|
if len(args) < 1 {
|
||||||
|
return fmt.Errorf("Alert name is required")
|
||||||
|
}
|
||||||
|
name := args[0]
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
kubeClient, err := utils.kubeClient(kubeconfig)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
namespacedName := types.NamespacedName{
|
||||||
|
Namespace: namespace,
|
||||||
|
Name: name,
|
||||||
|
}
|
||||||
|
var alert notificationv1.Alert
|
||||||
|
err = kubeClient.Get(ctx, namespacedName, &alert)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Actionf("resuming Alert %s in %s namespace", name, namespace)
|
||||||
|
alert.Spec.Suspend = false
|
||||||
|
if err := kubeClient.Update(ctx, &alert); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
logger.Successf("Alert resumed")
|
||||||
|
|
||||||
|
logger.Waitingf("waiting for Alert reconciliation")
|
||||||
|
if err := wait.PollImmediate(pollInterval, timeout,
|
||||||
|
isAlertResumed(ctx, kubeClient, name, namespace)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Successf("Alert reconciliation completed")
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func isAlertResumed(ctx context.Context, kubeClient client.Client, name, namespace string) wait.ConditionFunc {
|
||||||
|
return func() (bool, error) {
|
||||||
|
var alert notificationv1.Alert
|
||||||
|
namespacedName := types.NamespacedName{
|
||||||
|
Namespace: namespace,
|
||||||
|
Name: name,
|
||||||
|
}
|
||||||
|
|
||||||
|
err := kubeClient.Get(ctx, namespacedName, &alert)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if c := meta.GetCondition(alert.Status.Conditions, meta.ReadyCondition); c != nil {
|
||||||
|
switch c.Status {
|
||||||
|
case corev1.ConditionTrue:
|
||||||
|
return true, nil
|
||||||
|
case corev1.ConditionFalse:
|
||||||
|
if c.Reason == meta.SuspendedReason {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
return false, fmt.Errorf(c.Message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,76 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2020 The Flux CD contributors.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
"k8s.io/apimachinery/pkg/types"
|
||||||
|
|
||||||
|
notificationv1 "github.com/fluxcd/notification-controller/api/v1beta1"
|
||||||
|
)
|
||||||
|
|
||||||
|
var suspendAlertCmd = &cobra.Command{
|
||||||
|
Use: "alert [name]",
|
||||||
|
Aliases: []string{},
|
||||||
|
Short: "Suspend reconciliation of Alert",
|
||||||
|
Long: "The suspend command disables the reconciliation of a Alert resource.",
|
||||||
|
Example: ` # Suspend reconciliation for an existing Alert
|
||||||
|
gotk suspend alert main
|
||||||
|
`,
|
||||||
|
RunE: suspendAlertCmdRun,
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
suspendCmd.AddCommand(suspendAlertCmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
func suspendAlertCmdRun(cmd *cobra.Command, args []string) error {
|
||||||
|
if len(args) < 1 {
|
||||||
|
return fmt.Errorf("Alert name is required")
|
||||||
|
}
|
||||||
|
name := args[0]
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
kubeClient, err := utils.kubeClient(kubeconfig)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
namespacedName := types.NamespacedName{
|
||||||
|
Namespace: namespace,
|
||||||
|
Name: name,
|
||||||
|
}
|
||||||
|
var alert notificationv1.Alert
|
||||||
|
err = kubeClient.Get(ctx, namespacedName, &alert)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Actionf("suspending Alert %s in %s namespace", name, namespace)
|
||||||
|
alert.Spec.Suspend = true
|
||||||
|
if err := kubeClient.Update(ctx, &alert); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
logger.Successf("Alert suspended")
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
Loading…
Reference in New Issue