Skip to content

Commit

Permalink
Add relationship summarizers
Browse files Browse the repository at this point in the history
  • Loading branch information
ibuildthecloud committed Jun 22, 2020
1 parent 1923f3f commit eac4a54
Show file tree
Hide file tree
Showing 4 changed files with 279 additions and 2 deletions.
153 changes: 153 additions & 0 deletions pkg/summary/coretypes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package summary

import (
"github.com/rancher/wrangler/pkg/data"
"github.com/rancher/wrangler/pkg/data/convert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func checkPodSelector(obj data.Object, condition []Condition, summary Summary) Summary {
selector := obj.Map("spec", "selector")
if selector == nil {
return summary
}

if !isKind(obj, "ReplicaSet", "apps/", "extension/") &&
!isKind(obj, "DaemonSet", "apps/", "extension/") &&
!isKind(obj, "StatefulSet", "apps/", "extension/") &&
!isKind(obj, "Deployment", "apps/", "extension/") &&
!isKind(obj, "Job", "batch/") &&
!isKind(obj, "Service") {
return summary
}

_, hasMatch := selector["matchLabels"]
if !hasMatch {
_, hasMatch = selector["matchExpressions"]
}
sel := metav1.LabelSelector{}
if hasMatch {
if err := convert.ToObj(selector, &sel); err != nil {
return summary
}
} else {
sel.MatchLabels = map[string]string{}
for k, v := range selector {
sel.MatchLabels[k] = convert.ToString(v)
}
}

t := "creates"
if obj["kind"] == "Service" {
t = "selects"
}

summary.Relationships = append(summary.Relationships, Relationship{
Kind: "Pod",
APIVersion: "v1",
Type: t,
Selector: &sel,
})
return summary
}

func checkPod(obj data.Object, condition []Condition, summary Summary) Summary {
if !isKind(obj, "Pod") {
return summary
}
if obj.String("kind") != "Pod" || obj.String("apiVersion") != "v1" {
return summary
}
summary = checkPodConfigMaps(obj, condition, summary)
summary = checkPodSecrets(obj, condition, summary)
summary = checkPodServiceAccount(obj, condition, summary)
summary = checkPodProjectedVolume(obj, condition, summary)
summary = checkPodPullSecret(obj, condition, summary)
return summary
}

func checkPodPullSecret(obj data.Object, condition []Condition, summary Summary) Summary {
for _, pullSecret := range obj.Slice("imagePullSecrets") {
if name := pullSecret.String("name"); name != "" {
summary.Relationships = append(summary.Relationships, Relationship{
Name: name,
Kind: "Secret",
APIVersion: "v1",
Type: "uses",
})
}
}
return summary
}

func checkPodProjectedVolume(obj data.Object, condition []Condition, summary Summary) Summary {
for _, vol := range obj.Slice("spec", "volumes") {
for _, source := range vol.Slice("projected", "sources") {
if secretName := source.String("secret", "name"); secretName != "" {
summary.Relationships = append(summary.Relationships, Relationship{
Name: secretName,
Kind: "Secret",
APIVersion: "v1",
Type: "uses",
})
}
if configMap := source.String("configMap", "name"); configMap != "" {
summary.Relationships = append(summary.Relationships, Relationship{
Name: configMap,
Kind: "Secret",
APIVersion: "v1",
Type: "uses",
})
}
}
}
return summary
}

func checkPodConfigMaps(obj data.Object, condition []Condition, summary Summary) Summary {
names := map[string]bool{}
for _, vol := range obj.Slice("spec", "volumes") {
name := vol.String("configMap", "name")
if name == "" || names[name] {
continue
}
names[name] = true
summary.Relationships = append(summary.Relationships, Relationship{
Name: name,
Kind: "ConfigMap",
APIVersion: "v1",
Type: "uses",
})
}
return summary
}

func checkPodSecrets(obj data.Object, condition []Condition, summary Summary) Summary {
names := map[string]bool{}
for _, vol := range obj.Slice("spec", "volumes") {
name := vol.String("secret", "secretName")
if name == "" || names[name] {
continue
}
names[name] = true
summary.Relationships = append(summary.Relationships, Relationship{
Name: name,
Kind: "Secret",
APIVersion: "v1",
Type: "uses",
})
}
return summary
}

