aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.55k stars 3.87k forks source link

(aws-eks): `KubernetsPatch`'s applyPatch, restorePatch type is incorrect when 'PatchType.JSON' #11775

Open foriequal0 opened 3 years ago

foriequal0 commented 3 years ago

Json Patch standard denotes that https://tools.ietf.org/html/rfc6902#section-3

A JSON Patch document is a JSON [RFC4627] document that represents an array of objects. Each object represents a single operation to be applied to the target JSON document.

However KubernetesPatch.applyPatch type is { [key: string]; any }. https://github.com/aws/aws-cdk/blob/master/packages/%40aws-cdk/aws-eks/lib/k8s-patch.ts#L20-L28

It is not wrong, but somewhat confusing.

Reproduction Steps

What did you expect to happen?

it should be { [key: string]; any} | {op: "add"|"remove"|"replace"|"move"|"copy"|"test", path: string, value: any}[]

What actually happened?

Environment

Other


This is :bug: Bug Report

kromanow94 commented 3 years ago

Is there any update on that? I have following issue when using python:

This is simplified version of my code:

eks.KubernetesPatch(
    scope=self,
    id="some-id",
    resource_name="some-name",
    cluster=self.cluster,
    patch_type=eks.PatchType.JSON,
    apply_patch=[{"op": "replace", "path": "/webhooks/0/rules/0/operations/0", "value": "DELETE"}],
)

And this is the result in cdk.out json:

"ApplyPatchJson": "{\"0\":{\"op\":\"replace\",\"path\":\"/webhooks/0/rules/0/operations/0\",\"value\":\"DELETE\"}}",

Which is not good. From CloudWatch logs:

kubectl([ 'patch', resource_name, '-n', resource_namespace, '-p', patch_json, '--type', patch_type ])

Where patch_json is the one mentioned above which results in:

Error from server (BadRequest): json: cannot unmarshal object into Go value of type jsonpatch.Patch

Using:

aws-cdk.aws-eks==1.103.0
$ cdk --version
1.105.0 (build 4813992)
Vad1mo commented 2 years ago

Any updates on that?

gabrielibagon commented 2 years ago

I could not get the above JSON format to work in the Python CDK.

A workaround is to remove the {"op": ..., "path": ..., "value": ...} format, and patch your resource using the manifest format. For example:

Convert this:

patch = { "op" : "add", "path" : "/spec/tls/0/hosts/-", "value" : '"myhost.com"'}

to this:

patch = {"spec": {"tls": [{"hosts": ['myhost.com']}]

And then apply:

eks.KubernetesPatch(self, "IngressPatch",
                    cluster=eks_cluster,
                    resource_name="ingress/my-ingress",
                    resource_namespace='ingress',
                    apply_patch=patch,
                    restore_patch={}
                    )

You may need to play around with PatchType and contents of your patch to get this right, but was a quick workaround to implement patches into my Stack in the meantime. Official support for other patch formats within Python CDK would be appreciated.