diff --git a/pkg/model/attr.go b/pkg/model/attr.go index 5a29c9d5..701322f7 100644 --- a/pkg/model/attr.go +++ b/pkg/model/attr.go @@ -21,10 +21,11 @@ import ( ) type Attr struct { - Names names.Names - GoType string - Shape *awssdkmodel.Shape - GoTag string + Names names.Names + GoType string + Shape *awssdkmodel.Shape + GoTag string + IsImmutable bool } func NewAttr( diff --git a/pkg/model/model.go b/pkg/model/model.go index eb0594e5..5df18a74 100644 --- a/pkg/model/model.go +++ b/pkg/model/model.go @@ -541,6 +541,9 @@ func (m *Model) processNestedFieldTypeDefs( if field.FieldConfig.GoTag != nil { setTypeDefAttributeGoTag(crd, fieldPath, field, tdefs) } + if field.IsImmutable() { + setTypeDefAttributeImmutable(crd, fieldPath, tdefs) + } } } } @@ -674,6 +677,15 @@ func setTypeDefAttributeGoTag(crd *CRD, fieldPath string, f *Field, tdefs []*Typ } } +// setTypeDefAttributeImmutable sets the IsImmutable flag for the corresponding +// attribute represented by fieldPath of nested field. +func setTypeDefAttributeImmutable(crd *CRD, fieldPath string, tdefs []*TypeDef) { + _, fieldAttr := getAttributeFromPath(crd, fieldPath, tdefs) + if fieldAttr != nil { + fieldAttr.IsImmutable = true + } +} + // updateTypeDefAttributeWithReference adds a new AWSResourceReference attribute // for the corresponding attribute represented by fieldPath of nested field func updateTypeDefAttributeWithReference(crd *CRD, fieldPath string, tdefs []*TypeDef) { diff --git a/templates/apis/type_def.go.tpl b/templates/apis/type_def.go.tpl index 6034ea71..0166fe9f 100644 --- a/templates/apis/type_def.go.tpl +++ b/templates/apis/type_def.go.tpl @@ -7,6 +7,9 @@ type {{ .Names.Camel }} struct { {{- if $attr.Shape.Documentation }} {{ $attr.Shape.Documentation }} {{- end }} + {{- if $attr.IsImmutable }} + // +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Value is immutable once set" + {{- end }} {{ $attr.Names.Camel }} {{ $attr.GoType }} {{ $attr.GetGoTag }} {{- end }} }