func generateClusterConfig()

in cmd/eksctl-anywhere/cmd/generateclusterconfig.go [59:170]


func generateClusterConfig(clusterName string) error {
	var resources [][]byte
	var datacenterYaml []byte
	var machineGroupYaml [][]byte
	var clusterConfigOpts []v1alpha1.ClusterGenerateOpt
	switch strings.ToLower(viper.GetString("provider")) {
	case constants.DockerProviderName:
		datacenterConfig := v1alpha1.NewDockerDatacenterConfigGenerate(clusterName)
		clusterConfigOpts = append(clusterConfigOpts, v1alpha1.WithDatacenterRef(datacenterConfig))
		clusterConfigOpts = append(clusterConfigOpts,
			v1alpha1.ControlPlaneConfigCount(1),
			v1alpha1.ExternalETCDConfigCount(1),
			v1alpha1.WorkerNodeConfigCount(1),
			v1alpha1.WorkerNodeConfigName(constants.DefaultWorkerNodeGroupName),
		)
		dcyaml, err := yaml.Marshal(datacenterConfig)
		if err != nil {
			return fmt.Errorf("error outputting yaml: %v", err)
		}
		datacenterYaml = dcyaml
	case constants.VSphereProviderName:
		clusterConfigOpts = append(clusterConfigOpts, v1alpha1.WithClusterEndpoint())
		datacenterConfig := v1alpha1.NewVSphereDatacenterConfigGenerate(clusterName)
		clusterConfigOpts = append(clusterConfigOpts, v1alpha1.WithDatacenterRef(datacenterConfig))
		clusterConfigOpts = append(clusterConfigOpts,
			v1alpha1.ControlPlaneConfigCount(2),
			v1alpha1.ExternalETCDConfigCount(3),
			v1alpha1.WorkerNodeConfigCount(2),
			v1alpha1.WorkerNodeConfigName(constants.DefaultWorkerNodeGroupName),
		)
		dcyaml, err := yaml.Marshal(datacenterConfig)
		if err != nil {
			return fmt.Errorf("error outputting yaml: %v", err)
		}
		datacenterYaml = dcyaml
		// need to default control plane config name to something different from the cluster name based on assumption
		// in controller code
		cpMachineConfig := v1alpha1.NewVSphereMachineConfigGenerate(clusterName + "-cp")
		workerMachineConfig := v1alpha1.NewVSphereMachineConfigGenerate(clusterName)
		etcdMachineConfig := v1alpha1.NewVSphereMachineConfigGenerate(fmt.Sprintf("%s-etcd", clusterName))
		clusterConfigOpts = append(clusterConfigOpts,
			v1alpha1.WithCPMachineGroupRef(cpMachineConfig),
			v1alpha1.WithWorkerMachineGroupRef(workerMachineConfig),
			v1alpha1.WithEtcdMachineGroupRef(etcdMachineConfig),
		)
		cpMcYaml, err := yaml.Marshal(cpMachineConfig)
		if err != nil {
			return fmt.Errorf("error outputting yaml: %v", err)
		}
		workerMcYaml, err := yaml.Marshal(workerMachineConfig)
		if err != nil {
			return fmt.Errorf("error outputting yaml: %v", err)
		}
		etcdMcYaml, err := yaml.Marshal(etcdMachineConfig)
		if err != nil {
			return fmt.Errorf("error outputting yaml: %v", err)
		}
		machineGroupYaml = append(machineGroupYaml, cpMcYaml, workerMcYaml, etcdMcYaml)
	case constants.TinkerbellProviderName:
		if features.IsActive(features.TinkerbellProvider()) {
			clusterConfigOpts = append(clusterConfigOpts, v1alpha1.WithClusterEndpoint())
			datacenterConfig := v1alpha1.NewTinkerbellDatacenterConfigGenerate(clusterName)
			clusterConfigOpts = append(clusterConfigOpts, v1alpha1.WithDatacenterRef(datacenterConfig))
			clusterConfigOpts = append(clusterConfigOpts,
				v1alpha1.ControlPlaneConfigCount(1),
				v1alpha1.WorkerNodeConfigCount(1),
				v1alpha1.WorkerNodeConfigName(constants.DefaultWorkerNodeGroupName),
			)
			dcyaml, err := yaml.Marshal(datacenterConfig)
			if err != nil {
				return fmt.Errorf("error outputting yaml: %v", err)
			}
			datacenterYaml = dcyaml

			cpMachineConfig := v1alpha1.NewTinkerbellMachineConfigGenerate(clusterName + "-cp")
			workerMachineConfig := v1alpha1.NewTinkerbellMachineConfigGenerate(clusterName)
			clusterConfigOpts = append(clusterConfigOpts,
				v1alpha1.WithCPMachineGroupRef(cpMachineConfig),
				v1alpha1.WithWorkerMachineGroupRef(workerMachineConfig),
			)
			cpMcYaml, err := yaml.Marshal(cpMachineConfig)
			if err != nil {
				return fmt.Errorf("error outputting yaml: %v", err)
			}
			workerMcYaml, err := yaml.Marshal(workerMachineConfig)
			if err != nil {
				return fmt.Errorf("error outputting yaml: %v", err)
			}
			machineGroupYaml = append(machineGroupYaml, cpMcYaml, workerMcYaml)
		} else {
			return fmt.Errorf("The tinkerbell infrastructure provider is still in development!")
		}
	default:
		return fmt.Errorf("not a valid provider")
	}
	config := v1alpha1.NewClusterGenerate(clusterName, clusterConfigOpts...)

	configMarshal, err := yaml.Marshal(config)
	if err != nil {
		return fmt.Errorf("error outputting yaml: %v", err)
	}
	clusterYaml, err := api.CleanupPathsFromYaml(configMarshal, removeFromDefaultConfig)
	if err != nil {
		return fmt.Errorf("error cleaning paths from yaml: %v", err)
	}
	resources = append(resources, clusterYaml, datacenterYaml)
	if len(machineGroupYaml) > 0 {
		resources = append(resources, machineGroupYaml...)
	}
	fmt.Println(string(templater.AppendYamlResources(resources...)))
	return nil
}