Skip to content

Best practices for updating a CRD's metadata #1127

@dprotaso

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)

Metadata

Metadata

Assignees

Labels

area/APIAPI objects and controllerskind/cleanupCategorizes issue or PR as related to cleaning up code, process, or technical debt.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions