diff --git a/deployment.go b/deployment.go index b444924..145ffe9 100644 --- a/deployment.go +++ b/deployment.go @@ -6,9 +6,9 @@ import ( type deployment struct { Id string - Metadata *metadata - Owner *resource - RootOwner *resource + Metadata metadata + Owner resource + RootOwner resource } type deploymentResolver struct { diff --git a/gobuild.sh b/gobuild.sh index d30b9e6..1da9ce1 100644 --- a/gobuild.sh +++ b/gobuild.sh @@ -2,8 +2,8 @@ go get -u github.com/kardianos/govendor $GOPATH/bin/govendor sync -CGO_ENABLED=0 go build -a -v -ldflags '-s' -#go build +#CGO_ENABLED=0 go build -a -v -ldflags '-s' +go build # # Run unit tests and generate code coverage reports -- an html one # # for local viewing and a cobertura one for jenkins builds. #go get -u github.com/t-yuki/gocover-cobertura diff --git a/metadata.go b/metadata.go index ec02b09..78d9f7d 100644 --- a/metadata.go +++ b/metadata.go @@ -18,7 +18,7 @@ type metadata struct { type metadataResolver struct { ctx context.Context - m *metadata + m metadata } func mapToMetadata(jsonObj map[string]interface{}) metadata { @@ -72,7 +72,7 @@ func (r *metadataResolver) OwnerReferences() []*resourceResolver { var ownerResolvers []*resourceResolver for _, owner := range *r.m.OwnerReferences { own := owner - ownerResolvers = append(ownerResolvers, &resourceResolver{r.ctx, &own}) + ownerResolvers = append(ownerResolvers, &resourceResolver{r.ctx, own}) } return ownerResolvers } diff --git a/pods.go b/pods.go index 332ca02..ab3fd82 100644 --- a/pods.go +++ b/pods.go @@ -6,9 +6,9 @@ import ( type pod struct { Id string - Metadata *metadata - Owner *resource - RootOwner *resource + Metadata metadata + Owner resource + RootOwner resource } type podResolver struct { @@ -20,7 +20,7 @@ func mapToPod(jsonObj map[string]interface{}) pod { owner, rootOwner := getOwners(jsonObj) meta := mapToMetadata(mapItem(jsonObj, "metadata")) return pod{(mapItem(jsonObj, "metadata")["uid"]).(string), - &meta, + meta, owner, rootOwner} } @@ -31,16 +31,13 @@ func (r *podResolver) Id() string { func (r *podResolver) Metadata() *metadataResolver { meta := r.p.Metadata - if meta == nil { - meta = getPodMetadata(r.p) - } return &metadataResolver{r.ctx, meta} } func (r *podResolver) Owner() *resourceResolver { owner := r.p.Owner if owner == nil { - owner = getPodOwner(r.p.Id) + return &resourceResolver{r.ctx, getPodOwner(r.ctx, r.p.Id)} } return &resourceResolver{r.ctx, owner} } @@ -48,40 +45,40 @@ func (r *podResolver) Owner() *resourceResolver { func (r *podResolver) RootOwner() *resourceResolver { rootOwner := r.p.RootOwner if rootOwner == nil { - rootOwner = getPodRootOwner(r.p.Id) + return &resourceResolver{r.ctx, getPodRootOwner(r.ctx, r.p.Id)} } return &resourceResolver{r.ctx, rootOwner} } -func getPodMetadata(p *pod) *metadata { +func getPodMetadata(p *pod) metadata { meta := mapToMetadata(mapItem(getK8sResource(p.Id), "Metadata")) - return &meta + return meta } -func getPodOwner(pid string) *resource { +func getPodOwner(ctx context.Context, pid string) resource { if podval := getK8sResource(pid); podval != nil { if orefs := podval["OwnerReferences"]; orefs != nil { orefArray := orefs.([]map[string]interface{}) if len(orefArray) > 0 { if res := getK8sResource( orefArray[0]["uid"].(string)); res != nil { - return mapToResource(res) + return mapToResource(ctx, res) } } } else { - return mapToResource(podval) + return mapToResource(ctx, podval) } } return nil } -func getPodRootOwner(pid string) *resource { - result := getPodOwner(pid) +func getPodRootOwner(ctx context.Context, pid string) resource { + result := getPodOwner(ctx, pid) - if (*result).Id() == pid { + if result.Id() == pid { return result } - return getPodRootOwner((*getPodOwner((*result).Id())).Id()) + return getPodRootOwner(ctx, getPodOwner(ctx, result.Id()).Id()) } diff --git a/replicaset.go b/replicaset.go index 67cb999..9ce32c5 100644 --- a/replicaset.go +++ b/replicaset.go @@ -6,9 +6,9 @@ import ( type replicaSet struct { Id string - Metadata *metadata - Owner *resource - RootOwner *resource + Metadata metadata + Owner resource + RootOwner resource } type replicaSetResolver struct { diff --git a/resource.go b/resource.go index b75786e..6f37d7b 100644 --- a/resource.go +++ b/resource.go @@ -13,25 +13,34 @@ type resource interface { type resourceResolver struct { ctx context.Context - r *resource + r resource } -func mapToResource(rMap map[string]interface{}) *resource { +func mapToResource( + ctx context.Context, + rMap map[string]interface{}) resource { + kind := getKind(rMap) + if kind == "Deployment" { + return &deploymentResolver{ + ctx, + &deployment{getUid(rMap), getMetadata(rMap), nil, nil}} + } + return nil } func (r *resourceResolver) Id() string { - return (*r.r).Id() + return r.r.Id() } func (r *resourceResolver) Metadata() *metadataResolver { - return (*r.r).Metadata() + return r.r.Metadata() } func (r *resourceResolver) Owner() *resourceResolver { - return (*r.r).Owner() + return r.r.Owner() } func (r *resourceResolver) RootOwner() *resourceResolver { - return (*r.r).RootOwner() + return r.r.RootOwner() } diff --git a/util.go b/util.go index 1b7a1f6..b2b08bb 100644 --- a/util.go +++ b/util.go @@ -1,13 +1,56 @@ package main import ( -// "context" + // "context" + "errors" + "fmt" ) -func getOwners(resourceMap map[string]interface{}) (*resource, *resource) { +func getOwners(resourceMap map[string]interface{}) (resource, resource) { return nil, nil } func mapItem(obj map[string]interface{}, item string) map[string]interface{} { return obj[item].(map[string]interface{}) } + +func getMetadata(resourceMap map[string]interface{}) metadata { + if meta, ok := resourceMap["metadata"]; ok { + if mmap, ok := meta.(map[string]interface{}); ok { + return mapToMetadata(mmap) + } + } + + panic(errors.New( + fmt.Sprintf("Invalid Kubernetes resource: %v", resourceMap))) +} + +func getKind(resourceMap map[string]interface{}) string { + if meta, ok := resourceMap["metadata"]; ok { + if mmap, ok := meta.(map[string]interface{}); ok { + if kind, ok := mmap["kind"]; ok { + if kindstr, ok := kind.(string); ok { + return kindstr + } + } + } + } + + panic(errors.New( + fmt.Sprintf("Invalid Kubernetes resource: %v", resourceMap))) +} + +func getUid(resourceMap map[string]interface{}) string { + if meta, ok := resourceMap["metadata"]; ok { + if mmap, ok := meta.(map[string]interface{}); ok { + if uid, ok := mmap["uid"]; ok { + if uidstr, ok := uid.(string); ok { + return uidstr + } + } + } + } + + panic(errors.New( + fmt.Sprintf("Invalid Kubernetes resource: %v", resourceMap))) +}