github.com/cilium/cilium@v1.16.2/pkg/hive/health/types/types.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package types
     5  
     6  import (
     7  	"fmt"
     8  	"strconv"
     9  	"strings"
    10  	"time"
    11  
    12  	"github.com/cilium/hive/cell"
    13  )
    14  
    15  // Provider has functionality to create health reporters, scoped a
    16  // module.
    17  type Provider interface {
    18  	ForModule(mid cell.FullModuleID) cell.Health
    19  }
    20  
    21  type pathIdent []string
    22  
    23  func (p pathIdent) String() string {
    24  	if len(p) == 0 {
    25  		return ""
    26  	}
    27  	return strings.Join(p, ".")
    28  }
    29  
    30  // HealthID is used as the key for the primary index for health status
    31  // tables.
    32  type HealthID string
    33  
    34  // Identifier is a fully qualified, path based identifier for health status
    35  // which is made up of module ID and component ID parts.
    36  type Identifier struct {
    37  	Module    cell.FullModuleID
    38  	Component pathIdent
    39  }
    40  
    41  // WithSubComponent returns view of an identifier with an appended
    42  // subcomponent.
    43  func (i Identifier) WithSubComponent(name string) Identifier {
    44  	return Identifier{
    45  		Module:    i.Module,
    46  		Component: append(i.Component, name),
    47  	}
    48  }
    49  
    50  func (i Identifier) String() string {
    51  	return strings.Join([]string{i.Module.String(), i.Component.String()}, ".")
    52  }
    53  
    54  func (i Identifier) HealthID() HealthID {
    55  	return HealthID(i.String())
    56  }
    57  
    58  // Status represents a current health status update.
    59  type Status struct {
    60  	ID      Identifier
    61  	Level   Level
    62  	Message string
    63  	Error   string
    64  	LastOK  time.Time
    65  	Updated time.Time
    66  	Stopped time.Time
    67  	// Final is the final message set when a status is stopped.
    68  	Final string
    69  	Count uint64
    70  }
    71  
    72  func (Status) TableHeader() []string {
    73  	return []string{"Module", "Component", "Level", "Message", "Error", "LastOK", "UpdatedAt", "Count"}
    74  }
    75  
    76  func (s Status) TableRow() []string {
    77  	return []string{
    78  		s.ID.Module.String(),
    79  		s.ID.Component.String(),
    80  		string(s.Level),
    81  		s.Message,
    82  		s.Error,
    83  		s.LastOK.Format(time.RFC3339),
    84  		s.Updated.Format(time.RFC3339),
    85  		strconv.FormatUint(s.Count, 10),
    86  	}
    87  }
    88  
    89  func (s Status) String() string {
    90  	if s.Error != "" {
    91  		return fmt.Sprintf("%s: [%s] %s: %s", s.ID.String(), s.Level, s.Message, s.Error)
    92  	} else {
    93  		return fmt.Sprintf("%s: [%s] %s", s.ID.String(), s.Level, s.Message)
    94  	}
    95  }
    96  
    97  type Level string
    98  
    99  const (
   100  	LevelOK       = "OK"
   101  	LevelDegraded = "Degraded"
   102  	LevelStopped  = "Stopped"
   103  )