Skip to content

Commit

Permalink
Refactor kubebuilder alpha config-gen to use embed
Browse files Browse the repository at this point in the history
  • Loading branch information
pwittrock committed Jun 7, 2021
1 parent 1c23bd8 commit e4e7733
Show file tree
Hide file tree
Showing 12 changed files with 156 additions and 256 deletions.
7 changes: 2 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,18 @@ require (
github.com/go-logr/logr v0.3.0 // indirect
github.com/gobuffalo/flect v0.2.2
github.com/joelanford/go-apidiff v0.1.0
// TODO: remove this in favor of embed once using 1.16
github.com/markbates/pkger v0.17.1 // for `kubebuilder alpha config-gen`
github.com/onsi/ginkgo v1.15.0
github.com/onsi/gomega v1.10.5
github.com/spf13/afero v1.2.2
github.com/spf13/cobra v1.1.1
github.com/spf13/pflag v1.0.5
golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e
// for `kubebuilder alpha config-gen`
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 // indirect
k8s.io/api v0.20.2 // indirect
k8s.io/apiextensions-apiserver v0.20.1 // indirect
k8s.io/apimachinery v0.20.2 // for `kubebuilder alpha config-gen`
k8s.io/utils v0.0.0-20210111153108-fddb29f9d009 // indirect
sigs.k8s.io/controller-runtime v0.8.3 // for `kubebuilder alpha config-gen`
sigs.k8s.io/controller-tools v0.3.0 // for `kubebuilder alpha config-gen`
sigs.k8s.io/kustomize/kyaml v0.10.10 // for `kubebuilder alpha config-gen`
sigs.k8s.io/kustomize/kyaml v0.10.20 // for `kubebuilder alpha config-gen`
sigs.k8s.io/yaml v1.2.0
)
76 changes: 49 additions & 27 deletions go.sum

Large diffs are not rendered by default.

21 changes: 8 additions & 13 deletions pkg/cli/alpha/config-gen/cert-generation-filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,12 @@ func (c CertFilter) Filter(input []*yaml.RNode) ([]*yaml.RNode, error) {
return nil, err
}

s := &framework.Selector{
matches, err := (&framework.Selector{
Kinds: []string{
"ValidatingWebhookConfiguration",
"MutatingWebhookConfiguration",
},
}
matches, err := s.GetMatches(&framework.ResourceList{Items: input})
}).Filter(input)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -82,17 +81,13 @@ func (c CertFilter) Filter(input []*yaml.RNode) ([]*yaml.RNode, error) {
}
}

