8
persistentvolumeclaim := &apiv1.PersistentVolumeClaim{
        ObjectMeta: metav1.ObjectMeta{
            Name: "mysql-pv-claim",
        },
        Spec: apiv1.PersistentVolumeClaimSpec{
            StorageClassName: "manual",
            },
    }

StorageClassName parameter takes pointer to string, but compiler gives error when i'm passing string "manual" into it.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
manjeet
  • 121
  • 1
  • 2
  • 5

2 Answers2

22

You cannot get the address of a string constant/literal, but if you have a string local variable (set to the value you want) you can then pass the address of that local:

Declare a string local first and assign the constant string literal to it, then pass the address of that local as the parameter argument with the & operator:

persistentvolumeclaim := &apiv1.PersistentVolumeClaim {

        manualStr := "manual"

        ObjectMeta: metav1.ObjectMeta {
            Name: "mysql-pv-claim",
        },
        Spec: apiv1.PersistentVolumeClaimSpec {
            StorageClassName: &manualStr,
        },
    }
Dai
  • 141,631
  • 28
  • 261
  • 374
0

Use utils.StringPtr:

persistentvolumeclaim := &apiv1.PersistentVolumeClaim{
        ObjectMeta: metav1.ObjectMeta{
            Name: "mysql-pv-claim",
        },
        Spec: apiv1.PersistentVolumeClaimSpec{
            StorageClassName: utils.StringPtr("manual"),
            },
    }
thwd
  • 11
  • Now it's showing :- undeclared name: utils. What package i have to import for this. it shows error when i import this package: "github.com/kubernetes/utils/pointer" – manjeet Apr 03 '20 at 04:11
  • Don't import an entire package to get a one line function. You can write that function in your code if you need to do this more than a couple times. – JimB Apr 03 '20 at 15:34