Documentation
¶
Index ¶
- Constants
- Variables
- func BuildLocalClient() (*kubernetes.Clientset, error)
- func CollectAPIServerHealth(ctx context.Context, kc *KubeClient) bool
- func CollectEtcdHealth(ctx context.Context, kc *KubeClient) (bool, error)
- func CollectPerformanceMetrics(ctx context.Context, kc *KubeClient) (*portainer.PerformanceMetrics, error)
- func CreateSnapshot(edgeKey string) (*portainer.KubernetesSnapshot, error)
- type APIServerLatencyHistogram
- type ClusterRawMetrics
- type ExecProcessParams
- type KubeClient
- func (kcl *KubeClient) ApplyManagedField(ctx context.Context, target ResourceTarget, fieldPath []string, ...) error
- func (kcl *KubeClient) CRDResourceReady(ctx context.Context, crdName string) bool
- func (kcl *KubeClient) DeleteResource(ctx context.Context, apiVersion, kind, name, namespace string) error
- func (kcl *KubeClient) GetDaemonSetStatus(namespace string, name string) ResourceStatus
- func (kcl *KubeClient) GetDeploymentStatus(namespace string, name string) ResourceStatus
- func (kcl *KubeClient) GetPodStatus(namespace string, name string) ResourceStatus
- func (kcl *KubeClient) GetResource(ctx context.Context, apiVersion, kind, name, namespace string) (any, error)
- func (kcl *KubeClient) GetStatefulSetStatus(namespace string, name string) ResourceStatus
- func (kcl *KubeClient) ListNamespaces(ctx context.Context) (map[string]string, error)
- func (kcl *KubeClient) PatchResource(ctx context.Context, apiVersion, kind, name, namespace, patch string) error
- func (kcl *KubeClient) RestoreManagedField(ctx context.Context, target ResourceTarget, fieldPath []string, ...) error
- func (kcl *KubeClient) StartExecProcess(params ExecProcessParams) error
- type NodeReadyStatus
- type ResourceStatus
- type ResourceTarget
- type TLSCertInfo
Constants ¶
const RestoreAnnotation = "resource-patch.portainer.io/restore"
RestoreAnnotation holds a JSON snapshot of the values a resource carried before the patcher.
Variables ¶
var ErrAPIServerRequestLatencyUnsupported = errors.New("api server request latency histogram family is not available")
Functions ¶
func BuildLocalClient ¶
func BuildLocalClient() (*kubernetes.Clientset, error)
func CollectAPIServerHealth ¶
func CollectAPIServerHealth(ctx context.Context, kc *KubeClient) bool
CollectAPIServerHealth probes /livez on the Kubernetes API server and returns true when the server reports healthy (200), false for any non-200 response or connection failure. A connection failure is treated as unhealthy because the agent uses the same REST client for all API calls — if /livez is unreachable, the API server is effectively down from the agent's perspective.
func CollectEtcdHealth ¶
func CollectEtcdHealth(ctx context.Context, kc *KubeClient) (bool, error)
CollectEtcdHealth calls /readyz?verbose on the API server and returns true when the etcd health check line reports healthy, false when it reports unhealthy. It returns an error when etcd health cannot be derived.
func CollectPerformanceMetrics ¶
func CollectPerformanceMetrics(ctx context.Context, kc *KubeClient) (*portainer.PerformanceMetrics, error)
CollectPerformanceMetrics collects CPU, memory, disk, and network usage by querying each node's kubelet stats/summary endpoint directly via the Kubernetes API proxy (GET /api/v1/nodes/{name}/proxy/stats/summary). The kubelet proxy is used instead of metrics.k8s.io because the Metrics API does not expose filesystem data, and the kubelet endpoint works without metrics-server installed.
func CreateSnapshot ¶
func CreateSnapshot(edgeKey string) (*portainer.KubernetesSnapshot, error)
CreateSnapshot creates a snapshot of a specific Kubernetes environment(endpoint)
Types ¶
type APIServerLatencyHistogram ¶
type APIServerLatencyHistogram struct {
Buckets map[float64]float64 // le upper bound -> cumulative observation count
Count float64 // total observations (the +Inf bucket)
}
APIServerLatencyHistogram is the API server request-duration histogram pooled across every verb/resource into a single cluster-wide classic histogram.
The agent only parses and pools; quantile estimation is left to the evaluator, which re-exposes Buckets as le-labelled series and computes histogram_quantile(0.99, rate(...)) in the alert rule.
func CollectAPIServerRequestLatency ¶
func CollectAPIServerRequestLatency(ctx context.Context, kc *KubeClient) (APIServerLatencyHistogram, error)
CollectAPIServerRequestLatency scrapes the API server /metrics endpoint and returns its request-duration histogram pooled across all verbs and resources. It prefers the SLI family (which already excludes legitimately-slow WATCH and long LIST calls) and falls back to the SLO family.
type ClusterRawMetrics ¶
type ClusterRawMetrics struct {
CPUUsageNanoCores uint64
CPUCapacityNanoCores uint64
MemoryWorkingSetBytes uint64
MemoryCapacityBytes uint64
DiskUsedBytes uint64
DiskCapacityBytes uint64
NetworkRxBytes uint64
NetworkTxBytes uint64
HasCPU, HasMemory, HasDisk, HasNetwork bool
}
ClusterRawMetrics holds raw (non-percentage) cluster-wide resource usage and capacity.
func CollectRawMetrics ¶
func CollectRawMetrics(ctx context.Context, kc *KubeClient) (*ClusterRawMetrics, error)
CollectRawMetrics collects raw (non-percentage) CPU, memory, disk, and network values by querying each node's kubelet stats/summary endpoint. Returns uint64 values suitable for direct ingestion into a TSDB.
type ExecProcessParams ¶
type ExecProcessParams struct {
Token string
Namespace string
PodName string
ContainerName string
Command []string
Stdin io.Reader // bound to the exec process stdin
Stdout io.Writer // receives exec process output
ResizeQueue remotecommand.TerminalSizeQueue // nil if resize not needed
}
ExecProcessParams holds parameters for StartExecProcess.
type KubeClient ¶
type KubeClient struct {
// contains filtered or unexported fields
}
KubeClient can be used to query the Kubernetes API
func NewKubeClient ¶
func NewKubeClient() (*KubeClient, error)
NewKubeClient returns a pointer to a new KubeClient instance
func (*KubeClient) ApplyManagedField ¶
func (kcl *KubeClient) ApplyManagedField(ctx context.Context, target ResourceTarget, fieldPath []string, values map[string][]byte, owns func(key string) bool, exclusive, createIfMissing bool) error
ApplyManagedField sets every key in values at fieldPath, snapshotting prior values for restore. owns scopes only the destructive cleanup: exclusive mode wipes owned keys the caller dropped, additive mode prunes keys it previously set, and restore reverts owned keys. Callers must own every key they set, or it will leak on detach. Creates a missing target iff createIfMissing. An empty fieldPath targets the object root. Idempotent.
func (*KubeClient) CRDResourceReady ¶
func (kcl *KubeClient) CRDResourceReady(ctx context.Context, crdName string) bool
CRDResourceReady checks whether a CRD's API endpoint is registered in discovery by attempting to list resources of that type. crdName is the full CRD name (e.g. "k8spspflexvolumes.constraints.gatekeeper.sh").
func (*KubeClient) DeleteResource ¶
func (kcl *KubeClient) DeleteResource(ctx context.Context, apiVersion, kind, name, namespace string) error
DeleteResource deletes a Kubernetes resource group/version/resource and name/namespace
func (*KubeClient) GetDaemonSetStatus ¶
func (kcl *KubeClient) GetDaemonSetStatus(namespace string, name string) ResourceStatus
func (*KubeClient) GetDeploymentStatus ¶
func (kcl *KubeClient) GetDeploymentStatus(namespace string, name string) ResourceStatus
func (*KubeClient) GetPodStatus ¶
func (kcl *KubeClient) GetPodStatus(namespace string, name string) ResourceStatus
func (*KubeClient) GetResource ¶
func (kcl *KubeClient) GetResource(ctx context.Context, apiVersion, kind, name, namespace string) (any, error)
GetResource retrieves a Kubernetes resource by group/version/resource and name/namespace
func (*KubeClient) GetStatefulSetStatus ¶
func (kcl *KubeClient) GetStatefulSetStatus(namespace string, name string) ResourceStatus
func (*KubeClient) ListNamespaces ¶
ListNamespaces returns the resourceVersion of every namespace on the cluster, keyed by name. resourceVersion changes on every update to a namespace object (not just create/delete, e.g. a label edit), which makes this suitable for cheap drift detection: callers can diff two snapshots to tell whether anything about the namespace topology changed since the last check, without re-fetching or re-rendering anything themselves.
func (*KubeClient) PatchResource ¶
func (kcl *KubeClient) PatchResource(ctx context.Context, apiVersion, kind, name, namespace, patch string) error
PatchResource applies a JSON merge patch to a Kubernetes resource
func (*KubeClient) RestoreManagedField ¶
func (kcl *KubeClient) RestoreManagedField(ctx context.Context, target ResourceTarget, fieldPath []string, owns func(key string) bool) error
RestoreManagedField reverts the caller's owned keys at fieldPath to their prior values (keys absent before the first apply are deleted), removes just those keys from the snapshot, and drops the annotation only when it becomes empty. No-op if the resource/annotation is absent.
func (*KubeClient) StartExecProcess ¶
func (kcl *KubeClient) StartExecProcess(params ExecProcessParams) error
StartExecProcess starts an exec process inside a container.
type NodeReadyStatus ¶
NodeReadyStatus holds the readiness state of a single node.
func CollectNodeConditions ¶
func CollectNodeConditions(ctx context.Context, kc *KubeClient) ([]NodeReadyStatus, error)
CollectNodeConditions lists all nodes and returns their Ready condition status.
type ResourceStatus ¶
func AggregateStatuses ¶
func AggregateStatuses(statuses []ResourceStatus) ResourceStatus
type ResourceTarget ¶
type TLSCertInfo ¶
TLSCertInfo is a simplified API server certificate view for metrics export.
func CollectAPIServerCert ¶
func CollectAPIServerCert(ctx context.Context, kc *KubeClient) (*TLSCertInfo, error)
CollectAPIServerCert collects the API server leaf TLS certificate details.
InsecureSkipVerify is intentional: we are inspecting the certificate itself (reading NotAfter), not authenticating the server. The cert must be readable even when it is expired, which is exactly the condition we need to report.