Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(controller): add support for mute timing #1817

Merged
merged 1 commit into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 125 additions & 0 deletions api/v1beta1/grafanamutetiming_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
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
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
// +kubebuilder:default=true
Editable bool `json:"editable"`
}

type TimeInterval struct {
// The date 1-31 of a month. Negative values can also be used to represent days that begin at the end of the month.
// For example: -1 for the last day of the month.
// +optional
DaysOfMonth []string `json:"days_of_month,omitempty"`

// Depending on the location, the time range is displayed in local time.
// +optional
Location string `json:"location,omitempty"`

// The months of the year in either numerical or the full calendar month.
// For example: 1, may.
// +optional
Months []string `json:"months,omitempty"`

// The time inclusive of the start and exclusive of the end time (in UTC if no location has been selected, otherwise local time).
// +optional
Times []*TimeRange `json:"times,omitempty"`

// The day or range of days of the week.
// For example: monday, thursday
// +optional
Weekdays []string `json:"weekdays,omitempty"`

// The year or years for the interval.
// For example: 2021
// +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:printcolumn:name="Last resync",type="date",format="date-time",JSONPath=".status.lastResync",description=""
// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp",description=""
// +kubebuilder:resource:categories={grafana-operator}
chenlujjj marked this conversation as resolved.
Show resolved Hide resolved
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{})
}
64 changes: 64 additions & 0 deletions api/v1beta1/grafanamutetiming_types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
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()

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

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