doumiang2297
doumiang2297
2016-03-05 18:34

带有模板字符串的kubectl get失败,类型不兼容,无法进行比较

  • kubernetes
已采纳

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条回答

  • doujiao1948 doujiao1948 5年前

    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.

    点赞 评论 复制链接分享