github.com/openshift/installer@v1.4.17/pkg/agent/validations.go (about)

     1  package agent
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"github.com/pkg/errors"
     7  	"github.com/sirupsen/logrus"
     8  
     9  	"github.com/openshift/assisted-service/api/common"
    10  	"github.com/openshift/assisted-service/models"
    11  )
    12  
    13  // These validation status strings are defined to match the ones from assisted-service/internal.
    14  // These statuses are the statuses that mattered to us for this implementation.
    15  // Cluster: https://github.com/openshift/assisted-service/blob/master/internal/cluster/validator.go
    16  // Host: https://github.com/openshift/assisted-service/blob/master/internal/host/validator.go
    17  const (
    18  	validationFailure string = "failure"
    19  	validationError   string = "error"
    20  	validationSuccess string = "success"
    21  )
    22  
    23  type validationResults struct {
    24  	ClusterValidationHistory map[string]*validationResultHistory
    25  	HostValidationHistory    map[string]map[string]*validationResultHistory
    26  }
    27  
    28  type validationResultHistory struct {
    29  	numFailures     int
    30  	seen            bool
    31  	currentStatus   string
    32  	currentMessage  string
    33  	previousStatus  string
    34  	previousMessage string
    35  }
    36  
    37  func checkValidations(cluster *models.Cluster, validationResults *validationResults, log *logrus.Logger, hostLogPrefix string) error {
    38  	clusterLogPrefix := "Cluster validation: "
    39  	updatedClusterValidationHistory, err := updateValidationResultHistory(clusterLogPrefix, cluster.ValidationsInfo, validationResults.ClusterValidationHistory, log)
    40  	if err != nil {
    41  		return err
    42  	}
    43  	validationResults.ClusterValidationHistory = updatedClusterValidationHistory
    44  
    45  	for _, h := range cluster.Hosts {
    46  		if hostLogPrefix == "" {
    47  			hostLogPrefix = "Host " + h.RequestedHostname + " validation: "
    48  		}
    49  		if _, ok := validationResults.HostValidationHistory[h.RequestedHostname]; !ok {
    50  			validationResults.HostValidationHistory[h.RequestedHostname] = make(map[string]*validationResultHistory)
    51  		}
    52  		updatedHostValidationHistory, err := updateValidationResultHistory(hostLogPrefix, h.ValidationsInfo, validationResults.HostValidationHistory[h.RequestedHostname], log)
    53  		if err != nil {
    54  			return err
    55  		}
    56  		validationResults.HostValidationHistory[h.RequestedHostname] = updatedHostValidationHistory
    57  	}
    58  	return nil
    59  }
    60  
    61  func updateValidationResultHistory(logPrefix string, validationsInfoString string, validationHistory map[string]*validationResultHistory, log *logrus.Logger) (map[string]*validationResultHistory, error) {
    62  
    63  	if validationsInfoString == "" {
    64  		return validationHistory, nil
    65  	}
    66  
    67  	validationsInfo := common.ValidationsStatus{}
    68  	err := json.Unmarshal([]byte(validationsInfoString), &validationsInfo)
    69  	if err != nil {
    70  		return nil, errors.Wrap(err, "unable to verify validations")
    71  	}
    72  
    73  	for _, validationResults := range validationsInfo {
    74  		for _, r := range validationResults {
    75  			// If validation ID does not exist create it
    76  			if _, ok := validationHistory[r.ID]; !ok {
    77  				validationHistory[r.ID] = &validationResultHistory{}
    78  			}
    79  			validationHistory[r.ID].previousMessage = validationHistory[r.ID].currentMessage
    80  			validationHistory[r.ID].previousStatus = validationHistory[r.ID].currentStatus
    81  			validationHistory[r.ID].currentMessage = r.Message
    82  			validationHistory[r.ID].currentStatus = r.Status
    83  			switch r.Status {
    84  			case validationFailure, validationError:
    85  				validationHistory[r.ID].numFailures++
    86  			}
    87  			logValidationHistory(logPrefix, validationHistory[r.ID], log)
    88  		}
    89  	}
    90  	return validationHistory, nil
    91  }
    92  
    93  func logValidationHistory(logPrefix string, history *validationResultHistory, log *logrus.Logger) {
    94  	// First time we print something
    95  	if !history.seen {
    96  		history.seen = true
    97  		switch history.currentStatus {
    98  		case validationSuccess:
    99  			log.Debug(logPrefix + history.currentMessage)
   100  		case validationFailure, validationError:
   101  			log.Warning(logPrefix + history.currentMessage)
   102  		default:
   103  			log.Trace(logPrefix + history.currentMessage)
   104  		}
   105  		return
   106  	}
   107  	// We have already printed something
   108  	if history.currentMessage != history.previousMessage {
   109  		switch history.currentStatus {
   110  		case validationSuccess:
   111  			if history.previousStatus == validationError || history.previousStatus == validationFailure {
   112  				log.Info(logPrefix + history.currentMessage)
   113  			}
   114  		case validationFailure, validationError:
   115  			log.Warning(logPrefix + history.currentMessage)
   116  		default:
   117  			log.Trace(logPrefix + history.currentMessage)
   118  		}
   119  	}
   120  }