func checkPodServiceAccount(obj data.Object, condition []Condition, summary Summary) Summary {
saName := obj.String("spec", "serviceAccountName")
summary.Relationships = append(summary.Relationships, Relationship{
Name: saName,
Kind: "ServiceAccount",
APIVersion: "v1",
Type: "uses",
})
return summary

}
96 changes: 96 additions & 0 deletions pkg/summary/summarizers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import (
"github.com/rancher/wrangler/pkg/data"
"github.com/rancher/wrangler/pkg/data/convert"
"github.com/rancher/wrangler/pkg/kv"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

const (
kindSep = ", Kind="
)

var (
Expand Down Expand Up @@ -98,7 +103,33 @@ func init() {
checkInitializing,
checkRemoving,
checkLoadBalancer,
checkPod,
checkPodSelector,
checkOwner,
checkApplyOwned,
}
}

func checkOwner(obj data.Object, conditions []Condition, summary Summary) Summary {
ustr := &unstructured.Unstructured{
Object: obj,
}
for _, ownerref := range ustr.GetOwnerReferences() {
rel := Relationship{
Name: ownerref.Name,
Kind: ownerref.Kind,
APIVersion: ownerref.APIVersion,
Type: "owner",
Inbound: true,
}
if ownerref.Controller != nil && *ownerref.Controller {
rel.ControlledBy = true
}

summary.Relationships = append(summary.Relationships, rel)
}

return summary
}

func checkErrors(_ data.Object, conditions []Condition, summary Summary) Summary {
Expand Down Expand Up @@ -257,3 +288,68 @@ func checkLoadBalancer(obj data.Object, _ []Condition, summary Summary) Summary

return summary
}

func isKind(obj data.Object, kind string, apiGroups ...string) bool {
if obj.String("kind") != kind {
return false
}

if len(apiGroups) == 0 {
return obj.String("apiVersion") == "v1"
}

if len(apiGroups) == 0 {
apiGroups = []string{""}
}

for _, group := range apiGroups {
switch {
case group == "":
if obj.String("apiVersion") == "v1" {
return true
}
case group[len(group)-1] == '/':
if strings.HasPrefix(obj.String("apiVersion"), group) {
return true
}
default:
if obj.String("apiVersion") != group {
return true
}
}
}

return false
}

func checkApplyOwned(obj data.Object, conditions []Condition, summary Summary) Summary {
if len(obj.Slice("metadata", "ownerReferences")) > 0 {
return summary
}

annotations := obj.Map("metadata", "annotations")
gvkString := convert.ToString(annotations["objectset.rio.cattle.io/owner-gvk"])
i := strings.Index(gvkString, kindSep)
if i <= 0 {
return summary
}

name := convert.ToString(annotations["objectset.rio.cattle.io/owner-name"])
namespace := convert.ToString(annotations["objectset.rio.cattle.io/owner-namespace"])

apiVersion := gvkString[:i]
kind := gvkString[i+len(kindSep):]

rel := Relationship{
Name: name,
Namespace: namespace,
Kind: kind,
APIVersion: apiVersion,
Type: "applies",
Inbound: true,
}

summary.Relationships = append(summary.Relationships, rel)

return summary
}
17 changes: 15 additions & 2 deletions pkg/summary/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,30 @@ package summary
import (
"strings"

"k8s.io/apimachinery/pkg/runtime"

"github.com/rancher/wrangler/pkg/data"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
)

type Summary struct {
State string
Error bool
Transitioning bool
Message []string
Attributes map[string]interface{}
Relationships []Relationship
}

type Relationship struct {
Name string
Namespace string
ControlledBy bool
Kind string
APIVersion string
Inbound bool
Type string
Selector *metav1.LabelSelector
}

func (s Summary) String() string {
Expand Down
15 changes: 15 additions & 0 deletions pkg/yaml/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,21 @@ func CleanObjectForExport(obj runtime.Object) (runtime.Object, error) {
return unstr, nil
}

func CleanAnnotationsForExport(annotations map[string]string) map[string]string {
result := make(map[string]string, len(annotations))

outer:
for k := range annotations {
for _, prefix := range cleanPrefix {
if strings.HasPrefix(k, prefix) {
continue outer
}
}
result[k] = annotations[k]
}
return result
}

func cleanMap(annoLabels map[string]string) {
for k := range annoLabels {
for _, prefix := range cleanPrefix {
Expand Down

0 comments on commit eac4a54

Please sign in to comment.