Skip to content
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
14 changes: 7 additions & 7 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,6 @@ var initCmd = &cobra.Command{
return fmt.Errorf("failed to set up environment: %w", err)
}

ctx = context.WithValue(ctx, "quiet", false)
ctx = context.WithValue(ctx, "decrypt", false)
initPipeline, err := pipelines.WithPipeline(injector, ctx, "initPipeline")
if err != nil {
return fmt.Errorf("failed to set up init pipeline: %w", err)
}

configHandler := injector.Resolve("configHandler").(config.ConfigHandler)

if initBackend != "" {
Expand Down Expand Up @@ -130,6 +123,13 @@ var initCmd = &cobra.Command{
}
}

ctx = context.WithValue(ctx, "quiet", false)
ctx = context.WithValue(ctx, "decrypt", false)
initPipeline, err := pipelines.WithPipeline(injector, ctx, "initPipeline")
if err != nil {
return fmt.Errorf("failed to set up init pipeline: %w", err)
}

return initPipeline.Execute(ctx)
},
}
Expand Down
69 changes: 49 additions & 20 deletions pkg/config/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,53 @@ var commonTerraformConfig = terraform.TerraformConfig{
},
}

// commonClusterConfig_NoHostPorts is the base cluster configuration without hostports,
// used for VM drivers that use native networking (colima, docker)
var commonClusterConfig_NoHostPorts = cluster.ClusterConfig{
Enabled: ptrBool(true),
Platform: ptrString("local"),
Driver: ptrString("talos"),
ControlPlanes: cluster.NodeGroupConfig{
Count: ptrInt(1),
CPU: ptrInt(constants.DEFAULT_TALOS_CONTROL_PLANE_CPU),
Memory: ptrInt(constants.DEFAULT_TALOS_CONTROL_PLANE_RAM),
Nodes: make(map[string]cluster.NodeConfig),
HostPorts: []string{},
},
Workers: cluster.NodeGroupConfig{
Count: ptrInt(1),
CPU: ptrInt(constants.DEFAULT_TALOS_WORKER_CPU),
Memory: ptrInt(constants.DEFAULT_TALOS_WORKER_RAM),
Nodes: make(map[string]cluster.NodeConfig),
HostPorts: []string{},
Volumes: []string{"${WINDSOR_PROJECT_ROOT}/.volumes:/var/local"},
},
}

// commonClusterConfig_WithHostPorts is the base cluster configuration with hostports,
// used for VM drivers that need port forwarding (docker-desktop)
var commonClusterConfig_WithHostPorts = cluster.ClusterConfig{
Enabled: ptrBool(true),
Platform: ptrString("local"),
Driver: ptrString("talos"),
ControlPlanes: cluster.NodeGroupConfig{
Count: ptrInt(1),
CPU: ptrInt(constants.DEFAULT_TALOS_CONTROL_PLANE_CPU),
Memory: ptrInt(constants.DEFAULT_TALOS_CONTROL_PLANE_RAM),
Nodes: make(map[string]cluster.NodeConfig),
HostPorts: []string{},
},
Workers: cluster.NodeGroupConfig{
Count: ptrInt(1),
CPU: ptrInt(constants.DEFAULT_TALOS_WORKER_CPU),
Memory: ptrInt(constants.DEFAULT_TALOS_WORKER_RAM),
Nodes: make(map[string]cluster.NodeConfig),
HostPorts: []string{"8080:30080/tcp", "8443:30443/tcp", "9292:30292/tcp", "8053:30053/udp"},
Volumes: []string{"${WINDSOR_PROJECT_ROOT}/.volumes:/var/local"},
},
}

