Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Sort resources using dependency order before applying #1117

Merged
merged 3 commits into from
Jun 26, 2018

Conversation

squaremo
Copy link
Member

@squaremo squaremo commented Jun 4, 2018

Kubernetes resource kinds have a partial order relation of (loosely) "may refer to": a Deployment may mount a ConfigMap as a volume; most resources are scoped to a namespace; a RoleBinding refers to a Role or ClusterRole; and so on.

Usually, you want the referenced resources to be created or changed before the referring resources. In general, Kubernetes is designed so that it will sort itself out eventually, but things go more smoothly if things are present before they are needed.

Here I've boiled the ordering down to a small number of ranks, with each rank containing kinds that refer only to kinds in the ranks before. When applying resources, they are sorted by rank, so that resources that depend on other resources will get updated or created after those others.

We used to separate resources into those that don't mention a namespace and those that do, before applying each set in turn. This was for two reasons:

  • namespaces don't have a namespace, so these would come first
  • we wanted to supply a --namespace argument to kubectl, because otherwise un-namespaced resources would be created in the same namespace as flux; but, if you supply that argument, applying anything that does have a namespace can be an error.

If we give kubectl a context by putting one in /root/.kube/config though, we don't have to use the --namespace argument, and can apply everything together.

squaremo added 3 commits June 4, 2018 11:38
Was named `release.go` for historical reasons, now named `sync.go` for
clarity reasons. I've also moved the code that accumulates changes
into sync.go.
Kubernetes resource kinds have a partial order relation of (loosely)
"may refer to": a Deployment may mount a ConfigMap as a volume; most
resources are scoped to a namespace; a RoleBinding refers to a Role or
ClusterRole; and so on.

Usually, you want the referenced resources to be created or changed
before the referring resources. In general, Kubernetes is designed so
that it will sort itself out eventually, but things go more smoothly
if things are present before they are needed.

Here I've boiled the ordering down to a small number of ranks, with
each rank containing kinds that refer only to kinds in the ranks
before. When applying resources, they are sorted by rank, so that
resources that depend on other resources will get updated or created
after those others.
`kubectl` run from the pod uses the service account token, and
inherits its default namespace from the service account. We want it to
be the default default (named "default"), to mimic the behaviour of
`kubectl` used from a terminal and thereby avoid some surprises.

To reset the default namespace, we copy a Kubernetes config file with
a context specifying the "default" namespace, to the place `kubectl`
expects to find it, in the flux image.
@samb1729
Copy link
Contributor

If we give kubectl a context by putting one in /root/.kube/config though, we don't have to use the --namespace argument, and can apply everything together.

Oh nice

Copy link
Contributor

@aaron7 aaron7 left a comment

Choose a reason for hiding this comment

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

lgtm

@squaremo squaremo merged commit 335c768 into master Jun 26, 2018
@squaremo squaremo deleted the feature/careful-sync-order branch June 26, 2018 12:04
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants