diff --git a/docs/get-started/go.md b/docs/get-started/go.md index 29771fcb10..469c82cc95 100644 --- a/docs/get-started/go.md +++ b/docs/get-started/go.md @@ -20,12 +20,17 @@ 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](https://cdk8s.io/docs/latest/cli/init/) command. -1. In a terminal window, run the following command in an empty directory: + 1. In a terminal window, create a new directory: +```bash +mkdir cdk8s-golang +``` + +2. Next, run the following command in an empty directory: ```console cdk8s init go-app ``` -1. In your preferred code editor, open the `main.go` file. +3. In your preferred code editor, open the `main.go` file. ```go package main @@ -65,17 +70,75 @@ This sample shows the basic structure of a cdk8s application with the essential * 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 "go" 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 -Lorem ipsum. -### Copy the code sample -1. Copy and paste the following code sample into the existing `main.go` file of your project. -```go + ## 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. +### Update the `go.mod` file +1. Open your `go.mod` file and add the following line at the bottom of the file: +```go +replace example.com/cdk8s-golang/imports/k8s => ./imports/k8s ``` -A few things worth noting about this sample: + ### Copy the code sample +1. Copy and paste the following code sample into the existing `main.go` file of your project. + ```typescript +package main -- Lorem ipsum. +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. @@ -89,12 +152,30 @@ cdk8s synth ### View the manifest 1. Open the `dist/getting-started.k8s.yaml` file. You should see a Kubernetes manifest similar to the following: -```yaml - +```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 -Lorem ipsum. +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.