| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414 |
- package costmodel
- import (
- "fmt"
- "github.com/opencost/opencost/pkg/env"
- "github.com/opencost/opencost/pkg/prom"
- )
- type containerKey struct {
- Cluster string
- Namespace string
- Pod string
- Container string
- }
- func (k containerKey) String() string {
- return fmt.Sprintf("%s/%s/%s/%s", k.Cluster, k.Namespace, k.Pod, k.Container)
- }
- func newContainerKey(cluster, namespace, pod, container string) containerKey {
- return containerKey{
- Cluster: cluster,
- Namespace: namespace,
- Pod: pod,
- Container: container,
- }
- }
- // resultContainerKey converts a Prometheus query result to a containerKey by
- // looking up values associated with the given label names. For example,
- // passing "cluster_id" for clusterLabel will use the value of the label
- // "cluster_id" as the containerKey's Cluster field. If a given field does not
- // exist on the result, an error is returned. (The only exception to that is
- // clusterLabel, which we expect may not exist, but has a default value.)
- func resultContainerKey(res *prom.QueryResult, clusterLabel, namespaceLabel, podLabel, containerLabel string) (containerKey, error) {
- key := containerKey{}
- cluster, err := res.GetString(clusterLabel)
- if err != nil {
- cluster = env.GetClusterID()
- }
- key.Cluster = cluster
- namespace, err := res.GetString(namespaceLabel)
- if err != nil {
- return key, err
- }
- key.Namespace = namespace
- pod, err := res.GetString(podLabel)
- if err != nil {
- return key, err
- }
- key.Pod = pod
- container, err := res.GetString(containerLabel)
- if err != nil {
- return key, err
- }
- key.Container = container
- return key, nil
- }
- type podKey struct {
- namespaceKey
- Pod string
- }
- func (k podKey) String() string {
- return fmt.Sprintf("%s/%s/%s", k.Cluster, k.Namespace, k.Pod)
- }
- func newPodKey(cluster, namespace, pod string) podKey {
- return podKey{
- namespaceKey: namespaceKey{
- Cluster: cluster,
- Namespace: namespace,
- },
- Pod: pod,
- }
- }
- // resultPodKey converts a Prometheus query result to a podKey by looking
- // up values associated with the given label names. For example, passing
- // "cluster_id" for clusterLabel will use the value of the label "cluster_id"
- // as the podKey's Cluster field. If a given field does not exist on the
- // result, an error is returned. (The only exception to that is clusterLabel,
- // which we expect may not exist, but has a default value.)
- func resultPodKey(res *prom.QueryResult, clusterLabel, namespaceLabel string) (podKey, error) {
- key := podKey{}
- cluster, err := res.GetString(clusterLabel)
- if err != nil {
- cluster = env.GetClusterID()
- }
- key.Cluster = cluster
- namespace, err := res.GetString(namespaceLabel)
- if err != nil {
- return key, err
- }
- key.Namespace = namespace
- pod, err := res.GetString("pod")
- if pod == "" || err != nil {
- pod, err = res.GetString("pod_name")
- if err != nil {
- return key, err
- }
- }
- key.Pod = pod
- return key, nil
- }
- type namespaceKey struct {
- Cluster string
- Namespace string
- }
- func (k namespaceKey) String() string {
- return fmt.Sprintf("%s/%s", k.Cluster, k.Namespace)
- }
- func newNamespaceKey(cluster, namespace string) namespaceKey {
- return namespaceKey{
- Cluster: cluster,
- Namespace: namespace,
- }
- }
- // resultNamespaceKey converts a Prometheus query result to a namespaceKey by
- // looking up values associated with the given label names. For example,
- // passing "cluster_id" for clusterLabel will use the value of the label
- // "cluster_id" as the namespaceKey's Cluster field. If a given field does not
- // exist on the result, an error is returned. (The only exception to that is
- // clusterLabel, which we expect may not exist, but has a default value.)
- func resultNamespaceKey(res *prom.QueryResult, clusterLabel, namespaceLabel string) (namespaceKey, error) {
- key := namespaceKey{}
- cluster, err := res.GetString(clusterLabel)
- if err != nil {
- cluster = env.GetClusterID()
- }
- key.Cluster = cluster
- namespace, err := res.GetString(namespaceLabel)
- if err != nil {
- return key, err
- }
- key.Namespace = namespace
- return key, nil
- }
- type controllerKey struct {
- Cluster string
- Namespace string
- ControllerKind string
- Controller string
- }
- func (k controllerKey) String() string {
- return fmt.Sprintf("%s/%s/%s/%s", k.Cluster, k.Namespace, k.ControllerKind, k.Controller)
- }
- func newControllerKey(cluster, namespace, controllerKind, controller string) controllerKey {
- return controllerKey{
- Cluster: cluster,
- Namespace: namespace,
- ControllerKind: controllerKind,
- Controller: controller,
- }
- }
- // resultControllerKey converts a Prometheus query result to a controllerKey by
- // looking up values associated with the given label names. For example,
- // passing "cluster_id" for clusterLabel will use the value of the label
- // "cluster_id" as the controllerKey's Cluster field. If a given field does not
- // exist on the result, an error is returned. (The only exception to that is
- // clusterLabel, which we expect may not exist, but has a default value.)
- func resultControllerKey(controllerKind string, res *prom.QueryResult, clusterLabel, namespaceLabel, controllerLabel string) (controllerKey, error) {
- key := controllerKey{}
- cluster, err := res.GetString(clusterLabel)
- if err != nil {
- cluster = env.GetClusterID()
- }
- key.Cluster = cluster
- namespace, err := res.GetString(namespaceLabel)
- if err != nil {
- return key, err
- }
- key.Namespace = namespace
- controller, err := res.GetString(controllerLabel)
- if err != nil {
- return key, err
- }
- key.Controller = controller
- key.ControllerKind = controllerKind
- return key, nil
- }
- // resultDeploymentKey creates a controllerKey for a Deployment.
- // (See resultControllerKey for more.)
- func resultDeploymentKey(res *prom.QueryResult, clusterLabel, namespaceLabel, controllerLabel string) (controllerKey, error) {
- return resultControllerKey("deployment", res, clusterLabel, namespaceLabel, controllerLabel)
- }
- // resultStatefulSetKey creates a controllerKey for a StatefulSet.
- // (See resultControllerKey for more.)
- func resultStatefulSetKey(res *prom.QueryResult, clusterLabel, namespaceLabel, controllerLabel string) (controllerKey, error) {
- return resultControllerKey("statefulset", res, clusterLabel, namespaceLabel, controllerLabel)
- }
- // resultDaemonSetKey creates a controllerKey for a DaemonSet.
- // (See resultControllerKey for more.)
- func resultDaemonSetKey(res *prom.QueryResult, clusterLabel, namespaceLabel, controllerLabel string) (controllerKey, error) {
- return resultControllerKey("daemonset", res, clusterLabel, namespaceLabel, controllerLabel)
- }
- // resultJobKey creates a controllerKey for a Job.
- // (See resultControllerKey for more.)
- func resultJobKey(res *prom.QueryResult, clusterLabel, namespaceLabel, controllerLabel string) (controllerKey, error) {
- return resultControllerKey("job", res, clusterLabel, namespaceLabel, controllerLabel)
- }
- // resultReplicaSetKey creates a controllerKey for a Job.
- // (See resultControllerKey for more.)
- func resultReplicaSetKey(res *prom.QueryResult, clusterLabel, namespaceLabel, controllerLabel string) (controllerKey, error) {
- return resultControllerKey("replicaset", res, clusterLabel, namespaceLabel, controllerLabel)
- }
- type serviceKey struct {
- Cluster string
- Namespace string
- Service string
- }
- func (k serviceKey) String() string {
- return fmt.Sprintf("%s/%s/%s", k.Cluster, k.Namespace, k.Service)
- }
- func newServiceKey(cluster, namespace, service string) serviceKey {
- return serviceKey{
- Cluster: cluster,
- Namespace: namespace,
- Service: service,
- }
- }
- // resultServiceKey converts a Prometheus query result to a serviceKey by
- // looking up values associated with the given label names. For example,
- // passing "cluster_id" for clusterLabel will use the value of the label
- // "cluster_id" as the serviceKey's Cluster field. If a given field does not
- // exist on the result, an error is returned. (The only exception to that is
- // clusterLabel, which we expect may not exist, but has a default value.)
- func resultServiceKey(res *prom.QueryResult, clusterLabel, namespaceLabel, serviceLabel string) (serviceKey, error) {
- key := serviceKey{}
- cluster, err := res.GetString(clusterLabel)
- if err != nil {
- cluster = env.GetClusterID()
- }
- key.Cluster = cluster
- namespace, err := res.GetString(namespaceLabel)
- if err != nil {
- return key, err
- }
- key.Namespace = namespace
- service, err := res.GetString(serviceLabel)
- if err != nil {
- return key, err
- }
- key.Service = service
- return key, nil
- }
- type nodeKey struct {
- Cluster string
- Node string
- }
- func (k nodeKey) String() string {
- return fmt.Sprintf("%s/%s", k.Cluster, k.Node)
- }
- func newNodeKey(cluster, node string) nodeKey {
- return nodeKey{
- Cluster: cluster,
- Node: node,
- }
- }
- // resultNodeKey converts a Prometheus query result to a nodeKey by
- // looking up values associated with the given label names. For example,
- // passing "cluster_id" for clusterLabel will use the value of the label
- // "cluster_id" as the nodeKey's Cluster field. If a given field does not
- // exist on the result, an error is returned. (The only exception to that is
- // clusterLabel, which we expect may not exist, but has a default value.)
- func resultNodeKey(res *prom.QueryResult, clusterLabel, nodeLabel string) (nodeKey, error) {
- key := nodeKey{}
- cluster, err := res.GetString(clusterLabel)
- if err != nil {
- cluster = env.GetClusterID()
- }
- key.Cluster = cluster
- node, err := res.GetString(nodeLabel)
- if err != nil {
- return key, err
- }
- key.Node = node
- return key, nil
- }
- type pvcKey struct {
- Cluster string
- Namespace string
- PersistentVolumeClaim string
- }
- func (k pvcKey) String() string {
- return fmt.Sprintf("%s/%s/%s", k.Cluster, k.Namespace, k.PersistentVolumeClaim)
- }
- func newPVCKey(cluster, namespace, persistentVolumeClaim string) pvcKey {
- return pvcKey{
- Cluster: cluster,
- Namespace: namespace,
- PersistentVolumeClaim: persistentVolumeClaim,
- }
- }
- // resultPVCKey converts a Prometheus query result to a pvcKey by
- // looking up values associated with the given label names. For example,
- // passing "cluster_id" for clusterLabel will use the value of the label
- // "cluster_id" as the pvcKey's Cluster field. If a given field does not
- // exist on the result, an error is returned. (The only exception to that is
- // clusterLabel, which we expect may not exist, but has a default value.)
- func resultPVCKey(res *prom.QueryResult, clusterLabel, namespaceLabel, pvcLabel string) (pvcKey, error) {
- key := pvcKey{}
- cluster, err := res.GetString(clusterLabel)
- if err != nil {
- cluster = env.GetClusterID()
- }
- key.Cluster = cluster
- namespace, err := res.GetString(namespaceLabel)
- if err != nil {
- return key, err
- }
- key.Namespace = namespace
- pvc, err := res.GetString(pvcLabel)
- if err != nil {
- return key, err
- }
- key.PersistentVolumeClaim = pvc
- return key, nil
- }
- type pvKey struct {
- Cluster string
- PersistentVolume string
- }
- func (k pvKey) String() string {
- return fmt.Sprintf("%s/%s", k.Cluster, k.PersistentVolume)
- }
- func newPVKey(cluster, persistentVolume string) pvKey {
- return pvKey{
- Cluster: cluster,
- PersistentVolume: persistentVolume,
- }
- }
- // resultPVKey converts a Prometheus query result to a pvKey by
- // looking up values associated with the given label names. For example,
- // passing "cluster_id" for clusterLabel will use the value of the label
- // "cluster_id" as the pvKey's Cluster field. If a given field does not
- // exist on the result, an error is returned. (The only exception to that is
- // clusterLabel, which we expect may not exist, but has a default value.)
- func resultPVKey(res *prom.QueryResult, clusterLabel, persistentVolumeLabel string) (pvKey, error) {
- key := pvKey{}
- cluster, err := res.GetString(clusterLabel)
- if err != nil {
- cluster = env.GetClusterID()
- }
- key.Cluster = cluster
- persistentVolume, err := res.GetString(persistentVolumeLabel)
- if err != nil {
- return key, err
- }
- key.PersistentVolume = persistentVolume
- return key, nil
- }
|