-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #86 from newrelic/addAlertNotifications
feat(alertChannel): add initial support for alertsChannel CRD
- Loading branch information
Showing
54 changed files
with
2,870 additions
and
463 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ Currently the operator supports managing the following resources: | |
- Alert Policies | ||
- NRQL Alert Conditions. | ||
- Alert Conditions for APM, Browser and mobile | ||
- Alert Channels | ||
|
||
|
||
# Quick Start | ||
|
@@ -198,6 +199,44 @@ The operator will create and update alert policies and NRQL alert conditions as | |
region: "US" | ||
``` | ||
### Create an Alerts Channel | ||
1. We'll be using the following [example alerts channel](/examples/example_alerts_channel.yaml) configuration file. You will need to update the [`api_key`](/examples/example_alerts_channel.yaml#6) field with your New Relic [personal API key](https://docs.newrelic.com/docs/apis/get-started/intro-apis/types-new-relic-api-keys#personal-api-key). <br> | ||
|
||
**examples/example_alerts_channel.yaml** | ||
|
||
```yaml | ||
apiVersion: nr.k8s.newrelic.com/v1 | ||
kind: AlertsChannel | ||
metadata: | ||
name: my-channel1 | ||
spec: | ||
api_key: <your New Relic personal API key> | ||
# api_key_secret: | ||
# name: nr-api-key | ||
# namespace: default | ||
# key_name: api-key | ||
name: "my alert channel" | ||
region: "US" | ||
type: "email" | ||
links: | ||
# Policy links can be by NR PolicyID, NR PolicyName AND/OR K8s AlertPolicy object reference | ||
policy_ids: | ||
- 1 | ||
policy_names: | ||
- "k8s created policy" | ||
policy_kubernetes_objects: | ||
- name: "my-policy" | ||
namespace: "default" | ||
configuration: | ||
recipients: "[email protected]" | ||
``` | ||
|
||
> <small>**Note:** The New Relic Alerts API does not allow updating Alerts Channels. In order to change a channel, you will need to either rename the k8s AlertsChannel object to create a new one and delete the old one or manually delete the k8s AlertsChannel object and create a new one. </small> | ||
|
||
|
||
|
||
### Uninstall the operator | ||
|
||
The Operator can be removed with the reverse of installation, namely building the kubernetes resource files with `kustomize` and running `kubectl delete` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package v1 | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/newrelic/newrelic-client-go/pkg/alerts" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
// AlertsChannelSpec defines the desired state of AlertsChannel | ||
type AlertsChannelSpec struct { | ||
ID int `json:"id,omitempty"` | ||
Name string `json:"name"` | ||
APIKey string `json:"api_key,omitempty"` | ||
APIKeySecret NewRelicAPIKeySecret `json:"api_key_secret,omitempty"` | ||
Region string `json:"region,omitempty"` | ||
Type string `json:"type,omitempty"` | ||
Links ChannelLinks `json:"links,omitempty"` | ||
Configuration AlertsChannelConfiguration `json:"configuration,omitempty"` | ||
} | ||
|
||
// ChannelLinks - copy of alerts.ChannelLinks | ||
type ChannelLinks struct { | ||
PolicyIDs []int `json:"policy_ids,omitempty"` | ||
PolicyNames []string `json:"policy_names,omitempty"` | ||
PolicyKubernetesObjects []metav1.ObjectMeta `json:"policy_kubernetes_objects,omitempty"` | ||
} | ||
|
||
// AlertsChannelStatus defines the observed state of AlertsChannel | ||
type AlertsChannelStatus struct { | ||
AppliedSpec *AlertsChannelSpec `json:"applied_spec"` | ||
ChannelID int `json:"channel_id"` | ||
AppliedPolicyIDs []int `json:"appliedPolicyIDs"` | ||
} | ||
|
||
// AlertsChannelConfiguration - copy of alerts.ChannelConfiguration | ||
type AlertsChannelConfiguration struct { | ||
Recipients string `json:"recipients,omitempty"` | ||
IncludeJSONAttachment string `json:"include_json_attachment,omitempty"` | ||
AuthToken string `json:"auth_token,omitempty"` | ||
APIKey string `json:"api_key,omitempty"` | ||
Teams string `json:"teams,omitempty"` | ||
Tags string `json:"tags,omitempty"` | ||
URL string `json:"url,omitempty"` | ||
Channel string `json:"channel,omitempty"` | ||
Key string `json:"key,omitempty"` | ||
RouteKey string `json:"route_key,omitempty"` | ||
ServiceKey string `json:"service_key,omitempty"` | ||
BaseURL string `json:"base_url,omitempty"` | ||
AuthUsername string `json:"auth_username,omitempty"` | ||
AuthPassword string `json:"auth_password,omitempty"` | ||
PayloadType string `json:"payload_type,omitempty"` | ||
Region string `json:"region,omitempty"` | ||
UserID string `json:"user_id,omitempty"` | ||
} | ||
|
||
// +kubebuilder:object:root=true | ||
// +kubebuilder:printcolumn:name="Created",type="boolean",JSONPath=".status.created" | ||
|
||
// AlertsChannel is the Schema for the AlertsChannel API | ||
type AlertsChannel struct { | ||
metav1.TypeMeta `json:",inline"` | ||
metav1.ObjectMeta `json:"metadata,omitempty"` | ||
|
||
Spec AlertsChannelSpec `json:"spec,omitempty"` | ||
Status AlertsChannelStatus `json:"status,omitempty"` | ||
} | ||
|
||
// +kubebuilder:object:root=true | ||
|
||
// AlertsChannelList contains a list of AlertsChannel | ||
type AlertsChannelList struct { | ||
metav1.TypeMeta `json:",inline"` | ||
metav1.ListMeta `json:"metadata,omitempty"` | ||
Items []AlertsChannel `json:"items"` | ||
} | ||
|
||
func init() { | ||
SchemeBuilder.Register(&AlertsChannel{}, &AlertsChannelList{}) | ||
} | ||
|
||
// APIChannel - Converts AlertsChannelSpec object to alerts.Channel | ||
func (in AlertsChannelSpec) APIChannel() alerts.Channel { | ||
jsonString, _ := json.Marshal(in) | ||
|
||
var APIChannel alerts.Channel | ||
json.Unmarshal(jsonString, &APIChannel) //nolint | ||
APIChannel.Links = alerts.ChannelLinks{} | ||
|
||
return APIChannel | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package v1 | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
|
||
"github.com/newrelic/newrelic-client-go/pkg/alerts" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("AlertsChannelSpec", func() { | ||
var alertsChannelSpec AlertsChannelSpec | ||
|
||
BeforeEach(func() { | ||
alertsChannelSpec = AlertsChannelSpec{ | ||
ID: 88, | ||
Name: "my alert channel", | ||
APIKey: "api-key", | ||
APIKeySecret: NewRelicAPIKeySecret{}, | ||
Region: "US", | ||
Type: "email", | ||
Links: ChannelLinks{ | ||
PolicyIDs: []int{ | ||
1, | ||
2, | ||
}, | ||
}, | ||
Configuration: AlertsChannelConfiguration{ | ||
Recipients: "[email protected]", | ||
}, | ||
} | ||
|
||
}) | ||
|
||
Describe("APIChannel", func() { | ||
It("converts AlertsChannelSpec object to alerts.Channel object from go client, retaining field values", func() { | ||
apiChannel := alertsChannelSpec.APIChannel() | ||
|
||
Expect(fmt.Sprint(reflect.TypeOf(apiChannel))).To(Equal("alerts.Channel")) | ||
Expect(apiChannel.ID).To(Equal(88)) | ||
Expect(apiChannel.Type).To(Equal(alerts.ChannelTypes.Email)) | ||
Expect(apiChannel.Name).To(Equal("my alert channel")) | ||
apiConfiguration := apiChannel.Configuration | ||
Expect(apiConfiguration.Recipients).To(Equal("[email protected]")) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.