Skip to content

Commit ba5dda3

Browse files
author
Christoph Bühler
committed
refactor: add CRD with kustomize CLI
1 parent d77e110 commit ba5dda3

File tree

9 files changed

+132
-25
lines changed

9 files changed

+132
-25
lines changed

src/KubeOps.Abstractions/KubeOps.Abstractions.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@
2020

2121

2222

23+
2324
</Project>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using k8s;
2+
3+
namespace KubeOps.Abstractions.Kustomize;
4+
5+
/// <summary>
6+
/// (Partial) definition for a kustomization yaml.
7+
/// </summary>
8+
public class KustomizationConfig : KubernetesObject
9+
{
10+
public KustomizationConfig()
11+
{
12+
ApiVersion = "kustomize.config.k8s.io/v1beta1";
13+
Kind = "Kustomization";
14+
}
15+
16+
/// <summary>
17+
/// Namespace that should be set.
18+
/// </summary>
19+
public string? Namespace { get; set; }
20+
21+
/// <summary>
22+
/// Name prefix that should be set.
23+
/// </summary>
24+
public string? NamePrefix { get; set; }
25+
26+
/// <summary>
27+
/// Common labels for the resources.
28+
/// </summary>
29+
public IDictionary<string, string>? CommonLabels { get; set; }
30+
31+
/// <summary>
32+
/// Resource list.
33+
/// </summary>
34+
public IList<string>? Resources { get; set; }
35+
36+
/// <summary>
37+
/// List of merge patches.
38+
/// </summary>
39+
public IList<string>? PatchesStrategicMerge { get; set; }
40+
41+
/// <summary>
42+
/// List of <see cref="KustomizationImage"/>.
43+
/// </summary>
44+
public IList<KustomizationImage>? Images { get; set; }
45+
46+
/// <summary>
47+
/// List of <see cref="KustomizationConfigMapGenerator"/>.
48+
/// </summary>
49+
public IList<KustomizationConfigMapGenerator>? ConfigMapGenerator { get; set; }
50+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace KubeOps.Abstractions.Kustomize;
2+
3+
/// <summary>
4+
/// Entitiy for config map generators in a kustomization.yaml file.
5+
/// </summary>
6+
public class KustomizationConfigMapGenerator
7+
{
8+
/// <summary>
9+
/// The name of the config map.
10+
/// </summary>
11+
public string Name { get; set; } = string.Empty;
12+
13+
/// <summary>
14+
/// List of files that should be added to the generated config map.
15+
/// </summary>
16+
public IList<string>? Files { get; set; }
17+
18+
/// <summary>
19+
/// Config literals to add to the config map in the form of:
20+
/// - NAME=value.
21+
/// </summary>
22+
public IList<string>? Literals { get; set; }
23+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace KubeOps.Abstractions.Kustomize;
2+
3+
/// <summary>
4+
/// Definition for an "image" in a kustomization yaml.
5+
/// </summary>
6+
public class KustomizationImage
7+
{
8+
/// <summary>
9+
/// Name of the image.
10+
/// </summary>
11+
public string Name { get; set; } = string.Empty;
12+
13+
/// <summary>
14+
/// New name of the image.
15+
/// </summary>
16+
public string NewName { get; set; } = string.Empty;
17+
18+
/// <summary>
19+
/// New tag of the image.
20+
/// </summary>
21+
public string NewTag { get; set; } = string.Empty;
22+
}

src/KubeOps.Cli/Commands/Generator/CrdGenerator.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using KubeOps.Cli.Output;
1+
using KubeOps.Abstractions.Kustomize;
2+
using KubeOps.Cli.Output;
23
using KubeOps.Cli.SyntaxObjects;
34

45
using McMaster.Extensions.CommandLineUtils;
@@ -53,11 +54,21 @@ public async Task<int> OnExecuteAsync()
5354
_output.WriteLine($"Generate CRDs from project: {projectFile}.");
5455

5556
var parser = new ProjectParser(projectFile);
56-
foreach (var crd in Transpiler.Crds.Transpile(await parser.Entities().ToListAsync()))
57+
var crds = Transpiler.Crds.Transpile(await parser.Entities().ToListAsync()).ToList();
58+
foreach (var crd in crds)
5759
{
5860
_result.Add($"{crd.Metadata.Name.Replace('.', '_')}.{Format.ToString().ToLowerInvariant()}", crd);
5961
}
6062

63+
_result.Add(
64+
$"kustomization.{Format.ToString().ToLowerInvariant()}",
65+
new KustomizationConfig
66+
{
67+
Resources = crds
68+
.ConvertAll(crd => $"{crd.Metadata.Name.Replace('.', '_')}.{Format.ToString().ToLower()}"),
69+
CommonLabels = new Dictionary<string, string> { { "operator-element", "crd" } },
70+
});
71+
6172
if (OutputPath is not null)
6273
{
6374
await _result.Write(OutputPath);

src/KubeOps.Transpiler/Crds.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ public static V1CustomResourceDefinition Transpile(Type type)
5757
Plural = meta.PluralName,
5858
ShortNames = type.GetCustomAttributes<KubernetesEntityShortNamesAttribute>(true)
5959
.SelectMany(a => a.ShortNames).ToList() switch
60-
{
61-
{ Count: > 0 } p => p,
62-
_ => null,
63-
},
60+
{
61+
{ Count: > 0 } p => p,
62+
_ => null,
63+
},
6464
};
6565
crd.Spec.Scope = scope;
6666

@@ -453,10 +453,10 @@ public static string PropertyName(PropertyInfo prop)
453453
.Where(p => p.GetCustomAttribute<IgnoreAttribute>() == null)
454454
.Select(PropertyName)
455455
.ToList() switch
456-
{
457-
{ Count: > 0 } p => p,
458-
_ => null,
459-
},
456+
{
457+
{ Count: > 0 } p => p,
458+
_ => null,
459+
},
460460
}
461461
: null;
462462

src/KubeOps.Transpiler/Entities.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,19 @@ public static class Entities
2121
public static (EntityMetadata Metadata, string Scope) ToEntityMetadata(Type entityType)
2222
=> (entityType.GetCustomAttribute<KubernetesEntityAttribute>(),
2323
entityType.GetCustomAttribute<EntityScopeAttribute>()) switch
24-
{
25-
(null, _) => throw new ArgumentException("The given type is not a valid Kubernetes entity."),
26-
({ } attr, var scope) => (new(
27-
Defaulted(attr.Kind, entityType.Name),
28-
Defaulted(attr.ApiVersion, "v1"),
29-
attr.Group,
30-
attr.PluralName),
31-
scope switch
32-
{
33-
null => Enum.GetName(typeof(EntityScope), EntityScope.Namespaced) ?? "namespaced",
34-
_ => Enum.GetName(typeof(EntityScope), scope.Scope) ?? "namespaced",
35-
}),
36-
};
24+
{
25+
(null, _) => throw new ArgumentException("The given type is not a valid Kubernetes entity."),
26+
({ } attr, var scope) => (new(
27+
Defaulted(attr.Kind, entityType.Name),
28+
Defaulted(attr.ApiVersion, "v1"),
29+
attr.Group,
30+
attr.PluralName),
31+
scope switch
32+
{
33+
null => Enum.GetName(typeof(EntityScope), EntityScope.Namespaced) ?? "namespaced",
34+
_ => Enum.GetName(typeof(EntityScope), scope.Scope) ?? "namespaced",
35+
}),
36+
};
3737

3838
private static string Defaulted(string value, string defaultValue) =>
3939
string.IsNullOrWhiteSpace(value) ? defaultValue : value;

test/KubeOps.Transpiler.Test/Rbac.Test.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void Should_Group_Same_Types_Together()
3737
rule => rule.Resources.Contains("rbactest2s"));
3838
roles.Should().HaveCount(2);
3939
}
40-
40+
4141
[Fact]
4242
public void Should_Group_Types_With_Same_Verbs_Together()
4343
{

test/KubeOps.Transpiler.Test/TestEntities/TestSpecEntity.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public enum TestSpecEnum
153153
[KubernetesEntity(Group = "kubeops.test.dev", ApiVersion = "V1")]
154154
[GenericAdditionalPrinterColumn(".metadata.namespace", "Namespace", "string")]
155155
[GenericAdditionalPrinterColumn(".metadata.creationTimestamp", "Age", "date")]
156-
public class TestSpecEntity : IKubernetesObject<V1ObjectMeta>, ISpec<TestSpecEntitySpec>
156+
public class TestSpecEntity : IKubernetesObject<V1ObjectMeta>, ISpec<TestSpecEntitySpec>
157157
{
158158
public string ApiVersion { get; set; } = "kubeops.test.dev/v1";
159159
public string Kind { get; set; } = "TestSpecEntity";

0 commit comments

Comments
 (0)