Skip to content

Commit

Permalink
Implement JSON Patch in Xds Translator (#1606)
Browse files Browse the repository at this point in the history
* Implement JSON Patch in Xds Translator

Relates to #24

Signed-off-by: Arko Dasgupta <arko@tetrate.io>

* use temp variable to unmarshal into

Signed-off-by: Arko Dasgupta <arko@tetrate.io>

* lint

Signed-off-by: Arko Dasgupta <arko@tetrate.io>

* fix test

Signed-off-by: Arko Dasgupta <arko@tetrate.io>

* use apiextensionsv1.JSON

Signed-off-by: Arko Dasgupta <arko@tetrate.io>

* routeConfig test

Signed-off-by: Arko Dasgupta <arko@tetrate.io>

* add entire resource and more test cases

Signed-off-by: Arko Dasgupta <arko@tetrate.io>

* lint

Signed-off-by: Arko Dasgupta <arko@tetrate.io>

* move marshaller out of for loop

Signed-off-by: Arko Dasgupta <arko@tetrate.io>

* address comments

Signed-off-by: Arko Dasgupta <arko@tetrate.io>

---------

Signed-off-by: Arko Dasgupta <arko@tetrate.io>
Co-authored-by: zirain <zirain2009@gmail.com>
  • Loading branch information
arkodg and zirain authored Jul 5, 2023
1 parent 3974be6 commit e171678
Show file tree
Hide file tree
Showing 11 changed files with 585 additions and 22 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/davecgh/go-spew v1.1.1
github.com/envoyproxy/go-control-plane v0.11.1
github.com/envoyproxy/ratelimit v1.4.1-0.20230427142404-e2a87f41d3a7
github.com/evanphx/json-patch/v5 v5.6.0
github.com/go-logr/logr v1.2.4
github.com/go-logr/zapr v1.2.4
github.com/golang/protobuf v1.5.3
Expand All @@ -25,6 +26,7 @@ require (
google.golang.org/protobuf v1.31.0
gopkg.in/yaml.v3 v3.0.1
k8s.io/api v0.27.3
k8s.io/apiextensions-apiserver v0.27.2
k8s.io/apimachinery v0.27.3
k8s.io/cli-runtime v0.27.3
k8s.io/client-go v0.27.3
Expand All @@ -45,7 +47,6 @@ require (
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/envoyproxy/protoc-gen-validate v1.0.1 // indirect
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
github.com/exponent-io/jsonpath v0.0.0-20151013193312-d6023ce2651d // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-asn1-ber/asn1-ber v1.5.4 // indirect
Expand Down Expand Up @@ -104,7 +105,6 @@ require (
google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
k8s.io/apiextensions-apiserver v0.27.2 // indirect
k8s.io/component-base v0.27.3 // indirect
k8s.io/klog/v2 v2.100.1 // indirect
k8s.io/kube-openapi v0.0.0-20230501164219-8b0f38b5fd1f // indirect
Expand Down
29 changes: 29 additions & 0 deletions internal/ir/xds.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/tetratelabs/multierror"

apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

egv1a1 "github.com/envoyproxy/gateway/api/v1alpha1"
Expand Down Expand Up @@ -54,6 +55,9 @@ type Xds struct {
TCP []*TCPListener
// UDP Listeners exposed by the gateway.
UDP []*UDPListener
// JSONPatches are the JSON Patches that
// are to be applied to generaed Xds linked to the gateway.
JSONPatches []*JSONPatchConfig
}

// Validate the fields within the Xds structure.
Expand Down Expand Up @@ -792,3 +796,28 @@ type OpenTelemetryAccessLog struct {
Port uint32 `json:"port"`
Resources map[string]string `json:"resources"`
}

// JSONPatchConfig defines the configuration for patching a Envoy xDS Resource
// using JSONPatch semantics
// +k8s:deepcopy-gen=true
type JSONPatchConfig struct {
// Type is the typed URL of the Envoy xDS Resource
Type string `json:"type"`
// Name is the name of the resource
Name string `json:"name"`
// Patch defines the JSON Patch Operation
Operation JSONPatchOperation `json:"operation"`
}

// JSONPatchOperation defines the JSON Patch Operation as defined in
// https://datatracker.ietf.org/doc/html/rfc6902
// +k8s:deepcopy-gen=true
type JSONPatchOperation struct {
// Op is the type of operation to perform
Op string `json:"op"`
// Path is the location of the target document/field where the operation will be performed
// Refer to https://datatracker.ietf.org/doc/html/rfc6901 for more details.
Path string `json:"path"`
// Value is the new value of the path location.
Value apiextensionsv1.JSON `json:"value"`
}
43 changes: 43 additions & 0 deletions internal/ir/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e171678

Please sign in to comment.