Skip to content

Commit

Permalink
Add gin tls and code-generator demo (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
caoyingjunz authored Jun 6, 2023
1 parent 71acc3a commit 745926c
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ go-learning 适用于有一定 `kubernetes` 经验,且想更进一步的同学
- [kubernetes 网络分析](./doc/kubernetes/network.md)
- [kube-proxy 源码分析](./doc/kubernetes/kube-proxy.md)
- [operator 用法详解](./doc/kubernetes/operator.md)
- [code-generator](./doc/kubernetes/code-generator.md)
- [CSI 注册机制源码分析](./doc/kubernetes/csi.md)
- [cloud-provider-openstack](https://github.com/kubernetes/cloud-provider-openstack)

Expand Down
42 changes: 42 additions & 0 deletions doc/kubernetes/code-generator.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# code-generator

- deepcopy-gen: 生成深度拷贝方法,为每个 T 类型生成 func (t* T) DeepCopy() *T 方法,API 类型都需要实现深拷贝
- client-gen: 为资源生成标准的 clientset
- informer-gen: 生成 informer,提供事件机制来响应资源的事件
- lister-gen: 生成 Lister,为 get 和 list 请求提供只读缓存层(通过 indexer 获取)

```shell
git clone https://github.com/kubernetes/code-generator.git
git checkout v0.24.9
```

```shell
# 进行安装
go install ./cmd/{client-gen,deepcopy-gen,informer-gen,lister-gen}
```

```shell
# 验证安装
client-gen -h
```

```shell
代码结构可以参考
https://github.com/kubernetes/sample-controller
```

```shell
$ mkdir test && cd test
$ go mod init test
$ mkdir -p pkg/apis/example.com/v1
test tree
├── go.mod
├── go.sum
└── pkg
└── apis
└── example.com
└── v1
├── doc.go
├── register.go
└── types.go
```
16 changes: 16 additions & 0 deletions practise/gin-practise/tls/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# How to Test

## Generate RSA private key and digital certificate

## Starting server with tls
```shell
go run main.go
```

## Curl with cert
```shell
curl --cacert ./cert/tls.crt https://127.0.0.1:443/welcome

# 正常回显
{"status":"success"}
```
3 changes: 3 additions & 0 deletions practise/gin-practise/tls/cert/tls.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-----BEGIN CERTIFICATE-----
...
-----END CERTIFICATE-----
3 changes: 3 additions & 0 deletions practise/gin-practise/tls/cert/tls.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-----BEGIN PRIVATE KEY-----
...
-----END PRIVATE KEY-----
18 changes: 18 additions & 0 deletions practise/gin-practise/tls/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import (
"net/http"

"github.com/gin-gonic/gin"
)

func main() {
r := gin.Default()
r.GET("/welcome", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"status": "success",
})
})

r.RunTLS(":443", "./cert/tls.crt", "./cert/tls.key")
}

0 comments on commit 745926c

Please sign in to comment.