cryostatio / cryostat-operator

A Kubernetes Operator to facilitate the setup and management of Cryostat.
https://cryostat.io
Apache License 2.0
34 stars 19 forks source link

[Epic] Application annotations for Cryostat Agent integrations #784

Open andrewazores opened 5 months ago

andrewazores commented 5 months ago

Describe the feature

There should be some defined annotation prefix (like io.cryostat/) and keys like inject-agent, inject-tls-cert. Users should be able to add these annotations to their applications (on the Deployment or Pod) within any Namespace associated with a Cryostat instance. When the Operator observes that an application has such an annotation, an integration feature should be enabled:

This way we can automate end-to-end TLS in both directions between Cryostats and their Agent instances, with the user doing as little work as possible to enable this security feature.

Tasks:

andrewazores commented 5 months ago

Some more thoughts about the Agent injection/integration.

  1. We want our Operator to be able to supply the Agent JAR to the application - the user should be able but not required to rebuild their application image containing our Agent.
  2. Configuring the Agent should be as simple and automatic as possible - ideally, the user just flips one switch to opt in, which the Operator sees and takes care of the rest.

For point 1, packaging the Agent JAR version that corresponds to the Cryostat version which the Operator will deploy seems relatively simple. As discussed previously, the Operator can put this into some resource like a ConfigMap or Secret and mount it to the application Deployment.

If the Operator is already modifying the Deployment then it is trivial to add environment variables for configuring the Agent, to point it at the correct Cryostat instance, use the Pod IP for the callback URL, etc.


But how to actually attach the Agent to the application JVM? We have talked about trying to do some detection of the application's framework or base image and manipulating entry points, environment variables, etc. to add the -javaagent switch. This seems error-prone and conflict-prone.

We have also experimented with running an Attach Agent as a sidecar process and connecting to the main container, but this might be tricky to put together in practice and probably runs into a lot of security constraints.

What if we can do the equivalent of a kubectl exec on containers from within the Operator controller?

https://github.com/operator-framework/operator-sdk/issues/4302

https://github.com/zalando/postgres-operator/blob/master/pkg%2Fcluster%2Fexec.go#L18-L44

https://github.com/operator-framework/operator-sdk/issues/1561

If the Agent JAR is already available in the application container (via mounted ConfigMap), then there is an easy solution when combined with agent dynamic attach:

https://github.com/cryostatio/cryostat-agent/pull/234

Just exec the Agent process in PID autodetect mode.

andrewazores commented 5 months ago

If we can do an equivalent to kubectl cp to copy the Agent JAR from the Operator into the application container, instead of mounting a ConfigMap, then this can even be done without causing an application restart. Fully dynamic and online.

andrewazores commented 5 months ago

https://stackoverflow.com/questions/51686986/how-to-copy-file-to-container-with-kubernetes-client-go