github.com/verrazzano/verrazzano@v1.7.0/pkg/vzchecks/precheck.go (about)

     1  // Copyright (c) 2022, Oracle and/or its affiliates.
     2  // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     3  
     4  package vzchecks
     5  
     6  import (
     7  	"fmt"
     8  	"github.com/verrazzano/verrazzano/pkg/k8s/node"
     9  	k8score "k8s.io/api/core/v1"
    10  	"k8s.io/apimachinery/pkg/api/resource"
    11  	clipkg "sigs.k8s.io/controller-runtime/pkg/client"
    12  	"strconv"
    13  )
    14  
    15  // PrerequisiteCheck checks the prerequisites before applying the Verrazzano CR
    16  func PrerequisiteCheck(client clipkg.Client, profile ProfileType) []error {
    17  	return preCheck(client, profile)
    18  }
    19  
    20  func preCheck(client clipkg.Client, profile ProfileType) []error {
    21  	var errs []error
    22  	vzReq := getVZRequirement(profile)
    23  	if vzReq == (VZRequirement{}) {
    24  		return errs
    25  	}
    26  	nodeList, err := node.GetK8sNodeList(client)
    27  	if err != nil {
    28  		return []error{err}
    29  	}
    30  	if len(nodeList.Items) < vzReq.nodeCount {
    31  		errs = append(errs, fmt.Errorf(nodeCountReqMsg, vzReq.nodeCount, len(nodeList.Items)))
    32  	}
    33  
    34  	for _, node := range nodeList.Items {
    35  		var cpuAllocatable = node.Status.Allocatable[k8score.ResourceCPU]
    36  		var memoryAllocatable = node.Status.Allocatable[k8score.ResourceMemory]
    37  		var storageAllocatable = node.Status.Allocatable[k8score.ResourceEphemeralStorage]
    38  		if cpuAllocatable.MilliValue() < vzReq.cpu.allocatable.MilliValue() {
    39  			errs = append(errs, fmt.Errorf(cpuReqMsg, vzReq.cpu.allocatable.Value(),
    40  				node.Name, cpuAllocatable.Value()))
    41  		}
    42  		if memoryAllocatable.MilliValue() < vzReq.memory.allocatable.MilliValue() {
    43  			errs = append(errs, fmt.Errorf(memoryReqMsg, convertQuantityToString(vzReq.memory.allocatable),
    44  				node.Name, convertQuantityToString(memoryAllocatable)))
    45  		}
    46  		if storageAllocatable.MilliValue() < vzReq.ephemeralStorage.allocatable.MilliValue() {
    47  			errs = append(errs, fmt.Errorf(storageReqMsg, convertQuantityToString(vzReq.ephemeralStorage.allocatable),
    48  				node.Name, convertQuantityToString(storageAllocatable)))
    49  		}
    50  	}
    51  	return errs
    52  }
    53  
    54  // convertQuantityToString converts the given quantity value to a Gigabyte value
    55  func convertQuantityToString(quantity resource.Quantity) string {
    56  	gigabyteFormat := resource.MustParse("1G")
    57  	m := float64(quantity.Value()) / float64(gigabyteFormat.Value())
    58  	return strconv.FormatFloat(m, 'g', -1, 64)
    59  }