Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Commit

Permalink
support referencing an existing eks cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
eladb committed Oct 11, 2023
1 parent 5b25eb8 commit 9d160d2
Show file tree
Hide file tree
Showing 14 changed files with 166 additions and 85 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules/
target/
target/
values.yaml
20 changes: 17 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,22 @@ When executed in the Wing Simulator, the workload is started within a local Dock

### `tf-aws`

On AWS, an EKS cluster is provisioned and the workload is deployed through Helm into the Kubernetes
cluster.
By default an EKS cluster is provisioned and the workload is deployed through Helm into the
Kubernetes cluster.

To use an existing EKS cluster, the following platform values are required:

* `eks.cluster_name` - the name of the cluster
* `eks.endpoint` - the url of the kuberenets api endpoint of the cluster
* `eks.certificate` - the certificate authority of this cluster.

The `eks-values.sh` script can be used to query the values for an existing cluster and create a
values file:

```sh
$ ./eks-values.sh CLUSTER-NAME > values.yaml
$ wing compile -t tf-aws --values ./values.yaml main.w
```

## TODO

Expand All @@ -46,7 +60,7 @@ See [Captain's Log](https://winglang.slack.com/archives/C047QFSUL5R/p16968681568
- [x] EKS as a singleton
- [ ] Add support for local Dockerfiles (currently only images from Docker Hub are supported), this
includes publishing into an ECR.
- [ ] Reference existing EKS repository.
- [x] Reference existing EKS repository.
- [ ] Use a `cloud.Redis` database
- [ ] Implement `cloud.Service` using containers.
- [ ] Deploy multiple workloads (maybe guestbook?)
Expand Down
15 changes: 15 additions & 0 deletions eks-values.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/sh
if [ -z "$1" ]; then
echo "Usage: $0 <cluster-name>"
exit 1
fi

export cluster_name=$1
export cluster_endpoint=$(aws eks describe-cluster --name $cluster_name --query "cluster.endpoint")
export cluster_certificate=$(aws eks describe-cluster --name $cluster_name --query "cluster.certificateAuthority.data")

cat << EOF
eks.cluster_name: $cluster_name
eks.endpoint: $cluster_endpoint
eks.certificate: $cluster_certificate
EOF
72 changes: 32 additions & 40 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "wing-containers",
"version": "1.0.0",
"description": "",
"description": "A containers library for Wing",
"main": "index.js",
"scripts": {
"build": "wing test test/containers.test.w"
"build": "wing test test/*.test.w"
},
"author": {
"email": "[email protected]",
Expand All @@ -17,6 +17,6 @@
"@cdktf/provider-kubernetes": "^9.0.0",
"cdk8s-plus-27": "^2.7.33",
"cdktf": "^0.18.2",
"winglang": "^0.36.1"
"winglang": "^0.37.1"
}
}
18 changes: 18 additions & 0 deletions sim/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const child_process = require("child_process");

exports.shell = async function (command, args, cwd) {
return new Promise((resolve, reject) => {
child_process.execFile(command, args, { cwd }, (error, stdout, stderr) => {
if (error) {
console.error(stderr);
return reject(error);
}

return resolve(stdout ? stdout : stderr);
});
});
};

exports.entrypointDir = function (scope) {
return scope.node.root.entrypointDir;
};
4 changes: 2 additions & 2 deletions test/containers.test.w
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ let hello = new containers.Workload(
image: "paulbouwer/hello-kubernetes:1",
port: 8080,
readiness: "/",
replicas: 3,
replicas: 2,
env: {
"MESSAGE" => message,
},
Expand All @@ -18,7 +18,7 @@ new containers.Workload(
image: "hashicorp/http-echo",
port: 5678,
public: true,
replicas: 20,
replicas: 2,
args: ["-text=hello1234"],
) as "http-echo";

Expand Down
9 changes: 9 additions & 0 deletions test/simple.test.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
bring "../containers.w" as containers;

new containers.Workload(
image: "hashicorp/http-echo",
port: 5678,
public: true,
replicas: 2,
args: ["-text=bang_bang"],
);
6 changes: 5 additions & 1 deletion test/values.test.w
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
bring "../tf-aws/values.w" as values;
values.get("foo");
bring util;

if let x = values.tryGet("foo") {
log(x);
}
28 changes: 24 additions & 4 deletions tf-aws/eks.w
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,32 @@ class Cluster impl ICluster {

/** singleton */
pub static getOrCreate(scope: std.IResource): ICluster {
// values.get("foo");

let stack = cdktf.TerraformStack.of(scope);
let uid = "WingEksCluster";
let existing: Cluster? = unsafeCast(stack.node.tryFindChild(uid));
return existing ?? new Cluster() as uid in stack;
let existing: ICluster? = unsafeCast(stack.node.tryFindChild(uid));

let newCluster = (): ICluster => {
if let attrs = Cluster.tryGetClusterAttributes() {
return new ClusterRef(attrs) as uid in stack;
} else {
return new Cluster() as uid in stack;
}
};

return existing ?? newCluster();
}

static tryGetClusterAttributes(): ClusterAttributes? {
if !values.has("eks.cluster_name") {
return nil;
}

return ClusterAttributes {
name: values.get("eks.cluster_name"),
certificate: values.get("eks.certificate"),
endpoint: values.get("eks.endpoint"),
};

}

_attributes: ClusterAttributes;
Expand Down
18 changes: 0 additions & 18 deletions test/util.js → tf-aws/util.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,9 @@
const child_process = require("child_process");
const cdk8s = require('cdk8s');
const fs = require('fs');
const path = require('path');
const wingsdk = require('@winglang/sdk');
const crypto = require('crypto');

exports.shell = async function (command, args, cwd) {
return new Promise((resolve, reject) => {
child_process.execFile(command, args, { cwd }, (error, stdout, stderr) => {
if (error) {
console.error(stderr);
return reject(error);
}

return resolve(stdout ? stdout : stderr);
});
});
};

exports.entrypointDir = function (scope) {
return scope.node.root.entrypointDir;
};

exports.toHelmChart = function(chart) {
const app = cdk8s.App.of(chart);
const wingdir = wingsdk.core.App.of(chart).workdir;
Expand Down
Loading

0 comments on commit 9d160d2

Please sign in to comment.