github.com/netdata/go.d.plugin@v0.58.1/modules/k8s_state/state.go (about)

     1  // SPDX-License-Identifier: GPL-3.0-or-later
     2  
     3  package k8s_state
     4  
     5  import (
     6  	"sync"
     7  	"time"
     8  
     9  	corev1 "k8s.io/api/core/v1"
    10  )
    11  
    12  func newKubeState() *kubeState {
    13  	return &kubeState{
    14  		Mutex: &sync.Mutex{},
    15  		nodes: make(map[string]*nodeState),
    16  		pods:  make(map[string]*podState),
    17  	}
    18  }
    19  
    20  func newNodeState() *nodeState {
    21  	return &nodeState{
    22  		new:        true,
    23  		labels:     make(map[string]string),
    24  		conditions: make(map[string]*nodeStateCondition),
    25  	}
    26  }
    27  
    28  func newPodState() *podState {
    29  	return &podState{
    30  		new:            true,
    31  		labels:         make(map[string]string),
    32  		initContainers: make(map[string]*containerState),
    33  		containers:     make(map[string]*containerState),
    34  	}
    35  }
    36  
    37  func newContainerState() *containerState {
    38  	return &containerState{
    39  		new:                    true,
    40  		stateWaitingReasons:    make(map[string]*containerStateReason),
    41  		stateTerminatedReasons: make(map[string]*containerStateReason),
    42  	}
    43  }
    44  
    45  type kubeState struct {
    46  	*sync.Mutex
    47  	nodes map[string]*nodeState
    48  	pods  map[string]*podState
    49  }
    50  
    51  type (
    52  	nodeState struct {
    53  		new     bool
    54  		deleted bool
    55  
    56  		name            string
    57  		unSchedulable   bool
    58  		labels          map[string]string
    59  		creationTime    time.Time
    60  		allocatableCPU  int64
    61  		allocatableMem  int64
    62  		allocatablePods int64
    63  		conditions      map[string]*nodeStateCondition
    64  
    65  		stats nodeStateStats
    66  	}
    67  	nodeStateCondition struct {
    68  		new bool
    69  		// https://kubernetes.io/docs/concepts/architecture/nodes/#condition
    70  		//typ    corev1.NodeConditionType
    71  		status corev1.ConditionStatus
    72  	}
    73  	nodeStateStats struct {
    74  		reqCPU   int64
    75  		limitCPU int64
    76  		reqMem   int64
    77  		limitMem int64
    78  		pods     int64
    79  
    80  		podsCondPodReady        int64
    81  		podsCondPodScheduled    int64
    82  		podsCondPodInitialized  int64
    83  		podsCondContainersReady int64
    84  
    85  		podsReadinessReady   int64
    86  		podsReadinessUnready int64
    87  
    88  		podsPhaseRunning   int64
    89  		podsPhaseFailed    int64
    90  		podsPhaseSucceeded int64
    91  		podsPhasePending   int64
    92  
    93  		containers              int64
    94  		initContainers          int64
    95  		initContStateRunning    int64
    96  		initContStateWaiting    int64
    97  		initContStateTerminated int64
    98  		contStateRunning        int64
    99  		contStateWaiting        int64
   100  		contStateTerminated     int64
   101  	}
   102  )
   103  
   104  func (ns nodeState) id() string   { return ns.name }
   105  func (ns *nodeState) resetStats() { ns.stats = nodeStateStats{} }
   106  
   107  type (
   108  	podState struct {
   109  		new         bool
   110  		deleted     bool
   111  		unscheduled bool
   112  
   113  		name           string
   114  		nodeName       string
   115  		namespace      string
   116  		uid            string
   117  		labels         map[string]string
   118  		controllerKind string
   119  		controllerName string
   120  		qosClass       string
   121  		creationTime   time.Time
   122  		reqCPU         int64
   123  		reqMem         int64
   124  		limitCPU       int64
   125  		limitMem       int64
   126  		// https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#pod-conditions
   127  		condPodScheduled    corev1.ConditionStatus
   128  		condContainersReady corev1.ConditionStatus
   129  		condPodInitialized  corev1.ConditionStatus
   130  		condPodReady        corev1.ConditionStatus
   131  		// https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#pod-phase
   132  		phase corev1.PodPhase
   133  
   134  		initContainers map[string]*containerState
   135  		containers     map[string]*containerState
   136  	}
   137  )
   138  
   139  func (ps podState) id() string { return ps.namespace + "_" + ps.name }
   140  
   141  type (
   142  	containerState struct {
   143  		new bool
   144  
   145  		name string
   146  		uid  string
   147  
   148  		podName   string
   149  		nodeName  string
   150  		namespace string
   151  
   152  		ready                  bool
   153  		restarts               int64
   154  		stateRunning           bool
   155  		stateWaiting           bool
   156  		stateTerminated        bool
   157  		stateWaitingReasons    map[string]*containerStateReason
   158  		stateTerminatedReasons map[string]*containerStateReason
   159  	}
   160  	containerStateReason struct {
   161  		new    bool
   162  		reason string
   163  		active bool
   164  	}
   165  )