s = &framework.Selector{
Filter: func(n *yaml.RNode) bool {
// Allow-list conversion webhooks
m, _ := n.GetMeta()
if m.Kind != "CustomResourceDefinition" {
return true
}
return c.Spec.Webhooks.Conversions[m.Name]
matches, err = (&framework.Selector{
Kinds: []string{"CustomResourceDefinition"},
ResourceMatcher: func(m *yaml.RNode) bool {
meta, _ := m.GetMeta()
return c.Spec.Webhooks.Conversions[meta.Name]
},
}
matches, err = s.GetMatches(&framework.ResourceList{Items: input})
}).Filter(input)
if err != nil {
return nil, err
}
Expand Down
39 changes: 0 additions & 39 deletions pkg/cli/alpha/config-gen/cert-manager-patches.go

This file was deleted.

116 changes: 70 additions & 46 deletions pkg/cli/alpha/config-gen/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,75 +17,99 @@ limitations under the License.
package configgen

import (
// required to make sure the controller-tools is initialized fully
_ "sigs.k8s.io/controller-runtime/pkg/scheme"

"embed"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"

// TODO: switch to embed
"github.com/markbates/pkger"
"github.com/spf13/cobra"

// import pkged files
_ "sigs.k8s.io/kubebuilder/v3"
"sigs.k8s.io/kustomize/kyaml/fn/framework"
"sigs.k8s.io/kustomize/kyaml/fn/framework/command"
"sigs.k8s.io/kustomize/kyaml/fn/framework/parser"
"sigs.k8s.io/kustomize/kyaml/kio"
"sigs.k8s.io/kustomize/kyaml/yaml"
)

// NewCommand returns a new cobra command
func NewCommand() *cobra.Command {
kp := &KubebuilderConfigGen{}

// legacy kustomize function support
legacyPlugin := os.Getenv("KUSTOMIZE_PLUGIN_CONFIG_STRING")
err := yaml.Unmarshal([]byte(legacyPlugin), kp)
if err != nil {
log.Fatal(err)
}

// Eager check to make sure pkged templates are found.
err = pkger.Walk("/pkg/cli/alpha/config-gen/templates/resources", func(_ string, _ os.FileInfo, err error) error {
return err
})
if err != nil {
// this shouldn't fail if it was compiled correctly
log.Fatal(err)
}
// TemplateFS contains the templates used by config-gen
//go:embed templates/resources/* templates/patches/*
var TemplateFS embed.FS

c := framework.TemplateCommand{
API: kp,
func buildProcessor(value *KubebuilderConfigGen) framework.ResourceListProcessor {
return framework.TemplateProcessor{
MergeResources: true,

MergeResources: true, // apply additional inputs as patches

// these are run before the templates
PreProcessFilters: []kio.Filter{
// run controller-gen libraries to generate configuration from code
ControllerGenFilter{KubebuilderConfigGen: kp},
ControllerGenFilter{KubebuilderConfigGen: value},
// inject generated certificates
CertFilter{KubebuilderConfigGen: kp},
CertFilter{KubebuilderConfigGen: value},
},

// generate resources
// keep casting -- required by pkger to find the directory
TemplatesFn: framework.TemplatesFromDir(pkger.Dir("/pkg/cli/alpha/config-gen/templates/resources")),

// patch resources
PatchTemplatesFn: framework.PatchTemplatesFromDir(
CRDPatchTemplate(kp),
CertManagerPatchTemplate(kp),
ControllerManagerPatchTemplate(kp),
),
ResourceTemplates: []framework.ResourceTemplate{{
Templates: parser.TemplateFiles(filepath.Join("templates", "resources")).FromFS(TemplateFS),
}},
PatchTemplates: []framework.PatchTemplate{
&framework.ResourcePatchTemplate{
Selector: &framework.Selector{
Kinds: []string{"CustomResourceDefinition"},
ResourceMatcher: func(m *yaml.RNode) bool {
meta, _ := m.GetMeta()
return value.Spec.Webhooks.Conversions[meta.Name]
},
},
Templates: parser.TemplateFiles(filepath.Join("templates", "patches", "crd")).FromFS(TemplateFS),
},
&framework.ResourcePatchTemplate{
Selector: &framework.Selector{
Kinds: []string{"Deployment"},
Names: []string{"controller-manager"},
Labels: map[string]string{"control-plane": "controller-manager"},
},
Templates: parser.TemplateFiles(filepath.Join("templates", "patches", "controller-manager")).FromFS(TemplateFS),
},
&framework.ResourcePatchTemplate{
Selector: &framework.Selector{
Kinds: []string{
"CustomResourceDefinition",
"ValidatingWebhookConfiguration",
"MutatingWebhookConfiguration",
},
},
Templates: parser.TemplateFiles(filepath.Join("templates", "patches", "cert-manager")).FromFS(TemplateFS),
},
},

// perform final modifications
PostProcessFilters: []kio.Filter{
// sort the resources
ComponentFilter{KubebuilderConfigGen: kp},
SortFilter{KubebuilderConfigGen: kp},
ComponentFilter{KubebuilderConfigGen: value},
SortFilter{KubebuilderConfigGen: value},
},
}.GetCommand()
TemplateData: value,
}
}

func buildCmd() *cobra.Command {
kp := &KubebuilderConfigGen{}

// legacy kustomize function support
legacyPlugin := os.Getenv("KUSTOMIZE_PLUGIN_CONFIG_STRING")
err := yaml.Unmarshal([]byte(legacyPlugin), kp)
if err != nil {
log.Fatal(err)
}

cmd := command.Build(buildProcessor(kp), command.StandaloneEnabled, false)
return cmd
}

// NewCommand returns a new cobra command
func NewCommand() *cobra.Command {
c := buildCmd()

if os.Getenv("KUSTOMIZE_FUNCTION") == "true" {
// run as part of kustomize -- read from stdin
Expand Down
2 changes: 1 addition & 1 deletion pkg/cli/alpha/config-gen/component-filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (cf ComponentFilter) Filter(input []*yaml.RNode) ([]*yaml.RNode, error) {
Names: []string{"manager-config"},
Namespaces: []string{cf.Namespace},
}
matches, err := s.GetMatches(&framework.ResourceList{Items: input})
matches, err := s.Filter(input)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cli/alpha/config-gen/configgen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
)

func TestNewCommand(t *testing.T) {
test := frameworktestutil.ResultsChecker{
test := frameworktestutil.CommandResultsChecker{
Command: configgen.NewCommand,
// Uncomment this line to update the testdata directory
// UpdateExpectedFromActual: true,
Expand Down
39 changes: 0 additions & 39 deletions pkg/cli/alpha/config-gen/controller-manager-patches.go

This file was deleted.

40 changes: 0 additions & 40 deletions pkg/cli/alpha/config-gen/crd-patches.go

This file was deleted.

24 changes: 11 additions & 13 deletions pkg/cli/alpha/config-gen/examples/advancedextension/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"os"

"sigs.k8s.io/kustomize/kyaml/fn/framework"
"sigs.k8s.io/kustomize/kyaml/fn/framework/command"
"sigs.k8s.io/kustomize/kyaml/kio"
"sigs.k8s.io/kustomize/kyaml/yaml"
)

Expand All @@ -31,20 +33,15 @@ type API struct {
// Advanced function for transforming kubebuilder output by looking at the resources and modifying them.
func main() {
api := &API{}

// setup command to modify resources
c := framework.TemplateCommand{
API: api,
// process all the parsed resources
PreProcess: func(rl *framework.ResourceList) error {

// select the Deployment with the controller-manager name
c := framework.SimpleProcessor{
Config: api,
Filter: kio.FilterFunc(func(r []*yaml.RNode) ([]*yaml.RNode, error) {
matches, err := (&framework.Selector{
Kinds: []string{"Deployment"},
Names: []string{"controller-manager"},
}).GetMatches(rl)
}).Filter(r)
if err != nil {
return err
return nil, err
}

// set the replicas on all matching resources
Expand All @@ -55,11 +52,12 @@ func main() {
// set the value
yaml.Set(yaml.NewScalarRNode(fmt.Sprintf("%d", api.Replicas))))
}
return nil
},
return matches, nil
}),
}

if err := c.GetCommand().Execute(); err != nil {
cmd := command.Build(&c, command.StandaloneEnabled, false)
if err := cmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
Expand Down
Loading

0 comments on commit e4e7733

Please sign in to comment.