Skip to content

Change omitempty to omitzero on non-pointer API structs #4219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions internal/bridge/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ type ClusterApiResource struct {
Network string `json:"network_id,omitempty"`
Parent string `json:"parent_id,omitempty"`
Plan string `json:"plan_id,omitempty"`
PostgresVersion intstr.IntOrString `json:"postgres_version_id,omitempty"`
PostgresVersion intstr.IntOrString `json:"postgres_version_id,omitzero"`
Provider string `json:"provider_id,omitempty"`
Region string `json:"region_id,omitempty"`
Replicas []*ClusterApiResource `json:"replicas,omitempty"`
Expand Down Expand Up @@ -188,7 +188,7 @@ type PostClustersRequestPayload struct {
IsHA bool `json:"is_ha,omitempty"`
Keychain string `json:"keychain_id,omitempty"`
Network string `json:"network_id,omitempty"`
PostgresVersion intstr.IntOrString `json:"postgres_version_id,omitempty"`
PostgresVersion intstr.IntOrString `json:"postgres_version_id,omitzero"`
Provider string `json:"provider_id,omitempty"`
Region string `json:"region_id,omitempty"`
Storage int64 `json:"storage,omitempty"`
Expand All @@ -198,7 +198,7 @@ type PostClustersRequestPayload struct {
// changing its plan, upgrading its major version, or increasing its storage size.
type PostClustersUpgradeRequestPayload struct {
Plan string `json:"plan_id,omitempty"`
PostgresVersion intstr.IntOrString `json:"postgres_version_id,omitempty"`
PostgresVersion intstr.IntOrString `json:"postgres_version_id,omitzero"`
UpgradeStartTime string `json:"starting_from,omitempty"`
Storage int64 `json:"storage,omitempty"`
}
Expand All @@ -207,7 +207,7 @@ type PostClustersUpgradeRequestPayload struct {
// TODO: Implement the ability to update an upgrade (this isn't currently being used)
type PutClustersUpgradeRequestPayload struct {
Plan string `json:"plan_id,omitempty"`
PostgresVersion intstr.IntOrString `json:"postgres_version_id,omitempty"`
PostgresVersion intstr.IntOrString `json:"postgres_version_id,omitzero"`
UpgradeStartTime string `json:"starting_from,omitempty"`
Storage int64 `json:"storage,omitempty"`
UseMaintenanceWindow *bool `json:"use_cluster_maintenance_window,omitempty"`
Expand Down
72 changes: 28 additions & 44 deletions internal/testing/validation/postgrescluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ import (

"gotest.tools/v3/assert"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/util/intstr"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/yaml"

"github.com/crunchydata/postgres-operator/internal/controller/runtime"
"github.com/crunchydata/postgres-operator/internal/testing/cmp"
"github.com/crunchydata/postgres-operator/internal/testing/require"
v1 "github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1"
Expand All @@ -33,11 +32,6 @@ func TestPostgresAuthenticationRules(t *testing.T) {
// Start with a bunch of required fields.
require.UnmarshalInto(t, &base.Spec, `{
postgresVersion: 16,
backups: {
pgbackrest: {
repos: [{ name: repo1 }],
},
},
instances: [{
dataVolumeClaimSpec: {
accessModes: [ReadWriteOnce],
Expand Down Expand Up @@ -247,11 +241,6 @@ func TestPostgresConfigParameters(t *testing.T) {
// Start with a bunch of required fields.
require.UnmarshalInto(t, &base.Spec, `{
postgresVersion: 16,
backups: {
pgbackrest: {
repos: [{ name: repo1 }],
},
},
instances: [{
dataVolumeClaimSpec: {
accessModes: [ReadWriteOnce],
Expand All @@ -271,13 +260,15 @@ func TestPostgresConfigParameters(t *testing.T) {
key string
value any
}{
{"archive_timeout", int64(100)},
{"archive_timeout", 100},
{"archive_timeout", "20s"},
} {
t.Run(tt.key, func(t *testing.T) {
cluster := require.Value(runtime.ToUnstructuredObject(base))
assert.NilError(t, unstructured.SetNestedField(cluster.Object,
tt.value, "spec", "config", "parameters", tt.key))
cluster := base.DeepCopy()
require.UnmarshalInto(t, &cluster.Spec.Config,
require.Value(yaml.Marshal(map[string]any{
"parameters": map[string]any{tt.key: tt.value},
})))

assert.NilError(t, cc.Create(ctx, cluster, client.DryRunAll))
})
Expand All @@ -299,13 +290,15 @@ func TestPostgresConfigParameters(t *testing.T) {
{key: "listen_addresses", value: ""},
{key: "log_file_mode", value: ""},
{key: "logging_collector", value: "off"},
{key: "port", value: int64(5)},
{key: "port", value: 5},
{key: "wal_log_hints", value: "off"},
} {
t.Run(tt.key, func(t *testing.T) {
cluster := require.Value(runtime.ToUnstructuredObject(base))
assert.NilError(t, unstructured.SetNestedField(cluster.Object,
tt.value, "spec", "config", "parameters", tt.key))
cluster := base.DeepCopy()
require.UnmarshalInto(t, &cluster.Spec.Config,
require.Value(yaml.Marshal(map[string]any{
"parameters": map[string]any{tt.key: tt.value},
})))

err := cc.Create(ctx, cluster, client.DryRunAll)
assert.Assert(t, apierrors.IsInvalid(err))
Expand All @@ -332,9 +325,11 @@ func TestPostgresConfigParameters(t *testing.T) {
{key: "unix_socket_group", value: "two"},
} {
t.Run(tt.key, func(t *testing.T) {
cluster := require.Value(runtime.ToUnstructuredObject(base))
assert.NilError(t, unstructured.SetNestedField(cluster.Object,
tt.value, "spec", "config", "parameters", tt.key))
cluster := base.DeepCopy()
require.UnmarshalInto(t, &cluster.Spec.Config,
require.Value(yaml.Marshal(map[string]any{
"parameters": map[string]any{tt.key: tt.value},
})))

err := cc.Create(ctx, cluster, client.DryRunAll)
assert.Assert(t, apierrors.IsInvalid(err))
Expand All @@ -354,9 +349,11 @@ func TestPostgresConfigParameters(t *testing.T) {
{key: "recovery_target_name", value: "doot"},
} {
t.Run(tt.key, func(t *testing.T) {
cluster := require.Value(runtime.ToUnstructuredObject(base))
assert.NilError(t, unstructured.SetNestedField(cluster.Object,
tt.value, "spec", "config", "parameters", tt.key))
cluster := base.DeepCopy()
require.UnmarshalInto(t, &cluster.Spec.Config,
require.Value(yaml.Marshal(map[string]any{
"parameters": map[string]any{tt.key: tt.value},
})))

err := cc.Create(ctx, cluster, client.DryRunAll)
assert.Assert(t, apierrors.IsInvalid(err))
Expand Down Expand Up @@ -408,9 +405,11 @@ func TestPostgresConfigParameters(t *testing.T) {
{key: "recovery_min_apply_delay", value: ""},
} {
t.Run(tt.key, func(t *testing.T) {
cluster := require.Value(runtime.ToUnstructuredObject(base))
assert.NilError(t, unstructured.SetNestedField(cluster.Object,
tt.value, "spec", "config", "parameters", tt.key))
cluster := base.DeepCopy()
require.UnmarshalInto(t, &cluster.Spec.Config,
require.Value(yaml.Marshal(map[string]any{
"parameters": map[string]any{tt.key: tt.value},
})))

err := cc.Create(ctx, cluster, client.DryRunAll)
assert.Assert(t, apierrors.IsInvalid(err))
Expand All @@ -430,11 +429,6 @@ func TestPostgresUserOptions(t *testing.T) {
// Start with a bunch of required fields.
require.UnmarshalInto(t, &base.Spec, `{
postgresVersion: 16,
backups: {
pgbackrest: {
repos: [{ name: repo1 }],
},
},
instances: [{
dataVolumeClaimSpec: {
accessModes: [ReadWriteOnce],
Expand Down Expand Up @@ -542,11 +536,6 @@ func TestPostgresUserInterfaceAcrossVersions(t *testing.T) {
},
},
postgresVersion: 16,
backups: {
pgbackrest: {
repos: [{ name: repo1 }],
},
},
instances: [{
dataVolumeClaimSpec: {
accessModes: [ReadWriteOnce],
Expand All @@ -569,11 +558,6 @@ func TestPostgresUserInterfaceAcrossVersions(t *testing.T) {
},
},
postgresVersion: 16,
backups: {
pgbackrest: {
repos: [{ name: repo1 }],
},
},
instances: [{
dataVolumeClaimSpec: {
accessModes: [ReadWriteOnce],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type PostgresClusterSpec struct {

// PostgreSQL backup configuration
// +optional
Backups Backups `json:"backups,omitempty"`
Backups Backups `json:"backups,omitzero"`

// General configuration of the PostgreSQL server
// +optional
Expand Down Expand Up @@ -294,7 +294,7 @@ type PostgresClusterDataSource struct {

// Resource requirements for the pgBackRest restore Job.
// +optional
Resources corev1.ResourceRequirements `json:"resources,omitempty"`
Resources corev1.ResourceRequirements `json:"resources,omitzero"`

// Scheduling constraints of the pgBackRest restore Job.
// More info: https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node
Expand Down Expand Up @@ -363,7 +363,7 @@ type PostgresClusterStatus struct {
InstanceSets []PostgresInstanceSetStatus `json:"instances,omitempty"`

// +optional
Patroni v1beta1.PatroniStatus `json:"patroni,omitempty"`
Patroni v1beta1.PatroniStatus `json:"patroni,omitzero"`

// Status information for pgBackRest
// +optional
Expand All @@ -382,7 +382,7 @@ type PostgresClusterStatus struct {

// Current state of the PostgreSQL proxy.
// +optional
Proxy PostgresProxyStatus `json:"proxy,omitempty"`
Proxy PostgresProxyStatus `json:"proxy,omitzero"`

// The instance that should be started first when bootstrapping and/or starting a
// PostgresCluster.
Expand All @@ -402,7 +402,7 @@ type PostgresClusterStatus struct {

// Current state of PostgreSQL cluster monitoring tool configuration
// +optional
Monitoring MonitoringStatus `json:"monitoring,omitempty"`
Monitoring MonitoringStatus `json:"monitoring,omitzero"`

// DatabaseInitSQL state of custom database initialization in the cluster
// +optional
Expand Down Expand Up @@ -495,7 +495,7 @@ type PostgresInstanceSetSpec struct {

// Compute resources of a PostgreSQL container.
// +optional
Resources corev1.ResourceRequirements `json:"resources,omitempty"`
Resources corev1.ResourceRequirements `json:"resources,omitzero"`

// Configuration for instance sidecar containers
// +optional
Expand Down Expand Up @@ -617,7 +617,8 @@ type RegistrationRequirementStatus struct {
}

type PostgresProxyStatus struct {
PGBouncer v1beta1.PGBouncerPodStatus `json:"pgBouncer,omitempty"`
// +optional
PGBouncer v1beta1.PGBouncerPodStatus `json:"pgBouncer,omitzero"`
}

// PostgresStandbySpec defines if/how the cluster should be a hot standby.
Expand Down Expand Up @@ -663,7 +664,8 @@ func (s *UserInterfaceSpec) Default() {
type PostgresUserInterfaceStatus struct {

// The state of the pgAdmin user interface.
PGAdmin v1beta1.PGAdminPodStatus `json:"pgAdmin,omitempty"`
// +optional
PGAdmin v1beta1.PGAdminPodStatus `json:"pgAdmin,omitzero"`
}

// +kubebuilder:object:root=true
Expand All @@ -676,14 +678,17 @@ type PostgresCluster struct {
// - https://docs.k8s.io/concepts/overview/working-with-objects/names/#dns-subdomain-names
// - https://releases.k8s.io/v1.21.0/staging/src/k8s.io/apiextensions-apiserver/pkg/registry/customresource/validator.go#L60

metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
metav1.TypeMeta `json:",inline"`
// +optional
metav1.ObjectMeta `json:"metadata,omitzero"`

// NOTE(cbandy): Every PostgresCluster needs a Spec, but it is optional here
// so ObjectMeta can be managed independently.

Spec PostgresClusterSpec `json:"spec,omitempty"`
Status PostgresClusterStatus `json:"status,omitempty"`
// +optional
Spec PostgresClusterSpec `json:"spec,omitzero"`
// +optional
Status PostgresClusterStatus `json:"status,omitzero"`
}

// Default implements "sigs.k8s.io/controller-runtime/pkg/webhook.Defaulter" so
Expand All @@ -704,7 +709,7 @@ func (c *PostgresCluster) Default() {
// PostgresClusterList contains a list of PostgresCluster
type PostgresClusterList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
metav1.ListMeta `json:"metadata,omitzero"`
Items []PostgresCluster `json:"items"`
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,14 +204,17 @@ type CrunchyBridgeCluster struct {
// - https://releases.k8s.io/v1.21.0/staging/src/k8s.io/apiextensions-apiserver/pkg/registry/customresource/validator.go#L60

// In Bridge json, meta.name is "name"
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
metav1.TypeMeta `json:",inline"`
// +optional
metav1.ObjectMeta `json:"metadata,omitzero"`

// NOTE(cbandy): Every CrunchyBridgeCluster needs a Spec, but it is optional here
// so ObjectMeta can be managed independently.

Spec CrunchyBridgeClusterSpec `json:"spec,omitempty"`
Status CrunchyBridgeClusterStatus `json:"status,omitempty"`
// +optional
Spec CrunchyBridgeClusterSpec `json:"spec,omitzero"`
// +optional
Status CrunchyBridgeClusterStatus `json:"status,omitzero"`
}

// Default implements "sigs.k8s.io/controller-runtime/pkg/webhook.Defaulter" so
Expand All @@ -231,7 +234,9 @@ func (c *CrunchyBridgeCluster) Default() {
// CrunchyBridgeClusterList contains a list of CrunchyBridgeCluster
type CrunchyBridgeClusterList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`

// +optional
metav1.ListMeta `json:"metadata,omitzero"`
Items []CrunchyBridgeCluster `json:"items"`
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type InstrumentationSpec struct {
// Resources holds the resource requirements for the collector container.
// ---
// +optional
Resources corev1.ResourceRequirements `json:"resources,omitempty"`
Resources corev1.ResourceRequirements `json:"resources,omitzero"`

// Config is the place for users to configure exporters and provide files.
// ---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type PGAdminPodSpec struct {
// values will be loaded without validation. Be careful, as
// you may put pgAdmin into an unusable state.
// +optional
Config PGAdminConfiguration `json:"config,omitempty"`
Config PGAdminConfiguration `json:"config,omitzero"`

// Defines a PersistentVolumeClaim for pgAdmin data.
// More info: https://kubernetes.io/docs/concepts/storage/persistent-volumes
Expand Down Expand Up @@ -76,7 +76,7 @@ type PGAdminPodSpec struct {
// pgAdmin to restart.
// More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers
// +optional
Resources corev1.ResourceRequirements `json:"resources,omitempty"`
Resources corev1.ResourceRequirements `json:"resources,omitzero"`

// Specification of the service that exposes pgAdmin.
// +optional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ type BackupJobs struct {
// Resource limits for backup jobs. Includes manual, scheduled and replica
// create backups
// +optional
Resources corev1.ResourceRequirements `json:"resources,omitempty"`
Resources corev1.ResourceRequirements `json:"resources,omitzero"`

// Priority class name for the pgBackRest backup Job pods.
// More info: https://kubernetes.io/docs/concepts/scheduling-eviction/pod-priority-preemption/
Expand Down Expand Up @@ -208,7 +208,7 @@ type PGBackRestRepoHost struct {

// Resource requirements for a pgBackRest repository host
// +optional
Resources corev1.ResourceRequirements `json:"resources,omitempty"`
Resources corev1.ResourceRequirements `json:"resources,omitzero"`

// Tolerations of a PgBackRest repo host pod. Changing this value causes a restart.
// More info: https://kubernetes.io/docs/concepts/scheduling-eviction/taint-and-toleration
Expand Down Expand Up @@ -442,7 +442,7 @@ type PGBackRestDataSource struct {

// Resource requirements for the pgBackRest restore Job.
// +optional
Resources corev1.ResourceRequirements `json:"resources,omitempty"`
Resources corev1.ResourceRequirements `json:"resources,omitzero"`

// Scheduling constraints of the pgBackRest restore Job.
// More info: https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node
Expand Down
Loading
Loading