Skip to content

Commit

Permalink
some docs
Browse files Browse the repository at this point in the history
  • Loading branch information
jrjyipee committed Jul 18, 2018
1 parent 391be31 commit 8c1d894
Show file tree
Hide file tree
Showing 13 changed files with 267 additions and 53 deletions.
19 changes: 19 additions & 0 deletions daemonset.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// Copyright 2018 Yipee.io
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
Expand All @@ -6,6 +20,7 @@ import (
"strings"
)

// DaemonSets place a pod on each server
type daemonSet struct {
Metadata metadata
Owner resource
Expand All @@ -18,6 +33,7 @@ type daemonSetResolver struct {
d daemonSet
}

// Translate unmarshalled json into a deployment object
func mapToDaemonSet(
ctx context.Context,
jsonObj map[string]interface{}) daemonSet {
Expand All @@ -29,6 +45,7 @@ func mapToDaemonSet(
return daemonSet{meta, owner, rootOwner, nil}
}

// DaemonSets have pods as children
func getDaemonSetPods(ctx context.Context, d daemonSet) *[]pod {
dsName := d.Metadata.Name
dsNamePrefix := dsName + "-"
Expand All @@ -53,6 +70,7 @@ func getDaemonSetPods(ctx context.Context, d daemonSet) *[]pod {
return &results
}

// Resource method implementations
func (r *daemonSetResolver) Kind() string {
return DaemonSetKind
}
Expand All @@ -75,6 +93,7 @@ func (r *daemonSetResolver) RootOwner() *resourceResolver {
return &resourceResolver{r.ctx, r.d.RootOwner}
}

// Resolve child Pods
func (r *daemonSetResolver) Pods() []*podResolver {
if r.d.Pods == nil {
r.d.Pods = getDaemonSetPods(r.ctx, r.d)
Expand Down
20 changes: 20 additions & 0 deletions deployment.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// Copyright 2018 Yipee.io
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
Expand All @@ -6,6 +20,8 @@ import (
"strings"
)

// Top level Kubernetes replicated controller. Deployments are built out
// of ReplicaSets.
type deployment struct {
Metadata metadata
Owner resource
Expand All @@ -18,6 +34,7 @@ type deploymentResolver struct {
d deployment
}

// Translate unmarshalled json into a deployment object
func mapToDeployment(
ctx context.Context,
jsonObj map[string]interface{}) deployment {
Expand All @@ -26,6 +43,7 @@ func mapToDeployment(
return deployment{meta, nil, nil, nil}
}

// Retrieve the ReplicaSets comprising the deployment
func getReplicaSets(ctx context.Context, d deployment) *[]replicaSet {
depName := d.Metadata.Name
depNamePrefix := depName + "-"
Expand All @@ -50,6 +68,7 @@ func getReplicaSets(ctx context.Context, d deployment) *[]replicaSet {
return &results
}

// Resource method implementations
func (r *deploymentResolver) Kind() string {
return DeploymentKind
}
Expand All @@ -66,6 +85,7 @@ func (r *deploymentResolver) RootOwner() *resourceResolver {
return &resourceResolver{r.ctx, &deploymentResolver{r.ctx, r.d}}
}

// Resolve child ReplicaSets
func (r *deploymentResolver) ReplicaSets() []*replicaSetResolver {
if r.d.ReplicaSets == nil {
r.d.ReplicaSets = getReplicaSets(r.ctx, r.d)
Expand Down
19 changes: 19 additions & 0 deletions kubeaccess.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// Copyright 2018 Yipee.io
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
Expand All @@ -9,6 +23,9 @@ import (
"os/exec"
)

// Functions for retrieving Kubernetes information from a cluster

// Get a single resource instance from a namespace
func getK8sResource(kind, namespace, name string) map[string]interface{} {
return fromJson(
lookUpResource(kind, namespace, name)).(map[string]interface{})
Expand Down Expand Up @@ -44,6 +61,7 @@ func lookUpResource(kind, namespace, name string) []byte {
return bytes
}

// Get all resource instances of a specific kind
func getAllK8sObjsOfKind(
ctx context.Context,
kind string,
Expand Down Expand Up @@ -78,6 +96,7 @@ func getAllK8sObjsOfKind(
return results
}

// Get all resource instances of a specific kind in a specific namespace
func getAllK8sObjsOfKindInNamespace(
ctx context.Context,
kind, ns string,
Expand Down
28 changes: 21 additions & 7 deletions kubeicql.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
// Copyright 2018 Yipee.io
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"context"
)

// The schema below defines the objects and relationships that can be
// queried and modified. Each "type" below can be returned from
// queries so a "resolver" must be implemented. The resolver has a
// method for each field of the object and the graphql server calls a
// resolver method as needed based on what is requested by the
// user. Each type has a struct that holds its scalar values while set
// or list values are constructed on demand.
// The schema below defines the objects and relationships for Kubernetes.
// It is not yet complete.

var Schema = `
schema {
Expand Down Expand Up @@ -167,6 +176,7 @@ const DeploymentKind = "Deployment"
type Resolver struct {
}

// Pod lookups
func (r *Resolver) AllPods(ctx context.Context) *[]*podResolver {
pset := getAllK8sObjsOfKind(
ctx,
Expand Down Expand Up @@ -215,6 +225,7 @@ func (r *Resolver) PodByName(
return nil
}

// Deployment lookups
func (r *Resolver) AllDeployments(ctx context.Context) *[]*deploymentResolver {
dset := getAllK8sObjsOfKind(
ctx,
Expand Down Expand Up @@ -264,6 +275,7 @@ func (r *Resolver) DeploymentByName(
return nil
}

// ReplicaSet lookups
func (r *Resolver) AllReplicaSets(ctx context.Context) *[]*replicaSetResolver {
rset := getAllK8sObjsOfKind(
ctx,
Expand Down Expand Up @@ -313,6 +325,7 @@ func (r *Resolver) ReplicaSetByName(
return nil
}

// StatefulSet lookups
func (r *Resolver) AllStatefulSets(ctx context.Context) *[]*statefulSetResolver {
sset := getAllK8sObjsOfKind(
ctx,
Expand Down Expand Up @@ -362,6 +375,7 @@ func (r *Resolver) StatefulSetByName(
return nil
}

// DaemonSet lookups
func (r *Resolver) AllDaemonSets(ctx context.Context) *[]*daemonSetResolver {
dset := getAllK8sObjsOfKind(
ctx,
Expand Down
16 changes: 16 additions & 0 deletions label.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
// Copyright 2018 Yipee.io
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"context"
)

// Single label value within a Kubernetes object
type label struct {
Name string
Value string
Expand All @@ -14,6 +29,7 @@ type labelResolver struct {
l *label
}

// Translate unmarshalled json into a set of labels
func mapToLabels(lMap map[string]interface{}) *[]label {
var labels []label

Expand Down
18 changes: 18 additions & 0 deletions metadata.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
// Copyright 2018 Yipee.io
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"context"
)

// Kubernetes metadata
type metadata struct {
CreationTimestamp *string
GenerateName *string
Expand All @@ -21,6 +36,7 @@ type metadataResolver struct {
m metadata
}

// Translate unmarshalled json into a metadata object
func mapToMetadata(
ctx context.Context, ns string, jsonObj map[string]interface{}) metadata {
var m metadata
Expand All @@ -42,6 +58,7 @@ func mapToMetadata(
m.SelfLink = jsonObj["selfLink"].(string)
m.Uid = jsonObj["uid"].(string)

// Similar to getOwner
if orArray := jsonObj["ownerReferences"]; orArray != nil {
for _, oref := range orArray.([]interface{}) {
ormap := oref.(map[string]interface{})
Expand All @@ -58,6 +75,7 @@ func mapToMetadata(
return m
}

// Metadata methods
func (r *metadataResolver) CreationTimestamp() *string {
return r.m.CreationTimestamp
}
Expand Down
21 changes: 20 additions & 1 deletion owners.go → owner.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
// Copyright 2018 Yipee.io
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"context"
// "fmt"
)

// Tracks ownership relationships between Kubernetes objects. If an object
// has no owner, we treat it as its own owner
type ownerRef struct {
ctx context.Context
ref map[string]interface{}
cachedOwner resource
cachedOwner resource // cached info for on-demand lookup
}

// resource method implementations
func (r *ownerRef) Kind() string {
if r.cachedOwner == nil {
r.cachedOwner = getOwner(r.ctx, r.ref)
Expand Down Expand Up @@ -39,6 +56,8 @@ func (r *ownerRef) RootOwner() *resourceResolver {
return r.cachedOwner.RootOwner()
}

// Fetch owners by getting ownerReferences and doing lookups based
// on their contents
func getRawOwner(val map[string]interface{}) map[string]interface{} {
if orefs := getMetadataField(val, "ownerReferences"); orefs != nil {
oArray := orefs.([]interface{})
Expand Down
Loading

0 comments on commit 8c1d894

Please sign in to comment.