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

Apply metadata the same way as Kustomize did #1308

Merged
merged 11 commits into from
Jan 28, 2020

Conversation

ANeumann82
Copy link
Member

@ANeumann82 ANeumann82 commented Jan 24, 2020

This fix applies metadata in the enhancer recursivly.

An Alternative would be to put Kustomize back in, or handling any embedded resources manually, i.e. code for StatefulSets, Deployments, etc.

Fixes #1302 #1303

Signed-off-by: Andreas Neumann aneumann@mesosphere.com

Fixes #1302 #1303

Signed-off-by: Andreas Neumann <aneumann@mesosphere.com>
Signed-off-by: Andreas Neumann <aneumann@mesosphere.com>
@zmalik
Copy link
Member

zmalik commented Jan 24, 2020

I was working on this issue also, as we talked in yesterday sync
these two lines worked for me and I was working on tests and error handling

unstructured.SetNestedStringMap(objUnstructured.Object, annotations, "spec", "template", "metadata", "annotations")
unstructured.SetNestedStringMap(objUnstructured.Object, labels, "spec", "template", "metadata", "labels")

@ANeumann82
Copy link
Member Author

Yeah, that would work as well for StatefulSets and Deployments.

The recursively apply is more in Line with what Kustomize did, although your approach is less invasive. I really don't know which one is better.
The Problem with "SetNestedStringMap" is that it creates the path to the nested fields, if it does not exist. So for example in a pod.yaml it would create the "spec.template.metadata.labels" path, which might be an issue.
We could wrap that code in "NestedFieldNoCopy", which would allow us to check if the "spec.template.metadata" path exists before...

@zmalik
Copy link
Member

zmalik commented Jan 24, 2020

SetNestedStringMap is calling to setNestedFieldNoCopy
I tested it for pod already and it works as expected.

@ANeumann82
Copy link
Member Author

Yeah, but the code I see in setNestedFieldNoCopy creates missing objects in the tree, which is something we don't want.

@zmalik
Copy link
Member

zmalik commented Jan 24, 2020

ok, I understand what you mean here. But I don't see why we would need to take special care of that when unmarshalling the map back to the object would ignore those fields

@ANeumann82
Copy link
Member Author

Ok, I understand it now, at the end we convert it back from unstructured to the actual object type, which gets rid of any extra attributes.

Leaves the point of any other embedded resources. I don't know if there are any apart from StatefulSets and Deployments, to be honest.

…mple in a pod

Signed-off-by: Andreas Neumann <aneumann@mesosphere.com>
@zmalik
Copy link
Member

zmalik commented Jan 24, 2020

yes, correct. To see exactly what kustomize was doing-->
https://github.com/kubernetes-sigs/kustomize/blob/master/api/konfig/builtinpluginconsts/commonlabels.go
https://github.com/kubernetes-sigs/kustomize/blob/master/api/konfig/builtinpluginconsts/commonannotations.go

you can see they were adding far more labels then annotations, one example is spec/volumeClaimTemplates[]/metadata/labels with this current solution we will have both annotations and labels there. While with kustomize it would be just labels

Signed-off-by: Andreas Neumann <aneumann@mesosphere.com>
@ANeumann82
Copy link
Member Author

Yeah... I'm testing your approach now, seems recursively adding the labels/annotations fails the e2e test, something complains that only certain changes are allowed...

@nfnt
Copy link
Member

nfnt commented Jan 24, 2020

2020-01-24T14:06:29.661746206Z stderr F 2020/01/24 14:06:29 PlanExecution: A transient error when executing task deploy.main.everything.app. Will retry. Deployment.apps "nginx-deployment" is invalid: spec.template.metadata.labels: Invalid value: map[string]string{"heritage":"kudo", "kudo.dev/instance":"first-operator", "kudo.dev/operator":"first-operator"}: `selector` does not match template `labels`

Looks like it's overwriting the existing labels which were app: nginx.

Restructured enhancer to merge existing label and annotations maps

Signed-off-by: Andreas Neumann <aneumann@mesosphere.com>
@zmalik
Copy link
Member

zmalik commented Jan 24, 2020

@ANeumann82 we would be missing annotations for spec/jobTemplate/spec/template and spec/jobTemplate/spec/template/metadata/annotations for CronJob objects if somebody uses them with KUDO

and labels from more places like spec/volumeClaimTemplates[]/metadata/labels also
which KUDO 0.9.0 was doing with the help of kustomize

Signed-off-by: Andreas Neumann <aneumann@mesosphere.com>
Added code to handle unstructured object paths that contain slices

Signed-off-by: Andreas Neumann <aneumann@mesosphere.com>
Signed-off-by: Andreas Neumann <aneumann@mesosphere.com>
@ANeumann82
Copy link
Member Author

