Skip to content

Conversation

@mrIncompetent
Copy link
Contributor

What

ExtractItems now preserves empty maps and lists ({} and []) instead of converting them to null.

Why

The apply config that gets applied should be the same that gets later extracted. Today this isn't the case for empty, non-nil, fields (like volumes[].emptyDir).

Changes

  • Modified typed/remove.go to preserve empty maps and lists during extraction
  • Added test cases for empty maps and lists (nil and non-nil)

Additional

Attached a test case that uses the Pod apply config as a more specific example
package pdb

import (
	"encoding/json"
	"testing"

	"github.com/stretchr/testify/require"
	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	corev1apply "k8s.io/client-go/applyconfigurations/core/v1"
	"k8s.io/client-go/kubernetes/fake"
)

var (
	desiredApplyConf = corev1apply.Pod("test", "default").WithSpec(corev1apply.PodSpec().
		WithContainers(
			corev1apply.Container().
				WithName("main").
				WithImage("nginx:latest").
				WithVolumeMounts(
					corev1apply.VolumeMount().
						WithName("data").
						WithMountPath("/data"),
				),
		).
		WithVolumes(
			corev1apply.Volume().
				WithName("data").
				WithEmptyDir(corev1apply.EmptyDirVolumeSource()),
		))
)

func Test_ExtractPod(t *testing.T) {
	client := fake.NewClientset()
	pod, err := client.CoreV1().Pods("default").Apply(t.Context(), desiredApplyConf, v1.ApplyOptions{FieldManager: "test"})
	require.NoError(t, err)

	extractedApplyConf, err := corev1apply.ExtractPod(pod, "test")
	require.NoError(t, err)

	extractedJSON, err := json.Marshal(extractedApplyConf)
	require.NoError(t, err)
	t.Logf("Extracted Pod Apply Config: \n%s", string(extractedJSON))
	desiredJSON, err := json.Marshal(desiredApplyConf)
	require.NoError(t, err)
	t.Logf("Desired Pod Apply Config: \n%s", string(desiredJSON))

	require.JSONEq(t, string(desiredJSON), string(extractedJSON), "extracted apply config does not match the desired one")
}

Previously, ExtractItems converted empty collections ({} and []) to null.
This change preserves the distinction between null and empty values.
@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Sep 15, 2025
@k8s-ci-robot k8s-ci-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Sep 15, 2025
@k8s-ci-robot
Copy link
Contributor

Hi @mrIncompetent. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@k8s-ci-robot k8s-ci-robot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Sep 15, 2025
@mrIncompetent
Copy link
Contributor Author

Ping @jpbetz

Copy link
Contributor

@jpbetz jpbetz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/approve
/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Sep 16, 2025
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: jpbetz, mrIncompetent

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Sep 16, 2025
@k8s-ci-robot k8s-ci-robot merged commit 3b54170 into kubernetes-sigs:master Sep 16, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants