Skip to content

Commit

Permalink
feat: support notification template
Browse files Browse the repository at this point in the history
chore: pass linter
  • Loading branch information
chenlujjj committed Dec 30, 2024
1 parent 24ba0ca commit d2b665a
Show file tree
Hide file tree
Showing 16 changed files with 1,401 additions and 13 deletions.
84 changes: 84 additions & 0 deletions api/v1beta1/grafananotificationtemplate_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
Copyright 2022.
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 v1beta1

import (
"fmt"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// GrafanaNotificationTemplateSpec defines the desired state of GrafanaNotificationTemplate
// +kubebuilder:validation:XValidation:rule="((!has(oldSelf.editable) && !has(self.editable)) || (has(oldSelf.editable) && has(self.editable)))", message="spec.editable is immutable"
type GrafanaNotificationTemplateSpec struct {
GrafanaCommonSpec `json:",inline"`

// Template name
Name string `json:"name"`

// Template content
Template string `json:"template,omitempty"`

// Whether to enable or disable editing of the notification template in Grafana UI
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="spec.editable is immutable"
// +optional
Editable *bool `json:"editable,omitempty"`
}

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status

// GrafanaNotificationTemplate is the Schema for the GrafanaNotificationTemplate API
// +kubebuilder:resource:categories={grafana-operator}
type GrafanaNotificationTemplate struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec GrafanaNotificationTemplateSpec `json:"spec,omitempty"`
Status GrafanaCommonStatus `json:"status,omitempty"`
}

var _ CommonResource = (*GrafanaNotificationTemplate)(nil)

func (in *GrafanaNotificationTemplate) MatchLabels() *metav1.LabelSelector {
return in.Spec.InstanceSelector
}

func (in *GrafanaNotificationTemplate) MatchNamespace() string {
return in.ObjectMeta.Namespace
}

func (in *GrafanaNotificationTemplate) AllowCrossNamespace() bool {
return in.Spec.AllowCrossNamespaceImport
}

func (np *GrafanaNotificationTemplate) NamespacedResource() string {
return fmt.Sprintf("%v/%v/%v", np.ObjectMeta.Namespace, np.ObjectMeta.Name, np.ObjectMeta.UID)
}

//+kubebuilder:object:root=true

// GrafanaNotificationTemplateList contains a list of GrafanaNotificationTemplate
type GrafanaNotificationTemplateList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []GrafanaNotificationTemplate `json:"items"`
}

func init() {
SchemeBuilder.Register(&GrafanaNotificationTemplate{}, &GrafanaNotificationTemplateList{})
}
72 changes: 72 additions & 0 deletions api/v1beta1/grafananotificationtemplate_types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package v1beta1

import (
"context"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func newNotificationTemplate(name string, editable *bool) *GrafanaNotificationTemplate {
return &GrafanaNotificationTemplate{
TypeMeta: v1.TypeMeta{
APIVersion: APIVersion,
Kind: "GrafanaNotificationTemplate",
},
ObjectMeta: v1.ObjectMeta{
Name: name,
Namespace: "default",
},
Spec: GrafanaNotificationTemplateSpec{
Editable: editable,
GrafanaCommonSpec: GrafanaCommonSpec{
InstanceSelector: &v1.LabelSelector{
MatchLabels: map[string]string{
"test": "notificationtemplate",
},
},
},
Name: name,
Template: "mock template",
},
}
}

var _ = Describe("NotificationTemplate type", func() {
Context("Ensure NotificationTemplate spec.editable is immutable", func() {
ctx := context.Background()
refTrue := true
refFalse := false

It("Should block adding editable field when missing", func() {
notificationtemplate := newNotificationTemplate("missing-editable", nil)
By("Create new NotificationTemplate without editable")
Expect(k8sClient.Create(ctx, notificationtemplate)).To(Succeed())

By("Adding a editable")
notificationtemplate.Spec.Editable = &refTrue
Expect(k8sClient.Update(ctx, notificationtemplate)).To(HaveOccurred())
})

It("Should block removing editable field when set", func() {
notificationtemplate := newNotificationTemplate("existing-editable", &refTrue)
By("Creating NotificationTemplate with existing editable")
Expect(k8sClient.Create(ctx, notificationtemplate)).To(Succeed())

By("And setting editable to ''")
notificationtemplate.Spec.Editable = nil
Expect(k8sClient.Update(ctx, notificationtemplate)).To(HaveOccurred())
})

It("Should block changing value of editable", func() {
notificationtemplate := newNotificationTemplate("removing-editable", &refTrue)
By("Create new NotificationTemplate with existing editable")
Expect(k8sClient.Create(ctx, notificationtemplate)).To(Succeed())

By("Changing the existing editable")
notificationtemplate.Spec.Editable = &refFalse
Expect(k8sClient.Update(ctx, notificationtemplate)).To(HaveOccurred())
})
})
})
80 changes: 80 additions & 0 deletions api/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit d2b665a

Please sign in to comment.