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

🌱 flavorgen generates all flavors by default #2172

Merged
merged 1 commit into from
Aug 9, 2023
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
18 changes: 10 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ help: # Display this help

.PHONY: generate
generate: ## Run all generate targets
$(MAKE) generate-modules generate-manifests generate-go-deepcopy generate-go-conversions
$(MAKE) generate-modules generate-manifests generate-go-deepcopy generate-go-conversions generate-flavors

.PHONY: generate-manifests
generate-manifests: $(CONTROLLER_GEN) ## Generate manifests e.g. CRD, RBAC etc.
Expand Down Expand Up @@ -339,7 +339,7 @@ APIDIFF_OLD_COMMIT ?= $(shell git rev-parse origin/main)
apidiff: $(GO_APIDIFF) ## Check for API differences
$(GO_APIDIFF) $(APIDIFF_OLD_COMMIT) --print-compatible

ALL_VERIFY_CHECKS = boilerplate modules gen conversions doctoc
ALL_VERIFY_CHECKS = boilerplate modules gen conversions doctoc flavors

.PHONY: verify
verify: $(addprefix verify-,$(ALL_VERIFY_CHECKS)) lint-markdown lint-shell ## Run all verify-* targets
Expand Down Expand Up @@ -381,6 +381,13 @@ verify-boilerplate: ## Verify boilerplate text exists in each file
verify-container-images: ## Verify container images
TRACE=$(TRACE) ./hack/verify-container-images.sh

.PHONY: verify-flavors
verify-flavors: $(FLAVOR_DIR) generate-flavors ## Verify generated flavors
@if !(git diff --quiet HEAD -- $(FLAVOR_DIR)); then \
git diff $(FLAVOR_DIR); \
echo "flavor files in templates directory are out of date"; exit 1; \
fi

## --------------------------------------
## Build
## --------------------------------------
Expand Down Expand Up @@ -572,12 +579,7 @@ dev-flavors: $(OVERRIDES_DIR)

.PHONY: generate-flavors
generate-flavors: $(FLAVOR_DIR)
go run ./packaging/flavorgen -f vip > $(FLAVOR_DIR)/cluster-template.yaml
go run ./packaging/flavorgen -f external-loadbalancer > $(FLAVOR_DIR)/cluster-template-external-loadbalancer.yaml
go run ./packaging/flavorgen -f cluster-class > $(FLAVOR_DIR)/clusterclass-template.yaml
go run ./packaging/flavorgen -f cluster-topology > $(FLAVOR_DIR)/cluster-template-topology.yaml
go run ./packaging/flavorgen -f ignition > $(FLAVOR_DIR)/cluster-template-ignition.yaml
go run ./packaging/flavorgen -f node-ipam > $(FLAVOR_DIR)/cluster-template-node-ipam.yaml
go run ./packaging/flavorgen --output-dir $(FLAVOR_DIR)

.PHONY: release-staging
release-staging: ## Build and push container images to the staging registry
Expand Down
104 changes: 87 additions & 17 deletions packaging/flavorgen/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,41 @@ limitations under the License.
package cmd

import (
"fmt"
"os"
"path/filepath"

"github.com/pkg/errors"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/runtime"

"sigs.k8s.io/cluster-api-provider-vsphere/packaging/flavorgen/flavors"
"sigs.k8s.io/cluster-api-provider-vsphere/packaging/flavorgen/flavors/env"
"sigs.k8s.io/cluster-api-provider-vsphere/packaging/flavorgen/flavors/util"
)

const flavorFlag = "flavor"
const outputDirFlag = "output-dir"

var (
flavorMappings = map[string]string{
flavors.VIP: "cluster-template.yaml",
flavors.ExternalLoadBalancer: "cluster-template-external-loadbalancer.yaml",
flavors.ClusterClass: "clusterclass-template.yaml",
flavors.ClusterTopology: "cluster-template-topology.yaml",
flavors.Ignition: "cluster-template-ignition.yaml",
flavors.NodeIPAM: "cluster-template-node-ipam.yaml",
}

allFlavors = []string{
flavors.VIP,
flavors.ExternalLoadBalancer,
flavors.ClusterClass,
flavors.Ignition,
flavors.NodeIPAM,
flavors.ClusterTopology,
}
)

