Skip to content

Generated omitempty tags prevent setting required fields to empty string in composition functions #653

@ajnye

Description

@ajnye

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-awsSubscriptionFilterParameters 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:

  1. The Terraform schema has Required: true
  2. 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.gofilter_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-awsSubscriptionFilter.cloudwatchlogs (filterPattern field)
  • Likely any other Upjet-generated provider where a Terraform Required: true string field accepts "" as valid

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions