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

When removing fields from the yaml body, they are not removed from the applied manifest #174

Open
svanharmelen opened this issue Sep 24, 2024 · 6 comments
Labels
question Further information is requested triage

Comments

@svanharmelen
Copy link

When using the kubectl_manifest resource it only seems to update existing fields or add new fields to a deployed/applied manifest, but when you remove a field, the field is not removed from the manifest.

Is that by design or am I doing something wrong? I tried using server side applies but that didn't seem to make any differences.

@alekc
Copy link
Owner

alekc commented Sep 24, 2024 via email

@svanharmelen
Copy link
Author

I think so, will see if I can create a simple test case that I can share...

@svanharmelen
Copy link
Author

Didn't forget this one, but need to find some time. Maybe I can make some time this weekend. Will update when I have something you can test with...

@alekc alekc added question Further information is requested triage and removed waiting for reporter labels Oct 24, 2024
@svanharmelen
Copy link
Author

Sorry for taking so long! I just encountered this again today and will try to make a simple example in the coming days!

@svanharmelen
Copy link
Author

When trying to make a custom example it turns out I cannot reproduce it. So I'm assuming that whatever I saw, was not due to this resource but probably due to an operator also touching the same resource. Going to dig into that one further and will close this issue...

@svanharmelen
Copy link
Author

Well, maybe it's still a bug in this resource 😏

If I apply this CRD:

apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: removals.stable.example.com
spec:
  group: stable.example.com
  versions:
    - name: v1
      served: true
      storage: true
      schema:
        openAPIV3Schema:
          type: object
          properties:
            spec:
              type: object
              properties:
                config:
                  type: object
                  x-kubernetes-preserve-unknown-fields: true
  scope: Namespaced
  names:
    plural: removals
    singular: removal
    kind: Removal
    shortNames:
    - rms

And then apply a TF config like this:

resource "kubectl_manifest" "test" {
  yaml_body = yamlencode({
    apiVersion = "stable.example.com/v1"
    kind       = "Removal"
    metadata = {
      name      = "test"
      namespace = "default"
    }
    spec = {
      config = {
        key1 = "value1"
        key2 = "value2"
      }
    }
  })
}

I have a resource like I would expect:

apiVersion: stable.example.com/v1
kind: Removal
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"stable.example.com/v1","kind":"Removal","metadata":{"annotations":{},"name":"test","namespace":"default"},"spec":{"config":{"key1":"value1","key2":"value2"}}}
  creationTimestamp: "2024-10-29T17:55:11Z"
  generation: 11
  name: test
  namespace: default
  resourceVersion: "68102"
  uid: bf3b6028-3860-48f9-b7d6-a62134c38cf8
spec:
  config:
    key1: value1
    key2: value2

But if I then manually edit the resource directly in k8s (I understand this is something you should not do, but in this case there is a (reasonable complex) reason why one of our k8s operators sometimes needs to do this) giving it an additional field like this:

apiVersion: stable.example.com/v1
kind: Removal
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"stable.example.com/v1","kind":"Removal","metadata":{"annotations":{},"name":"test","namespace":"default"},"spec":{"config":{"key1":"value1","key2":"value2"}}}
  creationTimestamp: "2024-10-29T17:55:11Z"
  generation: 11
  name: test
  namespace: default
  resourceVersion: "68102"
  uid: bf3b6028-3860-48f9-b7d6-a62134c38cf8
spec:
  config:
    key1: value1
    key2: value2
    some: thing # <- this one is added

And then refresh and apply the existing TF config, it says there are no changes. And if I update the config to something like this:

resource "kubectl_manifest" "test" {
  yaml_body = yamlencode({
    apiVersion = "stable.example.com/v1"
    kind       = "Removal"
    metadata = {
      name      = "test"
      namespace = "default"
    }
    spec = {
      config = {
        key1 = "value1"
        key3 = "value3" # <= this changed from 'key2 = "value2"'
      }
    }
  })
}

It only sees that change, but ignores (doesn't even show) the value that was manually added:

image

So is this a bug, or is there a setting that can be used to "fix" this?

@svanharmelen svanharmelen reopened this Oct 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested triage
Projects
None yet
Development

No branches or pull requests

2 participants