Skip to content

Commit

Permalink
flavorgen generates all flavors by default
Browse files Browse the repository at this point in the history
  • Loading branch information
laozc committed Aug 8, 2023
1 parent f1341ab commit 671aea8
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 34 deletions.
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 @@ -335,7 +335,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
ALL_VERIFY_CHECKS = boilerplate modules gen conversions flavor

.PHONY: verify
verify: $(addprefix verify-,$(ALL_VERIFY_CHECKS)) lint-markdown lint-shell ## Run all verify-* targets
Expand Down Expand Up @@ -370,6 +370,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 @@ -561,12 +568,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
105 changes: 88 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",
flavors.ExternalLoadBalancer: "cluster-template-external-loadbalancer",
flavors.ClusterClass: "clusterclass-template",
flavors.ClusterTopology: "cluster-template-topology",
flavors.Ignition: "cluster-template-ignition",
flavors.NodeIPAM: "cluster-template-node-ipam",
}

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,74 @@ 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
}

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

yamlFileName := fileMapping + ".yaml"
if outputDir == "-" {
if generateMultiFlavors {
// 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 dump manifest content to file for flavor %s", f)
}
}

return nil
}

func generateSingle(flavor string) (string, error) {
util.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()
util.Replacements = append(util.Replacements, util.Replacement{
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), nil
}
11 changes: 2 additions & 9 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 All @@ -63,6 +62,7 @@ var (
FieldPath: []string{"spec", "template", "spec"},
},
}
Replacements = append([]Replacement{}, DefaultReplacements...)

stringVars = []string{
regexVar(env.ClusterNameVar),
Expand Down Expand Up @@ -191,13 +191,6 @@ func GenerateManifestYaml(objs []runtime.Object) string {
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()
}

0 comments on commit 671aea8

Please sign in to comment.