diff --git a/Makefile b/Makefile index 956bc8e..0655c82 100644 --- a/Makefile +++ b/Makefile @@ -29,7 +29,7 @@ BUNDLE_METADATA_OPTS ?= $(BUNDLE_CHANNELS) $(BUNDLE_DEFAULT_CHANNEL) # # For example, running 'make bundle-build bundle-push catalog-build catalog-push' will build and push both # lightrun.com/lightrun-k8s-operator-bundle:$VERSION and lightrun.com/lightrun-k8s-operator-catalog:$VERSION. -IMAGE_TAG_BASE ?= lightruncom/lightrun-k8s-operator#TODO: replace with dockerhub +IMAGE_TAG_BASE ?= lightruncom/lightrun-k8s-operator #IMAGE_TAG_BASE ?= local-registry:5432/lightrun-k8s-operator # BUNDLE_IMG defines the image:tag used for the bundle. diff --git a/api/v1beta/groupversion_info.go b/api/v1beta/groupversion_info.go index 2bb003f..f24258e 100644 --- a/api/v1beta/groupversion_info.go +++ b/api/v1beta/groupversion_info.go @@ -15,8 +15,8 @@ limitations under the License. */ // Package v1beta contains API Schema definitions for the agents v1beta API group -//+kubebuilder:object:generate=true -//+groupName=agents.lightrun.com +// +kubebuilder:object:generate=true +// +groupName=agents.lightrun.com package v1beta import ( diff --git a/api/v1beta/lightrunjavaagent_types.go b/api/v1beta/lightrunjavaagent_types.go index 79c9845..9bf9b11 100644 --- a/api/v1beta/lightrunjavaagent_types.go +++ b/api/v1beta/lightrunjavaagent_types.go @@ -55,9 +55,15 @@ type LightrunJavaAgentSpec struct { ServerHostname string `json:"serverHostname"` // Agent configuration to be changed from default values + // https://docs.lightrun.com/jvm/agent-configuration/#setting-agent-properties-from-the-agentconfig-file // +optional AgentConfig map[string]string `json:"agentConfig,omitempty"` + // Add cli flags to the agent "-agentpath:/lightrun/agent/lightrun_agent.so=" + // https://docs.lightrun.com/jvm/agent-configuration/#additional-command-line-flags + // +optional + AgentCliFlags string `json:"agentCliFlags,omitempty"` + // Agent tags that will be shown in the portal / IDE plugin AgentTags []string `json:"agentTags"` @@ -89,7 +95,7 @@ type LightrunJavaAgent struct { Status LightrunJavaAgentStatus `json:"status,omitempty"` } -//+kubebuilder:object:root=true +// +kubebuilder:object:root=true // LightrunJavaAgentList contains a list of LightrunJavaAgent type LightrunJavaAgentList struct { metav1.TypeMeta `json:",inline"` diff --git a/config/crd/bases/agents.lightrun.com_lightrunjavaagents.yaml b/config/crd/bases/agents.lightrun.com_lightrunjavaagents.yaml index 676729b..dd79605 100644 --- a/config/crd/bases/agents.lightrun.com_lightrunjavaagents.yaml +++ b/config/crd/bases/agents.lightrun.com_lightrunjavaagents.yaml @@ -49,10 +49,15 @@ spec: spec: description: LightrunJavaAgentSpec defines the desired state of LightrunJavaAgent properties: + agentCliFlags: + description: Add cli flags to the agent "-agentpath:/lightrun/agent/lightrun_agent.so=" + https://docs.lightrun.com/jvm/agent-configuration/#additional-command-line-flags + type: string agentConfig: additionalProperties: type: string description: Agent configuration to be changed from default values + https://docs.lightrun.com/jvm/agent-configuration/#setting-agent-properties-from-the-agentconfig-file type: object agentEnvVarName: description: Env variable that will be patched with the -agentpath diff --git a/config/samples/operator.yaml b/config/samples/operator.yaml index 18de717..8fd30d4 100644 --- a/config/samples/operator.yaml +++ b/config/samples/operator.yaml @@ -55,10 +55,15 @@ spec: spec: description: LightrunJavaAgentSpec defines the desired state of LightrunJavaAgent properties: + agentCliFlags: + description: Add cli flags to the agent "-agentpath:/lightrun/agent/lightrun_agent.so=" + https://docs.lightrun.com/jvm/agent-configuration/#additional-command-line-flags + type: string agentConfig: additionalProperties: type: string description: Agent configuration to be changed from default values + https://docs.lightrun.com/jvm/agent-configuration/#setting-agent-properties-from-the-agentconfig-file type: object agentEnvVarName: description: Env variable that will be patched with the -agentpath diff --git a/controllers/lightrunjavaagent_controller.go b/controllers/lightrunjavaagent_controller.go index fbd96af..5037402 100644 --- a/controllers/lightrunjavaagent_controller.go +++ b/controllers/lightrunjavaagent_controller.go @@ -144,7 +144,7 @@ func (r *LightrunJavaAgentReconciler) Reconcile(ctx context.Context, req ctrl.Re break } } - r.unpatchJavaToolEnv(&originalDeployment.Spec.Template.Spec.Containers[conIndex], lightrunJavaAgent.Spec.AgentEnvVarName, lightrunJavaAgent.Spec.InitContainer.SharedVolumeMountPath) + r.unpatchJavaToolEnv(&originalDeployment.Spec.Template.Spec.Containers[conIndex], lightrunJavaAgent.Spec.AgentEnvVarName, lightrunJavaAgent.Spec.InitContainer.SharedVolumeMountPath, lightrunJavaAgent.Spec.AgentCliFlags) } err = r.Patch(ctx, originalDeployment, clientSidePatch) if err != nil { @@ -267,7 +267,7 @@ func (r *LightrunJavaAgentReconciler) Reconcile(ctx context.Context, req ctrl.Re break } } - err = r.patchJavaToolEnv(&originalDeployment.Spec.Template.Spec.Containers[conIndex], lightrunJavaAgent.Spec.AgentEnvVarName, lightrunJavaAgent.Spec.InitContainer.SharedVolumeMountPath) + err = r.patchJavaToolEnv(&originalDeployment.Spec.Template.Spec.Containers[conIndex], lightrunJavaAgent.Spec.AgentEnvVarName, lightrunJavaAgent.Spec.InitContainer.SharedVolumeMountPath, lightrunJavaAgent.Spec.AgentCliFlags) if err != nil { log.Error(err, "failed to patch "+lightrunJavaAgent.Spec.AgentEnvVarName) return r.errorStatus(ctx, lightrunJavaAgent, err) diff --git a/controllers/lightrunjavaagent_controller_test.go b/controllers/lightrunjavaagent_controller_test.go index e09030f..98f4908 100644 --- a/controllers/lightrunjavaagent_controller_test.go +++ b/controllers/lightrunjavaagent_controller_test.go @@ -30,6 +30,8 @@ var _ = Describe("LightrunJavaAgent controller", func() { agentPlatform = "linux" initVolumeName = "lightrun-agent-init" javaEnv = "JAVA_TOOL_OPTIONS" + defaultAgentPath = "-agentpath:/lightrun/agent/lightrun_agent.so" + agentCliFlags = "--lightrun_extra_class_path=" ) var containerSelector = []string{"app", "app2"} var agentConfig map[string]string = map[string]string{"max_log_cpu_cost": "2"} @@ -91,6 +93,7 @@ var _ = Describe("LightrunJavaAgent controller", func() { AgentName: agentName, AgentTags: agentTags, AgentConfig: agentConfig, + AgentCliFlags: agentCliFlags, AgentEnvVarName: javaEnv, ContainerSelector: containerSelector, InitContainer: agentsv1beta.InitContainer{ @@ -198,7 +201,7 @@ var _ = Describe("LightrunJavaAgent controller", func() { }).Should(BeTrue()) }) - It("Should patch Env Vars of containers", func() { + It("Should patch Env Vars of containers with agentCliFlags value", func() { Eventually(func() bool { if err := k8sClient.Get(ctx, deplRequest, &patchedDepl); err != nil { return false @@ -207,13 +210,11 @@ var _ = Describe("LightrunJavaAgent controller", func() { for _, envVar := range container.Env { if envVar.Name == javaEnv { if container.Name == "app" { - if envVar.Value != "-agentpath:/lightrun/agent/lightrun_agent.so" { - //logger.Info("first container", envVar.Name, envVar.Value) + if envVar.Value != defaultAgentPath+"="+agentCliFlags { return false } } else if container.Name == "app2" { - if envVar.Value != "-Djava.net.preferIPv4Stack=true -agentpath:/lightrun/agent/lightrun_agent.so" { - //logger.Info("second container", envVar.Name, envVar.Value) + if envVar.Value != "-Djava.net.preferIPv4Stack=true "+defaultAgentPath+"="+agentCliFlags { return false } } @@ -481,6 +482,30 @@ var _ = Describe("LightrunJavaAgent controller", func() { }).Should(BeTrue()) }) + It("Should patch Env Vars of containers with default agent path", func() { + Eventually(func() bool { + if err := k8sClient.Get(ctx, deplRequest2, &patchedDepl2); err != nil { + return false + } + for _, container := range patchedDepl2.Spec.Template.Spec.Containers { + for _, envVar := range container.Env { + if envVar.Name == javaEnv { + if container.Name == "app" { + if envVar.Value != defaultAgentPath { + return false + } + } else if container.Name == "app2" { + if envVar.Value != "-Djava.net.preferIPv4Stack=true "+defaultAgentPath { + return false + } + } + } + } + } + return true + }).Should(BeTrue()) + }) + It("Should delete deployment", func() { depl := appsv1.Deployment{ ObjectMeta: metav1.ObjectMeta{ diff --git a/controllers/patch_funcs.go b/controllers/patch_funcs.go index e11e7fa..5dac6dd 100644 --- a/controllers/patch_funcs.go +++ b/controllers/patch_funcs.go @@ -158,8 +158,11 @@ func (r *LightrunJavaAgentReconciler) patchAppContainers(lightrunJavaAgent *agen } // Client side patch, as we can't update value from 2 sources -func (r *LightrunJavaAgentReconciler) patchJavaToolEnv(container *corev1.Container, targetEnvVar string, mountPath string) error { +func (r *LightrunJavaAgentReconciler) patchJavaToolEnv(container *corev1.Container, targetEnvVar string, mountPath string, agentCliFlags string) error { agentArg := "-agentpath:" + mountPath + "/agent/lightrun_agent.so" + if agentCliFlags != "" { + agentArg += "=" + agentCliFlags + } javaToolOptionsIndex := -1 @@ -186,8 +189,12 @@ func (r *LightrunJavaAgentReconciler) patchJavaToolEnv(container *corev1.Contain return nil } -func (r *LightrunJavaAgentReconciler) unpatchJavaToolEnv(container *corev1.Container, targetEnvVar string, mountPath string) *corev1.Container { +func (r *LightrunJavaAgentReconciler) unpatchJavaToolEnv(container *corev1.Container, targetEnvVar string, mountPath string, agentCliFlags string) *corev1.Container { agentArg := "-agentpath:" + mountPath + "/agent/lightrun_agent.so" + if agentCliFlags != "" { + agentArg += "=" + agentCliFlags + } + var updatedSlice []corev1.EnvVar for _, envVar := range container.Env { if envVar.Name == targetEnvVar { diff --git a/docs/README.md b/docs/README.md index 87a1584..58e6087 100644 --- a/docs/README.md +++ b/docs/README.md @@ -52,7 +52,8 @@ kubectl create namespace lightrun-agent-test kubectl apply -f https://raw.githubusercontent.com/lightrun-platform/lightrun-k8s-operator/main/examples/operator.yaml -n lightrun-operator ``` -3. Create simple deployment for test +3. Create simple deployment for test +> _App source code [PrimeMain.java](../examples/app/PrimeMain.java)_ ```sh kubectl apply -f https://raw.githubusercontent.com/lightrun-platform/lightrun-k8s-operator/main/examples/deployment.yaml -n lightrun-agent-test ``` diff --git a/examples/lightrunjavaagent.yaml b/examples/lightrunjavaagent.yaml index ce0f4bc..6713f67 100644 --- a/examples/lightrunjavaagent.yaml +++ b/examples/lightrunjavaagent.yaml @@ -9,7 +9,7 @@ spec: # platform - `linux/alpine` # agent version - first part of the tag (1.7.0) # init container sub-version - last part of the tag (init.0) - image: "lightruncom/k8s-operator-init-java-agent-linux:1.7.0-init.0" + image: "lightruncom/k8s-operator-init-java-agent-linux:1.8.3-init.0" # Volume name in case you have some convention in the names sharedVolumeName: lightrun-agent-init # Mount path where volume will be parked. Various distributions may have it's limitations. @@ -34,11 +34,18 @@ spec: # You can find list of available options here https://docs.lightrun.com/jvm/agent-configuration/ agentConfig: max_log_cpu_cost: "2" + + # agentCliFlags is used if need to pass additional flags to the agent, + # that are not part of configuration file. + # https://docs.lightrun.com/jvm/agent-configuration/#additional-command-line-flags + # Example below + # agentCliFlags: --lightrun_extra_class_path=:,lightrun_init_wait_time_ms + # Tags that agent will be using. You'll see them in the UI and in the IDE plugin as well agentTags: - operator - example - - 1.7.0 + - 1.8.3 # Agent name. If not provided, pod name will be used #agentName: "operator-test-agent" # List of container names inside the pod of the deployment diff --git a/examples/operator.yaml b/examples/operator.yaml index 458d684..75b082a 100644 --- a/examples/operator.yaml +++ b/examples/operator.yaml @@ -50,10 +50,15 @@ spec: spec: description: LightrunJavaAgentSpec defines the desired state of LightrunJavaAgent properties: + agentCliFlags: + description: Add cli flags to the agent "-agentpath:/lightrun/agent/lightrun_agent.so=" + https://docs.lightrun.com/jvm/agent-configuration/#additional-command-line-flags + type: string agentConfig: additionalProperties: type: string description: Agent configuration to be changed from default values + https://docs.lightrun.com/jvm/agent-configuration/#setting-agent-properties-from-the-agentconfig-file type: object agentEnvVarName: description: Env variable that will be patched with the -agentpath diff --git a/helm-chart/crds/lightrunjavaagent_crd.yaml b/helm-chart/crds/lightrunjavaagent_crd.yaml index 41981d2..beec6b5 100644 --- a/helm-chart/crds/lightrunjavaagent_crd.yaml +++ b/helm-chart/crds/lightrunjavaagent_crd.yaml @@ -48,10 +48,15 @@ spec: spec: description: LightrunJavaAgentSpec defines the desired state of LightrunJavaAgent properties: + agentCliFlags: + description: Add cli flags to the agent "-agentpath:/lightrun/agent/lightrun_agent.so=" + https://docs.lightrun.com/jvm/agent-configuration/#additional-command-line-flags + type: string agentConfig: additionalProperties: type: string description: Agent configuration to be changed from default values + https://docs.lightrun.com/jvm/agent-configuration/#setting-agent-properties-from-the-agentconfig-file type: object agentEnvVarName: description: Env variable that will be patched with the -agentpath