• Stars
    star
    111
  • Rank 313,355 (Top 7 %)
  • Language SCSS
  • License
    MIT License
  • Created over 4 years ago
  • Updated almost 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

GraphQL endpoint of Kubernetes API; dynamically powered by GraphQL Mesh

GraphQL endpoint of Kubernetes API & Docker; dynamically created by GraphQL Mesh

Simple setup for converting Kubernetes API server into GraphQL API.

This sample exposes GraphQL endpoint by using only API management tool GraphQL Mesh.

Advantage of GraphQL

  • Unique and readable query format

    • In GraphQL world, you edit fields in your query so that response data will include only fields that you need.
  • Completion or validation of query

    • When you edit your query with GraphQL playground or Graphiql, they have powerful completion function which enables you to easily construct query without remembering exact field name etc.

    Query. list pods

    kubectl get pods --namespace default -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.startTime}{"\n"}{end}'
    {
      # pods or pos
      Pods(namespace: "default") {
        items {
          metadata {
            name
            namespace
          }
          status {
            startTime
          }
        }
      }
    }

    Interactive visualiation by Postman

    Resource relation is visualized as tree form. Please refer to graphql-treeview.

Query example (Kubernetes)

Example 2: get pods with labels

kubectl get pods --namespace default -l run=mesh -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.metadata.namespace}{"\t"}{.metadata.labels}{"\t"}{.status.startTime}{"\n"}{end}'
{
  # pods or pos
	Pods(
    namespace: "default", 
    labelSelector: "run=mesh"
  ) {
    items {
      metadata {
        name
        namespace
        labels
      }
      status {
        startTime
      }
    }
  }
}

Example 2-2. get single pod with name

{
  # pod or po
  pod(
    name: "mesh"
    namespace: "default", 
  ) {
    metadata {
      name
      namespace
    }
  }
}

Example 3-1. parent/children, connected/connecting

These are original fields. Please refer to Original resolvers.

query q {
  Pods(namespace: "default") {
    items {
      metadata {
        name
        namespace
      }
      parent {
        # ReplicaSets (Pod's parent)
        ... on ReplicaSet {
          metadata {
            name
          }
          children {
            # Pods (ReplicaSet's children)
            items {
              metadata {
                name
              }
            }
          }
        }
      }
      connected {
        # Services (Pod's connected resources)
        items {
          metadata {
            name
          }
          connecting {
            # Pods (Service's connecting targets)
            items {
              metadata {
                name
              }
            }
          }
        }
      }
    }
  }
}

Example 3-2. Debugging with events

This is useful for Pod debugging.

kubectl get po; (check...) kubectl get deployment; (check...) kubectl get events ...

This query replaces many operations of kubectl.

query {
  Pods(namespace: "default") { # Pod
    items {
      metadata {
        name
        namespace
      }
      status {
        startTime
        containerStatuses {
          state {
            terminated {
              message
            }
            waiting {
              message
              reason
            }
          }
        }
      }
      events { # Get events list related to Pod
        ...eventsContents   
      }
      parent { # ReplicaSet or DaemonSet or StatefulSet (if any exists)
        ...on ReplicaSet { # Deployment (if any exists)
          events { # Get events for the above grandparent resources. 
            ...eventsContents
          }
        }
      }
    }
  }
}

fragment eventsContents on EventList {
  items {
    message
    reason
  }
}

Example 4. Helper and error case

Example: document generated according to OpenAPI

Error case

Query example (docker)

Example 1. Show volume list

query q {
  volumeList {
    volumes {
      name
    }
  }
}

Example 2. Show container list

query q {
  containerList{
    names
    ports {
      privatePort
      publicPort
    }
  }
}

Start up

Prerequisites

You have your own Kubernetes cluster or create one. For example, you can create it with the following command after installing kind.

# Create a Kubernetes cluster on Docker
kind create cluster --name kube-graphql --config kind-config.yaml

Case 1: with Kubernetes

# Run containers on Kubernetes. 
kubectl apply -f k8s -n default
# When you need cluster wide resouce access (e.g. Namespace), you can run this command additionally.
kubectl apply -f k8s/cluster-wide/

