github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/client/serviceregistration/checks/result.go (about)

     1  package checks
     2  
     3  import (
     4  	"net/http"
     5  	"time"
     6  
     7  	"github.com/hashicorp/nomad/nomad/structs"
     8  	"golang.org/x/exp/maps"
     9  )
    10  
    11  // GetCheckQuery extracts the needed info from c to actually execute the check.
    12  func GetCheckQuery(c *structs.ServiceCheck) *Query {
    13  	var protocol = c.Protocol // ensure appropriate default
    14  	if c.Type == "http" && protocol == "" {
    15  		protocol = "http"
    16  	}
    17  	return &Query{
    18  		Mode:        structs.GetCheckMode(c),
    19  		Type:        c.Type,
    20  		Timeout:     c.Timeout,
    21  		AddressMode: c.AddressMode,
    22  		PortLabel:   c.PortLabel,
    23  		Protocol:    protocol,
    24  		Path:        c.Path,
    25  		Method:      c.Method,
    26  		Headers:     maps.Clone(c.Header),
    27  		Body:        c.Body,
    28  	}
    29  }
    30  
    31  // A Query is derived from a ServiceCheck and contains the minimal
    32  // amount of information needed to actually execute that check.
    33  type Query struct {
    34  	Mode structs.CheckMode // readiness or healthiness
    35  	Type string            // tcp or http
    36  
    37  	Timeout time.Duration // connection / request timeout
    38  
    39  	AddressMode string // host, driver, or alloc
    40  	PortLabel   string // label or value
    41  
    42  	Protocol string      // http checks only (http or https)
    43  	Path     string      // http checks only
    44  	Method   string      // http checks only
    45  	Headers  http.Header // http checks only
    46  	Body     string      // http checks only
    47  }
    48  
    49  // A QueryContext contains allocation and service parameters necessary for
    50  // address resolution.
    51  type QueryContext struct {
    52  	ID               structs.CheckID
    53  	CustomAddress    string
    54  	ServicePortLabel string
    55  	Networks         structs.Networks
    56  	NetworkStatus    structs.NetworkStatus
    57  	Ports            structs.AllocatedPorts
    58  
    59  	Group   string
    60  	Task    string
    61  	Service string
    62  	Check   string
    63  }
    64  
    65  // Stub creates a temporary QueryResult for the check of ID in the Pending state
    66  // so we can represent the status of not being checked yet.
    67  func Stub(
    68  	id structs.CheckID, kind structs.CheckMode, now int64,
    69  	group, task, service, check string,
    70  ) *structs.CheckQueryResult {
    71  	return &structs.CheckQueryResult{
    72  		ID:        id,
    73  		Mode:      kind,
    74  		Status:    structs.CheckPending,
    75  		Output:    "nomad: waiting to run",
    76  		Timestamp: now,
    77  		Group:     group,
    78  		Task:      task,
    79  		Service:   service,
    80  		Check:     check,
    81  	}
    82  }
    83  
    84  // AllocationResults is a view of the check_id -> latest result for group and task
    85  // checks in an allocation.
    86  type AllocationResults map[structs.CheckID]*structs.CheckQueryResult
    87  
    88  // diff returns the set of IDs in ids that are not in m.
    89  func (m AllocationResults) diff(ids []structs.CheckID) []structs.CheckID {
    90  	var missing []structs.CheckID
    91  	for _, id := range ids {
    92  		if _, exists := m[id]; !exists {
    93  			missing = append(missing, id)
    94  		}
    95  	}
    96  	return missing
    97  }
    98  
    99  // ClientResults is a holistic view of alloc_id -> check_id -> latest result
   100  // group and task checks across all allocations on a client.
   101  type ClientResults map[string]AllocationResults
   102  
   103  func (cr ClientResults) Insert(allocID string, result *structs.CheckQueryResult) {
   104  	if _, exists := cr[allocID]; !exists {
   105  		cr[allocID] = make(AllocationResults)
   106  	}
   107  	cr[allocID][result.ID] = result
   108  }