Skip to content

Commit

Permalink
fix: ToExpression(this IEnumerable<LabelSelector> selectors) extensio…
Browse files Browse the repository at this point in the history
…n method returns wrong result

Current inplementation of ```ToExpression(this
IEnumerable<LabelSelector> selectors)``` extension method returns wrong
result

```
EqualsSelector { Label = app, Values = System.String[] },NotEqualsSelector { Label = srv, Values = System.String[] }
```

it just serialize record LabelSelector to ```EqualsSelector { Label =
app, Values = System.String[] }``` or to ```NotEqualsSelector { Label =
srv, Values = System.String[] }``` instead of calling implicit to string
convertor.


The proposed pull request corrects this unpleasant behavior. Tests are
attached.

Co-authored-by: Christoph Bühler <[email protected]>
  • Loading branch information
regme and buehler authored Nov 8, 2023
1 parent f74bac8 commit 5dcb4c6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/KubeOps.KubernetesClient/LabelSelectors/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ public static class Extensions
/// <param name="selectors">The list of selectors.</param>
/// <returns>A comma-joined string with all selectors converted to their expressions.</returns>
public static string ToExpression(this IEnumerable<LabelSelector> selectors) =>
string.Join(",", selectors);
string.Join(",", selectors.Select(x => (string)x));
}
19 changes: 19 additions & 0 deletions test/KubeOps.KubernetesClient.Test/LabelSelector.Test.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using KubeOps.KubernetesClient.LabelSelectors;

namespace KubeOps.KubernetesClient.Test;

public class LabelSelectorTest : IntegrationTestBase
{
[Fact]
public void Sould_Return_Correct_Expression()
{
var labelSelectors = new LabelSelector[] {
new EqualsSelector("app", Enumerable.Range(0,3).Select(x=>$"app-{x}").ToArray()),
new NotEqualsSelector("srv", Enumerable.Range(0,2).Select(x=>$"service-{x}").ToArray())
};

string expected = "app in (app-0,app-1,app-2),srv notin (service-0,service-1)";
var actual = labelSelectors.ToExpression();
Assert.Equal(expected, actual);
}
}

0 comments on commit 5dcb4c6

Please sign in to comment.