1

I have to check if my application container in a replication controller runs on a certain port. Here is the command with the go template string that I'm using.

kubectl get rc my-rc --namespace=nightly --template='{{range .spec.template.spec.containers}}{{if .ports}}{{range .ports}}{{if .containerPort}}{{if eq .containerPort 5445}}{{end}}{{end}}{{end}}{{end}}{{end}}'

I think it is not a string comparison since it is a port. even string comparison throws an error "error calling eq: incompatible types for comparison'"

I could just fetch an array of container ports and do the comparison outside but want to get it done inside the go template.

I am new to Go lang. Appreciate any suggestions to accomplish this using template string or using a template file.. Thanks

4

1 回答 1

2

检查 .containerPortprintf "%T" .containerPort表明它是一个 float64。如果您将端口与尾随进行比较,5445.0它应该可以工作。

您还有一些不必要的 if 语句。

--template='{{range .spec.template.spec.containers}}{{range .ports}}{{if eq .containerPort 5445.0}}True{{end}}{{end}}{{end}}'

您的示例还缺少将-o="go-template"输出指定为 Go 模板的标志。

于 2016-03-05T18:47:59.857 回答