-
Notifications
You must be signed in to change notification settings - Fork 431
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
feat: added dependency support to k8s #1487
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package core | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
log "github.com/sirupsen/logrus" | ||
|
||
"github.com/tailwarden/komiser/models" | ||
"github.com/tailwarden/komiser/providers" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
v1 "k8s.io/api/apps/v1" | ||
) | ||
|
||
func Replicasets(ctx context.Context, client providers.ProviderClient) ([]models.Resource, error) { | ||
resources := make([]models.Resource, 0) | ||
|
||
var config metav1.ListOptions | ||
|
||
for { | ||
res, err := client.K8sClient.Client.AppsV1().ReplicaSets("").List(ctx, config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, rs := range res.Items { | ||
tags := make([]models.Tag, 0) | ||
|
||
for key, value := range rs.Labels { | ||
tags = append(tags, models.Tag{ | ||
Key: key, | ||
Value: value, | ||
}) | ||
} | ||
|
||
if len(rs.OwnerReferences) > 0 { | ||
// we use the owner kind of first owner only as the owner tag | ||
ownerTags := []models.Tag{ | ||
{ | ||
Key: "owner_kind", | ||
Value: rs.OwnerReferences[0].Kind, | ||
}, | ||
{ | ||
Key: "owner_name", | ||
Value: rs.OwnerReferences[0].Name, | ||
}, | ||
} | ||
tags = append(tags, ownerTags...) | ||
} | ||
|
||
|
||
resources = append(resources, models.Resource{ | ||
Provider: "Kubernetes", | ||
Account: client.Name, | ||
Service: "Replicaset", | ||
ResourceId: string(rs.UID), | ||
Name: rs.Name, | ||
Region: rs.Namespace, | ||
Relations: getReplicasetRelation(rs), | ||
Cost: 0, | ||
CreatedAt: rs.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": "Replicaset", | ||
"resources": len(resources), | ||
}).Info("Fetched resources") | ||
return resources, nil | ||
} | ||
|
||
|
||
func getReplicasetRelation(rs v1.ReplicaSet) []models.Link { | ||
|
||
var rel []models.Link | ||
|
||
owners := rs.GetOwnerReferences() | ||
for _, owner := range owners { | ||
rel = append(rel, models.Link{ | ||
ResourceID: string(owner.UID), | ||
Type: owner.Kind, | ||
Name: owner.Name, | ||
Relation: "USES", | ||
}) | ||
} | ||
|
||
return rel | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package utils | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/tailwarden/komiser/providers" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
const ( | ||
SERVICES = "SERVICES" | ||
) | ||
|
||
func K8s_Cache(client *providers.ProviderClient, callerType string) (interface{}, error) { | ||
var response interface{} | ||
var err error | ||
switch callerType { | ||
case SERVICES: | ||
response, err = client.K8sClient.Client.CoreV1().Services("").List(context.Background(), metav1.ListOptions{}) | ||
if err != nil { | ||
return response, err | ||
} | ||
} | ||
|
||
client.Cache.Set(callerType, response, 0) | ||
|
||
return response, nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
explain
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if the resource or the relation was deleted, in the next cycle it will not be updated if this is not added
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cost = EXCLUDED.cost
means that if a conflict occurs, the cost column of the existing row will be updated with the value from the new row being inserted. Same goes for relationsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thats correct and we want to ensure this field gets updated