Skip to content

Commit f8c5f76

Browse files
author
Christoph Bühler
committed
feat(attributes): add ignore entity attribute.
This closes #25.
1 parent 9271fb1 commit f8c5f76

File tree

5 files changed

+43
-2
lines changed

5 files changed

+43
-2
lines changed

src/KubeOps/KubeOps.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,8 @@
4747
</AssemblyAttribute>
4848
</ItemGroup>
4949

50+
<ItemGroup>
51+
<Folder Include="Testing" />
52+
</ItemGroup>
53+
5054
</Project>

src/KubeOps/Operator/Commands/Generators/CrdGenerator.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Text;
77
using System.Threading.Tasks;
88
using k8s.Models;
9+
using KubeOps.Operator.Entities.Annotations;
910
using KubeOps.Operator.Entities.Extensions;
1011
using KubeOps.Operator.Entities.Kustomize;
1112
using KubeOps.Operator.Serialization;
@@ -75,15 +76,16 @@ public async Task<int> OnExecuteAsync(CommandLineApplication app)
7576
return ExitCodes.Success;
7677
}
7778

78-
public static IEnumerable<V1CustomResourceDefinition> GenerateCrds()
79+
public static IEnumerable<V1CustomResourceDefinition> GenerateCrds(Assembly? assembly = null)
7980
{
80-
var assembly = Assembly.GetEntryAssembly();
81+
assembly ??= Assembly.GetEntryAssembly();
8182
if (assembly == null)
8283
{
8384
throw new Exception("No Entry Assembly found.");
8485
}
8586

8687
return GetTypesWithAttribute<KubernetesEntityAttribute>(assembly)
88+
.Where(type => !type.GetCustomAttributes<IgnoreEntityAttribute>().Any())
8789
.Select(EntityToCrdExtensions.CreateCrd);
8890
}
8991

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
3+
namespace KubeOps.Operator.Entities.Annotations
4+
{
5+
/// <summary>
6+
/// Attribute that states that the given entity should be
7+
/// ignored during CRD generation.
8+
/// </summary>
9+
[AttributeUsage(AttributeTargets.Class)]
10+
public class IgnoreEntityAttribute : Attribute
11+
{
12+
}
13+
}

tests/KubeOps.Test/Operator/Entities/CrdGeneration.Test.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System;
22
using System.Linq;
3+
using System.Reflection;
34
using FluentAssertions;
45
using k8s.Models;
6+
using KubeOps.Operator.Commands.Generators;
57
using KubeOps.Operator.Entities.Extensions;
68
using KubeOps.Test.Operator.Entities.TestEntities;
79
using Xunit;
@@ -217,5 +219,13 @@ public void Should_Set_Required_Null_If_No_Required()
217219
var specProperties = crd.Spec.Versions.First().Schema.OpenAPIV3Schema.Properties["spec"];
218220
specProperties.Required.Should().BeNull();
219221
}
222+
223+
[Fact]
224+
public void Should_Ignore_Entity_With_Ignore_Attribute()
225+
{
226+
var crds = CrdGenerator.GenerateCrds(Assembly.GetExecutingAssembly()).ToList();
227+
crds.Should().NotContain(crd => crd.Spec.Names.Kind == "TestIgnoredEntity");
228+
crds.Should().Contain(crd => crd.Spec.Names.Kind == "TestSpecEntity");
229+
}
220230
}
221231
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using k8s.Models;
2+
using KubeOps.Operator.Entities;
3+
using KubeOps.Operator.Entities.Annotations;
4+
5+
namespace KubeOps.Test.Operator.Entities.TestEntities
6+
{
7+
[IgnoreEntity]
8+
[KubernetesEntity(Group = "kubeops.test.dev", ApiVersion = "V1")]
9+
public class TestIgnoredEntity : CustomKubernetesEntity
10+
{
11+
}
12+
}

0 commit comments

Comments
 (0)