Skip to content

Commit

Permalink
json/patch: Move EscapeJSONPointer to util/types package
Browse files Browse the repository at this point in the history
Make the helper useful for other packages.

Signed-off-by: Edward Haas <edwardh@redhat.com>
  • Loading branch information
EdDev committed Jul 31, 2022
1 parent 8de9b5c commit 19af700
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 11 deletions.
5 changes: 0 additions & 5 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,11 +311,6 @@ func GeneratePatchBytes(ops []string) []byte {
return []byte(fmt.Sprintf("[%s]", strings.Join(ops, ", ")))
}

func EscapeJSONPointer(ptr string) string {
s := strings.ReplaceAll(ptr, "~", "~0")
return strings.ReplaceAll(s, "/", "~1")
}

func SetVMIPhaseTransitionTimestamp(oldVMI *v1.VirtualMachineInstance, newVMI *v1.VirtualMachineInstance) {
if oldVMI.Status.Phase != newVMI.Status.Phase {
for _, transitionTimeStamp := range newVMI.Status.PhaseTransitionTimestamps {
Expand Down
6 changes: 6 additions & 0 deletions pkg/util/types/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package types
import (
"encoding/json"
"fmt"
"strings"
)

type PatchOperation struct {
Expand Down Expand Up @@ -70,3 +71,8 @@ func UnmarshalPatch(patch []byte) ([]PatchOperation, error) {

return p, err
}

func EscapeJSONPointer(ptr string) string {
s := strings.ReplaceAll(ptr, "~", "~0")
return strings.ReplaceAll(s, "/", "~1")
}
3 changes: 2 additions & 1 deletion pkg/virt-controller/watch/drain/disruptionbudget/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ go_library(
"//pkg/controller:go_default_library",
"//pkg/util/migrations:go_default_library",
"//pkg/util/pdbs:go_default_library",
"//pkg/util/types:go_default_library",
"//pkg/virt-config:go_default_library",
"//staging/src/kubevirt.io/api/core/v1:go_default_library",
"//staging/src/kubevirt.io/client-go/kubecli:go_default_library",
Expand All @@ -34,8 +35,8 @@ go_test(
],
deps = [
":go_default_library",
"//pkg/controller:go_default_library",
"//pkg/testutils:go_default_library",
"//pkg/util/types:go_default_library",
"//pkg/virt-config:go_default_library",
"//staging/src/kubevirt.io/api/core/v1:go_default_library",
"//staging/src/kubevirt.io/client-go/api:go_default_library",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"kubevirt.io/kubevirt/pkg/controller"
"kubevirt.io/kubevirt/pkg/util/migrations"
"kubevirt.io/kubevirt/pkg/util/pdbs"
kubevirttypes "kubevirt.io/kubevirt/pkg/util/types"
virtconfig "kubevirt.io/kubevirt/pkg/virt-config"
)

Expand Down Expand Up @@ -451,7 +452,7 @@ func (c *DisruptionBudgetController) deletePDB(key string, pdb *policyv1.PodDisr
func (c *DisruptionBudgetController) shrinkPDB(vmi *virtv1.VirtualMachineInstance, pdb *policyv1.PodDisruptionBudget) error {
if pdb != nil && pdb.DeletionTimestamp == nil && pdb.Spec.MinAvailable.IntValue() != 1 {
patch := []byte(fmt.Sprintf(`[{ "op": "replace", "path": "/spec/minAvailable", "value": 1 }, { "op": "remove", "path": "/metadata/labels/%s" }]`,
controller.EscapeJSONPointer(virtv1.MigrationNameLabel)))
kubevirttypes.EscapeJSONPointer(virtv1.MigrationNameLabel)))

_, err := c.clientset.PolicyV1().PodDisruptionBudgets(pdb.Namespace).Patch(context.Background(), pdb.Name, types.JSONPatchType, patch, v1.PatchOptions{})
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (
v1 "kubevirt.io/api/core/v1"
"kubevirt.io/client-go/kubecli"

ctrl_util "kubevirt.io/kubevirt/pkg/controller"
"kubevirt.io/kubevirt/pkg/testutils"
kubevirttypes "kubevirt.io/kubevirt/pkg/util/types"
virtconfig "kubevirt.io/kubevirt/pkg/virt-config"
"kubevirt.io/kubevirt/pkg/virt-controller/watch/drain/disruptionbudget"
)
Expand Down Expand Up @@ -111,7 +111,7 @@ var _ = Describe("Disruptionbudget", func() {
Expect(patch.GetPatchType()).To(Equal(types.JSONPatchType))

expectedPatch := fmt.Sprintf(`[{ "op": "replace", "path": "/spec/minAvailable", "value": 1 }, { "op": "remove", "path": "/metadata/labels/%s" }]`,
ctrl_util.EscapeJSONPointer(v1.MigrationNameLabel))
kubevirttypes.EscapeJSONPointer(v1.MigrationNameLabel))
Expect(string(patch.GetPatch())).To(Equal(expectedPatch))
return true, &policyv1.PodDisruptionBudget{}, nil
})
Expand Down
4 changes: 2 additions & 2 deletions pkg/virt-controller/watch/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -1116,7 +1116,7 @@ func (c *MigrationController) sync(key string, migration *virtv1.VirtualMachineI
if vmi.Annotations == nil {
patches = append(patches, fmt.Sprintf(`{ "op": "add", "path": "/metadata/annotations", "value": { "%s": "true"} }`, virtv1.DeprecatedNonRootVMIAnnotation))
} else if _, ok := vmi.Annotations[virtv1.DeprecatedNonRootVMIAnnotation]; !ok {
patches = append(patches, fmt.Sprintf(`{ "op": "add", "path": "/metadata/annotations/%s", "value": "true"}`, controller.EscapeJSONPointer(virtv1.DeprecatedNonRootVMIAnnotation)))
patches = append(patches, fmt.Sprintf(`{ "op": "add", "path": "/metadata/annotations/%s", "value": "true"}`, kubevirttypes.EscapeJSONPointer(virtv1.DeprecatedNonRootVMIAnnotation)))
}
} else {
// The cluster is configured for root VMs, ensure the VMI is root.
Expand All @@ -1127,7 +1127,7 @@ func (c *MigrationController) sync(key string, migration *virtv1.VirtualMachineI

if vmi.Annotations != nil {
if _, ok := vmi.Annotations[virtv1.DeprecatedNonRootVMIAnnotation]; ok {
patches = append(patches, fmt.Sprintf(`{ "op": "remove", "path": "/metadata/annotations/%s"}`, controller.EscapeJSONPointer(virtv1.DeprecatedNonRootVMIAnnotation)))
patches = append(patches, fmt.Sprintf(`{ "op": "remove", "path": "/metadata/annotations/%s"}`, kubevirttypes.EscapeJSONPointer(virtv1.DeprecatedNonRootVMIAnnotation)))
}
}
}
Expand Down

0 comments on commit 19af700

Please sign in to comment.