github.com/openshift/installer@v1.4.17/pkg/asset/manifests/ingress_test.go (about)

     1  package manifests
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"sigs.k8s.io/yaml"
     9  
    10  	configv1 "github.com/openshift/api/config/v1"
    11  	"github.com/openshift/installer/pkg/asset"
    12  	"github.com/openshift/installer/pkg/asset/installconfig"
    13  	"github.com/openshift/installer/pkg/types"
    14  )
    15  
    16  // installConfigFromTopologies generates an install config that would yield the
    17  // given topologies when determineTopologies is called on it
    18  func installConfigFromTopologies(t *testing.T, options []icOption,
    19  	controlPlaneTopology configv1.TopologyMode, infrastructureTopology configv1.TopologyMode) *types.InstallConfig {
    20  	installConfig := icBuild.build(options...)
    21  
    22  	highlyAvailable := int64(3)
    23  	singleReplica := int64(1)
    24  
    25  	switch controlPlaneTopology {
    26  	case configv1.HighlyAvailableTopologyMode:
    27  		installConfig.ControlPlane = &types.MachinePool{
    28  			Replicas: &highlyAvailable,
    29  		}
    30  	case configv1.SingleReplicaTopologyMode:
    31  		installConfig.ControlPlane = &types.MachinePool{
    32  			Replicas: &singleReplica,
    33  		}
    34  	}
    35  
    36  	switch infrastructureTopology {
    37  	case configv1.HighlyAvailableTopologyMode:
    38  		installConfig.Compute = []types.MachinePool{
    39  			{Replicas: &highlyAvailable},
    40  		}
    41  	case configv1.SingleReplicaTopologyMode:
    42  		installConfig.Compute = []types.MachinePool{
    43  			{Replicas: &singleReplica},
    44  		}
    45  	}
    46  
    47  	// Assert that this function actually works
    48  	generatedControlPlaneTopology, generatedInfrastructureTopology := determineTopologies(installConfig)
    49  	assert.Equal(t, generatedControlPlaneTopology, controlPlaneTopology)
    50  	assert.Equal(t, generatedInfrastructureTopology, infrastructureTopology)
    51  
    52  	return installConfig
    53  }
    54  
    55  func TestGenerateIngerssDefaultPlacement(t *testing.T) {
    56  	cases := []struct {
    57  		name                        string
    58  		installConfigBuildOptions   []icOption
    59  		controlPlaneTopology        configv1.TopologyMode
    60  		infrastructureTopology      configv1.TopologyMode
    61  		expectedIngressPlacement    configv1.DefaultPlacement
    62  		expectedIngressAWSLBType    configv1.AWSLBType
    63  		expectedIngressPlatformType configv1.PlatformType
    64  	}{
    65  		{
    66  			// AWS currently uses a load balancer even on single-node, so the
    67  			// default placement should be workers
    68  			name:                      "aws single node with 0 or 1 day-1 workers",
    69  			installConfigBuildOptions: []icOption{icBuild.forAWS()},
    70  			controlPlaneTopology:      configv1.SingleReplicaTopologyMode,
    71  			infrastructureTopology:    configv1.SingleReplicaTopologyMode,
    72  			expectedIngressAWSLBType:  configv1.Classic,
    73  			expectedIngressPlacement:  configv1.DefaultPlacementWorkers,
    74  		},
    75  		{
    76  			name:                      "aws multi-node with 1 day-1 worker",
    77  			installConfigBuildOptions: []icOption{icBuild.forAWS()},
    78  			controlPlaneTopology:      configv1.HighlyAvailableTopologyMode,
    79  			infrastructureTopology:    configv1.SingleReplicaTopologyMode,
    80  			expectedIngressAWSLBType:  configv1.Classic,
    81  			expectedIngressPlacement:  configv1.DefaultPlacementWorkers,
    82  		},
    83  		{
    84  			// AWS currently uses a load balancer even on single-node, so the
    85  			// default placement should be workers
    86  			name:                      "aws single-node with multiple day-1 workers",
    87  			installConfigBuildOptions: []icOption{icBuild.forAWS()},
    88  			controlPlaneTopology:      configv1.SingleReplicaTopologyMode,
    89  			infrastructureTopology:    configv1.HighlyAvailableTopologyMode,
    90  			expectedIngressAWSLBType:  configv1.Classic,
    91  			expectedIngressPlacement:  configv1.DefaultPlacementWorkers,
    92  		},
    93  		{
    94  			name:                      "vanilla aws",
    95  			installConfigBuildOptions: []icOption{icBuild.forAWS()},
    96  			controlPlaneTopology:      configv1.HighlyAvailableTopologyMode,
    97  			infrastructureTopology:    configv1.HighlyAvailableTopologyMode,
    98  			expectedIngressAWSLBType:  configv1.Classic,
    99  			expectedIngressPlacement:  configv1.DefaultPlacementWorkers,
   100  		},
   101  		{
   102  			name:                        "test setting of aws lb type to NLB",
   103  			installConfigBuildOptions:   []icOption{icBuild.withLBType(configv1.NLB)},
   104  			controlPlaneTopology:        configv1.HighlyAvailableTopologyMode,
   105  			infrastructureTopology:      configv1.HighlyAvailableTopologyMode,
   106  			expectedIngressPlacement:    configv1.DefaultPlacementWorkers,
   107  			expectedIngressAWSLBType:    configv1.NLB,
   108  			expectedIngressPlatformType: configv1.AWSPlatformType,
   109  		},
   110  		{
   111  			name:                        "test setting of aws lb type to Classic",
   112  			installConfigBuildOptions:   []icOption{icBuild.withLBType(configv1.Classic)},
   113  			controlPlaneTopology:        configv1.HighlyAvailableTopologyMode,
   114  			infrastructureTopology:      configv1.HighlyAvailableTopologyMode,
   115  			expectedIngressPlacement:    configv1.DefaultPlacementWorkers,
   116  			expectedIngressAWSLBType:    configv1.Classic,
   117  			expectedIngressPlatformType: configv1.AWSPlatformType,
   118  		},
   119  		{
   120  			name:                      "none-platform single node with 0 or 1 day-1 workers",
   121  			installConfigBuildOptions: []icOption{icBuild.forNone()},
   122  			controlPlaneTopology:      configv1.SingleReplicaTopologyMode,
   123  			infrastructureTopology:    configv1.SingleReplicaTopologyMode,
   124  			expectedIngressPlacement:  configv1.DefaultPlacementControlPlane,
   125  		},
   126  		{
   127  			name:                      "none-platform multi-node with 1 day-1 worker",
   128  			installConfigBuildOptions: []icOption{icBuild.forNone()},
   129  			controlPlaneTopology:      configv1.HighlyAvailableTopologyMode,
   130  			infrastructureTopology:    configv1.SingleReplicaTopologyMode,
   131  			expectedIngressPlacement:  configv1.DefaultPlacementWorkers,
   132  		},
   133  		{
   134  			// For the sake of consistency, we want ingress traffic to go
   135  			// through the single control plane node even when there are
   136  			// workers on day 1. This is even though it would make sense
   137  			// for the user to want to set up a day-1 load balancer in this
   138  			// situation for highly available ingress.
   139  			name:                      "none-platform single-node with multiple day-1 workers",
   140  			installConfigBuildOptions: []icOption{icBuild.forNone()},
   141  			controlPlaneTopology:      configv1.SingleReplicaTopologyMode,
   142  			infrastructureTopology:    configv1.HighlyAvailableTopologyMode,
   143  			expectedIngressPlacement:  configv1.DefaultPlacementControlPlane,
   144  		},
   145  		{
   146  			name:                      "vanilla none-platform",
   147  			installConfigBuildOptions: []icOption{icBuild.forNone()},
   148  			controlPlaneTopology:      configv1.HighlyAvailableTopologyMode,
   149  			infrastructureTopology:    configv1.HighlyAvailableTopologyMode,
   150  			expectedIngressPlacement:  configv1.DefaultPlacementWorkers,
   151  		},
   152  	}
   153  
   154  	for _, tc := range cases {
   155  		t.Run(tc.name, func(t *testing.T) {
   156  			parents := asset.Parents{}
   157  			parents.Add(
   158  				&installconfig.ClusterID{
   159  					UUID:    "test-uuid",
   160  					InfraID: "test-infra-id",
   161  				},
   162  				installconfig.MakeAsset(
   163  					installConfigFromTopologies(t, tc.installConfigBuildOptions,
   164  						tc.controlPlaneTopology, tc.infrastructureTopology),
   165  				),
   166  			)
   167  			ingressAsset := &Ingress{}
   168  			err := ingressAsset.Generate(context.Background(), parents)
   169  			if !assert.NoError(t, err, "failed to generate asset") {
   170  				return
   171  			}
   172  			if !assert.Len(t, ingressAsset.FileList, 1, "expected only one file to be generated") {
   173  				return
   174  			}
   175  			assert.Equal(t, ingressAsset.FileList[0].Filename, "manifests/cluster-ingress-02-config.yml")
   176  			var actualIngress configv1.Ingress
   177  			err = yaml.Unmarshal(ingressAsset.FileList[0].Data, &actualIngress)
   178  			if !assert.NoError(t, err, "failed to unmarshal infra manifest") {
   179  				return
   180  			}
   181  			assert.Equal(t, tc.expectedIngressPlacement, actualIngress.Status.DefaultPlacement)
   182  			if len(tc.expectedIngressPlatformType) != 0 {
   183  				assert.Equal(t, tc.expectedIngressAWSLBType, actualIngress.Spec.LoadBalancer.Platform.AWS.Type)
   184  				assert.Equal(t, tc.expectedIngressPlatformType, actualIngress.Spec.LoadBalancer.Platform.Type)
   185  			}
   186  		})
   187  	}
   188  }