|
59 | 59 | const ( |
60 | 60 | // LastAppliedConfigAnnotation is annotation key of last applied configuration |
61 | 61 | LastAppliedConfigAnnotation = "pingcap.com/last-applied-configuration" |
| 62 | + |
| 63 | + // SATokenProjectionVolumeName is the name of the projected service account token volume. |
| 64 | + SATokenProjectionVolumeName = "kube-api-access" |
| 65 | + // SATokenProjectionMountPath is the standard Kubernetes service account token mount path. |
| 66 | + SATokenProjectionMountPath = "/var/run/secrets/kubernetes.io/serviceaccount" // nolint:gosec |
62 | 67 | ) |
63 | 68 |
|
| 69 | +// SATokenProjectionVolume returns a projected volume that replicates the three files |
| 70 | +// that rest.InClusterConfig() reads from /var/run/secrets/kubernetes.io/serviceaccount: |
| 71 | +// token, ca.crt, and namespace. Use this when automountServiceAccountToken is false |
| 72 | +// but the container still needs to call the Kubernetes API. |
| 73 | +func SATokenProjectionVolume() corev1.Volume { |
| 74 | + expirationSeconds := int64(3607) |
| 75 | + return corev1.Volume{ |
| 76 | + Name: SATokenProjectionVolumeName, |
| 77 | + VolumeSource: corev1.VolumeSource{ |
| 78 | + Projected: &corev1.ProjectedVolumeSource{ |
| 79 | + Sources: []corev1.VolumeProjection{ |
| 80 | + { |
| 81 | + ServiceAccountToken: &corev1.ServiceAccountTokenProjection{ |
| 82 | + Path: "token", |
| 83 | + ExpirationSeconds: &expirationSeconds, |
| 84 | + }, |
| 85 | + }, |
| 86 | + { |
| 87 | + ConfigMap: &corev1.ConfigMapProjection{ |
| 88 | + LocalObjectReference: corev1.LocalObjectReference{Name: "kube-root-ca.crt"}, |
| 89 | + Items: []corev1.KeyToPath{ |
| 90 | + {Key: "ca.crt", Path: "ca.crt"}, |
| 91 | + }, |
| 92 | + }, |
| 93 | + }, |
| 94 | + { |
| 95 | + DownwardAPI: &corev1.DownwardAPIProjection{ |
| 96 | + Items: []corev1.DownwardAPIVolumeFile{ |
| 97 | + { |
| 98 | + Path: "namespace", |
| 99 | + FieldRef: &corev1.ObjectFieldSelector{ |
| 100 | + APIVersion: "v1", |
| 101 | + FieldPath: "metadata.namespace", |
| 102 | + }, |
| 103 | + }, |
| 104 | + }, |
| 105 | + }, |
| 106 | + }, |
| 107 | + }, |
| 108 | + }, |
| 109 | + }, |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +// SATokenProjectionVolumeMount returns the VolumeMount for SATokenProjectionVolume. |
| 114 | +func SATokenProjectionVolumeMount() corev1.VolumeMount { |
| 115 | + return corev1.VolumeMount{ |
| 116 | + Name: SATokenProjectionVolumeName, |
| 117 | + MountPath: SATokenProjectionMountPath, |
| 118 | + ReadOnly: true, |
| 119 | + } |
| 120 | +} |
| 121 | + |
64 | 122 | func GetOrdinalFromPodName(podName string) (int32, error) { |
65 | 123 | ordinalStr := podName[strings.LastIndex(podName, "-")+1:] |
66 | 124 | ordinalInt, err := strconv.ParseInt(ordinalStr, 10, 32) |
|
0 commit comments