Skip to content

Commit

Permalink
change resource interface
Browse files Browse the repository at this point in the history
  • Loading branch information
jrjyipee committed Jul 17, 2018
1 parent 056ce95 commit 12fb1ed
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 36 deletions.
6 changes: 3 additions & 3 deletions deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions gobuild.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type metadata struct {

type metadataResolver struct {
ctx context.Context
m *metadata
m metadata
}

func mapToMetadata(jsonObj map[string]interface{}) metadata {
Expand Down Expand Up @@ -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
}
Expand Down
33 changes: 15 additions & 18 deletions pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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}
}
Expand All @@ -31,57 +31,54 @@ 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}
}

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())
}
6 changes: 3 additions & 3 deletions replicaset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
21 changes: 15 additions & 6 deletions resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
47 changes: 45 additions & 2 deletions util.go
Original file line number Diff line number Diff line change
@@ -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)))
}

0 comments on commit 12fb1ed

Please sign in to comment.