Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ write multiple webhooks in one file and add defaulting #208

Merged
merged 3 commits into from
May 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/crd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (g Generator) Generate(ctx *genall.GenerationContext) error {
toTrivialVersions(&crd)
}
fileName := fmt.Sprintf("%s_%s.yaml", crd.Spec.Group, crd.Spec.Names.Plural)
if err := ctx.WriteYAML(crd, fileName); err != nil {
if err := ctx.WriteYAML(fileName, crd); err != nil {
return err
}
}
Expand Down
28 changes: 15 additions & 13 deletions pkg/genall/genall.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,27 +83,29 @@ type GenerationContext struct {
InputRule
}

// WriteYAML writes the given object out, serialized as YAML, using the
// WriteYAML writes the given objects out, serialized as YAML, using the
// context's OutputRule.
func (g GenerationContext) WriteYAML(obj interface{}, itemPath string) error {
yamlContent, err := yaml.Marshal(obj)
if err != nil {
return err
}

func (g GenerationContext) WriteYAML(itemPath string, objs ...interface{}) error {
out, err := g.Open(nil, itemPath)
if err != nil {
return err
}
defer out.Close()

n, err := out.Write(yamlContent)
if err != nil {
return err
}
if n < len(yamlContent) {
return io.ErrShortWrite
for _, obj := range objs {
yamlContent, err := yaml.Marshal(obj)
if err != nil {
return err
}
n, err := out.Write(append([]byte("\n---\n"), yamlContent...))
if err != nil {
return err
}
if n < len(yamlContent) {
return io.ErrShortWrite
}
}

return nil
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/rbac/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (g Generator) Generate(ctx *genall.GenerationContext) error {
return nil
}

if err := ctx.WriteYAML(rbacv1.ClusterRole{
if err := ctx.WriteYAML("role.yaml", rbacv1.ClusterRole{
TypeMeta: metav1.TypeMeta{
Kind: "ClusterRole",
APIVersion: rbacv1.SchemeGroupVersion.String(),
Expand All @@ -94,7 +94,7 @@ func (g Generator) Generate(ctx *genall.GenerationContext) error {
Name: g.RoleName,
},
Rules: rules,
}, "role.yaml"); err != nil {
}); err != nil {
return err
}

Expand Down
33 changes: 24 additions & 9 deletions pkg/webhook/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,14 @@ func (c Config) ToWebhook() admissionreg.Webhook {
FailurePolicy: &failurePolicy,
ClientConfig: admissionreg.WebhookClientConfig{
Service: &admissionreg.ServiceReference{
Path: &path,
Name: "webhook-service",
Namespace: "system",
Path: &path,
},
// OpenAPI marks the field as required before 1.13 because of a bug that got fixed in
// https://github.com/kubernetes/api/commit/e7d9121e9ffd63cea0288b36a82bcc87b073bd1b
// Put "\n" as an placeholder as a workaround til 1.13+ is almost everywhere.
CABundle: []byte("\n"),
},
}
}
Expand All @@ -121,6 +127,7 @@ type Generator struct{}
func (Generator) RegisterMarkers(into *markers.Registry) error {
return into.Register(ConfigDefinition)
}

func (Generator) Generate(ctx *genall.GenerationContext) error {
var mutatingCfgs []admissionreg.Webhook
var validatingCfgs []admissionreg.Webhook
Expand All @@ -140,28 +147,36 @@ func (Generator) Generate(ctx *genall.GenerationContext) error {
}
}

var objs []interface{}
if len(mutatingCfgs) > 0 {
if err := ctx.WriteYAML(admissionreg.MutatingWebhookConfiguration{
objs = append(objs, &admissionreg.MutatingWebhookConfiguration{
TypeMeta: metav1.TypeMeta{
Kind: "MutatingWebhookConfiguration",
APIVersion: admissionreg.SchemeGroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: "mutating-webhook-configuration",
},
Webhooks: mutatingCfgs,
}, "mutating-webhook.yaml"); err != nil {
return err
}
})
}

if len(validatingCfgs) > 0 {
if err := ctx.WriteYAML(admissionreg.ValidatingWebhookConfiguration{
objs = append(objs, &admissionreg.ValidatingWebhookConfiguration{
TypeMeta: metav1.TypeMeta{
Kind: "ValidatingWebhookConfiguration",
APIVersion: admissionreg.SchemeGroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: "validating-webhook-configuration",
},
Webhooks: validatingCfgs,
}, "validating-webhook.yaml"); err != nil {
return err
}
})

}

if err := ctx.WriteYAML("manifests.yaml", objs...); err != nil {
return err
}

return nil
Expand Down