Skip to content

Commit

Permalink
feat(controller): add support for mute timing
Browse files Browse the repository at this point in the history
  • Loading branch information
chenlujjj committed Jan 11, 2025
1 parent fbc0b9e commit f0b9e9b
Show file tree
Hide file tree
Showing 18 changed files with 1,820 additions and 0 deletions.
119 changes: 119 additions & 0 deletions api/v1beta1/grafanamutetiming_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
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"
)

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

// A unique name for the mute timing
Name string `json:"name"`

// Time intervals for muting
// +kubebuilder:validation:MinItems=1
TimeIntervals []*TimeInterval `json:"time_intervals"`

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

type TimeInterval struct {
// days of month
// +optional
DaysOfMonth []string `json:"days_of_month,omitempty"`

// location
// +optional
Location string `json:"location,omitempty"`

// months
// +optional
Months []string `json:"months,omitempty"`

// times
// +optional
Times []*TimeRange `json:"times,omitempty"`

// weekdays
// +optional
Weekdays []string `json:"weekdays,omitempty"`

// years
// +optional
Years []string `json:"years,omitempty"`
}

type TimeRange struct {
// start time
StartTime string `json:"start_time"`

// end time
EndTime string `json:"end_time"`
}

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

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

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

var _ CommonResource = (*GrafanaMuteTiming)(nil)

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

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

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

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

//+kubebuilder:object:root=true

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

func init() {
SchemeBuilder.Register(&GrafanaMuteTiming{}, &GrafanaMuteTimingList{})
}
86 changes: 86 additions & 0 deletions api/v1beta1/grafanamutetiming_types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package v1beta1

import (
"context"

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

func newMuteTiming(name string, editable *bool) *GrafanaMuteTiming {
return &GrafanaMuteTiming{
TypeMeta: v1.TypeMeta{
APIVersion: APIVersion,
Kind: "GrafanaMuteTiming",
},
ObjectMeta: v1.ObjectMeta{
Name: name,
Namespace: "default",
},
Spec: GrafanaMuteTimingSpec{
Editable: editable,
GrafanaCommonSpec: GrafanaCommonSpec{
InstanceSelector: &v1.LabelSelector{
MatchLabels: map[string]string{
"test": "mutetiming",
},
},
},
Name: name,
TimeIntervals: []*TimeInterval{
{
DaysOfMonth: []string{"1"},
Location: "Asia/Shanghai",
Months: []string{"1"},
Times: []*TimeRange{
{
StartTime: "00:00",
EndTime: "02:00",
},
},
Weekdays: []string{"1"},
Years: []string{"2025"},
},
},
},
}
}

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

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

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

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

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

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

By("Changing the existing editable")
mutetiming.Spec.Editable = &refFalse
Expect(k8sClient.Update(ctx, mutetiming)).To(HaveOccurred())
})
})
})
152 changes: 152 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 f0b9e9b

Please sign in to comment.