| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
- package kubemodel
- import (
- "testing"
- "time"
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/require"
- "github.com/opencost/opencost/core/pkg/model/kubemodel"
- "github.com/opencost/opencost/core/pkg/source"
- )
- func TestComputePods(t *testing.T) {
- start := time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC)
- end := start.Add(time.Hour)
- tests := []struct {
- name string
- overrides map[string]any
- want map[string]*kubemodel.Pod
- }{
- {
- name: "no data returns empty pod map",
- overrides: map[string]any{},
- want: map[string]*kubemodel.Pod{},
- },
- {
- name: "basic pod info and uptime",
- overrides: map[string]any{
- source.QueryPodInfo: []*source.PodInfoResult{
- {UID: "pod-1", Pod: "my-pod", NamespaceUID: "ns-1"},
- },
- source.QueryPodUptime: []*source.UptimeResult{
- {UID: "pod-1", First: start, Last: end},
- },
- },
- want: map[string]*kubemodel.Pod{
- "pod-1": {
- UID: "pod-1",
- Name: "my-pod",
- NamespaceUID: "ns-1",
- Start: start,
- End: end,
- },
- },
- },
- {
- name: "pod without uptime is not registered",
- overrides: map[string]any{
- source.QueryPodInfo: []*source.PodInfoResult{
- {UID: "pod-1", Pod: "my-pod", NamespaceUID: "ns-1"},
- },
- },
- want: map[string]*kubemodel.Pod{},
- },
- {
- name: "pod without namespace uid is not registered",
- overrides: map[string]any{
- source.QueryPodInfo: []*source.PodInfoResult{
- {UID: "pod-1", Pod: "my-pod"},
- },
- source.QueryPodUptime: []*source.UptimeResult{
- {UID: "pod-1", First: start, Last: end},
- },
- },
- want: map[string]*kubemodel.Pod{},
- },
- {
- name: "pod with node uid",
- overrides: map[string]any{
- source.QueryPodInfo: []*source.PodInfoResult{
- {UID: "pod-1", Pod: "my-pod", NamespaceUID: "ns-1", NodeUID: "node-1"},
- },
- source.QueryPodUptime: []*source.UptimeResult{
- {UID: "pod-1", First: start, Last: end},
- },
- },
- want: map[string]*kubemodel.Pod{
- "pod-1": {
- UID: "pod-1",
- Name: "my-pod",
- NamespaceUID: "ns-1",
- NodeUID: "node-1",
- Start: start,
- End: end,
- },
- },
- },
- {
- name: "pod owners are attached",
- overrides: map[string]any{
- source.QueryPodInfo: []*source.PodInfoResult{
- {UID: "pod-1", Pod: "my-pod", NamespaceUID: "ns-1"},
- },
- source.QueryPodUptime: []*source.UptimeResult{
- {UID: "pod-1", First: start, Last: end},
- },
- source.QueryPodOwners: []*source.OwnerResult{
- {UID: "pod-1", OwnerUID: "rs-1", OwnerKind: "ReplicaSet", Controller: true},
- },
- },
- want: map[string]*kubemodel.Pod{
- "pod-1": {
- UID: "pod-1",
- Name: "my-pod",
- NamespaceUID: "ns-1",
- Start: start,
- End: end,
- Owners: []kubemodel.Owner{
- {UID: "rs-1", Kind: kubemodel.OwnerKindReplicaSet, Controller: true},
- },
- },
- },
- },
- {
- name: "pod pvc volumes are attached",
- overrides: map[string]any{
- source.QueryPodInfo: []*source.PodInfoResult{
- {UID: "pod-1", Pod: "my-pod", NamespaceUID: "ns-1"},
- },
- source.QueryPodUptime: []*source.UptimeResult{
- {UID: "pod-1", First: start, Last: end},
- },
- source.QueryPodPVCVolumes: []*source.PodPVCVolumeResult{
- {UID: "pod-1", PVCUID: "pvc-1", PodVolumeName: "data"},
- },
- },
- want: map[string]*kubemodel.Pod{
- "pod-1": {
- UID: "pod-1",
- Name: "my-pod",
- NamespaceUID: "ns-1",
- Start: start,
- End: end,
- PVCVolumes: []kubemodel.PodPVCVolume{
- {Name: "data", PersistentVolumeClaimUID: "pvc-1"},
- },
- },
- },
- },
- {
- name: "pod labels and annotations are attached",
- overrides: map[string]any{
- source.QueryPodInfo: []*source.PodInfoResult{
- {UID: "pod-1", Pod: "my-pod", NamespaceUID: "ns-1"},
- },
- source.QueryPodUptime: []*source.UptimeResult{
- {UID: "pod-1", First: start, Last: end},
- },
- source.QueryPodLabels: []*source.PodLabelsResult{
- {UID: "pod-1", Labels: map[string]string{"app": "web"}},
- },
- source.QueryPodAnnotations: []*source.PodAnnotationsResult{
- {UID: "pod-1", Annotations: map[string]string{"team": "platform"}},
- },
- },
- want: map[string]*kubemodel.Pod{
- "pod-1": {
- UID: "pod-1",
- Name: "my-pod",
- NamespaceUID: "ns-1",
- Start: start,
- End: end,
- Labels: map[string]string{"app": "web"},
- Annotations: map[string]string{"team": "platform"},
- },
- },
- },
- {
- name: "uptime for unknown pod is ignored",
- overrides: map[string]any{
- source.QueryPodInfo: []*source.PodInfoResult{
- {UID: "pod-1", Pod: "my-pod", NamespaceUID: "ns-1"},
- },
- source.QueryPodUptime: []*source.UptimeResult{
- {UID: "pod-1", First: start, Last: end},
- {UID: "unknown-pod", First: start, Last: end},
- },
- },
- want: map[string]*kubemodel.Pod{
- "pod-1": {
- UID: "pod-1",
- Name: "my-pod",
- NamespaceUID: "ns-1",
- Start: start,
- End: end,
- },
- },
- },
- {
- name: "network egress: internet, cross-region, cross-zone traffic types",
- overrides: map[string]any{
- source.QueryPodInfo: []*source.PodInfoResult{
- {UID: "pod-1", Pod: "my-pod", NamespaceUID: "ns-1"},
- },
- source.QueryPodUptime: []*source.UptimeResult{
- {UID: "pod-1", First: start, Last: end},
- },
- source.QueryPodNetworkEgressBytes: []*source.PodNetworkBytesResult{
- {UID: "pod-1", Internet: true, Service: "svc-a", Value: 100},
- {UID: "pod-1", Internet: false, SameRegion: false, Service: "svc-b", Value: 200},
- {UID: "pod-1", Internet: false, SameRegion: true, SameZone: false, Service: "svc-c", Value: 300},
- {UID: "pod-1", Internet: false, SameRegion: true, SameZone: true, Service: "svc-d", Value: 400}, // no traffic type → skipped
- {UID: "pod-1", Internet: true, Service: "svc-e", Value: 0}, // zero bytes → skipped
- {UID: "unknown-pod", Internet: true, Service: "svc-f", Value: 500}, // unknown pod → skipped
- },
- },
- want: map[string]*kubemodel.Pod{
- "pod-1": {
- UID: "pod-1",
- Name: "my-pod",
- NamespaceUID: "ns-1",
- Start: start,
- End: end,
- NetworkTrafficDetails: []kubemodel.NetworkTrafficDetail{
- {PodUID: "pod-1", TrafficDirection: kubemodel.TrafficDirectionEgress, TrafficType: kubemodel.TrafficTypeInternet, Endpoint: "svc-a", Bytes: 100},
- {PodUID: "pod-1", TrafficDirection: kubemodel.TrafficDirectionEgress, TrafficType: kubemodel.TrafficTypeCrossRegion, Endpoint: "svc-b", Bytes: 200},
- {PodUID: "pod-1", TrafficDirection: kubemodel.TrafficDirectionEgress, TrafficType: kubemodel.TrafficTypeCrossZone, Endpoint: "svc-c", Bytes: 300},
- },
- },
- },
- },
- {
- name: "unknown uid in supplementary data is ignored",
- overrides: map[string]any{
- source.QueryPodInfo: []*source.PodInfoResult{
- {UID: "pod-1", Pod: "my-pod", NamespaceUID: "ns-1"},
- },
- source.QueryPodUptime: []*source.UptimeResult{
- {UID: "pod-1", First: start, Last: end},
- },
- // all of these carry an unknown UID — hit the !ok warn paths
- source.QueryPodOwners: []*source.OwnerResult{
- {UID: "unknown-pod", OwnerUID: "rs-1", OwnerKind: "ReplicaSet", Controller: true},
- },
- source.QueryPodPVCVolumes: []*source.PodPVCVolumeResult{
- {UID: "unknown-pod", PVCUID: "pvc-1", PodVolumeName: "data"},
- },
- source.QueryPodLabels: []*source.PodLabelsResult{
- {UID: "unknown-pod", Labels: map[string]string{"app": "web"}},
- },
- source.QueryPodAnnotations: []*source.PodAnnotationsResult{
- {UID: "unknown-pod", Annotations: map[string]string{"team": "platform"}},
- },
- // same-zone+same-region ingress: triggers !ok continue in ingress loop
- source.QueryPodNetworkIngressBytes: []*source.PodNetworkBytesResult{
- {UID: "pod-1", Internet: false, SameRegion: true, SameZone: true, Value: 100},
- },
- },
- want: map[string]*kubemodel.Pod{
- "pod-1": {
- UID: "pod-1",
- Name: "my-pod",
- NamespaceUID: "ns-1",
- Start: start,
- End: end,
- },
- },
- },
- {
- name: "network ingress traffic is recorded",
- overrides: map[string]any{
- source.QueryPodInfo: []*source.PodInfoResult{
- {UID: "pod-1", Pod: "my-pod", NamespaceUID: "ns-1"},
- },
- source.QueryPodUptime: []*source.UptimeResult{
- {UID: "pod-1", First: start, Last: end},
- },
- source.QueryPodNetworkIngressBytes: []*source.PodNetworkBytesResult{
- {UID: "pod-1", Internet: true, NatGateway: true, Service: "svc-a", Value: 512},
- {UID: "pod-1", Internet: false, SameRegion: false, Service: "svc-b", Value: 256},
- },
- },
- want: map[string]*kubemodel.Pod{
- "pod-1": {
- UID: "pod-1",
- Name: "my-pod",
- NamespaceUID: "ns-1",
- Start: start,
- End: end,
- NetworkTrafficDetails: []kubemodel.NetworkTrafficDetail{
- {PodUID: "pod-1", TrafficDirection: kubemodel.TrafficDirectionIngress, TrafficType: kubemodel.TrafficTypeInternet, IsNatGateway: true, Endpoint: "svc-a", Bytes: 512},
- {PodUID: "pod-1", TrafficDirection: kubemodel.TrafficDirectionIngress, TrafficType: kubemodel.TrafficTypeCrossRegion, Endpoint: "svc-b", Bytes: 256},
- },
- },
- },
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- ds := source.NewMockOpenCostDataSource()
- ds.ResolutionValue = 5 * time.Minute
- seedCluster(ds, start, end)
- for method, result := range tt.overrides {
- ds.Querier.SetOverride(method, result)
- }
- km, err := NewKubeModel(testClusterUID, false, ds)
- require.NoError(t, err)
- kms, err := km.ComputeKubeModelSet(start, end)
- require.NoError(t, err)
- assert.Equal(t, tt.want, kms.Pods)
- })
- }
- }
|