Skip to content

Rancher Development Best Practices

Craig Jellick edited this page Mar 24, 2019 · 4 revisions

Table of Contents

Never change the name of a lifecycle handler

When you register a lifecycle with a piece of code like this:

projects.AddClusterScopedLifecycle(ctx, "project-foobar-controller", cluster.ClusterName, projectLifecycle)
the name you supply is used to generate a finalizer on resources. If you change that name, then after a user upgrades Rancher, the controller will not match the finalizer for resources from before the upgrade and they will not be delete-able.

DeepCopy for modifications

When retrieving an object from a K8S cache (ie a lister or a custom index) you MUST deepCopy that object if you are going to modify it. If you don't you are corrupting the cached copy and negatively impacting other handlers/api components that use it. Example:

func (h *Handler) updateProjectPSPTID(request *types.APIContext, podSecurityPolicyTemplateName string (*v3.Project, error) {
	split := strings.Split(request.ID, ":")
	project, err := h.ProjectLister.Get(split[0], split[len(split)-1])
	if err != nil {
		return nil, fmt.Errorf("error getting project: %v", err)
	}
	project = project.DeepCopy()
	project.Status.PodSecurityPolicyTemplateName = podSecurityPolicyTemplateName

	return h.Projects.Update(project)
}

Debug for update loops

When developing, periodically turn on debug logs via `--debug=true` and watch for repeating patterns of handlers running and updating. This will allow you to find logic flaws that are causing endless update loops in your handlers.

Be Mindful of ERROR logging

Users alert on rancher logs that are at ERROR level. Only log something at error if you expect a user to take action on it

Good commit messages

Commit message subject lines should be 50 characters or less. Commit body lines should be 72 characters or shorter. Have meaningful commit messages that explain the problem and the solution. Use this as a guide: https://chris.beams.io/posts/git-commit/

Clone this wiki locally