If you have ingress controller in your cluster, you will see the service at http://graphql-mesh-k8s.127.0.0.1.xip.io.

Otherwise, this command allows you to forwarding port to localhost.

# Forward GraphQL Mesh to local machine
kubectl port-forward svc/mesh-svc 4000:4000 -n default

Access http://localhost:4000.

Case 2 : with Helm

After installing helm command, you can deploy helm chart.

helm repo add kubernetes-graphql https://onelittlenightmusic.github.io/kubernetes-graphql/helm-chart
helm install my-kubernetes-graphql kubernetes-graphql/kubernetes-graphql
# or with parameter
helm install my-kubernetes-graphql kubernetes-graphql/kubernetes-graphql --set kubernetes-api-proxy.serviceAccount.clusterWide=true

Access a URL which is displayed after running helm install (e.g. http://graphql-mesh-k8s.127.0.0.1.xip.io).

Helm chart parameters are as follows,

Name Description Default
kubernetes-api-proxy.serviceAccount.create If true, ServiceAccount will be created for GraphQL API true
kubernetes-api-proxy.serviceAccount.clusterWide If true, GraphQL API endpoint will be allowed to call cluster wide API like kubectl get nodes false
graphql-mesh.ingress.enabled If true, an Ingress will be created. In this case, you prepare Ingress Controller. true

Case 3 : Docker

Install Docker and docker-compose and run these commands.

cd docker
docker-compose up -d

Cleanup

kubectl delete -f k8s

Kubernetes Query

The query methods follow Kubernetes Resource Type.

  • Nodes
  • Namespaces
  • Pods(namespace: String!)
  • Services(namespace: String!)
  • Ingresses(namespace: String!)
  • ReplicaSets(namespace: String!)
  • Deployments(namespace: String!)
  • DaemonSets(namespace: String!)
  • StatefulSets(namespace: String!)
  • ReplicationControllers(namespace: String!)
  • HorizontalPodAutoscalers(namespace: String!)
  • ClusterRoleBindings
  • ClusterRoles
  • StorageClasses
  • PodSecurityPolicies
  • ComponentStatuses
  • CustomResourceDefinitions
  • PriorityClasses
  • CSIDrivers
  • CSINodes
  • VolumeAttachments
  • CronJobs(namespace: String!)
  • Jobs(namespace: String!)
  • RoleBindings(namespace: String!)
  • Roles(namespace: String!)
  • ServiceAccounts(namespace: String!)
  • ResourceQuotas(namespace: String!)
  • Endpoints(namespace: String!)
  • ControllerRevisions(namespace: String!)
  • NetworkPolicies(namespace: String!)
  • PodDisruptionBudgets(namespace: String!)
  • LimitRanges(namespace: String!)
  • PodTemplates(namespace: String!)
  • Events(namespace: String!)
  • PersistentVolumeClaims(namespace: String!)
  • PersistentVolumes(namespace: String!)
  • Secrets(namespace: String!)
  • ConfigMaps(namespace: String!)

etc.

Original resolvers (parent/children, connecting/connected, mounting, namespace and events)

There are some original resolvers inside some resource types. These are "links" from one resource to another.

Name Value Example
parent Follow a owner link which means dependency in Kubernetes Pod.parent -> ReplicaSet, ReplicaSet.parent -> Deployment
children Drill down owner links downward. Search for all child resource elements which has owner link to original resource. Deployment.children -> [ReplicaSet], ReplicaSet.children -> [Pod]
connecting Search for all resource elements which have the same labels as la belSelector of original resource. Service.connecting -> [Pod]
connected Search for resources whose labelSelector includes all labels of original resource Pod.conntected -> Service
mounting Search for resources which are mounted as Volumes in the Pod Pod.mounting -> [Secret or ConfigMap or PersistentVolumeClaim]
events Search for events related to the target resource Pod(and other resources).events -> [Event]
namespace Direct link to namespace object Pod(and other resources).namespace --> Namespace

Architecture

In Kubernetes

Pod
- pod: mesh
  - container: mesh (#1: GraphQL endpoint. runs GraphQL Mesh to convert #2 API to GraphQL)
  - container: proxy (#2: REST API endpoint. runs kubectl proxy to forward request to #4)

Service
- mesh-rc (#3: endpoint of GraphQL Mesh, connected to #1 and #2)
- kubernetes (#4: Automatically generated by Kubernetes. Kubernetes API endpoint)

Docker

Containers
- container: proxy (REST API: transfer Docker unix-socket API to http port)
- container: mesh-svc (GraphQL API: runs GraphQL Mesh to convert REST API to GraphQL)

Customize

Edit resources in src directory

src
β”œβ”€β”€ .meshrc.yaml # Configuration file for GraphQL Mesh
β”œβ”€β”€ example-query.graphql # Initial query to be shown in GraphQL Playground
└── mesh-init.sh # Initialization script for OpenAPI specification of Kubernetes

Compile into k8s/meshrc-cm.yaml with this command.

kubectl create cm meshrc-cm --from-file src/.meshrc.yaml --from-file src/mesh-init.sh --from-file src/example-query.graphql  --dry-run -o yaml> k8s/meshrc-cm.yaml

Reference

Similar projects

These are some existing orignal tools for the same motivation.

More Repositories

1

graphql-mesh-docker

Dockerfile / docker-compose / Kubernetes manifest / Helm chart for GraphQL Mesh
Mustache
39
star
2

opactl

A simple tool for converting Rego (OPA) rule into command.
Go
29
star
3

k8sasdb

k8sAsDb: Operator for database-like Kubernetes API usage. CRD for creating new schema. CRUD operation to schema.
Go
17
star
4

excel-graphiql-explorer

Excel addon in order to query GraphQL with GraphiQL explorer and put data in excel sheet
TypeScript
13
star
5

opa-entrypoint-authorizer

JavaScript
10
star
6

opa-up-and-running

Open Policy Agent: Up and Running
Open Policy Agent
5
star
7

schema-stitching-example

Example of Schema Stitching with GraphQL
TypeScript
4
star
8

graphql-psql-k8s

Prisma/PostgreSQL example on Kubernetes
JavaScript
4
star
9

graphql-mesh-operator

Aggregate OpenAPI endpoints etc inside Kubernetes and expose GraphQL API outside Kubernetes
Go
3
star
10

star-yaml

YAML format for star schema and snowflake schema
3
star
11

sample_nested_model_creation

Sample for Rails 5 nested_attributes parent-child model creation in one action
Ruby
2
star
12

keda-dapr-scaling-sample

Simple example of scaling down to zero using KEDA and Dapr.
Shell
2
star
13

mlflow-keras-mnist

Sample notebook showing how MLflow saves and loads MNIST CNN model
Jupyter Notebook
2
star
14

graphql-stargen

Generate graphql remote schema from star schema file
TypeScript
2
star
15

TapChain

TapChain: new programming interface for children (Android app)
Java
2
star
16

graphql-mesh-docker-mysql-example

Simpler docker setup for GraphQL Mesh and MySQL
Shell
2
star
17

minimal-coredns-etcd-docker-compose

Minimal docker-compose file to run CoreDNS and etcd
2
star
18

raspi-py-dht11-docker

Python
1
star
19

minimal-metallb

Minimal configuration for MetalLB
1
star
20

graphql-gw-example-add-field

Simple GraphQL gateway example adding an field into Github API
JavaScript
1
star
21

prisma-binding-typescript-sample

Demonstration of graphql-yoga server to forward requests to prisma server directly.
TypeScript
1
star
22

graphql-mesh-postgres-example

Simple example to convert PostgreSQL to GraphQL by GraphQL Mesh
PLpgSQL
1
star
23

nuxt-apollo-link-state-counter

Simple example using apollo-link-state in Nuxt.js
Vue
1
star
24

japan-cities-code-graphql

GraphQL API for Japan cities code by MIC Japan
TypeScript
1
star