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 7, 2023
1 parent a2f8e74 commit 81452ac
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 20 deletions.
19 changes: 11 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,14 @@ 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
go run ./packaging/flavorgen --output-dir $(FLAVOR_DIR)
@if !(git diff --quiet HEAD -- templates); then \
git diff; \
echo "flavor files in templates directory are out of date"; exit 1; \
fi

## --------------------------------------
## Build
## --------------------------------------
Expand Down Expand Up @@ -561,12 +569,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
80 changes: 72 additions & 8 deletions packaging/flavorgen/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,42 @@ 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,
// keep the ClusterTopology the last one
flavors.ClusterTopology,
}
)

func RootCmd() *cobra.Command {
rootCmd := &cobra.Command{
Expand All @@ -36,8 +61,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 template")

return rootCmd
}

Expand All @@ -52,13 +80,50 @@ 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 flavors []string
if flavor != "" {
flavors = append(flavors, flavor)
} else {
flavors = allFlavors
}
generateMultiFlavors := len(flavors) > 1
for _, f := range flavors {
objs, err := generateSingle(f)
if err != nil {
return err
}

yamlFileName := flavorMappings[f] + ".yaml"
if outputDir == "" {
if generateMultiFlavors {
fmt.Printf(fmt.Sprintf("### %s\n", yamlFileName))
}
util.PrintObjects(objs)
continue
}

yamlPath := filepath.Join(outputDir, yamlFileName)
err = util.DumpObjectsToFile(objs, yamlPath)
if err != nil {
return errors.Wrapf(err, "failed to dump manifest content to file for flavor %s", f)
}
}

return nil
}

func generateSingle(flavor string) ([]runtime.Object, error) {
switch flavor {
case flavors.VIP:
util.PrintObjects(flavors.MultiNodeTemplateWithKubeVIP())
return flavors.MultiNodeTemplateWithKubeVIP(), nil
case flavors.ExternalLoadBalancer:
util.PrintObjects(flavors.MultiNodeTemplateWithExternalLoadBalancer())
return flavors.MultiNodeTemplateWithExternalLoadBalancer(), nil
case flavors.ClusterClass:
util.PrintObjects(flavors.ClusterClassTemplateWithKubeVIP())
return flavors.ClusterClassTemplateWithKubeVIP(), nil
case flavors.ClusterTopology:
additionalReplacements := []util.Replacement{
{
Expand All @@ -69,13 +134,12 @@ func RunRoot(command *cobra.Command) error {
},
}
util.Replacements = append(util.Replacements, additionalReplacements...)
util.PrintObjects(flavors.ClusterTopologyTemplateKubeVIP())
return flavors.ClusterTopologyTemplateKubeVIP(), nil
case flavors.Ignition:
util.PrintObjects(flavors.MultiNodeTemplateWithKubeVIPIgnition())
return flavors.MultiNodeTemplateWithKubeVIPIgnition(), nil
case flavors.NodeIPAM:
util.PrintObjects(flavors.MultiNodeTemplateWithKubeVIPNodeIPAM())
return flavors.MultiNodeTemplateWithKubeVIPNodeIPAM(), nil
default:
return errors.Errorf("invalid flavor")
return nil, errors.Errorf("invalid flavor")
}
return nil
}
11 changes: 7 additions & 4 deletions packaging/flavorgen/flavors/util/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package util

import (
"fmt"
"os"
"reflect"
"regexp"
"strings"
Expand Down Expand Up @@ -192,10 +193,12 @@ func GenerateManifestYaml(objs []runtime.Object) string {
}

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

func DumpObjectsToFile(objs []runtime.Object, path string) error {
manifest := GenerateManifestYaml(objs)
return os.WriteFile(path, []byte(manifest), 0644)
}

func TypeToKind(i interface{}) string {
Expand Down

0 comments on commit 81452ac

Please sign in to comment.