@@ -145,23 +145,23 @@ func createANewTeam(context context.Context, bundle *bundle.Bundle, team string,
145145 return
146146 }
147147
148+ deployment , err := createDeploymentForTeam (context , bundle , team , passcodeHash )
149+ if err != nil {
150+ bundle .Log .Printf ("Failed to create deployment: %s" , err )
151+ http .Error (w , "failed to create deployment" , http .StatusInternalServerError )
152+ return
153+ }
154+
148155 if bundle .Config .JuiceShopConfig .LLM .Enabled {
149- err = createLLMTokenSecretForTeam (context , bundle , team )
156+ err = createLLMTokenSecretForTeam (context , bundle , team , deployment )
150157 if err != nil {
151158 bundle .Log .Printf ("Failed to create LLM token secret: %s" , err )
152159 http .Error (w , "failed to create LLM token secret" , http .StatusInternalServerError )
153160 return
154161 }
155162 }
156163
157- err = createDeploymentForTeam (context , bundle , team , passcodeHash )
158- if err != nil {
159- bundle .Log .Printf ("Failed to create deployment: %s" , err )
160- http .Error (w , "failed to create deployment" , http .StatusInternalServerError )
161- return
162- }
163-
164- err = createServiceForTeam (context , bundle , team )
164+ err = createServiceForTeam (context , bundle , team , deployment )
165165 if err != nil {
166166 bundle .Log .Printf ("Failed to create service: %s" , err )
167167 http .Error (w , "failed to create service" , http .StatusInternalServerError )
@@ -272,7 +272,21 @@ func writeUnauthorizedResponse(responseWriter http.ResponseWriter) {
272272 responseWriter .Write (errorResponseBody ) // nosemgrep: go.lang.security.audit.xss.no-direct-write-to-responsewriter.no-direct-write-to-responsewriter
273273}
274274
275- // uid of the balancer kubernetes deployment resource. used to "attach" created juice shop deployments and services to the balancer deployment so that they get deleted when the balancer gets deleted
275+ func getDeploymentOwnerReferences (deployment * appsv1.Deployment ) []metav1.OwnerReference {
276+ truePointer := true
277+ return []metav1.OwnerReference {
278+ {
279+ APIVersion : "apps/v1" ,
280+ Kind : "Deployment" ,
281+ Name : deployment .Name ,
282+ UID : deployment .UID ,
283+ Controller : & truePointer ,
284+ BlockOwnerDeletion : & truePointer ,
285+ },
286+ }
287+ }
288+
289+ // uid of the balancer kubernetes deployment resource. used to "attach" created juice shop deployments to the balancer deployment so that they get deleted when the balancer gets deleted
276290var deploymentUid types.UID
277291
278292func getOwnerReferences (context context.Context , bundle * bundle.Bundle ) ([]metav1.OwnerReference , error ) {
@@ -302,10 +316,10 @@ func getOwnerReferences(context context.Context, bundle *bundle.Bundle) ([]metav
302316 return ownerReferences , nil
303317}
304318
305- func createDeploymentForTeam (context context.Context , bundle * bundle.Bundle , team string , passcodeHash string ) error {
319+ func createDeploymentForTeam (context context.Context , bundle * bundle.Bundle , team string , passcodeHash string ) ( * appsv1. Deployment , error ) {
306320 ownerReferences , err := getOwnerReferences (context , bundle )
307321 if err != nil {
308- return err
322+ return nil , err
309323 }
310324
311325 podLabels := map [string ]string {}
@@ -433,16 +447,11 @@ func createDeploymentForTeam(context context.Context, bundle *bundle.Bundle, tea
433447 },
434448 }
435449
436- _ , err = bundle .ClientSet .AppsV1 ().Deployments (bundle .RuntimeEnvironment .Namespace ).Create (context , deployment , metav1.CreateOptions {})
437- return err
450+ created , err : = bundle .ClientSet .AppsV1 ().Deployments (bundle .RuntimeEnvironment .Namespace ).Create (context , deployment , metav1.CreateOptions {})
451+ return created , err
438452}
439453
440- func createServiceForTeam (context context.Context , bundle * bundle.Bundle , team string ) error {
441- ownerReferences , err := getOwnerReferences (context , bundle )
442- if err != nil {
443- return err
444- }
445-
454+ func createServiceForTeam (context context.Context , bundle * bundle.Bundle , team string , ownerDeployment * appsv1.Deployment ) error {
446455 service := & corev1.Service {
447456 ObjectMeta : metav1.ObjectMeta {
448457 Name : fmt .Sprintf ("juiceshop-%s" , team ),
@@ -454,7 +463,7 @@ func createServiceForTeam(context context.Context, bundle *bundle.Bundle, team s
454463 "app.kubernetes.io/instance" : fmt .Sprintf ("juice-shop-%s" , team ),
455464 "app.kubernetes.io/part-of" : "multi-juicer" ,
456465 },
457- OwnerReferences : ownerReferences ,
466+ OwnerReferences : getDeploymentOwnerReferences ( ownerDeployment ) ,
458467 },
459468 Spec : corev1.ServiceSpec {
460469 Selector : map [string ]string {
@@ -469,7 +478,7 @@ func createServiceForTeam(context context.Context, bundle *bundle.Bundle, team s
469478 },
470479 }
471480
472- _ , err = bundle .ClientSet .CoreV1 ().Services (bundle .RuntimeEnvironment .Namespace ).Create (context , service , metav1.CreateOptions {})
481+ _ , err : = bundle .ClientSet .CoreV1 ().Services (bundle .RuntimeEnvironment .Namespace ).Create (context , service , metav1.CreateOptions {})
473482 return err
474483}
475484
@@ -507,17 +516,12 @@ func buildJuiceShopEnv(bundle *bundle.Bundle, team string) []corev1.EnvVar {
507516 return envVars
508517}
509518
510- func createLLMTokenSecretForTeam (ctx context.Context , bundle * bundle.Bundle , team string ) error {
519+ func createLLMTokenSecretForTeam (ctx context.Context , bundle * bundle.Bundle , team string , ownerDeployment * appsv1. Deployment ) error {
511520 token , err := signutil .Sign (team , bundle .Config .CookieConfig .SigningKey )
512521 if err != nil {
513522 return fmt .Errorf ("failed to sign LLM token: %w" , err )
514523 }
515524
516- ownerReferences , err := getOwnerReferences (ctx , bundle )
517- if err != nil {
518- return err
519- }
520-
521525 secret := & corev1.Secret {
522526 ObjectMeta : metav1.ObjectMeta {
523527 Name : fmt .Sprintf ("juiceshop-%s" , team ),
@@ -526,7 +530,7 @@ func createLLMTokenSecretForTeam(ctx context.Context, bundle *bundle.Bundle, tea
526530 "app.kubernetes.io/component" : "llm-token" ,
527531 "app.kubernetes.io/part-of" : "multi-juicer" ,
528532 },
529- OwnerReferences : ownerReferences ,
533+ OwnerReferences : getDeploymentOwnerReferences ( ownerDeployment ) ,
530534 },
531535 Data : map [string ][]byte {
532536 "token" : []byte (token ),
0 commit comments