Skip to content

Commit 0307303

Browse files
committed
Parse the existing PYTHONPATH and remove the existing contrast and prefix instead.
1 parent 9a10d6b commit 0307303

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

src/Contrast.K8s.AgentOperator/Core/Reactions/Injecting/Patching/Agents/PythonAgentPatcher.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using k8s.Models;
88
using System;
99
using System.Collections.Generic;
10+
using System.Linq;
1011

1112
namespace Contrast.K8s.AgentOperator.Core.Reactions.Injecting.Patching.Agents;
1213

@@ -27,6 +28,7 @@ public IEnumerable<V1EnvVar> GenerateEnvVars(PatchingContext context)
2728
{
2829
yield return new V1EnvVar("CONTRAST__AGENT__PYTHON__REWRITE", "true");
2930
}
31+
3032
yield return new V1EnvVar("__CONTRAST_USING_RUNNER", "true");
3133
yield return new V1EnvVar("CONTRAST__AGENT__LOGGER__PATH", $"{context.WritableMountPath}/logs/contrast_agent.log");
3234
yield return new V1EnvVar("CONTRAST_INSTALLATION_TOOL", "KUBERNETES_OPERATOR");
@@ -37,15 +39,21 @@ public void PatchContainer(V1Container container, PatchingContext context)
3739
// Only modify this if CONTRAST_EXISTING_PYTHONPATH isn't already set. This is to prevent infinite loops.
3840
if (container.Env.FirstOrDefault("PYTHONPATH") is { Value: { } currentPath }
3941
&& !string.IsNullOrWhiteSpace(currentPath)
40-
&& !currentPath.Contains("contrast/loader", StringComparison.OrdinalIgnoreCase)
42+
&& !currentPath.EndsWith("contrast/loader", StringComparison.OrdinalIgnoreCase)
4143
&& container.Env.FirstOrDefault("CONTRAST_EXISTING_PYTHONPATH") is null)
4244
{
4345
container.Env.AddOrUpdate(new V1EnvVar("CONTRAST_EXISTING_PYTHONPATH", currentPath));
46+
47+
//Some operators add the existing PYTHONPATH to the middle of the new PYTHONPATH so remove our existing one and prefix it
48+
var splitPath = currentPath.Split(':').ToList();
49+
splitPath.Remove(context.AgentMountPath);
50+
splitPath.Remove($"{context.AgentMountPath}/contrast/loader");
51+
4452
container.Env.AddOrUpdate(new V1EnvVar("PYTHONPATH",
45-
$"{GetAgentPythonPath(context)}:{currentPath}"));
53+
$"{GetAgentPythonPath(context)}:{string.Join(':', splitPath)}"));
4654
}
4755
}
4856

49-
private static string GetAgentPythonPath(PatchingContext context) => $"{context.AgentMountPath}:{context.AgentMountPath}/contrast/loader";
50-
57+
private static string GetAgentPythonPath(PatchingContext context) =>
58+
$"{context.AgentMountPath}:{context.AgentMountPath}/contrast/loader";
5159
}

tests/Contrast.K8s.AgentOperator.Tests/Core/Reactions/Injecting/Patching/Agents/PythonAgentPatcherTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,5 +103,28 @@ public void PatchContainer_should_not_do_anything_if_we_already_injected()
103103
}
104104
}
105105

106+
[Fact]
107+
public void PatchContainer_should_move_contrast_injection_to_the_start()
108+
{
109+
var patcher = new PythonAgentPatcher(new InjectorOptions(false, false));
110+
var context = AutoFixture.Create<PatchingContext>();
111+
var existingPath = $"bleh:{context.AgentMountPath}:{context.AgentMountPath}/contrast/loader:bleh2";
112+
var container = AutoFixture.Build<V1Container>()
113+
.With(x => x.Env, new List<V1EnvVar> { new("PYTHONPATH", existingPath) }).Create();
114+
115+
// Act
116+
patcher.PatchContainer(container, context);
117+
118+
// Assert
119+
using (new AssertionScope())
120+
{
121+
container.Env.Should()
122+
.Contain(x => x.Name == "CONTRAST_EXISTING_PYTHONPATH" && x.Value == existingPath);
123+
container.Env.Should().Contain(x =>
124+
x.Name == "PYTHONPATH" && x.Value ==
125+
$"{context.AgentMountPath}:{context.AgentMountPath}/contrast/loader:bleh:bleh2");
126+
}
127+
}
128+
106129
}
107130
}

0 commit comments

Comments
 (0)