kubectl使用模板字符串的get命令在比较时出现不兼容类型的错误。

huangapple go评论66阅读模式
英文:

kubectl get with template string fails with incompatible types for comparison

问题

我必须检查我的应用程序容器在复制控制器上是否运行在特定的端口上。这是我正在使用的带有go模板字符串的命令。

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}}'

我认为这不是一个字符串比较,因为它是一个端口。即使是字符串比较也会抛出一个错误"error calling eq: incompatible types for comparison"

我可以只获取一个容器端口的数组,然后在外部进行比较,但是我想在go模板内部完成。

我对Go语言还不熟悉。感谢任何使用模板字符串或模板文件完成此任务的建议。谢谢。

英文:

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

答案1

得分: 3

检查.containerPort时,使用printf "%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模板。

英文:

Inspecting the .containerPort with printf "%T" .containerPort shows that it's a float64. if you compare your port with a trailing 5445.0 it should work.

You also have some unnecessary if statements.

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

Your example was also missing the -o="go-template" flag to specify the output as a Go template.

huangapple
  • 本文由 发表于 2016年3月6日 02:34:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/35818105.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定