github.com/openshift/installer@v1.4.17/pkg/asset/machines/gcp/gcpmachines_test.go (about)

     1  // Package gcp generates Machine objects for gcp.
     2  package gcp
     3  
     4  import (
     5  	"fmt"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	compute "google.golang.org/api/compute/v1"
    10  	v1 "k8s.io/api/core/v1"
    11  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    12  	"k8s.io/utils/ptr"
    13  	capg "sigs.k8s.io/cluster-api-provider-gcp/api/v1beta1"
    14  	capi "sigs.k8s.io/cluster-api/api/v1beta1"
    15  
    16  	"github.com/openshift/installer/pkg/asset/installconfig"
    17  	gcpconsts "github.com/openshift/installer/pkg/constants/gcp"
    18  	"github.com/openshift/installer/pkg/types"
    19  	gcptypes "github.com/openshift/installer/pkg/types/gcp"
    20  )
    21  
    22  const (
    23  	numReplicas = 3
    24  )
    25  
    26  func Test_GenerateMachines(t *testing.T) {
    27  	cases := []struct {
    28  		name              string
    29  		installConfig     *installconfig.InstallConfig
    30  		expectedGCPConfig *capg.GCPMachine
    31  		expectedError     string
    32  	}{
    33  		{
    34  			name:              "base configuration",
    35  			installConfig:     getBaseInstallConfig(),
    36  			expectedGCPConfig: getBaseGCPMachine(),
    37  		},
    38  		{
    39  			name:              "additional labels",
    40  			installConfig:     getICWithLabels(),
    41  			expectedGCPConfig: getGCPMachineWithLabels(),
    42  		},
    43  		{
    44  			name:              "onhostmaintenance",
    45  			installConfig:     getICWithOnHostMaintenance(),
    46  			expectedGCPConfig: getGCPMachineWithOnHostMaintenance(),
    47  		},
    48  		{
    49  			name:              "confidentialcompute",
    50  			installConfig:     getICWithConfidentialCompute(),
    51  			expectedGCPConfig: getGCPMachineWithConfidentialCompute(),
    52  		},
    53  		{
    54  			name:              "secureboot",
    55  			installConfig:     getICWithSecureBoot(),
    56  			expectedGCPConfig: getGCPMachineWithSecureBoot(),
    57  		},
    58  		{
    59  			name:              "serviceaccount",
    60  			installConfig:     getICWithServiceAccount(),
    61  			expectedGCPConfig: getGCPMachineWithServiceAccount(),
    62  		},
    63  		{
    64  			name:              "serviceaccount-controlplane-machine",
    65  			installConfig:     getICWithServiceAccountControlPlaneMachine(),
    66  			expectedGCPConfig: getGCPMachineWithServiceAccountControlPlaneMachine(),
    67  		},
    68  		{
    69  			name:              "usertags",
    70  			installConfig:     getICWithUserTags(),
    71  			expectedGCPConfig: getGCPMachineWithTags(),
    72  		},
    73  	}
    74  	for _, tc := range cases {
    75  		t.Run(tc.name, func(t *testing.T) {
    76  			installConfig := tc.installConfig
    77  			ic := installConfig.Config
    78  			pool := ic.ControlPlane
    79  			infraID := "012345678"
    80  			rhcosImage := "rhcos-415-92-202311241643-0-gcp-x86-64"
    81  
    82  			mpool := gcptypes.MachinePool{
    83  				InstanceType: "n2-standard-4",
    84  				OSDisk: gcptypes.OSDisk{
    85  					DiskSizeGB: 128,
    86  					DiskType:   "pd-ssd",
    87  				},
    88  			}
    89  			mpool.Set(ic.Platform.GCP.DefaultMachinePlatform)
    90  			mpool.Set(pool.Platform.GCP)
    91  			pool.Platform.GCP = &mpool
    92  
    93  			gcpMachines, err := GenerateMachines(
    94  				installConfig,
    95  				infraID,
    96  				pool,
    97  				rhcosImage,
    98  			)
    99  
   100  			if tc.expectedError != "" {
   101  				assert.Equal(t, tc.expectedError, err.Error())
   102  			} else {
   103  				assert.NoError(t, err)
   104  				assert.NotEmpty(t, gcpMachines)
   105  
   106  				assert.Equal(t, numReplicas*2, len(gcpMachines))
   107  				// Check first set of GCP and CAPI machines
   108  				actualGCPMachine := gcpMachines[0].Object
   109  				actualCapiMachine := gcpMachines[1].Object
   110  				assert.Equal(t, tc.expectedGCPConfig, actualGCPMachine)
   111  				assert.Equal(t, getBaseCapiMachine(), actualCapiMachine)
   112  			}
   113  		})
   114  	}
   115  }
   116  
   117  func getBaseInstallConfig() *installconfig.InstallConfig {
   118  	return &installconfig.InstallConfig{
   119  		AssetBase: installconfig.AssetBase{
   120  			Config: &types.InstallConfig{
   121  				ObjectMeta: metav1.ObjectMeta{
   122  					Name:      "ocp-edge-cluster-0",
   123  					Namespace: "cluster-0",
   124  				},
   125  				BaseDomain: "testing.com",
   126  				ControlPlane: &types.MachinePool{
   127  					Name:     "master",
   128  					Replicas: ptr.To(int64(numReplicas)),
   129  					Platform: types.MachinePoolPlatform{},
   130  				},
   131  				Platform: types.Platform{
   132  					GCP: &gcptypes.Platform{
   133  						ProjectID: "my-project",
   134  						Region:    "us-east1",
   135  					},
   136  				},
   137  			},
   138  		},
   139  	}
   140  }
   141  
   142  func getICWithLabels() *installconfig.InstallConfig {
   143  	ic := getBaseInstallConfig()
   144  	ic.Config.Platform.GCP.UserLabels = []gcptypes.UserLabel{{Key: "foo", Value: "bar"},
   145  		{Key: "id", Value: "1234"}}
   146  	return ic
   147  }
   148  
   149  func getICWithOnHostMaintenance() *installconfig.InstallConfig {
   150  	ic := getBaseInstallConfig()
   151  	ic.Config.Platform.GCP.DefaultMachinePlatform = &gcptypes.MachinePool{OnHostMaintenance: "Terminate"}
   152  	return ic
   153  }
   154  
   155  func getICWithConfidentialCompute() *installconfig.InstallConfig {
   156  	ic := getBaseInstallConfig()
   157  	ic.Config.Platform.GCP.DefaultMachinePlatform = &gcptypes.MachinePool{ConfidentialCompute: "Enabled"}
   158  	return ic
   159  }
   160  
   161  func getICWithSecureBoot() *installconfig.InstallConfig {
   162  	ic := getBaseInstallConfig()
   163  	ic.Config.Platform.GCP.DefaultMachinePlatform = &gcptypes.MachinePool{SecureBoot: "Enabled"}
   164  	return ic
   165  }
   166  
   167  func getICWithServiceAccount() *installconfig.InstallConfig {
   168  	ic := getBaseInstallConfig()
   169  	ic.Config.Platform.GCP.DefaultMachinePlatform = &gcptypes.MachinePool{ServiceAccount: "user-service-account@some-project.iam.gserviceaccount.com"}
   170  	return ic
   171  }
   172  
   173  func getICWithServiceAccountControlPlaneMachine() *installconfig.InstallConfig {
   174  	ic := getBaseInstallConfig()
   175  	ic.Config.Platform.GCP.DefaultMachinePlatform = &gcptypes.MachinePool{ServiceAccount: "user-service-account@some-project.iam.gserviceaccount.com"}
   176  	ic.Config.ControlPlane = &types.MachinePool{
   177  		Name:     "master",
   178  		Replicas: ptr.To(int64(numReplicas)),
   179  		Platform: types.MachinePoolPlatform{
   180  			GCP: &gcptypes.MachinePool{
   181  				ServiceAccount: "other-service-account@some-project.iam.gserviceaccount.com"},
   182  		},
   183  	}
   184  	return ic
   185  }
   186  
   187  func getICWithUserTags() *installconfig.InstallConfig {
   188  	ic := getBaseInstallConfig()
   189  	ic.Config.Platform.GCP.UserTags = []gcptypes.UserTag{{ParentID: "my-project", Key: "foo", Value: "bar"},
   190  		{ParentID: "other-project", Key: "id", Value: "1234"}}
   191  	return ic
   192  }
   193  
   194  func getBaseGCPMachine() *capg.GCPMachine {
   195  	subnet := "012345678-master-subnet"
   196  	image := "rhcos-415-92-202311241643-0-gcp-x86-64"
   197  	diskType := "pd-ssd"
   198  	gcpMachine := &capg.GCPMachine{
   199  		ObjectMeta: metav1.ObjectMeta{
   200  			Name: "012345678-master-0",
   201  			Labels: map[string]string{
   202  				"cluster.x-k8s.io/control-plane": "",
   203  			},
   204  		},
   205  		Spec: capg.GCPMachineSpec{
   206  			InstanceType: "n2-standard-4",
   207  			Subnet:       &subnet,
   208  			Image:        &image,
   209  			AdditionalLabels: capg.Labels{
   210  				fmt.Sprintf(gcpconsts.ClusterIDLabelFmt, "012345678"): "owned",
   211  			},
   212  			RootDeviceSize: 128,
   213  			RootDeviceType: ptr.To(capg.DiskType(diskType)),
   214  			ServiceAccount: &capg.ServiceAccount{
   215  				Email:  "012345678-m@my-project.iam.gserviceaccount.com",
   216  				Scopes: []string{compute.CloudPlatformScope},
   217  			},
   218  			ResourceManagerTags: []capg.ResourceManagerTag{},
   219  		},
   220  	}
   221  	gcpMachine.SetGroupVersionKind(capg.GroupVersion.WithKind("GCPMachine"))
   222  	return gcpMachine
   223  }
   224  
   225  func getGCPMachineWithLabels() *capg.GCPMachine {
   226  	gcpMachine := getBaseGCPMachine()
   227  	gcpMachine.Spec.AdditionalLabels = capg.Labels{
   228  		fmt.Sprintf(gcpconsts.ClusterIDLabelFmt, "012345678"): "owned",
   229  		"foo": "bar",
   230  		"id":  "1234"}
   231  	return gcpMachine
   232  }
   233  
   234  func getGCPMachineWithOnHostMaintenance() *capg.GCPMachine {
   235  	gcpMachine := getBaseGCPMachine()
   236  	var maint capg.HostMaintenancePolicy = "Terminate"
   237  	gcpMachine.Spec.OnHostMaintenance = &maint
   238  	return gcpMachine
   239  }
   240  
   241  func getGCPMachineWithConfidentialCompute() *capg.GCPMachine {
   242  	gcpMachine := getBaseGCPMachine()
   243  	var cc capg.ConfidentialComputePolicy = "Enabled"
   244  	gcpMachine.Spec.ConfidentialCompute = &cc
   245  	return gcpMachine
   246  }
   247  
   248  func getGCPMachineWithSecureBoot() *capg.GCPMachine {
   249  	gcpMachine := getBaseGCPMachine()
   250  	secureBoot := capg.GCPShieldedInstanceConfig{SecureBoot: capg.SecureBootPolicy("Enabled")}
   251  	gcpMachine.Spec.ShieldedInstanceConfig = &secureBoot
   252  	return gcpMachine
   253  }
   254  
   255  func getGCPMachineWithServiceAccount() *capg.GCPMachine {
   256  	gcpMachine := getBaseGCPMachine()
   257  	gcpMachine.Spec.ServiceAccount = &capg.ServiceAccount{
   258  		Email:  "user-service-account@some-project.iam.gserviceaccount.com",
   259  		Scopes: []string{compute.CloudPlatformScope},
   260  	}
   261  	return gcpMachine
   262  }
   263  
   264  func getGCPMachineWithServiceAccountControlPlaneMachine() *capg.GCPMachine {
   265  	gcpMachine := getBaseGCPMachine()
   266  	gcpMachine.Spec.ServiceAccount = &capg.ServiceAccount{
   267  		Email:  "other-service-account@some-project.iam.gserviceaccount.com",
   268  		Scopes: []string{compute.CloudPlatformScope},
   269  	}
   270  	return gcpMachine
   271  }
   272  
   273  func getBaseCapiMachine() *capi.Machine {
   274  	dataSecret := fmt.Sprintf("%s-master", "012345678")
   275  
   276  	capiMachine := &capi.Machine{
   277  		ObjectMeta: metav1.ObjectMeta{
   278  			Name: "012345678-master-0",
   279  			Labels: map[string]string{
   280  				"cluster.x-k8s.io/control-plane": "",
   281  			},
   282  		},
   283  		Spec: capi.MachineSpec{
   284  			ClusterName: "012345678",
   285  			Bootstrap: capi.Bootstrap{
   286  				DataSecretName: ptr.To(dataSecret),
   287  			},
   288  			InfrastructureRef: v1.ObjectReference{
   289  				APIVersion: capg.GroupVersion.String(),
   290  				Kind:       "GCPMachine",
   291  				Name:       "012345678-master-0",
   292  			},
   293  		},
   294  	}
   295  	capiMachine.SetGroupVersionKind(capi.GroupVersion.WithKind("Machine"))
   296  	return capiMachine
   297  }
   298  
   299  func getGCPMachineWithTags() *capg.GCPMachine {
   300  	gcpMachine := getBaseGCPMachine()
   301  	gcpMachine.Spec.ResourceManagerTags = []capg.ResourceManagerTag{
   302  		{ParentID: "my-project",
   303  			Key:   "foo",
   304  			Value: "bar"},
   305  		{ParentID: "other-project",
   306  			Key:   "id",
   307  			Value: "1234"}}
   308  	return gcpMachine
   309  }