Skip to content

Commit d241fa5

Browse files
authored
feat(datasinkref): openapi schema spec defaulting (#80)
empty object triggers nested defaulting when no dataSinkRef is provided On-behalf-of: @SAP christopher.junk@sap.com Signed-off-by: Christopher Junk <christopher.junk@sap.com>
1 parent 1798cee commit d241fa5

9 files changed

+45
-10
lines changed

api/v1alpha1/federatedmanagedmetric_types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type FederatedManagedMetricSpec struct {
4545
// If not specified, the DataSink named "default" in the operator's
4646
// namespace will be used.
4747
// +optional
48+
// +kubebuilder:default:={}
4849
DataSinkRef *DataSinkReference `json:"dataSinkRef,omitempty"`
4950

5051
FederatedClusterAccessRef FederateClusterAccessRef `json:"federateClusterAccessRef,omitempty"`

api/v1alpha1/federatedmetric_types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ type FederatedMetricSpec struct {
4848
// If not specified, the DataSink named "default" in the operator's
4949
// namespace will be used.
5050
// +optional
51+
// +kubebuilder:default:={}
5152
DataSinkRef *DataSinkReference `json:"dataSinkRef,omitempty"`
5253

5354
FederatedClusterAccessRef FederateClusterAccessRef `json:"federateClusterAccessRef,omitempty"`

api/v1alpha1/managedmetric_types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ type ManagedMetricSpec struct {
5050
// If not specified, the DataSink named "default" in the operator's
5151
// namespace will be used.
5252
// +optional
53+
// +kubebuilder:default:={}
5354
DataSinkRef *DataSinkReference `json:"dataSinkRef,omitempty"`
5455

5556
// +optional

api/v1alpha1/metric_types.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ const (
3939
// DataSinkReference holds a reference to a DataSink resource.
4040
type DataSinkReference struct {
4141
// Name is the name of the DataSink resource.
42-
// +kubebuilder:validation:Required
43-
Name string `json:"name"`
42+
// +optional
43+
// +kubebuilder:default:="default"
44+
Name string `json:"name,omitempty"`
4445
}
4546

4647
// MetricSpec defines the desired state of Metric
@@ -66,6 +67,7 @@ type MetricSpec struct {
6667
// If not specified, the DataSink named "default" in the operator's
6768
// namespace will be used.
6869
// +optional
70+
// +kubebuilder:default:={}
6971
DataSinkRef *DataSinkReference `json:"dataSinkRef,omitempty"`
7072

7173
// +optional

cmd/metrics-operator/embedded/crds/metrics.openmcp.cloud_federatedmanagedmetrics.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,16 @@ spec:
4141
description: FederatedManagedMetricSpec defines the desired state of FederatedManagedMetric
4242
properties:
4343
dataSinkRef:
44+
default: {}
4445
description: |-
4546
DataSinkRef specifies the DataSink to be used for this federated managed metric.
4647
If not specified, the DataSink named "default" in the operator's
4748
namespace will be used.
4849
properties:
4950
name:
51+
default: default
5052
description: Name is the name of the DataSink resource.
5153
type: string
52-
required:
53-
- name
5454
type: object
5555
description:
5656
type: string

cmd/metrics-operator/embedded/crds/metrics.openmcp.cloud_federatedmetrics.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,16 @@ spec:
4040
description: FederatedMetricSpec defines the desired state of FederatedMetric
4141
properties:
4242
dataSinkRef:
43+
default: {}
4344
description: |-
4445
DataSinkRef specifies the DataSink to be used for this federated metric.
4546
If not specified, the DataSink named "default" in the operator's
4647
namespace will be used.
4748
properties:
4849
name:
50+
default: default
4951
description: Name is the name of the DataSink resource.
5052
type: string
51-
required:
52-
- name
5353
type: object
5454
description:
5555
type: string

cmd/metrics-operator/embedded/crds/metrics.openmcp.cloud_managedmetrics.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,16 @@ spec:
5050
description: ManagedMetricSpec defines the desired state of ManagedMetric
5151
properties:
5252
dataSinkRef:
53+
default: {}
5354
description: |-
5455
DataSinkRef specifies the DataSink to be used for this managed metric.
5556
If not specified, the DataSink named "default" in the operator's
5657
namespace will be used.
5758
properties:
5859
name:
60+
default: default
5961
description: Name is the name of the DataSink resource.
6062
type: string
61-
required:
62-
- name
6363
type: object
6464
description:
6565
description: Sets the description that will be used to identify the

cmd/metrics-operator/embedded/crds/metrics.openmcp.cloud_metrics.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,16 @@ spec:
5050
description: MetricSpec defines the desired state of Metric
5151
properties:
5252
dataSinkRef:
53+
default: {}
5354
description: |-
5455
DataSinkRef specifies the DataSink to be used for this metric.
5556
If not specified, the DataSink named "default" in the operator's
5657
namespace will be used.
5758
properties:
5859
name:
60+
default: default
5961
description: Name is the name of the DataSink resource.
6062
type: string
61-
required:
62-
- name
6363
type: object
6464
description:
6565
description: Sets the description that will be used to identify the

internal/controller/metric_controller_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,36 @@ func TestMetricController(t *testing.T) {
165165
t.Run("TestReconcileMetricHappyPath", testReconcileMetricHappyPath)
166166
t.Run("TestReconcileMetricNotFound", testReconcileMetricNotFound)
167167
t.Run("TestReconcileDataSinkNotFound", testReconcileSecretNotFound)
168+
t.Run("TestDataSinkRefDefault", testDataSinkRefDefault)
169+
}
170+
171+
func testDataSinkRefDefault(t *testing.T) {
172+
metric := types.NamespacedName{
173+
Name: "data-sink-default",
174+
Namespace: "default",
175+
}
176+
ctx := context.Background()
177+
178+
// create metric
179+
in := &v1alpha1.Metric{
180+
ObjectMeta: metav1.ObjectMeta{
181+
Name: metric.Name,
182+
Namespace: metric.Namespace,
183+
},
184+
Spec: v1alpha1.MetricSpec{
185+
Name: "test",
186+
},
187+
}
188+
err := k8sClient.Create(ctx, in)
189+
require.NoError(t, err, "failed to create metric")
190+
191+
// fetch metric
192+
var out v1alpha1.Metric
193+
err = k8sClient.Get(ctx, metric, &out)
194+
require.NoError(t, err, "failed to fetch metric")
195+
196+
// verify result
197+
require.Equal(t, &v1alpha1.DataSinkReference{Name: "default"}, out.Spec.DataSinkRef)
168198
}
169199

170200
// testReconcileMetricNotFound tests the behavior when the Metric is not found

0 commit comments

Comments
 (0)