Skip to content

Commit

Permalink
🏃 write multiple webhooks in one file
Browse files Browse the repository at this point in the history
  • Loading branch information
Mengqi Yu committed May 14, 2019
1 parent 89f6323 commit 0f792e9
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 24 deletions.
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"), 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
18 changes: 10 additions & 8 deletions pkg/webhook/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,28 +140,30 @@ 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(),
},
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(),
},
Webhooks: validatingCfgs,
}, "validating-webhook.yaml"); err != nil {
return err
}
})

}

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

return nil
Expand Down

0 comments on commit 0f792e9

Please sign in to comment.