What happened?
Upjet generates Go struct tags with omitempty on all fields, including fields that are Required in Terraform and accept empty string "" as a valid value.
Example from provider-upjet-aws — SubscriptionFilterParameters in apis/cluster/cloudwatchlogs/v1beta1/zz_subscriptionfilter_types.go (source):
// Valid CloudWatch Logs filter pattern for subscribing to a filtered stream of log events.
// +kubebuilder:validation:Optional
FilterPattern *string `json:"filterPattern,omitempty" tf:"filter_pattern,omitempty"`
The Terraform schema marks this field as Required: true (docs), and Upjet correctly generates an XValidation rule enforcing it:
!('*' in self.managementPolicies || 'Create' in self.managementPolicies || 'Update' in self.managementPolicies) || has(self.forProvider.filterPattern) || (has(self.initProvider) && has(self.initProvider.filterPattern))
In Terraform, filter_pattern = "" is valid and means "match all log events." This is the most common usage for subscription filters that forward all logs to a destination.
When a composition function sets FilterPattern: ptr.To("") and converts to unstructured via composed.From(), the field is dropped. This is because composed.From() uses Kubernetes' runtime.DefaultUnstructuredConverter.ToUnstructured(), which omits *string fields pointing to "" when the struct tag includes omitempty.
The provider's admission webhook then rejects the resource:
spec.forProvider.filterPattern is a required parameter
Root cause
Kubernetes' runtime.DefaultUnstructuredConverter.ToUnstructured() treats a *string pointing to "" with an omitempty JSON tag as empty and omits it. This differs from standard encoding/json behavior, which preserves non-nil pointers regardless of the pointed-to value.
Verified:
// Standard json.Marshal with ptr.To("") + omitempty — PRESERVES the field:
// {"filterPattern":""}
// runtime.DefaultUnstructuredConverter.ToUnstructured() with ptr.To("") + omitempty — DROPS the field:
// (filterPattern absent from output)
Since the K8s converter behavior is intentional and stable, the fix should be in the generated struct tags.
Where to fix
The omitempty tag is generated by Upjet's code generation. The fix would be in the generator logic that produces struct tags — specifically, for fields where:
- The Terraform schema has
Required: true
- The field type is
TypeString
These fields should generate json:"filterPattern" (without omitempty) instead of json:"filterPattern,omitempty".
The Terraform provider source that defines this field: internal/service/logs/subscription_filter.go — filter_pattern is Required: true, Type: schema.TypeString.
Workaround
Manually set the field on the unstructured object after conversion:
desired, _ := composed.From(subscriptionFilter)
unstructured.SetNestedField(desired.Resource.Object, "", "spec", "forProvider", "filterPattern")
Affected provider
crossplane-contrib/provider-upjet-aws — SubscriptionFilter.cloudwatchlogs (filterPattern field)
- Likely any other Upjet-generated provider where a Terraform
Required: true string field accepts "" as valid
What happened?
Upjet generates Go struct tags with
omitemptyon all fields, including fields that are Required in Terraform and accept empty string""as a valid value.Example from
provider-upjet-aws—SubscriptionFilterParametersinapis/cluster/cloudwatchlogs/v1beta1/zz_subscriptionfilter_types.go(source):The Terraform schema marks this field as
Required: true(docs), and Upjet correctly generates an XValidation rule enforcing it:In Terraform,
filter_pattern = ""is valid and means "match all log events." This is the most common usage for subscription filters that forward all logs to a destination.When a composition function sets
FilterPattern: ptr.To("")and converts to unstructured viacomposed.From(), the field is dropped. This is becausecomposed.From()uses Kubernetes'runtime.DefaultUnstructuredConverter.ToUnstructured(), which omits*stringfields pointing to""when the struct tag includesomitempty.The provider's admission webhook then rejects the resource:
Root cause
Kubernetes'
runtime.DefaultUnstructuredConverter.ToUnstructured()treats a*stringpointing to""with anomitemptyJSON tag as empty and omits it. This differs from standardencoding/jsonbehavior, which preserves non-nil pointers regardless of the pointed-to value.Verified:
Since the K8s converter behavior is intentional and stable, the fix should be in the generated struct tags.
Where to fix
The
omitemptytag is generated by Upjet's code generation. The fix would be in the generator logic that produces struct tags — specifically, for fields where:Required: trueTypeStringThese fields should generate
json:"filterPattern"(withoutomitempty) instead ofjson:"filterPattern,omitempty".The Terraform provider source that defines this field:
internal/service/logs/subscription_filter.go—filter_patternisRequired: true, Type: schema.TypeString.Workaround
Manually set the field on the unstructured object after conversion:
Affected provider
crossplane-contrib/provider-upjet-aws—SubscriptionFilter.cloudwatchlogs(filterPatternfield)Required: truestring field accepts""as valid