# pulumi study notes
### Tools to be installed
aws: aws command for was 
aws-ault: manage aws authentication
kubectl: Command line tool for k8s control

[DBaaS Operation 101](https://github.com/tidbcloud/infra/blob/master/docs/operation-101.md#dbaas-operation-101)
```bash
# in Non-interactive terminal,we need this option to save ak to a secret file.
aws-vault --backend=file add dev

 
# open a terminal which was logined to aws.if we use --backend=file option when add dev,we need to have this option as well.
aws-vault exec dev --no-session
```

## the code files of Pulumi
```bash
*.ts           #typescript code file
index.tx       #enter file
Pulumi.*.yaml  #environment param files
```

## environments for pulumi
```
 
# the environment was bound to param file pulumi.${PROJECT}.yaml

PROJECT=xxx
```

this environment was used to designated project path，we can assign it by `-C ${PULUMI_DIR}` or choise current path by defult.
```bash
export PULUMI_DIR=$PWD
# need config
export PULUMI_CONFIG_PASSPHRASE=wearepingcap
```

## local test 

pulumi service is a space for storing the stack which in `.pulumi`.  
it is a dirctory,which can be a local directory or a s3.  
In develop enviroment,we use s3.  
In local test,we can choise a empty directory.


``` bash
pulumi -C ${PULUMI_DIR} login --cloud-url=file:///tmp/liusx/
# init stack
pulumi -C ${PULUMI_DIR} stack init dev-base
pulumi -C ${PULUMI_DIR} up
```

## login to development environment
the `pulumi service` config was effective global
we need  login to `pulumi service` which was in s3 when we need develop dbaas。
```bash
export PULUMI_DIR=$PWD
BUCKET=${PROJECT}-pulumi-state
pulumi -C ${PULUMI_DIR} login --cloud-url=s3://${BUCKET}
```

## others

### Install dependencies
``` bash
npm install ${xxx}
```
### switch stack
```bash
## stack was bound to ${PROJECT}
pulumi -C ${PULUMI_DIR} stack select
```


### sync state
```bash
# if state was destroyed,we can sync state from cloud
pulumi -C ${PULUMI_DIR} refresh
```
### Destruction of deployment 
destroy application,resource and config which was deployed
```bash
pulumi -C ${PULUMI_DIR} destroy
```

## Deploy Kubernetes Apps Nginx
### New A Project
after login a pulumi service
find a empty directory to create a new pulumi project。
in interactive stty，we need choise
template，project name，stack name。

we choise aws-typtescript template
``` bash
pulumi new

project name: (test) 
project description: (A minimal AWS TypeScript Pulumi program) 
Created project 'test'

stack name: (dev) 
Sorry, could not create stack 'dev': stack 'dev' already exists
stack name: (dev) dev-test
Created stack 'dev-test'

Your new project is ready to go!
```

after new projrect is ready，we can write typescript code to `index.ts`

### A simple pulumi code
deploy a nginx to kubernetes,which can forward http reqeust to kubernetes dashboard.

``` typescript
import * as pulumi from "@pulumi/pulumi";
import * as k8s from "@pulumi/kubernetes";
import * as fs from 'fs';


const config = new pulumi.Config();
// Observability is a component write by DBaaS SRE.
// Currently it responsible for logs collection of the shoot cluster.
const namespace = new k8s.core.v1.Namespace(
  "kubernetes-dashboard-nginx-test",
  {
    metadata: {
      name: "kubernetes-dashboard-nginx-test",
    },
  },
);

const endpoint = "kubernetes-dashboard.kubernetes-dashboard.svc"
const appName = "kubernetes-dashboard-proxy"


const appLabels = { app: appName };
const nginxConfig = fs.readFileSync('nginx.conf', 'utf8');

const dashboardNginxConfigMap = new k8s.core.v1.ConfigMap(appName, {
  metadata: {
    namespace: namespace.metadata.name,
    labels: appLabels
  },
  data: {
    "nginx.conf": nginxConfig
  }
}
)

const dashboardNginxDeployment = new k8s.apps.v1.Deployment(appName, {
  metadata: {
    namespace: namespace.metadata.name
  },
  spec: {
    selector: { matchLabels: appLabels },
    replicas: 1,
    template: {
      metadata: {
        labels: appLabels,

      },
      spec: {
        containers: [{
          name: appName, image: "nginx",
          volumeMounts: [
            {
              mountPath: "/etc/nginx",
              name: "nginx-conf",
            },
          ],
          resources: {
            requests: {
              cpu: "100m",
            },
          },
        }],
        volumes: [
          {
            name: "nginx-conf",
            configMap: {
              name: dashboardNginxConfigMap.metadata.name,
            },
          }
        ]
      }
    }
  }
});

const dashboardNginxService = new k8s.core.v1.Service(appName, {
  metadata: {
    name: appName,
    labels: appLabels,
    namespace: namespace.metadata.name
  },
  spec: {
    type: "ClusterIP",
    ports: [
      {
        port: 9090
      }
    ],
    selector: appLabels
  }
})
```
### Others
#### install kubernetes deployment.
```bash
npm install @pulumi/kubernetes
```

#### Perform the deployment
```
pulumi up
```