Skip to content

Commit

Permalink
unti tests for zones. WIP managed
Browse files Browse the repository at this point in the history
  • Loading branch information
mtulio committed Mar 13, 2024
1 parent 2d83bc3 commit b6522ab
Show file tree
Hide file tree
Showing 3 changed files with 248 additions and 18 deletions.
10 changes: 6 additions & 4 deletions pkg/asset/manifests/aws/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,12 @@ func GenerateClusterAssets(ic *installconfig.InstallConfig, clusterID *installco

// Set the VPC and zones (managed) or subnets (BYO VPC) based in the
// install-config.yaml.
err := setZones(&zoneConfigInput{
Config: ic,
ClusterID: clusterID,
Cluster: awsCluster,
err = setZones(&zoneConfigInput{
InstallConfig: ic,
Config: ic.Config,
Meta: ic.AWS,
ClusterID: clusterID,
Cluster: awsCluster,
})
if err != nil {
return nil, errors.Wrap(err, "failed to set cluster zones or subnets")
Expand Down
35 changes: 21 additions & 14 deletions pkg/asset/manifests/aws/zones.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ import (
)

type zoneConfigInput struct {
Config *installconfig.InstallConfig
Cluster *capa.AWSCluster
ClusterID *installconfig.ClusterID
InstallConfig *installconfig.InstallConfig
Config *types.InstallConfig
Meta *aws.Metadata
Cluster *capa.AWSCluster
ClusterID *installconfig.ClusterID
}

// setZones creates the CAPI NetworkSpec structures for managed or
Expand All @@ -35,7 +37,7 @@ func setZones(in *zoneConfigInput) error {
// setZonesManagedVPC creates the CAPI NetworkSpec.Subnets setting the
// desired subnets from install-config.yaml in the BYO VPC deployment.
func setZonesBYOVPC(in *zoneConfigInput) error {
privateSubnets, err := in.Config.AWS.PrivateSubnets(context.TODO())
privateSubnets, err := in.Meta.PrivateSubnets(context.TODO())
if err != nil {
return errors.Wrap(err, "failed to get private subnets")
}
Expand All @@ -48,7 +50,7 @@ func setZonesBYOVPC(in *zoneConfigInput) error {
})
}

publicSubnets, err := in.Config.AWS.PublicSubnets(context.TODO())
publicSubnets, err := in.Meta.PublicSubnets(context.TODO())
if err != nil {
return errors.Wrap(err, "failed to get public subnets")
}
Expand All @@ -61,7 +63,7 @@ func setZonesBYOVPC(in *zoneConfigInput) error {
})
}

vpc, err := in.Config.AWS.VPC(context.TODO())
vpc, err := in.Meta.VPC(context.TODO())
if err != nil {
return errors.Wrap(err, "failed to get VPC")
}
Expand All @@ -81,11 +83,11 @@ func setZonesManagedVPC(in *zoneConfigInput) error {
return errors.Wrap(err, "failed to get availability zones")
}

mainCIDR := capiutils.CIDRFromInstallConfig(in.Config)
mainCIDR := capiutils.CIDRFromInstallConfig(in.InstallConfig)

// Fallback to available zones in the region.
if len(zones) == 0 {
// Q? Do we need to leave CAPA choose it automatically?
// Q? Do we need to use standard query or leave CAPA choose the zones automatically?
// zonesMeta, err := in.Config.AWS.AvailabilityZones(context.TODO())
// if err != nil {
// return errors.Wrap(err, "failed to get availability zones")
Expand All @@ -112,7 +114,7 @@ func setZonesManagedVPC(in *zoneConfigInput) error {
numSubnets := len(zones) + 1

// Public subnets consumes one range from base blocks.
isPublishingExternal := in.Config.Config.Publish == types.ExternalPublishingStrategy
isPublishingExternal := in.Config.Publish == types.ExternalPublishingStrategy
if isPublishingExternal {
numSubnets += 1
}
Expand Down Expand Up @@ -166,21 +168,26 @@ func extractZonesFromInstallConfig(in *zoneConfigInput) ([]*aws.Zone, error) {
var zones []*aws.Zone
zonesMap := make(map[string]struct{})

cfg := in.Config.Config
if len(cfg.ControlPlane.Platform.AWS.Zones) > 0 {
if in.Config == nil {
return nil, errors.New("unable to retreive Config")
}

cfg := in.Config
if cfg.ControlPlane != nil && cfg.ControlPlane.Platform.AWS != nil &&
len(cfg.ControlPlane.Platform.AWS.Zones) > 0 {
for _, zone := range cfg.ControlPlane.Platform.AWS.Zones {
if _, ok := zonesMap[zone]; !ok {
zonesMap[zone] = struct{}{}
zones = append(zones, &aws.Zone{Name: zone})
}
}
}

for _, compute := range cfg.Compute {
if compute.Platform.AWS == nil {
continue
}
if len(compute.Platform.AWS.Zones) > 0 {
for _, zone := range compute.Platform.AWS.Zones {
if _, ok := zonesMap[zone]; !ok {
zonesMap[zone] = struct{}{}
zones = append(zones, &aws.Zone{Name: zone})
}
}
Expand Down
221 changes: 221 additions & 0 deletions pkg/asset/manifests/aws/zones_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
package aws

import (
"reflect"
"testing"

"github.com/davecgh/go-spew/spew"
"github.com/openshift/installer/pkg/asset/installconfig/aws"
"github.com/openshift/installer/pkg/ipnet"
"github.com/openshift/installer/pkg/types"
awstypes "github.com/openshift/installer/pkg/types/aws"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var (
stubDefaultCIDR = "10.0.0.0/16"
stubInstallConfigZones = &types.InstallConfig{
TypeMeta: metav1.TypeMeta{
APIVersion: types.InstallConfigVersion,
},
ObjectMeta: metav1.ObjectMeta{
Name: "test-cluster",
},
Networking: &types.Networking{
MachineNetwork: []types.MachineNetworkEntry{
{
CIDR: *ipnet.MustParseCIDR(stubDefaultCIDR),
},
},
},
}
stubInstallConfigCustomZonesPoolControl = &types.MachinePool{
Name: "master",
Platform: types.MachinePoolPlatform{
AWS: &awstypes.MachinePool{
Zones: []string{"a", "b"},
},
},
}
stubInstallConfigCustomZonesPoolCompute = []types.MachinePool{
{
Name: "worker",
Platform: types.MachinePoolPlatform{
AWS: &awstypes.MachinePool{
Zones: []string{"b", "c"},
},
},
},
}
)

func Test_extractZonesFromInstallConfig(t *testing.T) {
type args struct {
in *zoneConfigInput
}
tests := []struct {
name string
args args
want []*aws.Zone
wantErr bool
}{
{
name: "empty install config",
args: args{
in: &zoneConfigInput{
Config: nil,
},
},
wantErr: true,
},
{
name: "default zones",
args: args{
in: &zoneConfigInput{
Config: stubInstallConfigZones,
},
},
want: nil,
},
{
name: "custom zones control plane pool",
args: args{
in: &zoneConfigInput{
Config: func() *types.InstallConfig {
config := types.InstallConfig{
ControlPlane: stubInstallConfigCustomZonesPoolControl,
Compute: nil,
}
return &config
}(),
},
},
want: []*aws.Zone{{Name: "a"}, {Name: "b"}},
},
{
name: "custom zones compute pool",
args: args{
in: &zoneConfigInput{
Config: func() *types.InstallConfig {
config := types.InstallConfig{
ControlPlane: nil,
Compute: stubInstallConfigCustomZonesPoolCompute,
}
return &config
}(),
},
},
want: []*aws.Zone{{Name: "b"}, {Name: "c"}},
},
{
name: "custom zones control plane and compute pools",
args: args{
in: &zoneConfigInput{
Config: func() *types.InstallConfig {
c := &types.InstallConfig{}
c.ControlPlane = stubInstallConfigCustomZonesPoolControl
c.Compute = stubInstallConfigCustomZonesPoolCompute
return c
}(),
},
},
want: []*aws.Zone{{Name: "a"}, {Name: "b"}, {Name: "c"}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := extractZonesFromInstallConfig(tt.args.in)
if (err != nil) != tt.wantErr {
t.Errorf("extractZonesFromInstallConfig() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
spew.Printf("Got: %v\n", got)
t.Errorf("extractZonesFromInstallConfig() = %v, want %v", got, tt.want)
}
})
}
}

// var stubAwsCluster = &capa.AWSCluster{
// ObjectMeta: metav1.ObjectMeta{
// Name: "infraId",
// Namespace: capiutils.Namespace,
// },
// Spec: capa.AWSClusterSpec{},
// }

// func getStubInstallConfig() *installconfig.InstallConfig {
// ic := &installconfig.InstallConfig{}
// ic.Config = stubInstallConfigZones
// ic.Config.ControlPlane = stubInstallConfigCustomZonesPoolControl
// ic.Config.Compute = stubInstallConfigCustomZonesPoolCompute
// return ic
// }

// func Test_setZonesManagedVPC(t *testing.T) {
// type args struct {
// in *zoneConfigInput
// }
// tests := []struct {
// name string
// args args
// wantErr bool
// want *capa.AWSCluster
// }{
// // TODO: Add test cases.
// {
// name: "empty clusterx",
// args: args{
// in: &zoneConfigInput{
// ClusterID: func() *installconfig.ClusterID {
// return &installconfig.ClusterID{
// InfraID: "infra-id",
// }
// }(),
// Config: func() *types.InstallConfig {
// c := stubInstallConfigZones
// c.ControlPlane = stubInstallConfigCustomZonesPoolControl
// c.Compute = nil
// return c
// }(),
// Cluster: stubAwsCluster,
// InstallConfig: func() *installconfig.InstallConfig {
// ic := &installconfig.InstallConfig{}
// ic.Config = stubInstallConfigZones
// ic.Config.ControlPlane = stubInstallConfigCustomZonesPoolControl
// ic.Config.Compute = nil
// return ic
// }(),
// },
// },
// want: func() *capa.AWSCluster {
// c := stubAwsCluster
// c.Spec.NetworkSpec.VPC = capa.VPCSpec{CidrBlock: stubDefaultCIDR}
// c.Spec.NetworkSpec.Subnets = append(c.Spec.NetworkSpec.Subnets, capa.SubnetSpec{
// ID: "infra-id-subnet-a",
// AvailabilityZone: "a",
// IsPublic: true,
// CidrBlock: "10.0.0.0/18x",
// })
// return nil
// }(),
// },
// }
// for _, tt := range tests {
// t.Run(tt.name, func(t *testing.T) {
// // if err := setZonesManagedVPC(tt.args.in); (err != nil) != tt.wantErr {
// // t.Errorf("setZonesManagedVPC() error = %v, wantErr %v", err, tt.wantErr)
// // }
// var got *capa.AWSCluster
// if tt.args.in.Cluster != nil {
// got = tt.args.in.Cluster
// }
// spew.Printf("\nWant: %v, \nGot: %v\n", tt.want, got)
// if !reflect.DeepEqual(got, tt.want) {
// // spew.Printf("\nGot: %v\n", got)
// t.Errorf("setZonesManagedVPC() = %v, want %v", got, tt.want)
// }
// })
// }
// }

0 comments on commit b6522ab

Please sign in to comment.