@@ -19,9 +19,17 @@ const BuildKitContainer = "buildkitd"
1919
2020func (r * LocalRegistry ) ensureDeployment (ctx devspacecontext.Context ) (* appsv1.Deployment , error ) {
2121 // Switching from a persistent registry, delete the statefulset.
22- _ , err := ctx .KubeClient ().KubeClient ().AppsV1 ().StatefulSets (r .Namespace ).Get (ctx .Context (), r .Name , metav1.GetOptions {})
22+ _ , err := ctx .KubeClient ().
23+ KubeClient ().
24+ AppsV1 ().
25+ StatefulSets (r .Namespace ).
26+ Get (ctx .Context (), r .Name , metav1.GetOptions {})
2327 if err == nil {
24- err := ctx .KubeClient ().KubeClient ().AppsV1 ().StatefulSets (r .Namespace ).Delete (ctx .Context (), r .Name , metav1.DeleteOptions {})
28+ err := ctx .KubeClient ().
29+ KubeClient ().
30+ AppsV1 ().
31+ StatefulSets (r .Namespace ).
32+ Delete (ctx .Context (), r .Name , metav1.DeleteOptions {})
2533 if err != nil && kerrors .IsNotFound (err ) {
2634 return nil , err
2735 }
@@ -31,29 +39,41 @@ func (r *LocalRegistry) ensureDeployment(ctx devspacecontext.Context) (*appsv1.D
3139 var existing * appsv1.Deployment
3240 desired := r .getDeployment ()
3341 kubeClient := ctx .KubeClient ()
34- err = wait .PollUntilContextTimeout (ctx . Context (), time . Second , 30 * time . Second , true , func ( ctx context. Context ) ( bool , error ) {
35- var err error
36-
37- existing , err = kubeClient . KubeClient (). AppsV1 (). Deployments ( r . Namespace ). Get ( ctx , r . Name , metav1. GetOptions {})
38- if err == nil {
39- return true , nil
40- }
42+ err = wait .PollUntilContextTimeout (
43+ ctx . Context (),
44+ time . Second ,
45+ 30 * time . Second ,
46+ true ,
47+ func ( ctx context. Context ) ( bool , error ) {
48+ var err error
4149
42- if kerrors .IsNotFound (err ) {
43- existing , err = kubeClient .KubeClient ().AppsV1 ().Deployments (r .Namespace ).Create (ctx , desired , metav1.CreateOptions {})
50+ existing , err = kubeClient .KubeClient ().
51+ AppsV1 ().
52+ Deployments (r .Namespace ).
53+ Get (ctx , r .Name , metav1.GetOptions {})
4454 if err == nil {
4555 return true , nil
4656 }
4757
48- if kerrors .IsAlreadyExists (err ) {
49- return false , nil
58+ if kerrors .IsNotFound (err ) {
59+ existing , err = kubeClient .KubeClient ().
60+ AppsV1 ().
61+ Deployments (r .Namespace ).
62+ Create (ctx , desired , metav1.CreateOptions {})
63+ if err == nil {
64+ return true , nil
65+ }
66+
67+ if kerrors .IsAlreadyExists (err ) {
68+ return false , nil
69+ }
70+
71+ return false , err
5072 }
5173
5274 return false , err
53- }
54-
55- return false , err
56- })
75+ },
76+ )
5777 if err != nil {
5878 return nil , err
5979 }
@@ -72,7 +92,8 @@ func (r *LocalRegistry) ensureDeployment(ctx devspacecontext.Context) (*appsv1.D
7292 },
7393 )
7494 if kerrors .IsUnsupportedMediaType (err ) {
75- ctx .Log ().Debugf ("Server-side apply not available on the server for localRegistry deployment: (%v)" , err )
95+ ctx .Log ().
96+ Debugf ("Server-side apply not available on the server for localRegistry deployment: (%v)" , err )
7697 // Unsupport server-side apply, we use existing or created deployment
7798 return existing , nil
7899 }
@@ -96,11 +117,18 @@ func (r *LocalRegistry) getDeployment() *appsv1.Deployment {
96117 Labels : map [string ]string {
97118 "app" : r .Name ,
98119 },
99- Annotations : getAnnotations (r .LocalBuild ),
120+ Annotations : getAnnotations (r .LocalBuild , r . Annotations ),
100121 },
101122 Spec : corev1.PodSpec {
102123 EnableServiceLinks : new (bool ),
103- Containers : getContainers (r .RegistryImage , r .BuildKitImage , "registry" , int32 (r .Port ), r .LocalBuild ),
124+ Containers : getContainers (
125+ r .RegistryImage ,
126+ r .BuildKitImage ,
127+ "registry" ,
128+ int32 (r .Port ),
129+ r .LocalBuild ,
130+ r .Resources ,
131+ ),
104132 Volumes : []corev1.Volume {
105133 {
106134 VolumeSource : corev1.VolumeSource {
@@ -121,24 +149,42 @@ func (r *LocalRegistry) getDeployment() *appsv1.Deployment {
121149 }
122150}
123151
124- func getAnnotations (localbuild bool ) map [string ]string {
125- if ! localbuild {
126- return map [string ]string {
152+ func getAnnotations (isLocalbuild bool , annotations map [ string ] string ) map [string ]string {
153+ if ! isLocalbuild {
154+ combined := map [string ]string {
127155 "container.apparmor.security.beta.kubernetes.io/buildkitd" : "unconfined" ,
128156 }
157+
158+ for k , v := range annotations {
159+ combined [k ] = v
160+ }
161+
162+ return combined
129163 }
130- return map [string ]string {}
164+
165+ return annotations
131166}
132167
133168// this returns a different deployment, if we're using a local docker build or not.
134- func getContainers (registryImage , buildKitImage , volume string , port int32 , localbuild bool ) []corev1.Container {
135- buildContainers := getRegistryContainers (registryImage , volume , port )
169+ func getContainers (
170+ registryImage , buildKitImage , volume string ,
171+ port int32 ,
172+ localbuild bool ,
173+ registryResources * corev1.ResourceRequirements ,
174+ ) []corev1.Container {
175+ buildContainers := getRegistryContainers (registryImage , volume , port , registryResources )
136176 if localbuild {
137177 // in case we're using local builds just return the deployment with only the
138178 // registry container inside
139179 return buildContainers
140180 }
141181
182+ resources := corev1.ResourceRequirements {}
183+
184+ if registryResources != nil {
185+ resources = * registryResources
186+ }
187+
142188 buildKitContainer := []corev1.Container {
143189 {
144190 Name : BuildKitContainer ,
@@ -185,6 +231,7 @@ func getContainers(registryImage, buildKitImage, volume string, port int32, loca
185231 MountPath : "/home/user/.local/share/buildkit" ,
186232 },
187233 },
234+ Resources : resources ,
188235 },
189236 }
190237
@@ -193,7 +240,18 @@ func getContainers(registryImage, buildKitImage, volume string, port int32, loca
193240 return append (buildKitContainer , buildContainers ... )
194241}
195242
196- func getRegistryContainers (registryImage , volume string , port int32 ) []corev1.Container {
243+ func getRegistryContainers (
244+ registryImage , volume string ,
245+ port int32 ,
246+ registryResources * corev1.ResourceRequirements ,
247+ ) []corev1.Container {
248+
249+ resources := corev1.ResourceRequirements {}
250+
251+ if registryResources != nil {
252+ resources = * registryResources
253+ }
254+
197255 return []corev1.Container {
198256 {
199257 Name : "registry" ,
@@ -240,6 +298,7 @@ func getRegistryContainers(registryImage, volume string, port int32) []corev1.Co
240298 MountPath : "/var/lib/registry" ,
241299 },
242300 },
301+ Resources : resources ,
243302 },
244303 }
245304}
0 commit comments