2015-10-07 68 views
3

我使用kube去客户端用kube api来访问kube数据。我目前没有找到任何特定吊舱日志的API调用。Kubernetes去客户端api登录特定的pod

kubectl logs pod-name 

返回特定吊舱的日志。我如何使用go客户端来做到这一点? 我正在使用kubernetes的v1.0.6。

我可以用

client.Pods("namespace").Get("pod-name") 

回答

4

看着kubectl如何实现它的命令获得有关如何使用客户端库的感觉时很有帮助得到吊舱。在这种情况下,kubectl's implementation of the logs command看起来像这样:

req := client.RESTClient.Get(). 
    Namespace(namespace). 
    Name(podID). 
    Resource("pods"). 
    SubResource("log"). 
    Param("follow", strconv.FormatBool(logOptions.Follow)). 
    Param("container", logOptions.Container). 
    Param("previous", strconv.FormatBool(logOptions.Previous)). 
    Param("timestamps", strconv.FormatBool(logOptions.Timestamps)) 

if logOptions.SinceSeconds != nil { 
    req.Param("sinceSeconds", strconv.FormatInt(*logOptions.SinceSeconds, 10)) 
} 
if logOptions.SinceTime != nil { 
    req.Param("sinceTime", logOptions.SinceTime.Format(time.RFC3339)) 
} 
if logOptions.LimitBytes != nil { 
    req.Param("limitBytes", strconv.FormatInt(*logOptions.LimitBytes, 10)) 
} 
if logOptions.TailLines != nil { 
    req.Param("tailLines", strconv.FormatInt(*logOptions.TailLines, 10)) 
} 
readCloser, err := req.Stream() 
if err != nil { 
    return err 
} 

defer readCloser.Close() 
_, err = io.Copy(out, readCloser) 
return err 
+0

这对我来说完全适用,兄弟。谢谢。 – sadlil