@zmalik I've added the missing paths and added code to handle the slice parts of paths for volumeClaimTemplates. Added some more unit tests as well.

@ANeumann82 ANeumann82 self-assigned this Jan 27, 2020
@ANeumann82 ANeumann82 changed the title Apply metadata recursivly as Kustomize did Apply metadata the same way as Kustomize did Jan 27, 2020
@ANeumann82 ANeumann82 added this to Ready For Review in KUDO Global Jan 27, 2020
# Conflicts:
#	pkg/engine/renderer/enhancer.go
Copy link
Member

@kensipe kensipe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this adds needed functionality and I have lots of comments / questions but nothing to stop the merge.

I would appreciate:

  1. some restructuring / reordering
  2. naming that better communicates intent

Future work should include "discovery" of labels as opposed to static paths

func addLabels(obj map[string]interface{}, metadata Metadata) error {
// List of paths for labels from here:
// https://github.com/kubernetes-sigs/kustomize/blob/master/api/konfig/builtinpluginconsts/commonlabels.go
labelPaths := [][]string{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't it be better to "discover" the label locations? this static approach works with existing kube objects... We could "discover" the labels (granted it is by naming convention).

 k explain job --recursive | grep -A 10 labels
      labels	<map[string]string>
      managedFields	<[]Object>
         apiVersion	<string>
         fieldsType	<string>
         fieldsV1	<map[string]>
         manager	<string>
         operation	<string>
         time	<string>
      name	<string>
      namespace	<string>
      ownerReferences	<[]Object>
--
            labels	<map[string]string>
            managedFields	<[]Object>
               apiVersion	<string>
               fieldsType	<string>
               fieldsV1	<map[string]>
               manager	<string>
               operation	<string>
               time	<string>
            name	<string>
            namespace	<string>
            ownerReferences	<[]Object>

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My first approach was just to walk the yaml tree recursively, looking for metadata and applying the labels/annotations there. The problem with that is, that there's a lot of metadata that can't be changed once the resource is created. I think that's why Kustomize has a list of static places as well.

func addAnnotations(obj map[string]interface{}, metadata Metadata) error {
// List of pathsfor annotations from here:
// https://github.com/kubernetes-sigs/kustomize/blob/master/api/konfig/builtinpluginconsts/commonannotations.go
annotationPaths := [][]string{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment as above for labels... we should "discover" annotations

k explain job --recursive | grep -A 10 annotat
      annotations	<map[string]string>
      clusterName	<string>
      creationTimestamp	<string>
      deletionGracePeriodSeconds	<integer>
      deletionTimestamp	<string>
      finalizers	<[]string>
      generateName	<string>
      generation	<integer>
      labels	<map[string]string>
      managedFields	<[]Object>
         apiVersion	<string>
--
            annotations	<map[string]string>
            clusterName	<string>
            creationTimestamp	<string>
            deletionGracePeriodSeconds	<integer>
            deletionTimestamp	<string>
            finalizers	<[]string>
            generateName	<string>
            generation	<integer>
            labels	<map[string]string>
            managedFields	<[]Object>
               apiVersion	<string>

pkg/engine/renderer/enhancer.go Outdated Show resolved Hide resolved
pkg/engine/renderer/enhancer.go Outdated Show resolved Hide resolved
pkg/engine/renderer/enhancer.go Outdated Show resolved Hide resolved
pkg/engine/renderer/enhancer.go Show resolved Hide resolved
pkg/engine/renderer/enhancer_test.go Outdated Show resolved Hide resolved
pkg/engine/renderer/enhancer_test.go Outdated Show resolved Hide resolved
ANeumann82 and others added 2 commits January 28, 2020 17:50
Signed-off-by: Andreas Neumann <aneumann@mesosphere.com>
Signed-off-by: Ken Sipe <kensipe@gmail.com>
@kensipe kensipe merged commit 8b67df5 into master Jan 28, 2020
KUDO Global automation moved this from Ready For Review to Done Jan 28, 2020
@kensipe kensipe deleted the an/apply-metadata-recursivly branch January 28, 2020 21:23
ANeumann82 added a commit that referenced this pull request Feb 13, 2020
Co-authored-by: Ken Sipe <kensipe@gmail.com>
Signed-off-by: Andreas Neumann <aneumann@mesosphere.com>
runyontr pushed a commit that referenced this pull request Mar 11, 2020
Co-authored-by: Ken Sipe <kensipe@gmail.com>
Signed-off-by: Andreas Neumann <aneumann@mesosphere.com>
Signed-off-by: Thomas Runyon <runyontr@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
KUDO Global
  
Done
Development

Successfully merging this pull request may close these issues.

KUDO 0.10.0 does not perform rolling update if ConfigMap changes
5 participants