|
| 1 | +// Package kubernetes collects resource and performance metrics from a Kubernetes |
| 2 | +// cluster via the API server and Kubelet stats endpoints, covering nodes, pods, |
| 3 | +// deployments, services, namespaces, storage, network policies, HPAs, PDBs, |
| 4 | +// workload controllers, events, and pod logs. |
| 5 | +// |
| 6 | +// TelemetryFlow Agent - Community Enterprise Observability Platform |
| 7 | +// Copyright (c) 2024-2026 TelemetryFlow. All rights reserved. |
| 8 | +// Open Source Software built by DevOpsCorner Indonesia. |
| 9 | +// |
| 10 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 11 | +// you may not use this file except in compliance with the License. |
| 12 | +// You may obtain a copy of the License at |
| 13 | +// |
| 14 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 15 | +// |
| 16 | +// Unless required by applicable law or agreed to in writing, software |
| 17 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 18 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 19 | +// See the License for the specific language governing permissions and |
| 20 | +// limitations under the License. |
| 21 | +package kubernetes |
| 22 | + |
| 23 | +import ( |
| 24 | + "context" |
| 25 | + |
| 26 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 27 | + "k8s.io/client-go/kubernetes" |
| 28 | + |
| 29 | + "github.com/telemetryflow/telemetryflow-agent/internal/collector" |
| 30 | +) |
| 31 | + |
| 32 | +// collectNetworkPolicies gathers Kubernetes NetworkPolicy resources. |
| 33 | +func collectNetworkPolicies( |
| 34 | + ctx context.Context, |
| 35 | + cs kubernetes.Interface, |
| 36 | + cfg Config, |
| 37 | + cluster string, |
| 38 | +) ([]collector.Metric, []NetworkPolicyState, error) { |
| 39 | + npList, err := cs.NetworkingV1().NetworkPolicies("").List(ctx, metav1.ListOptions{}) |
| 40 | + if err != nil { |
| 41 | + return nil, nil, err |
| 42 | + } |
| 43 | + |
| 44 | + var metrics []collector.Metric |
| 45 | + var states []NetworkPolicyState |
| 46 | + nsCounts := make(map[string]int) |
| 47 | + |
| 48 | + for i := range npList.Items { |
| 49 | + np := &npList.Items[i] |
| 50 | + |
| 51 | + if !cfg.shouldCollectNamespace(np.Namespace) { |
| 52 | + continue |
| 53 | + } |
| 54 | + |
| 55 | + nsCounts[np.Namespace]++ |
| 56 | + |
| 57 | + // Policy types |
| 58 | + var policyTypes []string |
| 59 | + for _, pt := range np.Spec.PolicyTypes { |
| 60 | + policyTypes = append(policyTypes, string(pt)) |
| 61 | + } |
| 62 | + |
| 63 | + // Pod selector (which pods this policy applies to) |
| 64 | + podSelector := make(map[string]string) |
| 65 | + for k, v := range np.Spec.PodSelector.MatchLabels { |
| 66 | + podSelector[k] = v |
| 67 | + } |
| 68 | + |
| 69 | + // Ingress rules count |
| 70 | + ingressRuleCount := len(np.Spec.Ingress) |
| 71 | + |
| 72 | + // Egress rules count |
| 73 | + egressRuleCount := len(np.Spec.Egress) |
| 74 | + |
| 75 | + // Build ingress rules detail |
| 76 | + var ingressRules []NetworkPolicyRule |
| 77 | + for _, rule := range np.Spec.Ingress { |
| 78 | + var ports []NetworkPolicyPort |
| 79 | + for _, p := range rule.Ports { |
| 80 | + npp := NetworkPolicyPort{ |
| 81 | + Protocol: "TCP", |
| 82 | + } |
| 83 | + if p.Protocol != nil { |
| 84 | + npp.Protocol = string(*p.Protocol) |
| 85 | + } |
| 86 | + if p.Port != nil { |
| 87 | + npp.Port = p.Port.String() |
| 88 | + } |
| 89 | + ports = append(ports, npp) |
| 90 | + } |
| 91 | + var fromPeers []NetworkPolicyPeer |
| 92 | + for _, from := range rule.From { |
| 93 | + peer := NetworkPolicyPeer{} |
| 94 | + if from.PodSelector != nil { |
| 95 | + peer.PodSelector = from.PodSelector.MatchLabels |
| 96 | + } |
| 97 | + if from.NamespaceSelector != nil { |
| 98 | + peer.NamespaceSelector = from.NamespaceSelector.MatchLabels |
| 99 | + } |
| 100 | + if from.IPBlock != nil { |
| 101 | + peer.IPBlock = &NetworkPolicyIPBlock{ |
| 102 | + CIDR: from.IPBlock.CIDR, |
| 103 | + Except: from.IPBlock.Except, |
| 104 | + } |
| 105 | + } |
| 106 | + fromPeers = append(fromPeers, peer) |
| 107 | + } |
| 108 | + ingressRules = append(ingressRules, NetworkPolicyRule{ |
| 109 | + Ports: ports, |
| 110 | + Peers: fromPeers, |
| 111 | + }) |
| 112 | + } |
| 113 | + |
| 114 | + // Build egress rules detail |
| 115 | + var egressRules []NetworkPolicyRule |
| 116 | + for _, rule := range np.Spec.Egress { |
| 117 | + var ports []NetworkPolicyPort |
| 118 | + for _, p := range rule.Ports { |
| 119 | + npp := NetworkPolicyPort{ |
| 120 | + Protocol: "TCP", |
| 121 | + } |
| 122 | + if p.Protocol != nil { |
| 123 | + npp.Protocol = string(*p.Protocol) |
| 124 | + } |
| 125 | + if p.Port != nil { |
| 126 | + npp.Port = p.Port.String() |
| 127 | + } |
| 128 | + ports = append(ports, npp) |
| 129 | + } |
| 130 | + var toPeers []NetworkPolicyPeer |
| 131 | + for _, to := range rule.To { |
| 132 | + peer := NetworkPolicyPeer{} |
| 133 | + if to.PodSelector != nil { |
| 134 | + peer.PodSelector = to.PodSelector.MatchLabels |
| 135 | + } |
| 136 | + if to.NamespaceSelector != nil { |
| 137 | + peer.NamespaceSelector = to.NamespaceSelector.MatchLabels |
| 138 | + } |
| 139 | + if to.IPBlock != nil { |
| 140 | + peer.IPBlock = &NetworkPolicyIPBlock{ |
| 141 | + CIDR: to.IPBlock.CIDR, |
| 142 | + Except: to.IPBlock.Except, |
| 143 | + } |
| 144 | + } |
| 145 | + toPeers = append(toPeers, peer) |
| 146 | + } |
| 147 | + egressRules = append(egressRules, NetworkPolicyRule{ |
| 148 | + Ports: ports, |
| 149 | + Peers: toPeers, |
| 150 | + }) |
| 151 | + } |
| 152 | + |
| 153 | + states = append(states, NetworkPolicyState{ |
| 154 | + Name: np.Name, |
| 155 | + Namespace: np.Namespace, |
| 156 | + PolicyTypes: policyTypes, |
| 157 | + PodSelector: podSelector, |
| 158 | + IngressRuleCount: ingressRuleCount, |
| 159 | + EgressRuleCount: egressRuleCount, |
| 160 | + IngressRules: ingressRules, |
| 161 | + EgressRules: egressRules, |
| 162 | + Labels: np.Labels, |
| 163 | + CreatedAt: np.CreationTimestamp.UnixMilli(), |
| 164 | + }) |
| 165 | + } |
| 166 | + |
| 167 | + // Metrics: network policy count per namespace |
| 168 | + for ns, count := range nsCounts { |
| 169 | + metrics = append(metrics, |
| 170 | + collector.NewMetric("k8s.networkpolicy.count", float64(count), collector.MetricTypeGauge). |
| 171 | + WithLabel("cluster", cluster). |
| 172 | + WithLabel("namespace", ns). |
| 173 | + WithDescription("NetworkPolicy count per namespace"), |
| 174 | + ) |
| 175 | + } |
| 176 | + |
| 177 | + return metrics, states, nil |
| 178 | +} |
0 commit comments