Skip to content

Best practices for updating a CRD's metadata #1127

Closed

Description

@dprotaso

Once we transition to k8s 1.11 and merge the subresources PR #786 we need some best practices around updating the CRD metadata.

The code below is problematic as it may trigger a generation bump

func (c *Controller) updateLabel(serviceName, namespace string) (*v1alpha1.Service, error) {
	serviceClient := c.KnativeClientSet.ServingV1alpha1().Services(service.Namespace)
        service, _ := serviceClient.Get(service.Name, metav1.GetOptions{})
	service.ObjectMetadata.Labels[key] = someValue
	return serviceClient.Update(service)
}

Triggering a generation bump becomes inherently expensive for certain CRDs. For a Configuration this will trigger a new revision and it's subsequent pods, services etc. to be created.

Whether a generation bump occurs will depend on the CRD definition.

Few approaches to resolve this

  1. (maybe not possible) update the CRD definitions to make sure marshalling always produces the same output. ie. When creating a CRD via kubectl, if a user omits a field in the spec of a CRD, when a controller marshals that CRD it should also omit that field. We shouldn't see null or empty fields from the controller
  2. (easiest) use the clientset's Patch call to only update the label

Related:
Best practices for updating [CRD] status (#1107)
Best practices for updating a CRD's spec (#1128)

Activity

  1. added
    area/API
    kind/cleanup
    on Jun 11, 2018
  2. mattmoor commented on Jun 11, 2018

    @mattmoor
    Contributor

    @dprotaso @bsnchan We need this when 1.11 lands, but we could adopt this now, correct?

  3. mattmoor commented on Jun 11, 2018

    @mattmoor
    Contributor

    Triggering a generation bump becomes inherently expensive for certain CRDs. For a Configuration this will trigger a new revision and it's subsequent pods, services etc. to be created.

    How do we avoid this today? Is the webhook only called for edits to spec?

  4. grantr commented on Jun 11, 2018

    @grantr
    Contributor

    Is the webhook only called for edits to spec?

    Today the configuration controller uses Spec.Generation to detect when a configuration has changed, ignoring Metadata.Generation. The webhook only updates Spec.Generation when Spec has changed:

    // if there's an old object and spec has changed, set generation to oldGeneration + 1

  5. mattmoor commented on Jul 8, 2018

    @mattmoor
    Contributor

    @dprotaso is the only place that we do this today the Route controller's labelling of Configurations? (note: this previously included Revisions, but @mdemirhan removed it).

    To repeat my earlier question can we tackle this even before we transition to 1.11?

  6. dprotaso commented on Jul 11, 2018

    @dprotaso
    MemberAuthor

    is the only place that we do this today the Route controller's labelling of Configurations?

    Yup - just the route controller sets labels

    To repeat my earlier question can we tackle this even before we transition to 1.11?

    Yes - I think we can use the config's clientset's Patch. I'll take it on

    /assign @dprotaso

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area/APIkind/cleanup

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions