Skip to content

Commit

Permalink
feat: Implement spec.uid for ContactPoints (#1730)
Browse files Browse the repository at this point in the history
  • Loading branch information
Baarsgaard authored Nov 11, 2024
1 parent 3b42b31 commit 466366f
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ vet: ## Run go vet against code.
go vet ./...

.PHONY: test
test: manifests generate code/gofumpt api-docs vet envtest ## Run tests.
test: manifests generate code/gofumpt code/golangci-lint api-docs vet envtest ## Run tests.
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... -coverprofile cover.out

##@ Build
Expand Down
14 changes: 14 additions & 0 deletions api/v1beta1/grafanacontactpoint_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ import (
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

// GrafanaContactPointSpec defines the desired state of GrafanaContactPoint
// +kubebuilder:validation:XValidation:rule="((!has(oldSelf.uid) && !has(self.uid)) || (has(oldSelf.uid) && has(self.uid)))", message="spec.uid is immutable"
type GrafanaContactPointSpec struct {
// Manually specify the UID the Contact Point is created with
// +optional
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="spec.uid is immutable"
CustomUID string `json:"uid,omitempty"`

// +optional
// +kubebuilder:validation:Type=string
// +kubebuilder:validation:Format=duration
Expand Down Expand Up @@ -84,6 +90,14 @@ type GrafanaContactPointList struct {
Items []GrafanaContactPoint `json:"items"`
}

// Wrapper around CustomUID or default metadata.uid
func (in *GrafanaContactPoint) CustomUIDOrUID() string {
if in.Spec.CustomUID != "" {
return in.Spec.CustomUID
}
return string(in.ObjectMeta.UID)
}

func init() {
SchemeBuilder.Register(&GrafanaContactPoint{}, &GrafanaContactPointList{})
}
11 changes: 11 additions & 0 deletions config/crd/bases/grafana.integreatly.org_grafanacontactpoints.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ spec:
- hipchat
- oncall
type: string
uid:
description: Manually specify the UID the Contact Point is created
with
type: string
x-kubernetes-validations:
- message: spec.uid is immutable
rule: self == oldSelf
valuesFrom:
items:
properties:
Expand Down Expand Up @@ -199,6 +206,10 @@ spec:
- name
- settings
type: object
x-kubernetes-validations:
- message: spec.uid is immutable
rule: ((!has(oldSelf.uid) && !has(self.uid)) || (has(oldSelf.uid) &&
has(self.uid)))
status:
description: GrafanaContactPointStatus defines the observed state of GrafanaContactPoint
properties:
Expand Down
6 changes: 3 additions & 3 deletions controllers/grafanacontactpoint_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func (r *GrafanaContactPointReconciler) reconcileWithInstance(ctx context.Contex
Name: contactPoint.Spec.Name,
Type: &contactPoint.Spec.Type,
Settings: settings,
UID: string(contactPoint.UID),
UID: contactPoint.CustomUIDOrUID(),
}
_, err := cl.Provisioning.PostContactpoints(provisioning.NewPostContactpointsParams().WithBody(cp)) //nolint:errcheck
if err != nil {
Expand Down Expand Up @@ -245,7 +245,7 @@ func (r *GrafanaContactPointReconciler) getContactPointFromUID(ctx context.Conte
return models.EmbeddedContactPoint{}, fmt.Errorf("getting contact points: %w", err)
}
for _, cp := range remote.Payload {
if cp.UID == string(contactPoint.UID) {
if cp.UID == contactPoint.CustomUIDOrUID() {
return *cp, nil
}
}
Expand Down Expand Up @@ -279,7 +279,7 @@ func (r *GrafanaContactPointReconciler) removeFromInstance(ctx context.Context,
if err != nil {
return fmt.Errorf("getting contact point by UID: %w", err)
}
_, err = cl.Provisioning.DeleteContactpoints(string(contactPoint.UID)) //nolint:errcheck
_, err = cl.Provisioning.DeleteContactpoints(contactPoint.CustomUIDOrUID()) //nolint:errcheck
if err != nil {
return fmt.Errorf("deleting contact point: %w", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ spec:
- hipchat
- oncall
type: string
uid:
description: Manually specify the UID the Contact Point is created
with
type: string
x-kubernetes-validations:
- message: spec.uid is immutable
rule: self == oldSelf
valuesFrom:
items:
properties:
Expand Down Expand Up @@ -199,6 +206,10 @@ spec:
- name
- settings
type: object
x-kubernetes-validations:
- message: spec.uid is immutable
rule: ((!has(oldSelf.uid) && !has(self.uid)) || (has(oldSelf.uid) &&
has(self.uid)))
status:
description: GrafanaContactPointStatus defines the observed state of GrafanaContactPoint
properties:
Expand Down
11 changes: 11 additions & 0 deletions deploy/kustomize/base/crds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,13 @@ spec:
- hipchat
- oncall
type: string
uid:
description: Manually specify the UID the Contact Point is created
with
type: string
x-kubernetes-validations:
- message: spec.uid is immutable
rule: self == oldSelf
valuesFrom:
items:
properties:
Expand Down Expand Up @@ -507,6 +514,10 @@ spec:
- name
- settings
type: object
x-kubernetes-validations:
- message: spec.uid is immutable
rule: ((!has(oldSelf.uid) && !has(self.uid)) || (has(oldSelf.uid) &&
has(self.uid)))
status:
description: GrafanaContactPointStatus defines the observed state of GrafanaContactPoint
properties:
Expand Down
11 changes: 11 additions & 0 deletions docs/docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,8 @@ GrafanaContactPoint is the Schema for the grafanacontactpoints API
<td>object</td>
<td>
GrafanaContactPointSpec defines the desired state of GrafanaContactPoint<br/>
<br/>
<i>Validations</i>:<li>((!has(oldSelf.uid) && !has(self.uid)) || (has(oldSelf.uid) && has(self.uid))): spec.uid is immutable</li>
</td>
<td>false</td>
</tr><tr>
Expand Down Expand Up @@ -747,6 +749,15 @@ GrafanaContactPointSpec defines the desired state of GrafanaContactPoint
<i>Enum</i>: alertmanager, prometheus-alertmanager, dingding, discord, email, googlechat, kafka, line, opsgenie, pagerduty, pushover, sensugo, sensu, slack, teams, telegram, threema, victorops, webhook, wecom, hipchat, oncall<br/>
</td>
<td>false</td>
</tr><tr>
<td><b>uid</b></td>
<td>string</td>
<td>
Manually specify the UID the Contact Point is created with<br/>
<br/>
<i>Validations</i>:<li>self == oldSelf: spec.uid is immutable</li>
</td>
<td>false</td>
</tr><tr>
<td><b><a href="#grafanacontactpointspecvaluesfromindex">valuesFrom</a></b></td>
<td>[]object</td>
Expand Down

0 comments on commit 466366f

Please sign in to comment.