简体   繁体   English

如何比较Go模板中的字符串?

[英]How to compare strings in go templates?

I'd like to show entries that have .metadata.labels.app set to "myapp" value. 我想显示将.metadata.labels.app设置为"myapp"值的条目。

Command: 命令:

kubectl get pods -o go-template --template="{{range .items}}{{if eq .metadata.labels.app "myapp"}}{{.metadata.name}} {{end}}{{end}}"

It gives an error: 它给出了一个错误:

output:1: function "myapp" not defined 输出:1:未定义函数“ myapp”

The structures look like this: 结构如下所示:

- apiVersion: v1
  kind: Pod
  metadata:
    creationTimestamp: 2017-09-15T08:18:26Z
    generateName: myapp-2830765207-
    labels:
      app: myapp
      pod-template-hash: "2830765207"
    name: myapp-2830765207-dh359
    namespace: default

I haven't used kubetcl before, but I am familiar with shell commands in general, from which I can tell you one thing that's going wrong, and maybe that's all you need. 我以前没有使用过kubetcl,但是我通常对Shell命令很熟悉,从中我可以告诉您一件出错的事情,也许这就是您所需要的。 (I'm also somewhat familiar with Go templates, and your string comparison looks fine to me.) By using double quotes around your template and within your template, you're actually closing the string you're passing in as the template at the first double quote in "myapp" . (我对Go模板也有些熟悉,对我来说,您的字符串比较看起来不错。)通过在模板周围和模板中使用双引号,实际上是在关闭作为模板传递的字符串。 "myapp"第一个双引号。 Using single quotes around the template should help: 在模板周围使用单引号应有帮助:

kubectl get pods -o go-template --template='{{range .items}}{{if eq .metadata.labels.app "myapp"}}{{.metadata.name}} {{end}}{{end}}'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM