|
| 1 | +package resources |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + corev1 "k8s.io/api/core/v1" |
| 8 | + |
| 9 | + paperclipv1alpha1 "github.com/paperclipinc/paperclip-operator/api/v1alpha1" |
| 10 | +) |
| 11 | + |
| 12 | +const ( |
| 13 | + // DefaultTailscaleImage is the default image repository for the Tailscale sidecar. |
| 14 | + DefaultTailscaleImage = "ghcr.io/tailscale/tailscale" |
| 15 | + // DefaultTailscaleTag is the default Tailscale sidecar image tag. |
| 16 | + DefaultTailscaleTag = "stable" |
| 17 | + // DefaultTailscaleAuthKeyKey is the default Secret key holding the auth key. |
| 18 | + DefaultTailscaleAuthKeyKey = "authkey" |
| 19 | + |
| 20 | + // TailscaleContainerName is the name of the Tailscale sidecar container. |
| 21 | + TailscaleContainerName = "tailscale" |
| 22 | + |
| 23 | + // TailscaleModeServe exposes the instance to tailnet members only. |
| 24 | + TailscaleModeServe = "serve" |
| 25 | + // TailscaleModeFunnel exposes the instance to the public internet via Funnel. |
| 26 | + TailscaleModeFunnel = "funnel" |
| 27 | + |
| 28 | + // TailscaleStatePath is the directory where tailscaled stores state. Placed |
| 29 | + // under an emptyDir so tailscaled owns the path with a read-only root fs. |
| 30 | + TailscaleStatePath = "/tmp/tailscale" |
| 31 | + // TailscaleSocketDir is the directory containing the tailscaled Unix socket. |
| 32 | + TailscaleSocketDir = "/var/run/tailscale" |
| 33 | + // TailscaleSocketPath is the full path to the tailscaled Unix socket. |
| 34 | + TailscaleSocketPath = "/var/run/tailscale/tailscaled.sock" |
| 35 | + |
| 36 | + // TailscaleServeConfigKey is the ConfigMap data key for the serve config JSON. |
| 37 | + TailscaleServeConfigKey = "tailscale-serve.json" |
| 38 | + // tailscaleSocketVolumeName is the emptyDir volume holding the tailscaled socket. |
| 39 | + tailscaleSocketVolumeName = "tailscale-socket" |
| 40 | + // tailscaleStateVolumeName is the emptyDir volume holding tailscaled state. |
| 41 | + tailscaleStateVolumeName = "tailscale-tmp" |
| 42 | + // tailscaleConfigVolumeName is the ConfigMap volume holding the serve config. |
| 43 | + tailscaleConfigVolumeName = "tailscale-config" |
| 44 | +) |
| 45 | + |
| 46 | +// TailscaleConfigMapName returns the name of the Tailscale serve-config ConfigMap. |
| 47 | +func TailscaleConfigMapName(instance *paperclipv1alpha1.Instance) string { |
| 48 | + return instance.Name + "-tailscale" |
| 49 | +} |
| 50 | + |
| 51 | +// tailscaleServeConfig is the JSON structure for TS_SERVE_CONFIG. |
| 52 | +type tailscaleServeConfig struct { |
| 53 | + TCP map[string]*tailscaleTCPHandler `json:"TCP"` |
| 54 | + Web map[string]*tailscaleWebConfig `json:"Web,omitempty"` |
| 55 | + // AllowFunnel controls whether Tailscale Funnel (public internet) is enabled. |
| 56 | + AllowFunnel map[string]bool `json:"AllowFunnel,omitempty"` |
| 57 | +} |
| 58 | + |
| 59 | +type tailscaleTCPHandler struct { |
| 60 | + HTTPS bool `json:"HTTPS"` |
| 61 | +} |
| 62 | + |
| 63 | +type tailscaleWebConfig struct { |
| 64 | + Handlers map[string]*tailscaleWebHandler `json:"Handlers"` |
| 65 | +} |
| 66 | + |
| 67 | +type tailscaleWebHandler struct { |
| 68 | + Proxy string `json:"Proxy"` |
| 69 | +} |
| 70 | + |
| 71 | +// GetTailscaleImage returns the full Tailscale sidecar image reference. |
| 72 | +func GetTailscaleImage(instance *paperclipv1alpha1.Instance) string { |
| 73 | + repo := instance.Spec.Tailscale.Image.Repository |
| 74 | + if repo == "" { |
| 75 | + repo = DefaultTailscaleImage |
| 76 | + } |
| 77 | + if instance.Spec.Tailscale.Image.Digest != "" { |
| 78 | + return repo + "@" + instance.Spec.Tailscale.Image.Digest |
| 79 | + } |
| 80 | + tag := instance.Spec.Tailscale.Image.Tag |
| 81 | + if tag == "" { |
| 82 | + tag = DefaultTailscaleTag |
| 83 | + } |
| 84 | + return repo + ":" + tag |
| 85 | +} |
| 86 | + |
| 87 | +// TailscaleHostname returns the configured Tailscale device name, defaulting to |
| 88 | +// the instance name. |
| 89 | +func TailscaleHostname(instance *paperclipv1alpha1.Instance) string { |
| 90 | + if instance.Spec.Tailscale.Hostname != "" { |
| 91 | + return instance.Spec.Tailscale.Hostname |
| 92 | + } |
| 93 | + return instance.Name |
| 94 | +} |
| 95 | + |
| 96 | +// BuildTailscaleServeConfig returns the TS_SERVE_CONFIG JSON that proxies the |
| 97 | +// tailnet HTTPS endpoint to the local Paperclip app port. Funnel is enabled |
| 98 | +// when mode is "funnel". |
| 99 | +func BuildTailscaleServeConfig(instance *paperclipv1alpha1.Instance) string { |
| 100 | + proxy := fmt.Sprintf("http://127.0.0.1:%d", servicePort(instance)) |
| 101 | + |
| 102 | + cfg := tailscaleServeConfig{ |
| 103 | + TCP: map[string]*tailscaleTCPHandler{ |
| 104 | + "443": {HTTPS: true}, |
| 105 | + }, |
| 106 | + Web: map[string]*tailscaleWebConfig{ |
| 107 | + "${TS_CERT_DOMAIN}:443": { |
| 108 | + Handlers: map[string]*tailscaleWebHandler{ |
| 109 | + "/": {Proxy: proxy}, |
| 110 | + }, |
| 111 | + }, |
| 112 | + }, |
| 113 | + } |
| 114 | + |
| 115 | + mode := instance.Spec.Tailscale.Mode |
| 116 | + if mode == "" { |
| 117 | + mode = TailscaleModeServe |
| 118 | + } |
| 119 | + if mode == TailscaleModeFunnel { |
| 120 | + cfg.AllowFunnel = map[string]bool{ |
| 121 | + "${TS_CERT_DOMAIN}:443": true, |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + data, _ := json.Marshal(cfg) |
| 126 | + return string(data) |
| 127 | +} |
| 128 | + |
| 129 | +// BuildTailscaleContainer builds the ephemeral Tailscale sidecar container that |
| 130 | +// runs userspace tailscaled and Serves the Paperclip app over the tailnet. |
| 131 | +func BuildTailscaleContainer(instance *paperclipv1alpha1.Instance) corev1.Container { |
| 132 | + env := []corev1.EnvVar{ |
| 133 | + {Name: "TS_USERSPACE", Value: "true"}, |
| 134 | + {Name: "TS_STATE_DIR", Value: TailscaleStatePath}, |
| 135 | + {Name: "TS_SOCKET", Value: TailscaleSocketPath}, |
| 136 | + {Name: "TS_SERVE_CONFIG", Value: "/etc/tailscale/serve/" + TailscaleServeConfigKey}, |
| 137 | + {Name: "TS_HOSTNAME", Value: TailscaleHostname(instance)}, |
| 138 | + // Ephemeral nodes are removed from the tailnet when the pod is deleted. |
| 139 | + {Name: "TS_EXTRA_ARGS", Value: "--ephemeral"}, |
| 140 | + } |
| 141 | + |
| 142 | + if instance.Spec.Tailscale.AuthKey != nil { |
| 143 | + key := instance.Spec.Tailscale.AuthKey.Key |
| 144 | + if key == "" { |
| 145 | + key = DefaultTailscaleAuthKeyKey |
| 146 | + } |
| 147 | + env = append(env, corev1.EnvVar{ |
| 148 | + Name: "TS_AUTHKEY", |
| 149 | + ValueFrom: &corev1.EnvVarSource{ |
| 150 | + SecretKeyRef: &corev1.SecretKeySelector{ |
| 151 | + LocalObjectReference: instance.Spec.Tailscale.AuthKey.SecretRef, |
| 152 | + Key: key, |
| 153 | + }, |
| 154 | + }, |
| 155 | + }) |
| 156 | + } |
| 157 | + |
| 158 | + return corev1.Container{ |
| 159 | + Name: TailscaleContainerName, |
| 160 | + Image: GetTailscaleImage(instance), |
| 161 | + ImagePullPolicy: corev1.PullIfNotPresent, |
| 162 | + Env: env, |
| 163 | + VolumeMounts: []corev1.VolumeMount{ |
| 164 | + { |
| 165 | + Name: tailscaleSocketVolumeName, |
| 166 | + MountPath: TailscaleSocketDir, |
| 167 | + }, |
| 168 | + { |
| 169 | + Name: tailscaleConfigVolumeName, |
| 170 | + MountPath: "/etc/tailscale/serve/" + TailscaleServeConfigKey, |
| 171 | + SubPath: TailscaleServeConfigKey, |
| 172 | + ReadOnly: true, |
| 173 | + }, |
| 174 | + { |
| 175 | + Name: tailscaleStateVolumeName, |
| 176 | + MountPath: "/tmp", |
| 177 | + }, |
| 178 | + }, |
| 179 | + Resources: instance.Spec.Tailscale.Resources, |
| 180 | + SecurityContext: &corev1.SecurityContext{ |
| 181 | + AllowPrivilegeEscalation: Ptr(false), |
| 182 | + ReadOnlyRootFilesystem: Ptr(true), |
| 183 | + RunAsNonRoot: Ptr(true), |
| 184 | + Capabilities: &corev1.Capabilities{ |
| 185 | + Drop: []corev1.Capability{"ALL"}, |
| 186 | + }, |
| 187 | + SeccompProfile: &corev1.SeccompProfile{ |
| 188 | + Type: corev1.SeccompProfileTypeRuntimeDefault, |
| 189 | + }, |
| 190 | + }, |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +// TailscaleVolumes returns the volumes required by the Tailscale sidecar: two |
| 195 | +// emptyDirs (socket dir and state dir) and the serve-config ConfigMap. |
| 196 | +func TailscaleVolumes(instance *paperclipv1alpha1.Instance) []corev1.Volume { |
| 197 | + return []corev1.Volume{ |
| 198 | + { |
| 199 | + Name: tailscaleSocketVolumeName, |
| 200 | + VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}}, |
| 201 | + }, |
| 202 | + { |
| 203 | + Name: tailscaleStateVolumeName, |
| 204 | + VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}}, |
| 205 | + }, |
| 206 | + { |
| 207 | + Name: tailscaleConfigVolumeName, |
| 208 | + VolumeSource: corev1.VolumeSource{ |
| 209 | + ConfigMap: &corev1.ConfigMapVolumeSource{ |
| 210 | + LocalObjectReference: corev1.LocalObjectReference{ |
| 211 | + Name: TailscaleConfigMapName(instance), |
| 212 | + }, |
| 213 | + }, |
| 214 | + }, |
| 215 | + }, |
| 216 | + } |
| 217 | +} |
| 218 | + |
| 219 | +// BuildTailscaleConfigMap builds the ConfigMap holding the TS_SERVE_CONFIG JSON |
| 220 | +// mounted into the Tailscale sidecar. |
| 221 | +func BuildTailscaleConfigMap(instance *paperclipv1alpha1.Instance) *corev1.ConfigMap { |
| 222 | + return &corev1.ConfigMap{ |
| 223 | + ObjectMeta: ObjectMeta(instance, TailscaleConfigMapName(instance)), |
| 224 | + Data: map[string]string{ |
| 225 | + TailscaleServeConfigKey: BuildTailscaleServeConfig(instance), |
| 226 | + }, |
| 227 | + } |
| 228 | +} |
0 commit comments