diff --git a/pkg/providers/tinkerbell/hardware/config.go b/pkg/providers/tinkerbell/hardware/catalogue.go similarity index 74% rename from pkg/providers/tinkerbell/hardware/config.go rename to pkg/providers/tinkerbell/hardware/catalogue.go index e84184d2eb08..810f0d766b97 100644 --- a/pkg/providers/tinkerbell/hardware/config.go +++ b/pkg/providers/tinkerbell/hardware/catalogue.go @@ -1,10 +1,12 @@ package hardware import ( + "bufio" "encoding/json" + "errors" "fmt" - "io/ioutil" - "strings" + "io" + "os" pbnjv1alpha1 "github.com/tinkerbell/cluster-api-provider-tinkerbell/pbnj/api/v1alpha1" tinkv1alpha1 "github.com/tinkerbell/cluster-api-provider-tinkerbell/tink/api/v1alpha1" @@ -14,6 +16,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" apimachineryvalidation "k8s.io/apimachinery/pkg/util/validation" + yamlutil "k8s.io/apimachinery/pkg/util/yaml" "sigs.k8s.io/yaml" "github.com/aws/eks-anywhere/pkg/api/v1alpha1" @@ -25,69 +28,22 @@ import ( const Provisioning = "provisioning" -type HardwareConfig struct { - Hardwares []tinkv1alpha1.Hardware - Bmcs []pbnjv1alpha1.BMC - Secrets []corev1.Secret +type Catalogue struct { + Hardware []tinkv1alpha1.Hardware + BMCs []pbnjv1alpha1.BMC + Secrets []corev1.Secret } -func (hc *HardwareConfig) ParseHardwareConfig(hardwareFileName string) error { - err := hc.setHardwareConfigFromFile(hardwareFileName) - if err != nil { - return fmt.Errorf("unable to parse hardware file %s: %v", hardwareFileName, err) - } - return nil -} - -func (hc *HardwareConfig) setHardwareConfigFromFile(hardwareFileName string) error { - content, err := ioutil.ReadFile(hardwareFileName) - if err != nil { - return fmt.Errorf("unable to read file due to: %v", err) - } - - for _, c := range strings.Split(string(content), v1alpha1.YamlSeparator) { - var resource unstructured.Unstructured - if err = yaml.Unmarshal([]byte(c), &resource); err != nil { - return fmt.Errorf("unable to parse %s\nyaml: %s\n %v", hardwareFileName, c, err) - } - switch resource.GetKind() { - case "Hardware": - var hardware tinkv1alpha1.Hardware - err = yaml.UnmarshalStrict([]byte(c), &hardware) - if err != nil { - return fmt.Errorf("unable to parse hardware CRD\n%s \n%v", c, err) - } - hc.Hardwares = append(hc.Hardwares, hardware) - case "BMC": - var bmc pbnjv1alpha1.BMC - err = yaml.UnmarshalStrict([]byte(c), &bmc) - if err != nil { - return fmt.Errorf("unable to parse bmc CRD\n%s \n%v", c, err) - } - hc.Bmcs = append(hc.Bmcs, bmc) - case "Secret": - var secret corev1.Secret - err = yaml.UnmarshalStrict([]byte(c), &secret) - if err != nil { - return fmt.Errorf("unable to parse k8s secret\n%s \n%v", c, err) - } - hc.Secrets = append(hc.Secrets, secret) - } - } - - return nil -} - -func (hc *HardwareConfig) ValidateHardware(skipPowerActions, force bool, tinkHardwareMap map[string]*tinkhardware.Hardware, tinkWorkflowMap map[string]*tinkworkflow.Workflow) error { +func (c *Catalogue) ValidateHardware(skipPowerActions, force bool, tinkHardwareMap map[string]*tinkhardware.Hardware, tinkWorkflowMap map[string]*tinkworkflow.Workflow) error { bmcRefMap := map[string]*tinkv1alpha1.Hardware{} if !skipPowerActions { - bmcRefMap = hc.initBmcRefMap() + bmcRefMap = c.initBmcRefMap() } // A database of observed hardware IDs so we can check for uniqueness. - hardwareIdsDb := make(map[string]struct{}, len(hc.Hardwares)) + hardwareIdsDb := make(map[string]struct{}, len(c.Hardware)) - for _, hw := range hc.Hardwares { + for _, hw := range c.Hardware { if hw.Name == "" { return fmt.Errorf("hardware name is required") } @@ -160,10 +116,10 @@ func (hc *HardwareConfig) ValidateHardware(skipPowerActions, force bool, tinkHar return nil } -func (hc *HardwareConfig) ValidateBMC() error { - secretRefMap := hc.initSecretRefMap() - bmcIpMap := make(map[string]struct{}, len(hc.Bmcs)) - for _, bmc := range hc.Bmcs { +func (c *Catalogue) ValidateBMC() error { + secretRefMap := c.initSecretRefMap() + bmcIpMap := make(map[string]struct{}, len(c.BMCs)) + for _, bmc := range c.BMCs { if bmc.Name == "" { return fmt.Errorf("bmc name is required") } @@ -198,8 +154,8 @@ func (hc *HardwareConfig) ValidateBMC() error { return nil } -func (hc *HardwareConfig) ValidateBmcSecretRefs() error { - for _, s := range hc.Secrets { +func (c *Catalogue) ValidateBmcSecretRefs() error { + for _, s := range c.Secrets { if s.Name == "" { return fmt.Errorf("secret name is required") } @@ -228,34 +184,34 @@ func (hc *HardwareConfig) ValidateBmcSecretRefs() error { return nil } -func (hc *HardwareConfig) initBmcRefMap() map[string]*tinkv1alpha1.Hardware { - bmcRefMap := make(map[string]*tinkv1alpha1.Hardware, len(hc.Bmcs)) - for _, bmc := range hc.Bmcs { +func (c *Catalogue) initBmcRefMap() map[string]*tinkv1alpha1.Hardware { + bmcRefMap := make(map[string]*tinkv1alpha1.Hardware, len(c.BMCs)) + for _, bmc := range c.BMCs { bmcRefMap[bmc.Name] = nil } return bmcRefMap } -func (hc *HardwareConfig) initSecretRefMap() map[string]struct{} { - secretRefMap := make(map[string]struct{}, len(hc.Secrets)) - for _, s := range hc.Secrets { +func (c *Catalogue) initSecretRefMap() map[string]struct{} { + secretRefMap := make(map[string]struct{}, len(c.Secrets)) + for _, s := range c.Secrets { secretRefMap[s.Name] = struct{}{} } return secretRefMap } -func (hc *HardwareConfig) HardwareSpecMarshallable() ([]byte, error) { +func (c *Catalogue) HardwareSpecMarshallable() ([]byte, error) { var marshallables []v1alpha1.Marshallable - for _, hw := range hc.Hardwares { + for _, hw := range c.Hardware { marshallables = append(marshallables, hardwareMarshallable(hw)) } - for _, bmc := range hc.Bmcs { + for _, bmc := range c.BMCs { marshallables = append(marshallables, bmcMarshallable(bmc)) } - for _, secret := range hc.Secrets { + for _, secret := range c.Secrets { marshallables = append(marshallables, secretsMarshallable(secret)) } @@ -314,3 +270,57 @@ func secretsMarshallable(secret corev1.Secret) *corev1.Secret { return config } + +// ParseYAMLCatalogueFromFile parses filename, a YAML document, using ParseYamlCatalogue. +func ParseYAMLCatalogueFromFile(config *Catalogue, filename string) error { + fh, err := os.Open(filename) + if err != nil { + return err + } + + return ParseYAMLCatalogue(config, fh) +} + +// ParseCatalogue parses a YAML document, r, that represents a set of Kubernetes manifests. +// Manifests parsed include CAPT Hardware, PBnJ BMCs and associated Core API Secret. +func ParseYAMLCatalogue(catalogue *Catalogue, r io.Reader) error { + document := yamlutil.NewYAMLReader(bufio.NewReader(r)) + for { + manifest, err := document.Read() + if errors.Is(err, io.EOF) { + return nil + } + if err != nil { + return err + } + + var resource unstructured.Unstructured + if err = yaml.Unmarshal(manifest, &resource); err != nil { + return err + } + + switch resource.GetKind() { + case "Hardware": + var hardware tinkv1alpha1.Hardware + err = yaml.UnmarshalStrict(manifest, &hardware) + if err != nil { + return fmt.Errorf("unable to parse hardware manifest: %v", err) + } + catalogue.Hardware = append(catalogue.Hardware, hardware) + case "BMC": + var bmc pbnjv1alpha1.BMC + err = yaml.UnmarshalStrict(manifest, &bmc) + if err != nil { + return fmt.Errorf("unable to parse bmc manifest: %v", err) + } + catalogue.BMCs = append(catalogue.BMCs, bmc) + case "Secret": + var secret corev1.Secret + err = yaml.UnmarshalStrict(manifest, &secret) + if err != nil { + return fmt.Errorf("unable to parse secret manifest: %v", err) + } + catalogue.Secrets = append(catalogue.Secrets, secret) + } + } +} diff --git a/pkg/providers/tinkerbell/tinkerbell.go b/pkg/providers/tinkerbell/tinkerbell.go index bf68268ecf12..797c0866cd5c 100644 --- a/pkg/providers/tinkerbell/tinkerbell.go +++ b/pkg/providers/tinkerbell/tinkerbell.go @@ -73,11 +73,14 @@ type tinkerbellProvider struct { providerTinkClient ProviderTinkClient pbnj ProviderPbnjClient templateBuilder *TinkerbellTemplateBuilder - hardwareConfigFile string validator *Validator writer filewriter.FileWriter keyGenerator SSHAuthKeyGenerator + hardwareManifestPath string + // catalogue is a cache initialized during SetupAndValidateCreateCluster() from hardwareManifestPath. + catalogue hardware.Catalogue + skipIpCheck bool skipPowerActions bool setupTinkerbell bool @@ -128,7 +131,7 @@ func NewProvider( providerTinkbellClient TinkerbellClients, now types.NowFunc, skipIpCheck bool, - hardwareConfigFile string, + hardwareManifestPath string, skipPowerActions bool, setupTinkerbell bool, force bool, @@ -144,7 +147,7 @@ func NewProvider( &networkutils.DefaultNetClient{}, now, skipIpCheck, - hardwareConfigFile, + hardwareManifestPath, skipPowerActions, setupTinkerbell, force, @@ -162,7 +165,7 @@ func NewProviderCustomDep( netClient networkutils.NetClient, now types.NowFunc, skipIpCheck bool, - hardwareConfigFile string, + hardwareManifestPath string, skipPowerActions bool, setupTinkerbell bool, force bool, @@ -197,9 +200,9 @@ func NewProviderCustomDep( etcdMachineSpec: etcdMachineSpec, now: now, }, - hardwareConfigFile: hardwareConfigFile, - validator: NewValidator(providerTinkClient, netClient, hardware.HardwareConfig{}, pbnjClient), - writer: writer, + hardwareManifestPath: hardwareManifestPath, + validator: NewValidator(providerTinkClient, netClient, pbnjClient), + writer: writer, // (chrisdoherty4) We're hard coding the dependency and monkey patching in testing because the provider // isn't very testable right now and we already have tests in the `tinkerbell` package so can monkey patch @@ -238,7 +241,7 @@ func (p *tinkerbellProvider) PreCAPIInstallOnBootstrap(ctx context.Context, clus func (p *tinkerbellProvider) PostBootstrapSetup(ctx context.Context, clusterConfig *v1alpha1.Cluster, cluster *types.Cluster) error { // TODO: figure out if we need something else here - hardwareSpec, err := p.validator.hardwareConfig.HardwareSpecMarshallable() + hardwareSpec, err := p.catalogue.HardwareSpecMarshallable() if err != nil { return fmt.Errorf("failed marshalling resources for hardware spec: %v", err) } @@ -366,7 +369,7 @@ func (p *tinkerbellProvider) configureSshKeys() error { func (p *tinkerbellProvider) SetupAndValidateCreateCluster(ctx context.Context, clusterSpec *cluster.Spec) error { logger.Info("Warning: The tinkerbell infrastructure provider is still in development and should not be used in production") - hardware, err := p.providerTinkClient.GetHardware(ctx) + tinkHardware, err := p.providerTinkClient.GetHardware(ctx) if err != nil { return fmt.Errorf("retrieving tinkerbell hardware: %v", err) } @@ -382,10 +385,14 @@ func (p *tinkerbellProvider) SetupAndValidateCreateCluster(ctx context.Context, return err } - // ValidateHardwareConfig performs a lazy load of hardware configuration. Given subsequent steps need the hardware + if err := hardware.ParseYAMLCatalogueFromFile(&p.catalogue, p.hardwareManifestPath); err != nil { + return err + } + + // ValidateHardwareCatalogue performs a lazy load of hardware configuration. Given subsequent steps need the hardware // read into memory it needs to be done first. It also needs connection to // Tinkerbell steps to verify hardware availability on the stack - if err := p.validator.ValidateHardwareConfig(ctx, p.hardwareConfigFile, hardware, p.skipPowerActions, p.force); err != nil { + if err := p.validator.ValidateHardwareCatalogue(ctx, p.catalogue, tinkHardware, p.skipPowerActions, p.force); err != nil { return err } @@ -400,11 +407,11 @@ func (p *tinkerbellProvider) SetupAndValidateCreateCluster(ctx context.Context, } } - if err := p.scrubWorkflowsFromTinkerbell(ctx, p.validator.hardwareConfig.Hardwares, hardware); err != nil { + if err := p.scrubWorkflowsFromTinkerbell(ctx, p.catalogue.Hardware, tinkHardware); err != nil { return err } } else if !p.skipPowerActions { - if err := p.validator.ValidateMachinesPoweredOff(ctx); err != nil { + if err := p.validator.ValidateMachinesPoweredOff(ctx, p.catalogue); err != nil { return fmt.Errorf("validating machines are powered off: %w", err) } } @@ -425,7 +432,7 @@ func (p *tinkerbellProvider) SetupAndValidateCreateCluster(ctx context.Context, return fmt.Errorf("failed validating worker node template config: %v", err) } - if err := p.validator.ValidateMinimumRequiredTinkerbellHardwareAvailable(tinkerbellClusterSpec.Cluster.Spec); err != nil { + if err := p.validator.ValidateMinimumRequiredTinkerbellHardwareAvailable(tinkerbellClusterSpec.Cluster.Spec, p.catalogue); err != nil { return fmt.Errorf("minimum hardware not available: %v", err) } @@ -441,7 +448,7 @@ func (p *tinkerbellProvider) SetupAndValidateCreateCluster(ctx context.Context, } func (p *tinkerbellProvider) setHardwareStateToProvisining(ctx context.Context) error { - for _, hardware := range p.validator.hardwareConfig.Hardwares { + for _, hardware := range p.catalogue.Hardware { tinkHardware, err := p.providerTinkClient.GetHardwareByUuid(ctx, hardware.Spec.ID) if err != nil { return fmt.Errorf("getting hardware with UUID '%s': %v", hardware.Spec.ID, err) @@ -465,20 +472,16 @@ func (p *tinkerbellProvider) setHardwareStateToProvisining(ctx context.Context) return nil } -// setMachinesToPXEBoot iterates over all p.validator.hardwareConfig.Bmcs and instructs them to turn off -// and boot from PXE and turn on. +// setMachinesToPXEBoot iterates over all catalogue.BMCs and instructs them to turn off, one-time +// PXE boot, then turn on. func (p *tinkerbellProvider) setMachinesToPXEBoot(ctx context.Context) error { - // We're reaching into an unexported member of p.validator because of the lazy loading we're doing with - // hardware configuration. This effectively defines a concrete tight coupling between the validator and the - // Tinkerbell construct that desperately needs teething apart. - - secrets := make(map[string]corev1.Secret, len(p.validator.hardwareConfig.Secrets)) - for _, secret := range p.validator.hardwareConfig.Secrets { + secrets := make(map[string]corev1.Secret, len(p.catalogue.Secrets)) + for _, secret := range p.catalogue.Secrets { secrets[secret.Name] = secret } var errs []error - for _, bmc := range p.validator.hardwareConfig.Bmcs { + for _, bmc := range p.catalogue.BMCs { secret, found := secrets[bmc.Spec.AuthSecretRef.Name] if !found { errs = append(errs, fmt.Errorf("could not find bmc secret for '%v'", bmc.Name)) diff --git a/pkg/providers/tinkerbell/validator.go b/pkg/providers/tinkerbell/validator.go index c474ecd98971..b04d44ef7dae 100644 --- a/pkg/providers/tinkerbell/validator.go +++ b/pkg/providers/tinkerbell/validator.go @@ -21,18 +21,16 @@ import ( ) type Validator struct { - tink ProviderTinkClient - netClient networkutils.NetClient - hardwareConfig hardware.HardwareConfig - pbnj ProviderPbnjClient + tink ProviderTinkClient + netClient networkutils.NetClient + pbnj ProviderPbnjClient } -func NewValidator(tink ProviderTinkClient, netClient networkutils.NetClient, hardwareConfig hardware.HardwareConfig, pbnjClient ProviderPbnjClient) *Validator { +func NewValidator(tink ProviderTinkClient, netClient networkutils.NetClient, pbnjClient ProviderPbnjClient) *Validator { return &Validator{ - tink: tink, - netClient: netClient, - hardwareConfig: hardwareConfig, - pbnj: pbnjClient, + tink: tink, + netClient: netClient, + pbnj: pbnjClient, } } @@ -125,11 +123,7 @@ func (v *Validator) ValidateClusterMachineConfigs(ctx context.Context, tinkerbel return nil } -func (v *Validator) ValidateHardwareConfig(ctx context.Context, hardwareConfigFile string, hardwares []*tinkhardware.Hardware, skipPowerActions, force bool) error { - if err := v.hardwareConfig.ParseHardwareConfig(hardwareConfigFile); err != nil { - return fmt.Errorf("failed to get hardware Config: %v", err) - } - +func (v *Validator) ValidateHardwareCatalogue(ctx context.Context, catalogue hardware.Catalogue, hardwares []*tinkhardware.Hardware, skipPowerActions, force bool) error { tinkHardwareMap := getHardwareMap(hardwares) workflows, err := v.tink.GetWorkflow(ctx) @@ -141,20 +135,20 @@ func (v *Validator) ValidateHardwareConfig(ctx context.Context, hardwareConfigFi return fmt.Errorf("validating if the workflow exist for the given list of hardwares %v", err) } - if err := v.hardwareConfig.ValidateHardware(skipPowerActions, force, tinkHardwareMap, tinkWorkflowMap); err != nil { + if err := catalogue.ValidateHardware(skipPowerActions, force, tinkHardwareMap, tinkWorkflowMap); err != nil { return fmt.Errorf("failed validating Hardware BMC refs in hardware config: %v", err) } if !skipPowerActions { - if err := v.hardwareConfig.ValidateBMC(); err != nil { + if err := catalogue.ValidateBMC(); err != nil { return fmt.Errorf("failed validating BMCs in hardware config: %v", err) } - if err := v.hardwareConfig.ValidateBmcSecretRefs(); err != nil { + if err := catalogue.ValidateBmcSecretRefs(); err != nil { return fmt.Errorf("failed validating Secrets in hardware config: %v", err) } - if err := v.ValidateBMCSecretCreds(ctx, v.hardwareConfig); err != nil { + if err := v.ValidateBMCSecretCreds(ctx, catalogue); err != nil { return fmt.Errorf("failed validating BMC connection in hardware config: %v", err) } logger.MarkPass("BMC connectivity validated") @@ -165,12 +159,12 @@ func (v *Validator) ValidateHardwareConfig(ctx context.Context, hardwareConfigFi return nil } -func (v *Validator) ValidateBMCSecretCreds(ctx context.Context, hc hardware.HardwareConfig) error { - for index, bmc := range hc.Bmcs { +func (v *Validator) ValidateBMCSecretCreds(ctx context.Context, catalogue hardware.Catalogue) error { + for index, bmc := range catalogue.BMCs { bmcInfo := pbnj.BmcSecretConfig{ Host: bmc.Spec.Host, - Username: string(hc.Secrets[index].Data["username"]), - Password: string(hc.Secrets[index].Data["password"]), + Username: string(catalogue.Secrets[index].Data["username"]), + Password: string(catalogue.Secrets[index].Data["password"]), Vendor: bmc.Spec.Vendor, } if _, err := v.pbnj.GetPowerState(ctx, bmcInfo); err != nil { @@ -222,13 +216,7 @@ func (v *Validator) ValidateAndPopulateTemplate(ctx context.Context, datacenterC // ValidateMinimumRequiredTinkerbellHardwareAvailable ensures there is sufficient hardware registered relative to the // sum of requested control plane, etcd and worker node counts. // The system requires hardware >= to requested provisioning. -// ValidateMinimumRequiredTinkerbellHardwareAvailable requires v.ValidateHardwareConfig() to be called first. -func (v *Validator) ValidateMinimumRequiredTinkerbellHardwareAvailable(spec v1alpha1.ClusterSpec) error { - // ValidateMinimumRequiredTinkerbellHardwareAvailable relies on v.hardwareConfig being valid. A call to - // v.ValidateHardwareConfig parses the hardware config file. Consequently, we need to validate the hardware config - // prior to calling ValidateMinimumRequiredTinkerbellHardwareAvailable. We should decouple validation including - // isolation of io in the parsing of hardware config. - +func (v *Validator) ValidateMinimumRequiredTinkerbellHardwareAvailable(spec v1alpha1.ClusterSpec, catalogue hardware.Catalogue) error { requestedNodesCount := spec.ControlPlaneConfiguration.Count + sumWorkerNodeCounts(spec.WorkerNodeGroupConfigurations) @@ -237,10 +225,10 @@ func (v *Validator) ValidateMinimumRequiredTinkerbellHardwareAvailable(spec v1al requestedNodesCount += spec.ExternalEtcdConfiguration.Count } - if len(v.hardwareConfig.Hardwares) < requestedNodesCount { + if len(catalogue.Hardware) < requestedNodesCount { return fmt.Errorf( "have %v tinkerbell hardware; cluster spec requires >= %v hardware", - len(v.hardwareConfig.Hardwares), + len(catalogue.Hardware), requestedNodesCount, ) } @@ -249,14 +237,14 @@ func (v *Validator) ValidateMinimumRequiredTinkerbellHardwareAvailable(spec v1al } // ValidateMachinesPoweredOff validates the hardware submitted for provisioning is powered off. -func (v *Validator) ValidateMachinesPoweredOff(ctx context.Context) error { +func (v *Validator) ValidateMachinesPoweredOff(ctx context.Context, catalogue hardware.Catalogue) error { secrets := make(map[string]corev1.Secret) - for _, s := range v.hardwareConfig.Secrets { + for _, s := range catalogue.Secrets { secrets[s.Name] = s } var poweredOnHosts []string - for _, bmc := range v.hardwareConfig.Bmcs { + for _, bmc := range catalogue.BMCs { secret := secrets[bmc.Spec.AuthSecretRef.Name] state, err := v.pbnj.GetPowerState(ctx, pbnj.BmcSecretConfig{ Host: bmc.Spec.Host, diff --git a/pkg/providers/tinkerbell/validator_helper_test.go b/pkg/providers/tinkerbell/validator_helper_test.go deleted file mode 100644 index 76b8a5e296a7..000000000000 --- a/pkg/providers/tinkerbell/validator_helper_test.go +++ /dev/null @@ -1,8 +0,0 @@ -package tinkerbell - -import "github.com/aws/eks-anywhere/pkg/providers/tinkerbell/hardware" - -// SetValidatorHardwareConfig sets v internal hardwareConfig member to cfg. -func SetValidatorHardwareConfig(v *Validator, cfg hardware.HardwareConfig) { - v.hardwareConfig = cfg -} diff --git a/pkg/providers/tinkerbell/validator_test.go b/pkg/providers/tinkerbell/validator_test.go index 7b7f0224249b..0b6874549371 100644 --- a/pkg/providers/tinkerbell/validator_test.go +++ b/pkg/providers/tinkerbell/validator_test.go @@ -27,12 +27,11 @@ func TestValidateMinimumRequiredTinkerbellHardwareAvailable_SufficientHardware(t t.Run(name, func(t *testing.T) { t.Parallel() - hardwareConfig := newHardwareConfigWithHardware(tt.AvailableHardware) + catalogue := newCatalogueWithHardware(tt.AvailableHardware) var validator tinkerbell.Validator - tinkerbell.SetValidatorHardwareConfig(&validator, hardwareConfig) - assert.NoError(t, validator.ValidateMinimumRequiredTinkerbellHardwareAvailable(tt.ClusterSpec)) + assert.NoError(t, validator.ValidateMinimumRequiredTinkerbellHardwareAvailable(tt.ClusterSpec, catalogue)) }) } } @@ -40,24 +39,22 @@ func TestValidateMinimumRequiredTinkerbellHardwareAvailable_SufficientHardware(t func TestValidateMinimumRequiredTinkerbellHardwareAvailable_InsufficientHardware(t *testing.T) { clusterSpec := newValidClusterSpec(1, 1, 1) - hardwareConfig := newHardwareConfigWithHardware(2) + catalogue := newCatalogueWithHardware(2) var validator tinkerbell.Validator - tinkerbell.SetValidatorHardwareConfig(&validator, hardwareConfig) - assert.Error(t, validator.ValidateMinimumRequiredTinkerbellHardwareAvailable(clusterSpec)) + assert.Error(t, validator.ValidateMinimumRequiredTinkerbellHardwareAvailable(clusterSpec, catalogue)) } func TestValidateMinimumRequiredTinkerbellHardware_EtcdUnspecified(t *testing.T) { clusterSpec := newValidClusterSpec(1, 0, 1) clusterSpec.ExternalEtcdConfiguration = nil - hardwareConfig := newHardwareConfigWithHardware(3) + catalogue := newCatalogueWithHardware(3) var validator tinkerbell.Validator - tinkerbell.SetValidatorHardwareConfig(&validator, hardwareConfig) - assert.NoError(t, validator.ValidateMinimumRequiredTinkerbellHardwareAvailable(clusterSpec)) + assert.NoError(t, validator.ValidateMinimumRequiredTinkerbellHardwareAvailable(clusterSpec, catalogue)) } func TestValidateTinkerbellConfig_ValidAuthorities(t *testing.T) { @@ -66,10 +63,9 @@ func TestValidateTinkerbellConfig_ValidAuthorities(t *testing.T) { tink := tinkerbellmocks.NewMockProviderTinkClient(ctrl) net := networkutilsmocks.NewMockNetClient(ctrl) - var hardware hardware.HardwareConfig datacenter := newValidTinkerbellDatacenterConfig() - validator := tinkerbell.NewValidator(tink, net, hardware, pbnj) + validator := tinkerbell.NewValidator(tink, net, pbnj) err := validator.ValidateTinkerbellConfig(context.Background(), datacenter) assert.NoError(t, err) @@ -90,11 +86,10 @@ func TestValidateTinkerbellConfig_InvalidGrpcAuthority(t *testing.T) { tink := tinkerbellmocks.NewMockProviderTinkClient(ctrl) net := networkutilsmocks.NewMockNetClient(ctrl) - var hardware hardware.HardwareConfig datacenter := newValidTinkerbellDatacenterConfig() datacenter.Spec.TinkerbellGRPCAuth = address - validator := tinkerbell.NewValidator(tink, net, hardware, pbnj) + validator := tinkerbell.NewValidator(tink, net, pbnj) err := validator.ValidateTinkerbellConfig(context.Background(), datacenter) assert.Error(t, err) @@ -117,11 +112,10 @@ func TestValidateTinkerbellConfig_InvalidPbnjAuthority(t *testing.T) { tink := tinkerbellmocks.NewMockProviderTinkClient(ctrl) net := networkutilsmocks.NewMockNetClient(ctrl) - var hardware hardware.HardwareConfig datacenter := newValidTinkerbellDatacenterConfig() datacenter.Spec.TinkerbellPBnJGRPCAuth = address - validator := tinkerbell.NewValidator(tink, net, hardware, pbnj) + validator := tinkerbell.NewValidator(tink, net, pbnj) err := validator.ValidateTinkerbellConfig(context.Background(), datacenter) assert.Error(t, err) @@ -143,9 +137,9 @@ func newValidClusterSpec(cp, etcd, worker int) v1alpha1.ClusterSpec { } } -func newHardwareConfigWithHardware(hardwareCount int) hardware.HardwareConfig { - return hardware.HardwareConfig{ - Hardwares: make([]tinkv1alpha1.Hardware, hardwareCount), +func newCatalogueWithHardware(hardwareCount int) hardware.Catalogue { + return hardware.Catalogue{ + Hardware: make([]tinkv1alpha1.Hardware, hardwareCount), } }