// Preserve the original commonClusterConfig for backwards compatibility with DefaultConfig
var commonClusterConfig = cluster.ClusterConfig{
Enabled: ptrBool(true),
Platform: ptrString("local"),
Expand Down Expand Up @@ -102,25 +149,7 @@ var DefaultConfig_Localhost = v1alpha1.Context{
Docker: commonDockerConfig.Copy(),
Git: commonGitConfig.Copy(),
Terraform: commonTerraformConfig.Copy(),
Cluster: &cluster.ClusterConfig{
Enabled: ptrBool(true),
Platform: ptrString("local"),
Driver: ptrString("talos"),
ControlPlanes: cluster.NodeGroupConfig{
Count: ptrInt(1),
CPU: ptrInt(constants.DEFAULT_TALOS_CONTROL_PLANE_CPU),
Memory: ptrInt(constants.DEFAULT_TALOS_CONTROL_PLANE_RAM),
Nodes: make(map[string]cluster.NodeConfig),
},
Workers: cluster.NodeGroupConfig{
Count: ptrInt(1),
CPU: ptrInt(constants.DEFAULT_TALOS_WORKER_CPU),
Memory: ptrInt(constants.DEFAULT_TALOS_WORKER_RAM),
Nodes: make(map[string]cluster.NodeConfig),
HostPorts: []string{"8080:30080/tcp", "8443:30443/tcp", "9292:30292/tcp", "8053:30053/udp"},
Volumes: []string{"${WINDSOR_PROJECT_ROOT}/.volumes:/var/local"},
},
},
Cluster: commonClusterConfig_WithHostPorts.Copy(),
Network: &network.NetworkConfig{
CIDRBlock: ptrString(constants.DEFAULT_NETWORK_CIDR),
},
Expand All @@ -138,7 +167,7 @@ var DefaultConfig_Full = v1alpha1.Context{
Docker: commonDockerConfig.Copy(),
Git: commonGitConfig.Copy(),
Terraform: commonTerraformConfig.Copy(),
Cluster: commonClusterConfig.Copy(),
Cluster: commonClusterConfig_NoHostPorts.Copy(),
Network: &network.NetworkConfig{
CIDRBlock: ptrString(constants.DEFAULT_NETWORK_CIDR),
LoadBalancerIPs: &struct {
Expand Down
75 changes: 75 additions & 0 deletions pkg/config/defaults_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package config

import (
"testing"
)

func TestDefaultConfigurations_HostPorts(t *testing.T) {
t.Run("DefaultConfig_Localhost_HasHostPorts", func(t *testing.T) {
// Given the DefaultConfig_Localhost configuration (used for docker-desktop)
config := DefaultConfig_Localhost

// Then the workers should have hostports configured
if config.Cluster == nil {
t.Fatal("Expected cluster configuration to be present")
}

expectedHostPorts := []string{"8080:30080/tcp", "8443:30443/tcp", "9292:30292/tcp", "8053:30053/udp"}
actualHostPorts := config.Cluster.Workers.HostPorts

if len(actualHostPorts) != len(expectedHostPorts) {
t.Errorf("Expected %d hostports, got %d", len(expectedHostPorts), len(actualHostPorts))
}

for i, expected := range expectedHostPorts {
if i >= len(actualHostPorts) || actualHostPorts[i] != expected {
t.Errorf("Expected hostport %s at index %d, got %s", expected, i,
func() string {
if i < len(actualHostPorts) {
return actualHostPorts[i]
}
return "missing"
}())
}
}
})

t.Run("DefaultConfig_Full_HasNoHostPorts", func(t *testing.T) {
// Given the DefaultConfig_Full configuration (used for colima/docker)
config := DefaultConfig_Full

// Then the workers should have no hostports configured
if config.Cluster == nil {
t.Fatal("Expected cluster configuration to be present")
}

actualHostPorts := config.Cluster.Workers.HostPorts

if len(actualHostPorts) != 0 {
t.Errorf("Expected no hostports for DefaultConfig_Full, got %d: %v", len(actualHostPorts), actualHostPorts)
}

// And the controlplanes should also have no hostports
actualControlPlaneHostPorts := config.Cluster.ControlPlanes.HostPorts

if len(actualControlPlaneHostPorts) != 0 {
t.Errorf("Expected no hostports for DefaultConfig_Full controlplanes, got %d: %v", len(actualControlPlaneHostPorts), actualControlPlaneHostPorts)
}
})

t.Run("DefaultConfig_Localhost_ControlPlanes_HasNoHostPorts", func(t *testing.T) {
// Given the DefaultConfig_Localhost configuration
config := DefaultConfig_Localhost

// Then the controlplanes should have no hostports (only workers need them for docker-desktop)
if config.Cluster == nil {
t.Fatal("Expected cluster configuration to be present")
}

actualControlPlaneHostPorts := config.Cluster.ControlPlanes.HostPorts

if len(actualControlPlaneHostPorts) != 0 {
t.Errorf("Expected no hostports for DefaultConfig_Localhost controlplanes, got %d: %v", len(actualControlPlaneHostPorts), actualControlPlaneHostPorts)
}
})
}
37 changes: 20 additions & 17 deletions pkg/pipelines/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,20 @@ func (p *InitPipeline) Initialize(injector di.Injector, ctx context.Context) err
return err
}

// Generate context ID for saving config
if err := p.configHandler.GenerateContextID(); err != nil {
return fmt.Errorf("failed to generate context ID: %w", err)
}

// Save configuration before network manager assigns addresses to services
reset := false
if resetValue := ctx.Value("reset"); resetValue != nil {
reset = resetValue.(bool)
}
if err := p.saveConfiguration(reset); err != nil {
return err
}

// Component Collection Phase

kubernetesClient := p.withKubernetesClient()
Expand Down Expand Up @@ -231,29 +245,14 @@ func (p *InitPipeline) Initialize(injector di.Injector, ctx context.Context) err
return nil
}

// Execute performs initialization by writing reset tokens, generating context IDs,
// configuring defaults, saving configuration, processing templates, handling blueprints separately,
// Execute performs initialization by writing reset tokens, processing templates, handling blueprints separately,
// writing blueprint files, resolving Terraform modules, and generating final output files.
func (p *InitPipeline) Execute(ctx context.Context) error {

// Phase 1: Setup & configuration
// Phase 1: Setup
if _, err := p.shell.WriteResetToken(); err != nil {
return fmt.Errorf("Error writing reset token: %w", err)
}
if err := p.configHandler.GenerateContextID(); err != nil {
return fmt.Errorf("failed to generate context ID: %w", err)
}
reset := false
if resetValue := ctx.Value("reset"); resetValue != nil {
reset = resetValue.(bool)
}
contextName := p.determineContextName(ctx)
if err := p.setDefaultConfiguration(ctx, contextName); err != nil {
return err
}
if err := p.saveConfiguration(reset); err != nil {
return err
}

// Phase 2: Template processing
templateData, err := p.prepareTemplateData(ctx)
Expand All @@ -266,6 +265,10 @@ func (p *InitPipeline) Execute(ctx context.Context) error {
}

// Phase 3: Blueprint handling
reset := false
if resetValue := ctx.Value("reset"); resetValue != nil {
reset = resetValue.(bool)
}
if err := p.handleBlueprintLoading(ctx, renderedData, reset); err != nil {
return err
}
Expand Down
Loading
Loading