Skip to content

Commit

Permalink
feat(website): getting started revamp (#1285)
Browse files Browse the repository at this point in the history
### Tasks

- [X] Create the initial directory structure of language guides
- [X] Create the python guide for demonstration
- [X] Update/clean-up CLI installation guide
- [X] Create python code samples and guide
- [X] Create typescript code samples and guide
- [X] Create java code samples and guide
- [X] Create go code samples and guide
  • Loading branch information
tucktuck9 authored Oct 17, 2023
1 parent 81131ce commit 20e0320
Show file tree
Hide file tree
Showing 9 changed files with 682 additions and 1,248 deletions.
2 changes: 1 addition & 1 deletion docs/.pages
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
nav:
- What is cdk8s?: index.md
- getting-started.md
- Getting started: get-started
- basics
- cdk8s+: plus
- CLI: cli
Expand Down
36 changes: 28 additions & 8 deletions docs/cli/installation.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
# Installation
# Install the cdk8s CLI

To install the CLI use one of the following methods:
This page describes the different ways you can install the Cloud Development Kit for Kubernetes (cdk8s) command-line interface (CLI) tool on your local machine.

=== "npm"
`npm i -g cdk8s-cli`
## Installation
### Homebrew
To install the cdk8s CLI with [Homebrew](https://brew.sh/), run the following command:
```console
brew install cdk8s
```

=== "yarn"
`yarn global add cdk8s-cli`
### npm
To install the cdk8s CLI with [npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm), run the following command:
```console
npm i -g cdk8s-cli
```

=== "Homebrew"
`brew install cdk8s`
### Yarn
To install the cdk8s CLI with [yarn](https://classic.yarnpkg.com/lang/en/docs/install/#mac-stable), run the following command:
```console
yarn global add cdk8s-cli
```

## Check version
To check your version, run the following command:
```console
cdk8s --version
```
You should see something similar to the following:
```
2.2.31
```
6 changes: 6 additions & 0 deletions docs/get-started/.pages
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
nav:
- index.md
- Python: python.md
- TypeScript: typescript.md
- Java: java.md
- Go: go.md
176 changes: 176 additions & 0 deletions docs/get-started/go.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
# Getting started with cdk8s for Go
In this guide, we'll walk you through the process of building a basic Kubernetes application using cdk8s for Go.

## Prerequisites
1. To install the cdk8s CLI, you need the [Node Package Manager (npm)](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm) installed on your local machine.
2. cdk8s for Go supports Go versions 1.16+. Check your Go version:
```bash
go version
```

## Set up an environment
### Install the CLI
To initialize a cdk8s project and auto-generate Kubernetes manifests based on our code, we need the cdk8s CLI:

1. Run the following command to install the CLI using [npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm). For more installation methods, see [Install the cdk8s CLI](./../cli/installation.md).
```console
npm install -g cdk8s-cli
```

### Create a project
Next, we’ll initialize a project to create the directory structure and install the necessary dependencies using the [init](./../cli/init.md) command.

1. In a terminal window, create a new directory:
```bash
mkdir cdk8s-golang
```

2. Next, run the following command inside the `cdk8s-golang` directory:
```console
cdk8s init go-app
```

3. In your preferred code editor, open the `main.go` file.
```go
package main

import (
"github.com/aws/constructs-go/constructs/v10"
"github.com/aws/jsii-runtime-go"
"github.com/cdk8s-team/cdk8s-core-go/cdk8s/v2"
)

type MyChartProps struct {
cdk8s.ChartProps
}

func NewMyChart(scope constructs.Construct, id string, props *MyChartProps) cdk8s.Chart {
var cprops cdk8s.ChartProps
if props != nil {
cprops = props.ChartProps
}
chart := cdk8s.NewChart(scope, jsii.String(id), &cprops)

// define resources here

return chart
}

func main() {
app := cdk8s.NewApp(nil)
NewMyChart(app, "cdk8s-golang", nil)
app.Synth()
}
```

This sample shows the basic structure of a cdk8s application with the essential libraries: **constructs** and **cdk8s**. These libraries supply the foundational classes and methods required for working with cdk8s. It includes the following components:

* A `NewMyChart` function that creates a specific instance of a [Chart](https://pkg.go.dev/github.com/cdk8s-team/cdk8s-core-go/cdk8s/v2#Chart). It is responsible for initializing the Chart instance and registering the Kubernetes resources to it.
* A `main` function acting as the primary entry point of the cdk8s application. It instantiates the [App](https://pkg.go.dev/github.com/cdk8s-team/cdk8s-core-go/cdk8s/v2#App) instance, which oversees the application's lifecycle and resources.
* The function `NewMyChart` is called by passing the app instance and a string identifier, "go", as arguments. This action generates and registers the chart and all its resources to the app. Note that in this example, the "cdk8s-golang" string identifier is autogenerated based on the current directory name.
* The `Synth` method is called on the `app` instance, which produces the required Kubernetes YAML manifest files based on the defined resources. Note that in this example, we haven't defined any resources within the `NewMyChart` function, so running the "cdk8s synth" command in the CLI would generate a blank Kubernetes manifest.

## Define Kubernetes resources
Now, let's delve into defining Kubernetes resources for our application. In this example, we'll outline a basic Kubernetes [Deployment](https://kubernetes.io/docs/concepts/workloads/controllers/deployment/) for a sample app. We'll start by importing the `imports` directory and the `k8s` sub-directory, which houses all cdk8s Kubernetes classes and functions.

### Copy the code sample

1. Copy and paste the following code sample into the existing `main.go` file of your project.

```go
package main

import (
"example.com/cdk8s-golang/imports/k8s"
"github.com/aws/constructs-go/constructs/v10"
"github.com/aws/jsii-runtime-go"
"github.com/cdk8s-team/cdk8s-core-go/cdk8s/v2"
)

func NewChart(scope constructs.Construct, id string, ns string, appLabel string) cdk8s.Chart {

chart := cdk8s.NewChart(scope, jsii.String(id), &cdk8s.ChartProps{
Namespace: jsii.String(ns),
})

labels := map[string]*string{
"app": jsii.String(appLabel),
}

k8s.NewKubeDeployment(chart, jsii.String("deployment"), &k8s.KubeDeploymentProps{
Spec: &k8s.DeploymentSpec{
Replicas: jsii.Number(3),
Selector: &k8s.LabelSelector{
MatchLabels: &labels,
},
Template: &k8s.PodTemplateSpec{
Metadata: &k8s.ObjectMeta{
Labels: &labels,
},
Spec: &k8s.PodSpec{
Containers: &[]*k8s.Container{{
Name: jsii.String("app-container"),
Image: jsii.String("nginx:1.19.10"),
Ports: &[]*k8s.ContainerPort{{
ContainerPort: jsii.Number(80),
}},
}},
},
},
},
})

return chart
}

func main() {
app := cdk8s.NewApp(nil)

NewChart(app, "getting-started", "default", "my-app")

app.Synth()
}

A few things worth noting about this sample:

- The `NewChart` function utilizes Go's strong typing and interface-based design to construct a Kubernetes Deployment. This Deployment is set up with specific parameters like replica count, label selectors, and pod specifications. This method takes advantage of Go's map data structures to dynamically assign the "app" key in label selectors and metadata labels, offering a straightforward and efficient way to configure essential details.

## Generate Kubernetes manifests
After you have defined the Kubernetes resources for your application, you are ready to generate the Kubernetes manifest that will define your Deployment resource.

### Run the synth command
1. Open a terminal and navigate to your project directory.
2. Run the [synth](https://cdk8s.io/docs/latest/cli/synth/) command. This command generates a Kubernetes manifest file in the `dist` folder of your project directory. The manifest file contains all the resources you defined inside the `MyChart` class.
```console
cdk8s synth
```

### View the manifest
1. Open the `dist/getting-started.k8s.yaml` file. You should see a Kubernetes manifest similar to the following:
```bash
apiVersion: apps/v1
kind: Deployment
metadata:
name: getting-started-deployment-c80c7257
namespace: default
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- image: nginx:1.19.10
name: app-container
ports:
- containerPort: 80
```
## Conclusion
Throughout this guide, we introduced you to the cdk8s Go library and guided you through the process of creating a cdk8s Go application. We initiated a simple project and constructed a Kubernetes Deployment using cdk8s code. This included leveraging Go-specific programming language conventions to dynamically set the "app" key in "label" selectors and "metadata" labels for Kubernetes resources using Go's versatile handling of map data structures.
## Next up
- To run a complete code sample, we recommend diving into the Kubernetes [Deployment](https://kubernetes.io/docs/concepts/workloads/controllers/deployment/) and [Service](https://kubernetes.io/docs/concepts/services-networking/service/) using the [cdk8s-core](https://github.com/cdk8s-team/cdk8s-examples/blob/main/go/cdk8s-core/main.go) sample application.
8 changes: 8 additions & 0 deletions docs/get-started/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Getting started
CDK8s streamlines the automation of deployment, versioning, and configuration for your Kubernetes resources, so you can build and upgrade your clusters more easily. Whether you're creating new applications, managing microservices, or working with data processing pipelines, cdk8s has you covered. To kick start your journey, choose one of our language guides tailored to your preferred programming language.

### Choose a language guide
- [Python](python.md)
- [Typescript](typescript.md)
- [Java](java.md)
- [Go](go.md)
Loading

0 comments on commit 20e0320

Please sign in to comment.