Reconcile dependent objects k8s controller runtime

4/4/2020

I am trying to implement a k8s controller that should watch two custom resources and then reconcile them by creating a deployment, service, etc. The problem is that custom resource A and custom resource B should be created first (of course we don't know the order) and then I should perform reconciling. Inside Reconcile function I can have access to one of them at a time. What is the best approach to make sure both have been created before proceeding to reconcile?

-- Adib Rastegarnia
kubernetes

1 Answer

4/14/2020

As I mentioned in comments you could try to use either

Container probes

A Probe is a diagnostic performed periodically by the kubelet on a Container. To perform a diagnostic, the kubelet calls a Handler implemented by the Container. There are three types of handlers:

ExecAction: Executes a specified command inside the Container. The diagnostic is considered successful if the command exits with a status code of 0.

TCPSocketAction: Performs a TCP check against the Container’s IP address on a specified port. The diagnostic is considered successful if the port is open.

HTTPGetAction: Performs an HTTP Get request against the Container’s IP address on a specified port and path. The diagnostic is considered successful if the response has a status code greater than or equal to 200 and less than 400.


Each probe has one of three results:

Success: The Container passed the diagnostic. Failure: The Container failed the diagnostic. Unknown: The diagnostic failed, so no action should be taken.


The kubelet can optionally perform and react to three kinds of probes on running Containers:

livenessProbe: Indicates whether the Container is running. If the liveness probe fails, the kubelet kills the Container, and the Container is subjected to its restart policy. If a Container does not provide a liveness probe, the default state is Success.

readinessProbe: Indicates whether the Container is ready to service requests. If the readiness probe fails, the endpoints controller removes the Pod’s IP address from the endpoints of all Services that match the Pod. The default state of readiness before the initial delay is Failure. If a Container does not provide a readiness probe, the default state is Success.

startupProbe: Indicates whether the application within the Container is started. All other probes are disabled if a startup probe is provided, until it succeeds. If the startup probe fails, the kubelet kills the Container, and the Container is subjected to its restart policy. If a Container does not provide a startup probe, the default state is Success.

Additional links:

Helm

Helm is a tool for managing Charts. Charts are packages of pre-configured Kubernetes resources.

Use Helm to:

  • Find and use popular software packaged as Helm Charts to run in Kubernetes
  • Share your own applications as Helm Charts
  • Create reproducible builds of your Kubernetes applications
  • Intelligently manage your Kubernetes manifest files
  • Manage releases of Helm packages

With your situation you could use helm hooks, so it might wait until those resources gets created and then perform creating deployment,service,etc.

Additional links:

-- jt97
Source: StackOverflow