Skip to content
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

Support strategic merge patch for TemplateManagement Objects #681

Closed
Schnitzel opened this issue Nov 22, 2024 · 1 comment
Closed

Support strategic merge patch for TemplateManagement Objects #681

Schnitzel opened this issue Nov 22, 2024 · 1 comment
Assignees
Labels
enhancement Small feature, request or improvement suggestion

Comments

@Schnitzel
Copy link
Contributor

Working with the TemplateManagement Object will include a lot of enabling of templatechains in namespaces, which itself can be done with kubectl patch.

Assuming the provided TemplateManagement with a single Chain enabled in the target Namespace:

apiVersion: hmc.mirantis.com/v1alpha1
kind: TemplateManagement
metadata:
  name: hmc
spec:
  accessRules:
    - clusterTemplateChains:
        - demo-aws-hosted-cp-0.0.1
      targetNamespaces:
        list:
          - blue

and the following green-patch1.yaml:

spec:
  accessRules:
    - clusterTemplateChains:
        - demo-aws-hosted-cp-0.0.2
      targetNamespaces:
        list:
          - green

and then running

kubectl patch TemplateManagement  hmc --type=strategic --patch-file green-patch1.yaml

I would expect the hmc TemplateManagement object to look like this:

apiVersion: hmc.mirantis.com/v1alpha1
kind: TemplateManagement
metadata:
  name: hmc
spec:
  accessRules:
    - clusterTemplateChains:
        - demo-aws-hosted-cp-0.0.1
      targetNamespaces:
        list:
          - blue
    - clusterTemplateChains:
        - demo-aws-hosted-cp-0.0.2
      targetNamespaces:
        list:
          - green

but unfortunately it fails with:

error: application/strategic-merge-patch+json is not supported by hmc.mirantis.com/v1alpha1, Kind=TemplateManagement: the body of the request was in an unknown format - accepted media types include: application/json-patch+json, application/merge-patch+json, application/apply-patch+yaml

the other patch strategies unfortunately cause the existing accessRules to be overwriten and not merged together.
I can workaround this with some yq magic, but it would be great if we can support this with kubectl out of the box

@Schnitzel Schnitzel added the enhancement Small feature, request or improvement suggestion label Nov 22, 2024
@eromanova eromanova self-assigned this Nov 26, 2024
@eromanova
Copy link
Member

eromanova commented Nov 27, 2024

I’m not certain that implementing the strategic merge patch for our TemplateManagement CRD is feasible. The official Kubernetes documentation states that strategic merge patching is not supported for custom resources. For reference:

Additionally, for a strategic merge patch to work, the resource struct should include a field (e.g., name) to uniquely identify array elements during the merge (via patchMergeKey). In our TemplateManagement spec, we don’t have such a field that can be used as a key for merging array elements.

I’m not sure we need to introduce these API changes for our use case. Instead, I suggest using a JSON patch as an alternative. Below is an example of a JSON patch file (patch.json) that adds a new rule to the accessRules array:

[
  {
    "op": "add",
    "path": "/spec/accessRules/-",
    "value": {
      "targetNamespaces": {
         "list": ["green"]
      },
      "clusterTemplateChains": ["demo-aws-hosted-cp-0.0.2"]
    }
  }
]

To apply the patch, run the following command:
kubectl patch templatemanagement hmc --type=json -p "$(cat patch.json)"

The result will be:

spec:
  accessRules:
  - clusterTemplateChains:
    - demo-aws-hosted-cp-0.0.1
    targetNamespaces:
      list:
      - brue
  - clusterTemplateChains:
    - demo-aws-hosted-cp-0.0.2
    targetNamespaces:
      list:
      - green

Alternatively, you can edit the TemplateManagement resource directly using the kubectl edit command.

Please, feel free to reopen the issue if you have any disagreements.

@github-project-automation github-project-automation bot moved this from Todo to Done in Project 2A Nov 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Small feature, request or improvement suggestion
Projects
Status: Done
Development

No branches or pull requests

2 participants