0

This Kubernetes Pod object has an Env whose value is a string with one single quote '"0.0.0.0/0"'.

    clientSet, err := initClientSet()
    if err != nil {
        klog.ErrorS(err, "failed to init clientSet")
        return err
    }
    ctx := context.Background()

    job := &batchv1.Job{
        TypeMeta: metav1.TypeMeta{
            Kind:       "Job",
            APIVersion: "batch/v1",
        },
        ObjectMeta: metav1.ObjectMeta{
            Name: "tf-poc",
        },
        Spec: batchv1.JobSpec{
            Template: v1.PodTemplateSpec{
                Spec: v1.PodSpec{
                    Containers: []v1.Container{{
                        Name:            "tf-poc",
                        Image:           "nginx:1.9.4",
                        ImagePullPolicy: v1.PullIfNotPresent,
                        Env:             []v1.EnvVar{{Name: "TF_VAR_security_ips", Value: "'\"0.0.0.0/0\"'"}},
                    },
                    },
                    RestartPolicy: v1.RestartPolicyOnFailure,
                },
            },
        },
    }
    j, err := clientSet.BatchV1().Jobs("default").Create(ctx, job, metav1.CreateOptions{})

After it was created by Kubernetes client-go (as above) or Controller-runtime, the one single quote string became three single quotes.

spec:
  backoffLimit: 6
  completions: 1
  parallelism: 1
  selector:
    matchLabels:
      controller-uid: 53c93df0-b3b5-4dbc-b1d8-2a77316176a1
  template:
    metadata:
      creationTimestamp: null
      labels:
        controller-uid: 53c93df0-b3b5-4dbc-b1d8-2a77316176a1
        job-name: tf-poc
    spec:
      containers:
      - env:
        - name: TF_VAR_security_ips
          value: '''"0.0.0.0/0"'''

Here is a Kubernetes Job manifest in a Yaml file.

apiVersion: batch/v1
kind: Job
metadata:
  name: poc
spec:
  backoffLimit: 2147483647
  completions: 1
  parallelism: 1
  template:
    spec:
      containers:
        - command:
            - bash
            - -c
            - tail -f /dev/null
          env:
            - name: TF_VAR_security_ips
              value: '"0.0.0.0/0"'
          image: nginx:1.9.4
          imagePullPolicy: IfNotPresent
          name: terraform-executor
      restartPolicy: OnFailure

If I created it by kubectl apply -f, it worked as expected.

spec:
  backoffLimit: 2147483647
  completions: 1
  parallelism: 1
  selector:
    matchLabels:
      controller-uid: 1525d501-09f4-419e-8989-eb27ea4ddab5
  template:
    metadata:
      creationTimestamp: null
      labels:
        controller-uid: 1525d501-09f4-419e-8989-eb27ea4ddab5
        job-name: poc
    spec:
      containers:
      - command:
        - bash
        - -c
        - tail -f /dev/null
        env:
        - name: TF_VAR_security_ips
          value: '"0.0.0.0/0"'

How can make client-go or controller-runtime not generate three single quotes? Just keep the original number of single quotes.

David Maze
  • 130,717
  • 29
  • 175
  • 215
zzxwill
  • 536
  • 2
  • 6
  • 16

1 Answers1

2

You're getting the correct value from your Go code and just seeing a YAML serialization artifact.

In YAML, strings can be wrapped in single or double quotes. Since the value string starts with a quote character, it has to be quoted so that it's possible to escape the quotes inside the string. The serializer chose single quotes; inside a single-quoted string a double single quote '' is the way to escape a single quote (and other characters can't be escaped).

#      v               v start/end of string quoting
value: '''"0.0.0.0/0"'''
#       ^^           ^^  escaped single quotes

You could equivalently do this with double quotes and it would look exactly like your Go code

#      v             v start/end of string quoting
value: "'\"0.0.0.0\"'"
#        ^^       ^^   escaped double quotes

Your latter examples are not producing the same string. They are a YAML single-quoted string, that contains a string with double quotes but no single quotes. (Try kubectl exec job/poc -- sh -c 'echo $TF_VAR_security_ips' and see what comes back.)

David Maze
  • 130,717
  • 29
  • 175
  • 215