Skip to content

Commit fc172a9

Browse files
authored
fix: missing immutable setup under type_def (#649)
This PR fixes an issue where the `is_immutable` configuration was not being applied to nested fields (e.g. `HealthCheckConfig.Type`). Verified using `route53-controller`. The generator now correctly applies validation markers to immutable nested fields in `HealthCheckConfig`, resulting in the correct `x-kubernetes-validations` in the output CRD. ```diff diff --git a/apis/v1alpha1/types.go b/apis/v1alpha1/types.go index afc17d0..e25f221 100644 --- a/apis/v1alpha1/types.go +++ b/apis/v1alpha1/types.go @@ -148,14 +148,18 @@ type HealthCheckConfig struct { IPAddress *string `json:"ipAddress,omitempty"` InsufficientDataHealthStatus *string `json:"insufficientDataHealthStatus,omitempty"` Inverted *bool `json:"inverted,omitempty"` - MeasureLatency *bool `json:"measureLatency,omitempty"` - Port *int64 `json:"port,omitempty"` - Regions []*string `json:"regions,omitempty"` - RequestInterval *int64 `json:"requestInterval,omitempty"` - ResourcePath *string `json:"resourcePath,omitempty"` - RoutingControlARN *string `json:"routingControlARN,omitempty"` - SearchString *string `json:"searchString,omitempty"` - Type *string `json:"type,omitempty"` + // +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Value is immutable once set" + MeasureLatency *bool `json:"measureLatency,omitempty"` + Port *int64 `json:"port,omitempty"` + Regions []*string `json:"regions,omitempty"` + // +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Value is immutable once set" + RequestInterval *int64 `json:"requestInterval,omitempty"` + ResourcePath *string `json:"resourcePath,omitempty"` + // +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Value is immutable once set" + RoutingControlARN *string `json:"routingControlARN,omitempty"` + SearchString *string `json:"searchString,omitempty"` + // +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Value is immutable once set" + Type *string `json:"type,omitempty"` } // A complex type that contains the last failure reason as reported by one Amazon diff --git a/config/crd/bases/route53.services.k8s.aws_healthchecks.yaml b/config/crd/bases/route53.services.k8s.aws_healthchecks.yaml index ea5e040..d55c176 100644 --- a/config/crd/bases/route53.services.k8s.aws_healthchecks.yaml +++ b/config/crd/bases/route53.services.k8s.aws_healthchecks.yaml @@ -82,6 +82,9 @@ spec: type: string measureLatency: type: boolean + x-kubernetes-validations: + - message: Value is immutable once set + rule: self == oldSelf port: format: int64 type: integer @@ -92,14 +95,23 @@ spec: requestInterval: format: int64 type: integer + x-kubernetes-validations: + - message: Value is immutable once set + rule: self == oldSelf resourcePath: type: string routingControlARN: type: string + x-kubernetes-validations: + - message: Value is immutable once set + rule: self == oldSelf searchString: type: string type: type: string + x-kubernetes-validations: + - message: Value is immutable once set + rule: self == oldSelf type: object tags: description: |- ``` By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent e743d68 commit fc172a9

3 files changed

Lines changed: 20 additions & 4 deletions

File tree

pkg/model/attr.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ import (
2121
)
2222

2323
type Attr struct {
24-
Names names.Names
25-
GoType string
26-
Shape *awssdkmodel.Shape
27-
GoTag string
24+
Names names.Names
25+
GoType string
26+
Shape *awssdkmodel.Shape
27+
GoTag string
28+
IsImmutable bool
2829
}
2930

3031
func NewAttr(

pkg/model/model.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,9 @@ func (m *Model) processNestedFieldTypeDefs(
541541
if field.FieldConfig.GoTag != nil {
542542
setTypeDefAttributeGoTag(crd, fieldPath, field, tdefs)
543543
}
544+
if field.IsImmutable() {
545+
setTypeDefAttributeImmutable(crd, fieldPath, tdefs)
546+
}
544547
}
545548
}
546549
}
@@ -674,6 +677,15 @@ func setTypeDefAttributeGoTag(crd *CRD, fieldPath string, f *Field, tdefs []*Typ
674677
}
675678
}
676679

680+
// setTypeDefAttributeImmutable sets the IsImmutable flag for the corresponding
681+
// attribute represented by fieldPath of nested field.
682+
func setTypeDefAttributeImmutable(crd *CRD, fieldPath string, tdefs []*TypeDef) {
683+
_, fieldAttr := getAttributeFromPath(crd, fieldPath, tdefs)
684+
if fieldAttr != nil {
685+
fieldAttr.IsImmutable = true
686+
}
687+
}
688+
677689
// updateTypeDefAttributeWithReference adds a new AWSResourceReference attribute
678690
// for the corresponding attribute represented by fieldPath of nested field
679691
func updateTypeDefAttributeWithReference(crd *CRD, fieldPath string, tdefs []*TypeDef) {

templates/apis/type_def.go.tpl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ type {{ .Names.Camel }} struct {
77
{{- if $attr.Shape.Documentation }}
88
{{ $attr.Shape.Documentation }}
99
{{- end }}
10+
{{- if $attr.IsImmutable }}
11+
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Value is immutable once set"
12+
{{- end }}
1013
{{ $attr.Names.Camel }} {{ $attr.GoType }} {{ $attr.GetGoTag }}
1114
{{- end }}
1215
}

0 commit comments

Comments
 (0)