Skip to content

Commit

Permalink
Merge branch 'tailwarden:develop' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Azanul authored Oct 9, 2023
2 parents 7e865c5 + 52d7ea0 commit 9d1134e
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 10 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ An example configuration entry for configuring a Google Cloud account in the **`
[[gcp]]
name="production"
source="ENVIRONMENT_VARIABLES"
# path=./path/to/credentials/file specify if 'CREDENTIALS_FILE' is used as source
# path="./path/to/credentials/file" specify if 'CREDENTIALS_FILE' is used as source
profile="production"
```
Expand Down
11 changes: 6 additions & 5 deletions docs/configuration/cloud-providers/aws.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ sidebar_label: Amazon Web Services
- IAM policies
- IAM roles
- IAM SAML providers
- Kinesis Data Streams
- KMS keys
- Lambda functions
- RDS clusters
Expand Down Expand Up @@ -97,19 +98,19 @@ Add to config.toml file
[[aws]]
name="sandbox"
source="CREDENTIALS_FILE"
path=./path/to/credentials/file
path="./path/to/credentials/file"
profile="default"
[[aws]]
name="staging"
source="CREDENTIALS_FILE"
path=./path/to/credentials/file
path="./path/to/credentials/file"
profile="staging-account"
[[gcp]]
name="production"
source="ENVIRONMENT_VARIABLES"
# path=./path/to/credentials/file specify if CREDENTIALS_FILE is used
# path="./path/to/credentials/file" specify if CREDENTIALS_FILE is used
profile="production"
[postgres]
Expand All @@ -136,7 +137,7 @@ profile="production"
[[aws]]
name="sandbox"
source="CREDENTIALS_FILE"
path=./path/to/credentials/file
path="./path/to/credentials/file"
profile="default"
```
### Credentials file
Expand Down Expand Up @@ -502,4 +503,4 @@ metadata:
volumes:
- name: test-volume
configMap:
name: aws-configmap
name: aws-configmap
6 changes: 3 additions & 3 deletions docs/getting-started/quickstart.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,19 @@ The reason for this external data persistence is to improve the filtering, sorti
[[aws]]
name="sandbox"
source="CREDENTIALS_FILE"
path=./path/to/credentials/file
path="./path/to/credentials/file"
profile="default"
[[aws]]
name="staging"
source="CREDENTIALS_FILE"
path=./path/to/credentials/file
path="./path/to/credentials/file"
profile="staging-account"
[[gcp]]
name="production"
source="ENVIRONMENT_VARIABLES"
# path=./path/to/credentials/file specify if CREDENTIALS_FILE is used
# path="./path/to/credentials/file" specify if CREDENTIALS_FILE is used
profile="production"
[postgres]
Expand Down
77 changes: 77 additions & 0 deletions providers/k8s/core/namespaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package core

import (
"context"
"time"

log "github.com/sirupsen/logrus"

"github.com/tailwarden/komiser/models"
"github.com/tailwarden/komiser/providers"
oc "github.com/tailwarden/komiser/providers/k8s/opencost"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func Namespaces(ctx context.Context, client providers.ProviderClient) ([]models.Resource, error) {
resources := make([]models.Resource, 0)

var config metav1.ListOptions

opencostEnabled := true
namespacesCost, err := oc.GetOpencostInfo(client.K8sClient.OpencostBaseUrl, "namespace")
if err != nil {
log.Errorf("ERROR: Couldn't get namespaces info from OpenCost: %v", err)
log.Warn("Opencost disabled")
opencostEnabled = false
}

for {
res, err := client.K8sClient.Client.CoreV1().Namespaces().List(ctx, config)
if err != nil {
return nil, err
}

for _, namespace := range res.Items {
tags := make([]models.Tag, 0)

for key, value := range namespace.Labels {
tags = append(tags, models.Tag{
Key: key,
Value: value,
})
}

cost := 0.0
if opencostEnabled {
cost = namespacesCost[namespace.Name].TotalCost
}

resources = append(resources, models.Resource{
Provider: "Kubernetes",
Account: client.Name,
Service: "Namespace",
ResourceId: string(namespace.UID),
Name: namespace.Name,
Region: namespace.Namespace,
Cost: cost,
CreatedAt: namespace.CreationTimestamp.Time,
FetchedAt: time.Now(),
Tags: tags,
})
}

if res.GetContinue() == "" {
break
}

config.Continue = res.GetContinue()
}

log.WithFields(log.Fields{
"provider": "Kubernetes",
"account": client.Name,
"service": "Namespace",
"resources": len(resources),
}).Info("Fetched resources")
return resources, nil
}
16 changes: 15 additions & 1 deletion providers/k8s/core/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

. "github.com/tailwarden/komiser/models"
"github.com/tailwarden/komiser/providers"
oc "github.com/tailwarden/komiser/providers/k8s/opencost"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand All @@ -16,6 +17,14 @@ func Services(ctx context.Context, client providers.ProviderClient) ([]Resource,

var config metav1.ListOptions

opencostEnabled := true
serviceCost, err := oc.GetOpencostInfo(client.K8sClient.OpencostBaseUrl, "service")
if err != nil {
log.Errorf("ERROR: Couldn't get service info from OpenCost: %v", err)
log.Warn("Opencost disabled")
opencostEnabled = false
}

for {
res, err := client.K8sClient.Client.CoreV1().Services("").List(ctx, config)
if err != nil {
Expand All @@ -32,14 +41,19 @@ func Services(ctx context.Context, client providers.ProviderClient) ([]Resource,
})
}

cost := 0.0
if opencostEnabled {
cost = serviceCost[service.Name].TotalCost
}

resources = append(resources, Resource{
Provider: "Kubernetes",
Account: client.Name,
Service: "Service",
ResourceId: string(service.UID),
Name: service.Name,
Region: service.Namespace,
Cost: 0,
Cost: cost,
CreatedAt: service.CreationTimestamp.Time,
FetchedAt: time.Now(),
Tags: tags,
Expand Down
1 change: 1 addition & 0 deletions providers/k8s/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func listOfSupportedServices() []providers.FetchDataFunction {
core.PersistentVolumeClaims,
core.ServiceAccounts,
core.Nodes,
core.Namespaces,
}
}

Expand Down

0 comments on commit 9d1134e

Please sign in to comment.