# Tanzu - TAP at first glance
時間:2023/04/11 10:00~12:00
講師:VMware Tim/Timmy
目標:執行TAP InnerLoop,並在本機電腦安裝相依套件
## 安裝 k8s 相關環境套件
相關軟體放在 V:\科室資料夾\資訊室\不分組\52_TANZU\
### 安裝 Docker Desktop
BIOS 的 hypervisor 要啟用
1. With WSL(如要安裝WSL2則WIN10的組建要19041和更新版本)
https://learn.microsoft.com/zh-tw/windows/wsl/install
2. With HyperV
### 安裝 Tilt
1. 解壓縮 tilt.0.32.0.windows.x86_64.zip
2. 目錄加到 search path

### 安裝 Kubectl
將 kubectl-windows-v1.24.9+vmware.exe\kubectl.exe 加到 search path

### 測試是否可以連到 K8s 環境
1. 在自已的家目錄(如:C:\Users\13766\)下新增一個資料夾
```cmd
> md .kube
```

2. 將一份 k8s 環境的設定檔複制到 .kube 資料夾
複製 config 到 .kube 目錄下

3. 測試一下是否可以看的到 pod
```cmd
> kubectl get po -A
```

4. tap-dev 是開發用的 namespace
```cmd
> kubectl get po -n tap-dev
```

## 安裝開發相關環境套件
### 安裝 tanzu cli
解壓縮 tanzu-framework-windows-amd64-v0.25.4.1.zip


將 cli\core\v0.25.4\tanzu-core-windows_amd64.exe 重新命名為 tanzu.exe,並加到 search path

新增環境變數 TANZU_CLI_NO_INIT = True


tanzu plugin list
```text
NAME DESCRIPTION SCOPE DISCOVERY VERSION STATUS
login Login to the platform Standalone default v0.25.4 not installed
management-cluster Kubernetes management-cluster operations Standalone default v0.25.4 not installed
package Tanzu package management Standalone default v0.25.4 not installed
pinniped-auth Pinniped authentication operations (usually not directly invoked) Standalone default v0.25.4 not installed
secret Tanzu secret management Standalone default v0.25.4 not installed
telemetry Configure cluster-wide telemetry settings Standalone default v0.25.4 not installed
```
使用 tanzu cli 安裝 plugin
```cmd
> tanzu plugin install --local cli all # tanzu plugin install --local C:\tools\cli all
```
```text
PS C:\Users\13766> tanzu plugin install --local C:\tools\cli all
Installing plugin 'accelerator:v1.4.0'
Installing plugin 'apps:v0.10.0'
Installing plugin 'insight:v1.4.2'
Installing plugin 'package:v0.25.4'
Installing plugin 'secret:v0.25.4'
Installing plugin 'services:v0.5.0'
✔ successfully installed 'all' plugin
```
安裝後多了一些 plugin
```text
PS C:\Users\13766> tanzu plugin list
NAME DESCRIPTION SCOPE DISCOVERY VERSION STATUS
login Login to the platform Standalone default v0.25.4 not installed
management-cluster Kubernetes management-cluster operations Standalone default v0.25.4 not installed
package Tanzu package management Standalone default v0.25.4 installed
pinniped-auth Pinniped authentication operations (usually not directly invoked) Standalone default v0.25.4 not installed
secret Tanzu secret management Standalone default v0.25.4 installed
telemetry Configure cluster-wide telemetry settings Standalone default v0.25.4 not installed
apps Applications on Kubernetes Standalone v0.10.0 installed
insight post & query image, package, source, and vulnerability data Standalone v1.4.2 installed
services Commands for working with service instances, classes and claims Standalone v0.5.0 installed
accelerator Manage accelerators in a Kubernetes cluster Standalone v1.4.0 installed
```

### 安裝 vscode plugin,共 4 個套件
1. redhat.java-1.17.2023040704@win32-x64.vsix
2. redhat.vscode-yaml-1.12.2.vsix
3. vscjava.vscode-java-debug-0.49.2023032407.vsix
4. tanzu-vscode-extension-0.13.0.vsix (offline install)
名稱: Debugger for Java
識別碼: vscjava.vscode-java-debug
描述: A lightweight Java debugger for Visual Studio Code
版本: 0.49.1
發行者: Microsoft
VS Marketplace 連結: https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-java-debug
名稱: Language Support for Java(TM) by Red Hat
識別碼: redhat.java
描述: Java Linting, Intellisense, formatting, refactoring, Maven/Gradle support and more...
版本: 1.17.2023040704
發行者: Red Hat
VS Marketplace 連結: https://marketplace.visualstudio.com/items?itemName=redhat.java
名稱: YAML
識別碼: redhat.vscode-yaml
描述: YAML Language Support by Red Hat, with built-in Kubernetes syntax support
版本: 1.12.2
發行者: Red Hat
VS Marketplace 連結: https://marketplace.visualstudio.com/items?itemName=redhat.vscode-yaml
名稱: Tanzu Developer Tools
識別碼: VMware.tanzu
描述: Rapid app development on Kubernetes using the Tanzu Application Platform
版本: 0.13.0
發行者: VMware


### 調整 plugin 設定
+ tanzu plugin

"tanzu.enableLiveHover": true
"tanzu.namespace": "tap-dev"
"tanzu.sourceImage": "harbordev.cych.org.tw/liveupdate/12477"
+ extension: java

