Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Integration][Kubecost][Opencost] Add support for filters #749

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions integrations/kubecost/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

<!-- towncrier release notes start -->

# Port_Ocean 0.1.55 (2024-06-26)

### Features

- Added support for filter in 'cloud' and 'kubesystem' kind (#55)
- Added separate resource config for 'allocation' v1 and v2 (#55)

### Improvements

- Added separate resource config for 'cloudCost' v1 and v2 (#55)
- Separated 'kubesystem' resource config from 'cloud' (#55)


# Port_Ocean 0.1.54 (2024-06-23)

### Improvements
Expand Down
46 changes: 36 additions & 10 deletions integrations/kubecost/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@

import httpx
from loguru import logger

from integration import KubecostResourceConfig, KubecostSelector
from port_ocean.context.event import event
from port_ocean.utils import http_async_client

from .integration import (
CloudCostV1ResourceConfig,
CloudCostV2ResourceConfig,
KubecostV1ResourceConfig,
KubecostV2ResourceConfig,
)

KUBECOST_API_VERSION_1 = "v1"


Expand All @@ -17,7 +22,15 @@ def __init__(self, kubecost_host: str, kubecost_api_version: str):
self.kubecost_api_version = kubecost_api_version
self.http_client = http_async_client

def generate_params(self, selector: KubecostSelector) -> dict[str, str]:
def generate_params(
self,
selector: (
KubecostV1ResourceConfig.KubecostSelector
| KubecostV2ResourceConfig.KubecostSelector
| CloudCostV2ResourceConfig.CloudCostSelector
| CloudCostV1ResourceConfig.CloudCostSelector
),
) -> dict[str, str]:
params = selector.dict(exclude_unset=True, by_alias=True)
params.pop("query")
return params
Expand All @@ -26,7 +39,15 @@ async def get_kubesystem_cost_allocation(self) -> list[dict[str, Any]]:
"""Calls the Kubecost allocation endpoint to return data for cost and usage
https://docs.kubecost.com/apis/apis-overview/api-allocation
"""
selector = typing.cast(KubecostResourceConfig, event.resource_config).selector
if self.kubecost_api_version == KUBECOST_API_VERSION_1:
selector = typing.cast(
KubecostV1ResourceConfig, event.resource_config
).selector
else:
selector = typing.cast(
KubecostV2ResourceConfig, event.resource_config
).selector

params: dict[str, str] = {
"window": selector.window,
**self.generate_params(selector),
Expand All @@ -52,17 +73,22 @@ async def get_cloud_cost_allocation(self) -> list[dict[str, Any]]:
"""Calls the Kubecost cloud allocation API. It uses the Aggregate endpoint which returns detailed cloud cost data
https://docs.kubecost.com/apis/apis-overview/cloud-cost-api
"""
selector = typing.cast(KubecostResourceConfig, event.resource_config).selector
params: dict[str, str] = {
"window": selector.window,
**self.generate_params(selector),
}

if self.kubecost_api_version == KUBECOST_API_VERSION_1:
selector = typing.cast(
CloudCostV1ResourceConfig, event.resource_config
).selector
url = f"{self.kubecost_host}/model/cloudCost/aggregate"
else:
selector = typing.cast(
CloudCostV2ResourceConfig, event.resource_config
).selector
url = f"{self.kubecost_host}/model/cloudCost"

params: dict[str, str] = {
"window": selector.window,
**self.generate_params(selector),
}

try:
response = await self.http_client.get(
url=url,
Expand Down
Loading
Loading