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

🐛 Refactor alpha config-gen to use embed from go 1.16 #2204

Merged
merged 1 commit into from
Jun 8, 2021
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
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.

118 changes: 72 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,101 @@ 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{
TemplateData: value,
Kinds: []string{"Deployment"},
Names: []string{"controller-manager"},
Namespaces: []string{"{{ .Namespace }}"},
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.

Loading