@@ -2,10 +2,14 @@ package kube
22
33import (
44 "fmt"
5+ "regexp"
6+ "strings"
57
68 corev1 "k8s.io/api/core/v1"
79)
810
11+ var semverTagPattern = regexp .MustCompile (`^v?\d+\.\d+\.\d+([.-][0-9A-Za-z.-]+)?$` )
12+
913func PreparePodSpec (podSpec corev1.PodSpec , workspaceClaim , workspaceMountPath string , syncthingEnabled bool , syncthingImage string ) (corev1.PodSpec , error ) {
1014 spec := podSpec .DeepCopy ()
1115 if spec == nil {
@@ -43,9 +47,10 @@ func PreparePodSpec(podSpec corev1.PodSpec, workspaceClaim, workspaceMountPath s
4347 })
4448 if ! hasContainer (spec .Containers , "syncthing" ) {
4549 spec .Containers = append (spec .Containers , corev1.Container {
46- Name : "syncthing" ,
47- Image : syncthingImage ,
48- Command : []string {"sh" , "-lc" , "syncthing serve --home /var/syncthing --no-browser --gui-address=http://0.0.0.0:8384 --no-restart --skip-port-probing" },
50+ Name : "syncthing" ,
51+ Image : syncthingImage ,
52+ ImagePullPolicy : syncthingImagePullPolicy (syncthingImage ),
53+ Command : []string {"sh" , "-lc" , "syncthing serve --home /var/syncthing --no-browser --gui-address=http://0.0.0.0:8384 --no-restart --skip-port-probing" },
4954 Ports : []corev1.ContainerPort {
5055 {ContainerPort : 8384 , Name : "st-gui" },
5156 {ContainerPort : 22000 , Name : "st-sync" },
@@ -61,6 +66,29 @@ func PreparePodSpec(podSpec corev1.PodSpec, workspaceClaim, workspaceMountPath s
6166 return * spec , nil
6267}
6368
69+ func syncthingImagePullPolicy (image string ) corev1.PullPolicy {
70+ if strings .Contains (image , "@sha256:" ) {
71+ return corev1 .PullIfNotPresent
72+ }
73+ tag := imageTag (image )
74+ if tag == "" {
75+ return corev1 .PullAlways
76+ }
77+ if semverTagPattern .MatchString (tag ) {
78+ return corev1 .PullIfNotPresent
79+ }
80+ return corev1 .PullAlways
81+ }
82+
83+ func imageTag (image string ) string {
84+ lastSlash := strings .LastIndex (image , "/" )
85+ lastColon := strings .LastIndex (image , ":" )
86+ if lastColon <= lastSlash {
87+ return ""
88+ }
89+ return strings .TrimSpace (image [lastColon + 1 :])
90+ }
91+
6492func ensureVolume (volumes []corev1.Volume , v corev1.Volume ) []corev1.Volume {
6593 for _ , item := range volumes {
6694 if item .Name == v .Name {
0 commit comments