apiserver 对接github webhook

1.首先写一个监听的后台服务,比如监听在3000端口。

package main
import (
        "context"
        "encoding/json"
        "log"
        "net/http"
        "github.com/google/go-github/github"
        "golang.org/x/oauth2"
        authentication "k8s.io/api/authentication/v1beta1"
)

func main() {
        http.HandleFunc("/authenticate", func(w http.ResponseWriter, r *http.Request) {
                decoder := json.NewDecoder(r.Body)
                var tr authentication.TokenReview
                err := decoder.Decode(&tr)
                if err != nil {
                        log.Println("[Error]", err.Error())
                        w.WriteHeader(http.StatusBadRequest)
                        json.NewEncoder(w).Encode(map[string]interface{}{
                                "apiVersion": "authentication.k8s.io/v1beta1",
                                "kind":       "TokenReview",
                                "status": authentication.TokenReviewStatus{
                                        Authenticated: false,
                                },
                        })
                        return
                }
                log.Print("receving request")
                // Check User
                ts := oauth2.StaticTokenSource(
                        &oauth2.Token{AccessToken: tr.Spec.Token},
                )
                //得出一个带token信息的tokenresource
                tc := oauth2.NewClient(context.Background(), ts)
                //新建github的client
                client := github.NewClient(tc)
                user, _, err := client.Users.Get(context.Background(), "")
                if err != nil {
                        log.Println("[Error]", err.Error())
                        w.WriteHeader(http.StatusUnauthorized)
                        json.NewEncoder(w).Encode(map[string]interface{}{
                                "apiVersion": "authentication.k8s.io/v1beta1",
                                "kind":       "TokenReview",
                                "status": authentication.TokenReviewStatus{
                                        Authenticated: false,
                                },
                        })
                        return
                }
                log.Printf("[Success] login as %s", *user.Login)
                w.WriteHeader(http.StatusOK)
                trs := authentication.TokenReviewStatus{
                        Authenticated: true,
                        User: authentication.UserInfo{
                                Username: *user.Login,
                                UID:      *user.Login,
                        },
                }
                json.NewEncoder(w).Encode(map[string]interface{}{
                        "apiVersion": "authentication.k8s.io/v1beta1",
                        "kind":       "TokenReview",
                        "status":     trs,
                })
        })
        log.Println(http.ListenAndServe(":3000", nil))
}         
  1. 创建配置文件
    mkdir /etc/config cp web-config.json /etc/config
{
  "kind": "Config",
  "apiVersion": "v1",
  "preferences": {},
  "clusters": [
    {
      "name": "github-authn",
      "cluster": {
        "server": "http://192.168.34.2:3000/authenticate"
      }
    }
  ],
  "users": [
    {
      "name": "authn-apiserver",
      "user": {
        "token": "secret"
      }
    }
  ],
  "contexts": [
    {
      "name": "webhook",
      "context": {
        "cluster": "github-authn",
        "user": "authn-apiserver"
      }
    }
  ],
  "current-context": "webhook"
}
  1. 注册对应的config到/etc/kubernetes/manifest/kube-apiserver.yaml
- --authentication-token-webhook-config-file=/etc/config/webhook-config.json
以及
volumeMount下添加

   - mountPath: /etc/config
     - name: webhook-config
     readOnly: true
volumes:
    - hostPath:
        path: /etc/config
        type: DirectoryOrCreate
      name: webhook-config

启动服务

在 ~/.kube/config 中加一个user ,并加上token
- name: myuser1
  user:
    token: ******** #在github上生成的                                                     
root@cloud:~# kubectl get po --user myuser1
Error from server (Forbidden): pods is forbidden: User "wuyongmax" cannot list resource "pods" in API group "" in the namespace "default"
 ./authn-webhook 
2022/11/29 11:54:48 receving request
2022/11/29 11:54:49 [Success] login as wuyongmax
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值