關鍵字:server
"java.server.launchMode": "Standard"
https://learn.microsoft.com/zh-tw/java/openjdk/download
### 安裝 Java
+ #Install OpenJDK
https://learn.microsoft.com/en-us/java/openjdk/download
or
+ winget install Microsoft.OpenJDK.17
https://learn.microsoft.com/zh-tw/windows/package-manager/winget/




## 用個測試程式來驗證一下
程式放在 weatherforecast-steeltoe\,複製回來用 VSCode 開啟專案
### 修改部署設定檔
將所有 `weatherforecast-steeltoe` 的名字改成自己的專案名字 `weatherforecast-steeltoe-13766`
修改 **config\workload.yaml**
```yaml=workload.yaml
apiVersion: carto.run/v1alpha1
kind: Workload
metadata:
name: weatherforecast-steeltoe-13766
labels:
apps.tanzu.vmware.com/workload-type: web
app.kubernetes.io/part-of: weatherforecast-steeltoe-13766
tanzu.app.live.view: "true"
tanzu.app.live.view.application.flavours: steeltoe
tanzu.app.live.view.application.name: weatherforecast-steeltoe-13766
spec:
params:
- name: buildServiceBindings
value:
- kind: Secret
name: nuget-config
- name: annotations
value:
autoscaling.knative.dev/minScale: "1"
source:
git:
url: https://github.com/timwangvmw/dotnet
ref:
branch: main
```

修改 **Tiltfile**
```python
SOURCE_IMAGE = os.getenv("SOURCE_IMAGE", default='your-registry.io/project/steeltoe-weatherforecast-source') # update registry
LOCAL_PATH = os.getenv("LOCAL_PATH", default='.')
NAMESPACE = os.getenv("NAMESPACE", default='default')
OUTPUT_TO_NULL_COMMAND = os.getenv("OUTPUT_TO_NULL_COMMAND", default=' > /dev/null ')
NAME = "weatherforecast-steeltoe-13766"
local_resource(
'build --no-restore',
'dotnet publish -c Debug -o ./bin/out',
deps=['.'],
ignore=['./obj', './bin', './.vscode', './out'],
)
k8s_custom_deploy(
NAME,
apply_cmd="tanzu apps workload apply -f config/workload.yaml --update-strategy replace --debug --live-update" +
" --local-path " + LOCAL_PATH +
" --source-image " + SOURCE_IMAGE +
" --build-env BP_DEBUG_ENABLED=true" +
" --namespace " + NAMESPACE +
" --yes --registry-ca-cert ca-harbor.crt" +
OUTPUT_TO_NULL_COMMAND +
" && kubectl get workload " + NAME + " --namespace " + NAMESPACE + " -o yaml",
delete_cmd="tanzu apps workload delete " + NAME + " --namespace " + NAMESPACE + " --yes",
deps=['./bin'],
container_selector='workload',
live_update=[
sync('./bin', '/workspace')
]
)
k8s_resource(NAME, port_forwards=["8080:8080"],
extra_pod_selectors=[{'carto.run/workload-name': 'weatherforecast-steeltoe-13766', 'app.kubernetes.io/component': 'run'}])
allow_k8s_contexts('wld01-dev-admin@wld01-dev') # update the context
```

### 試 Run
在 Tiltfile 按右鍵 -> **Live Update start**
因為沒有 docker login 所以會失敗

失敗了

### 安裝憑證 ca-harbor.crt
安裝完憑證需要重啟 docker desktop
每次重啟 docker desktop 都要做一次 docker login
直接點2下 ca-harbor.crt 就可以安裝憑證,一直按下一步到完成安裝

完成

### 執行 docker login
```cmd
D:\proj\weatherforecast-steeltoe>docker login harbordev.cych.org.tw
Username: admin
Password:
Login Succeeded
```
帳號/密碼之後會在給

只要 docker desktop 重啟,就要做一次 docker login 的動件

### 看一下開發環境中有那些 workload
```cmd
C:\Users\13766>tanzu app workload list -n tap-dev
NAME TYPE APP READY AGE
weatherforecast-steeltoe-06481 web weatherforecast-steeltoe-06481 HealthyConditionRule 6m53s
weatherforecast-steeltoe-12382 web weatherforecast-steeltoe-12382 Ready 7m1s
weatherforecast-steeltoe-12477 web weatherforecast-steeltoe-12477 Ready 118s
weatherforecast-steeltoe-13766 web weatherforecast-steeltoe-13766 HealthyConditionRule 7m23s
weatherforecast-steeltoe-admin13752 web weatherforecast-steeltoe-admin13752 Ready 6m22s
```
### 再執行一次 Live Update start
可以用瀏覽器去看,從網址看起來是 localhost,其實底層有做 port forward
所以實際是是連到 tanzudev.cych.org.tw
Tilt started on http://localhost:10350/
v0.32.0, built 2023-03-13
Initial Build
Loading Tiltfile at: D:\proj\weatherforecast-steeltoe\Tiltfile
Successfully loaded Tiltfile (2.2132ms)
### 停止 workload

## 後記
### InnerLoop 的階段
1. 在本機,打包 source code 成 OCI 格式,藉由 docker push 到 harbor
2. 在遠端,抓取 source code 給 TBS 建置成映像檔,最後 deploy 到 tanzudev.cych.org.tw 環境
### 認證資訊
1. k8s context 要換成非 admin
2. docker login 要換成 robot account
3. 要安裝 tanzudev 的 ca
### 工程師隨手