func RootCmd() *cobra.Command {
rootCmd := &cobra.Command{
Expand All @@ -36,8 +60,11 @@ func RootCmd() *cobra.Command {
RunE: func(command *cobra.Command, args []string) error {
return RunRoot(command)
},
SilenceUsage: true,
}
rootCmd.Flags().StringP(flavorFlag, "f", "", "Name of flavor to compile")
rootCmd.Flags().StringP(outputDirFlag, "o", "", "Directory to store the generated flavor templates.\nBy default the current directory is used.\nUse '-' to output the result to stdout.")

return rootCmd
}

Expand All @@ -52,30 +79,73 @@ func RunRoot(command *cobra.Command) error {
if err != nil {
return errors.Wrapf(err, "error accessing flag %s for command %s", flavorFlag, command.Name())
}
outputDir, err := command.Flags().GetString(outputDirFlag)
if err != nil {
return errors.Wrapf(err, "error accessing flag %s for command %s", outputDirFlag, command.Name())
}
var outputFlavors []string
if flavor != "" {
outputFlavors = append(outputFlavors, flavor)
} else {
outputFlavors = allFlavors
}
generateMultiFlavors := len(outputFlavors) > 1
for _, f := range outputFlavors {
manifest, err := generateSingle(f)
if err != nil {
return err
}

yamlFileName, ok := flavorMappings[f]
if !ok {
return fmt.Errorf("file mapping for flavor %q is missng in flavorMappings", f)
}

if outputDir == "-" {
if generateMultiFlavors {
sbueringer marked this conversation as resolved.
Show resolved Hide resolved
// use the yaml filename as a section delimiter
fmt.Printf("### %s\n", yamlFileName)
}
fmt.Print(manifest)
continue
}

yamlPath := filepath.Join(outputDir, yamlFileName)
err = os.WriteFile(yamlPath, []byte(manifest), 0600)
if err != nil {
return errors.Wrapf(err, "failed to save manifest content to file for flavor %s", f)
}
}

return nil
}

func generateSingle(flavor string) (string, error) {
replacements := append([]util.Replacement{}, util.DefaultReplacements...)

var objs []runtime.Object
switch flavor {
case flavors.VIP:
util.PrintObjects(flavors.MultiNodeTemplateWithKubeVIP())
objs = flavors.MultiNodeTemplateWithKubeVIP()
case flavors.ExternalLoadBalancer:
util.PrintObjects(flavors.MultiNodeTemplateWithExternalLoadBalancer())
objs = flavors.MultiNodeTemplateWithExternalLoadBalancer()
case flavors.ClusterClass:
util.PrintObjects(flavors.ClusterClassTemplateWithKubeVIP())
objs = flavors.ClusterClassTemplateWithKubeVIP()
case flavors.ClusterTopology:
additionalReplacements := []util.Replacement{
{
Kind: "Cluster",
Name: "${CLUSTER_NAME}",
Value: env.ControlPlaneMachineCountVar,
FieldPath: []string{"spec", "topology", "controlPlane", "replicas"},
},
}
util.Replacements = append(util.Replacements, additionalReplacements...)
util.PrintObjects(flavors.ClusterTopologyTemplateKubeVIP())
objs = flavors.ClusterTopologyTemplateKubeVIP()
replacements = append(replacements, util.Replacement{
sbueringer marked this conversation as resolved.
Show resolved Hide resolved
Kind: "Cluster",
Name: "${CLUSTER_NAME}",
Value: env.ControlPlaneMachineCountVar,
FieldPath: []string{"spec", "topology", "controlPlane", "replicas"},
})
case flavors.Ignition:
util.PrintObjects(flavors.MultiNodeTemplateWithKubeVIPIgnition())
objs = flavors.MultiNodeTemplateWithKubeVIPIgnition()
case flavors.NodeIPAM:
util.PrintObjects(flavors.MultiNodeTemplateWithKubeVIPNodeIPAM())
objs = flavors.MultiNodeTemplateWithKubeVIPNodeIPAM()
default:
return errors.Errorf("invalid flavor")
return "", errors.Errorf("invalid flavor")
}
return nil

return util.GenerateManifestYaml(objs, replacements), nil
}
2 changes: 1 addition & 1 deletion packaging/flavorgen/flavors/crs/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func newConfigMapManifests(name string, o []runtime.Object) *v1.ConfigMap {
Namespace: env.NamespaceVar,
},
Data: map[string]string{
"data": util.GenerateManifestYaml(o),
"data": util.GenerateManifestYaml(o, util.DefaultReplacements),
},
}
}
Expand Down
14 changes: 3 additions & 11 deletions packaging/flavorgen/flavors/util/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package util

import (
"fmt"
"reflect"
"regexp"
"strings"
Expand All @@ -37,7 +36,7 @@ type Replacement struct {
}

var (
Replacements = []Replacement{
DefaultReplacements = []Replacement{
{
Kind: "KubeadmControlPlane",
Name: "${CLUSTER_NAME}",
Expand Down Expand Up @@ -180,24 +179,17 @@ func GenerateObjectYAML(obj runtime.Object, replacements []Replacement) string {
return str
}

func GenerateManifestYaml(objs []runtime.Object) string {
func GenerateManifestYaml(objs []runtime.Object, replacements []Replacement) string {
var sb strings.Builder

for _, o := range objs {
sb.WriteString("---\n")
sb.WriteString(GenerateObjectYAML(o, Replacements))
sb.WriteString(GenerateObjectYAML(o, replacements))
}

return sb.String()
}

func PrintObjects(objs []runtime.Object) {
for _, o := range objs {
o := o
fmt.Printf("---\n%s", GenerateObjectYAML(o, Replacements)) //nolint:forbidigo
}
}

func TypeToKind(i interface{}) string {
return reflect.ValueOf(i).Elem().Type().Name()
}