跳到主要内容

25.minionRegistry

PR #180

make minion first class [主机也是第一类成员]

之前我们的主机是通过 命令行参数直接传给 master,既不能动态修改,也不能通过 cloudcfg 来打印,这个pr将 minion 也变成和pod 一样的资源,但是目前没有存到etcd,存到了内存 stringset中

// Keep track of a set of minions. Safe for concurrent reading/writing.
type MinionRegistry interface {
List() (currentMinions []string, err error)
Insert(minion string) error
Delete(minion string) error
Contains(minion string) (bool, error)
}

// Initialize a minion registry with a list of minions.
func MakeMinionRegistry(minions []string) MinionRegistry {
m := &minionList{
minions: stringSet{},
}
for _, minion := range minions {
m.minions.insert(minion)
}
return m
}


type empty struct{}
type stringSet map[string]empty

func (s stringSet) insert(item string) {
s[item] = empty{}
}

func (s stringSet) delete(item string) {
delete(s, item)
}

func (s stringSet) has(item string) bool {
_, contained := s[item]
return contained
}

PR #205

reflector client

  1. 给client 添加 new 构造方法,首先生成 client,而不是每次都生成
  2. 使用新的链式调用方法
  3. 链式调用添加 GET result 方法
  4. select 变成 ParseSelector
  5. 有时候服务器会返回202,fix the bug

PR #217

Create a proxy server using cloudcfg.  Useful for demos [本pr实现了 kubectl portforward 原型,最初是为了方便演示demo]

    if *proxy {
server := cloudcfg.ProxyServer{
Host: *httpServer,
Client: &http.Client{},
}
http.Handle("/api/", &server)
log.Fatal(http.ListenAndServe(":8001", nil))
}


type ProxyServer struct {
Host string
Client *http.Client
}

func (s *ProxyServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
req, err := http.NewRequest(r.Method, s.Host+r.URL.Path, r.Body)
if err != nil {
log.Printf("Error: %#v", err)
return
}
res, err := s.Client.Do(req)
if err != nil {
log.Printf("Error: %#v", err)
return
}
w.WriteHeader(res.StatusCode)
defer res.Body.Close()
data, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Printf("Error: %#v", err)
return
}
w.Write(data)
}