Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions api/v1beta/groupversion_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
8 changes: 7 additions & 1 deletion api/v1beta/lightrunjavaagent_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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=<AgentCliFlags>"
// 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"`

Expand Down Expand Up @@ -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"`
Expand Down
5 changes: 5 additions & 0 deletions config/crd/bases/agents.lightrun.com_lightrunjavaagents.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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=<AgentCliFlags>"
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
Expand Down
5 changes: 5 additions & 0 deletions config/samples/operator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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=<AgentCliFlags>"
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
Expand Down
4 changes: 2 additions & 2 deletions controllers/lightrunjavaagent_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
35 changes: 30 additions & 5 deletions controllers/lightrunjavaagent_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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=<PATH_TO_JAR>"
)
var containerSelector = []string{"app", "app2"}
var agentConfig map[string]string = map[string]string{"max_log_cpu_cost": "2"}
Expand Down Expand Up @@ -91,6 +93,7 @@ var _ = Describe("LightrunJavaAgent controller", func() {
AgentName: agentName,
AgentTags: agentTags,
AgentConfig: agentConfig,
AgentCliFlags: agentCliFlags,
AgentEnvVarName: javaEnv,
ContainerSelector: containerSelector,
InitContainer: agentsv1beta.InitContainer{
Expand Down Expand Up @@ -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
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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{
Expand Down
11 changes: 9 additions & 2 deletions controllers/patch_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
11 changes: 9 additions & 2 deletions examples/lightrunjavaagent.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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=<PATH_TO_JAR>:<PATH_TO_JAR>,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
Expand Down
5 changes: 5 additions & 0 deletions examples/operator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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=<AgentCliFlags>"
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
Expand Down
5 changes: 5 additions & 0 deletions helm-chart/crds/lightrunjavaagent_crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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=<AgentCliFlags>"
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
Expand Down