-
Notifications
You must be signed in to change notification settings - Fork 401
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
1,748 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
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"` | ||
|
||
TimeIntervals []*TimeInterval `json:"time_intervals,omitempty"` | ||
|
||
// 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 { | ||
DaysOfMonth []string `json:"days_of_month"` | ||
Location string `json:"location"` | ||
Months []string `json:"months"` | ||
Times []*TimeRange `json:"times"` | ||
Weekdays []string `json:"weekdays"` | ||
Years []string `json:"years"` | ||
} | ||
|
||
type TimeRange struct { | ||
StartTime string `json:"start_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{}) | ||
} |
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,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()) | ||
}) | ||
}) | ||
}) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.