From 056ce956208f997ba678d1be02a2ac5ded1eda9f Mon Sep 17 00:00:00 2001 From: "Jerry R. Jackson" Date: Mon, 16 Jul 2018 17:17:12 -0600 Subject: [PATCH] checkpoint --- kubeaccess.go | 9 +++++++++ label.go | 4 ++++ metadata.go | 42 ++++++++++++++++++++++++------------------ pods.go | 48 +++++++++++++++++++++++++++++++++++++++--------- resource.go | 4 ++++ util.go | 13 +++++++++++++ 6 files changed, 93 insertions(+), 27 deletions(-) create mode 100644 kubeaccess.go create mode 100644 util.go diff --git a/kubeaccess.go b/kubeaccess.go new file mode 100644 index 0000000..f7daf0a --- /dev/null +++ b/kubeaccess.go @@ -0,0 +1,9 @@ +package main + +import ( +// "context" +) + +func getK8sResource(id string) map[string]interface{} { + return nil +} diff --git a/label.go b/label.go index e3b0766..243d10c 100644 --- a/label.go +++ b/label.go @@ -14,6 +14,10 @@ type labelResolver struct { l *label } +func mapToLabels(lMap map[string]interface{}) *[]label { + return nil +} + func (r *labelResolver) Name() string { return r.l.Name } diff --git a/metadata.go b/metadata.go index 692c503..ec02b09 100644 --- a/metadata.go +++ b/metadata.go @@ -21,6 +21,28 @@ type metadataResolver struct { m *metadata } +func mapToMetadata(jsonObj map[string]interface{}) metadata { + var m metadata + if ct, ok := jsonObj["CreationTimestamp"].(string); ok { + m.CreationTimestamp = &ct + } else { + m.CreationTimestamp = nil + } + m.GenerateName = jsonObj["GenerateName"].(string) + m.Labels = mapToLabels(mapItem(jsonObj, "labels")) + m.Name = jsonObj["Name"].(string) + m.Namespace = jsonObj["Namespace"].(string) + m.OwnerReferences = mapToOwnerReferences(mapItem(jsonObj, "OwnerReferences")) + m.SelfLink = jsonObj["SelfLink"].(string) + m.Uid = jsonObj["Uid"].(string) + + return m +} + +func mapToOwnerReferences(orMap map[string]interface{}) *[]resource { + return nil +} + func (r *metadataResolver) CreationTimestamp() *string { return r.m.CreationTimestamp } @@ -31,11 +53,7 @@ func (r *metadataResolver) GenerateName() string { func (r *metadataResolver) Labels() []*labelResolver { var labelResolvers []*labelResolver - labels := r.m.Labels - if labels == nil { - labels = getMetadataLabels(r.m) - } - for _, label := range *labels { + for _, label := range *r.m.Labels { lab := label labelResolvers = append(labelResolvers, &labelResolver{r.ctx, &lab}) } @@ -52,11 +70,7 @@ func (r *metadataResolver) Namespace() string { func (r *metadataResolver) OwnerReferences() []*resourceResolver { var ownerResolvers []*resourceResolver - owners := r.m.OwnerReferences - if owners == nil { - owners = getMetadataOwnerReferences(r.m) - } - for _, owner := range *owners { + for _, owner := range *r.m.OwnerReferences { own := owner ownerResolvers = append(ownerResolvers, &resourceResolver{r.ctx, &own}) } @@ -74,11 +88,3 @@ func (r *metadataResolver) SelfLink() string { func (r *metadataResolver) Uid() string { return r.m.Uid } - -func getMetadataLabels(m *metadata) *[]label { - return nil -} - -func getMetadataOwnerReferences(m *metadata) *[]resource { - return nil -} diff --git a/pods.go b/pods.go index a280928..332ca02 100644 --- a/pods.go +++ b/pods.go @@ -5,7 +5,7 @@ import ( ) type pod struct { - ID string + Id string Metadata *metadata Owner *resource RootOwner *resource @@ -16,8 +16,17 @@ type podResolver struct { p *pod } -func (r *podResolver) ID() string { - return r.p.ID +func mapToPod(jsonObj map[string]interface{}) pod { + owner, rootOwner := getOwners(jsonObj) + meta := mapToMetadata(mapItem(jsonObj, "metadata")) + return pod{(mapItem(jsonObj, "metadata")["uid"]).(string), + &meta, + owner, + rootOwner} +} + +func (r *podResolver) Id() string { + return r.p.Id } func (r *podResolver) Metadata() *metadataResolver { @@ -31,7 +40,7 @@ func (r *podResolver) Metadata() *metadataResolver { func (r *podResolver) Owner() *resourceResolver { owner := r.p.Owner if owner == nil { - owner = getPodOwner(r.p) + owner = getPodOwner(r.p.Id) } return &resourceResolver{r.ctx, owner} } @@ -39,19 +48,40 @@ func (r *podResolver) Owner() *resourceResolver { func (r *podResolver) RootOwner() *resourceResolver { rootOwner := r.p.RootOwner if rootOwner == nil { - rootOwner = getPodRootOwner(r.p) + rootOwner = getPodRootOwner(r.p.Id) } return &resourceResolver{r.ctx, rootOwner} } func getPodMetadata(p *pod) *metadata { - return nil + meta := mapToMetadata(mapItem(getK8sResource(p.Id), "Metadata")) + return &meta } -func getPodOwner(p *pod) *resource { +func getPodOwner(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) + } + } + } else { + return mapToResource(podval) + } + } + return nil } -func getPodRootOwner(p *pod) *resource { - return nil +func getPodRootOwner(pid string) *resource { + result := getPodOwner(pid) + + if (*result).Id() == pid { + return result + } + + return getPodRootOwner((*getPodOwner((*result).Id())).Id()) } diff --git a/resource.go b/resource.go index 09e659d..b75786e 100644 --- a/resource.go +++ b/resource.go @@ -16,6 +16,10 @@ type resourceResolver struct { r *resource } +func mapToResource(rMap map[string]interface{}) *resource { + return nil +} + func (r *resourceResolver) Id() string { return (*r.r).Id() } diff --git a/util.go b/util.go new file mode 100644 index 0000000..1b7a1f6 --- /dev/null +++ b/util.go @@ -0,0 +1,13 @@ +package main + +import ( +// "context" +) + +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{}) +}