Skip to content

Commit 7841fe9

Browse files
author
Christoph Bühler
committed
feat(webhook-generator): only use operations that are overwritten.
1 parent 807ec8c commit 7841fe9

File tree

3 files changed

+32
-12
lines changed

3 files changed

+32
-12
lines changed

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -183,30 +183,29 @@ internal static async Task Handler(IAnsiConsole console, InvocationContext ctx)
183183

184184
foreach (var entity in validatedEntities)
185185
{
186-
var (metadata, _) = parser.ToEntityMetadata(entity);
187186
validatorConfig.Webhooks.Add(new V1ValidatingWebhook
188187
{
189-
Name = $"validate.{metadata.SingularName}.{metadata.Group}.{metadata.Version}",
188+
Name = $"validate.{entity.Metadata.SingularName}.{entity.Metadata.Group}.{entity.Metadata.Version}",
190189
MatchPolicy = "Exact",
191190
AdmissionReviewVersions = new[] { "v1" },
192191
SideEffects = "None",
193192
Rules = new[]
194193
{
195194
new V1RuleWithOperations
196195
{
197-
Operations = new[] { "CREATE", "UPDATE", "DELETE" },
198-
Resources = new[] { metadata.PluralName },
199-
ApiGroups = new[] { metadata.Group },
200-
ApiVersions = new[] { metadata.Version },
196+
Operations = entity.GetOperations(),
197+
Resources = new[] { entity.Metadata.PluralName },
198+
ApiGroups = new[] { entity.Metadata.Group },
199+
ApiVersions = new[] { entity.Metadata.Version },
201200
},
202201
},
203202
ClientConfig = new Admissionregistrationv1WebhookClientConfig
204203
{
205-
CaBundle = Encoding.ASCII.GetBytes(Convert.ToBase64String(Encoding.ASCII.GetBytes(caCert.ToPem()))),
204+
CaBundle =
205+
Encoding.ASCII.GetBytes(Convert.ToBase64String(Encoding.ASCII.GetBytes(caCert.ToPem()))),
206206
Service = new Admissionregistrationv1ServiceReference
207207
{
208-
Name = "operator",
209-
Path = $"/validate/{entity.Name.ToLowerInvariant()}",
208+
Name = "operator", Path = entity.ValidatorPath,
210209
},
211210
},
212211
});

src/KubeOps.Cli/Transpilation/AssemblyLoader.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,13 @@ public static IEnumerable<CustomAttributeData> GetRbacAttributes(this MetadataLo
165165
}
166166
}
167167

168-
public static IEnumerable<Type> GetValidatedEntities(this MetadataLoadContext context) => context.GetAssemblies()
168+
public static IEnumerable<ValidatedEntity> GetValidatedEntities(this MetadataLoadContext context) => context
169+
.GetAssemblies()
169170
.SelectMany(a => a.DefinedTypes)
170171
.Where(t => t.BaseType?.Name == typeof(ValidationWebhook<>).Name &&
171172
t.BaseType?.Namespace == typeof(ValidationWebhook<>).Namespace)
172-
.Select(t => t.BaseType!.GenericTypeArguments[0])
173-
.Distinct();
173+
.Distinct()
174+
.Select(t => new ValidatedEntity(t, context.ToEntityMetadata(t.BaseType!.GenericTypeArguments[0]).Metadata));
174175

175176
[GeneratedRegex(".*")]
176177
private static partial Regex DefaultRegex();
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Reflection;
2+
3+
using KubeOps.Abstractions.Entities;
4+
5+
namespace KubeOps.Cli.Transpilation;
6+
7+
internal record ValidatedEntity(TypeInfo Validator, EntityMetadata Metadata)
8+
{
9+
private bool HasCreate => Validator.DeclaredMembers.Any(m => m.Name.StartsWith("Create"));
10+
11+
private bool HasUpdate => Validator.DeclaredMembers.Any(m => m.Name.StartsWith("Update"));
12+
13+
private bool HasDelete => Validator.DeclaredMembers.Any(m => m.Name.StartsWith("Delete"));
14+
15+
public string ValidatorPath => $"/validate/{Validator.BaseType!.GenericTypeArguments[0].Name.ToLowerInvariant()}";
16+
17+
public string[] GetOperations() =>
18+
new[] { HasCreate ? "CREATE" : null, HasUpdate ? "UPDATE" : null, HasDelete ? "DELETE" : null, }
19+
.Where(o => o is not null).ToArray()!;
20+
}

0 commit comments

Comments
 (0)