@@ -119,6 +119,63 @@ def list(self, request, **kwargs):
119119 return Response (serializer .data )
120120
121121
122+ class WorkflowManagerViewset (GenericViewSet ):
123+
124+ permission_classes = (permissions .IsWorkflowManager , )
125+
126+ def block (self , request , ** kwargs ):
127+ try :
128+ blocklist = models .Blocklist (
129+ id = kwargs ['id' ],
130+ type = models .Blocklist .get_type (kwargs ["type" ]),
131+ remark = request .data .get ("remark" )
132+ )
133+ apps = blocklist .related_apps
134+ [scale_app (app , app .owner , {key : 0 for key in app .structure .keys ()}) for app in apps ]
135+ blocklist .save ()
136+ except ValueError as e :
137+ logger .info (e )
138+ raise DryccException ("Unsupported block type: %s" % kwargs ["type" ])
139+
140+ def unblock (self , request , ** kwargs ):
141+ try :
142+ models .Blocklist .objects .filter (
143+ id = kwargs ['id' ],
144+ type = models .Blocklist .get_type (kwargs ["type" ])
145+ ).delete ()
146+ except ValueError as e :
147+ logger .info (e )
148+ raise DryccException ("Unsupported block type: %s" % kwargs ["type" ])
149+
150+
151+ class AdmissionWebhookViewSet (GenericViewSet ):
152+
153+ permission_classes = (AllowAny , )
154+
155+ def scale (self , request , ** kwargs ):
156+ token = kwargs ['token' ]
157+ data = json .loads (request .body .decode ("utf8" ))["request" ]
158+ if settings .DRYCC_ADMISSION_WEBHOOK_TOKEN == token :
159+ allowed = True
160+ app_id = data ["object" ]["metadata" ]["namespace" ]
161+ app = models .App .objects .filter (id = app_id ).first ()
162+ replicas = data ["object" ]["spec" ].get ("replicas" , 0 )
163+ container_type = data ["object" ]["metadata" ]["name" ].replace (f"{ app_id } -" , "" , 1 )
164+ if app and app .structure .get (container_type ) != replicas : # sync replicas
165+ app .structure [container_type ] = replicas
166+ super (models .App , app ).save (update_fields = ["structure" , ])
167+ else :
168+ allowed = False
169+ return Response ({
170+ "apiVersion" : "admission.k8s.io/v1" ,
171+ "kind" : "AdmissionReview" ,
172+ "response" : {
173+ "uid" : data ["uid" ],
174+ "allowed" : allowed ,
175+ }
176+ })
177+
178+
122179class BaseDryccViewSet (viewsets .OwnerViewSet ):
123180 """
124181 A generic ViewSet for objects related to Drycc.
@@ -508,8 +565,9 @@ def users(self, request, *args, **kwargs):
508565 app = get_object_or_404 (models .App , id = kwargs ['id' ])
509566 request .user = get_object_or_404 (User , username = kwargs ['username' ])
510567 # check the user is authorized for this app
511- if not permissions .is_app_user (request , app ):
512- raise PermissionDenied ()
568+ has_permission , message = permissions .has_app_permission (request , app )
569+ if not has_permission :
570+ raise PermissionDenied (message )
513571
514572 data = {request .user .username : []}
515573 keys = models .Key .objects \
@@ -537,8 +595,9 @@ def create(self, request, *args, **kwargs):
537595 app = get_object_or_404 (models .App , id = request .data ['receive_repo' ])
538596 self .user = request .user = get_object_or_404 (User , username = request .data ['receive_user' ])
539597 # check the user is authorized for this app
540- if not permissions .is_app_user (request , app ):
541- raise PermissionDenied ()
598+ has_permission , message = permissions .has_app_permission (request , app )
599+ if not has_permission :
600+ raise PermissionDenied (message )
542601 request .data ['app' ] = app
543602 request .data ['owner' ] = self .user
544603 super (BuildHookViewSet , self ).create (request , * args , ** kwargs )
@@ -559,8 +618,9 @@ def create(self, request, *args, **kwargs):
559618 app = get_object_or_404 (models .App , id = request .data ['receive_repo' ])
560619 request .user = get_object_or_404 (User , username = request .data ['receive_user' ])
561620 # check the user is authorized for this app
562- if not permissions .is_app_user (request , app ):
563- raise PermissionDenied ()
621+ has_permission , message = permissions .has_app_permission (request , app )
622+ if not has_permission :
623+ raise PermissionDenied (message )
564624 config = app .release_set .filter (failed = False ).latest ().config
565625 serializer = self .get_serializer (config )
566626 return Response (serializer .data , status = status .HTTP_200_OK )
@@ -908,32 +968,3 @@ def status(self, request, **kwargs):
908968 "networks" : self ._get_networks (
909969 app_id , container_type , start , stop , every )
910970 })
911-
912-
913- class AdmissionWebhook (GenericViewSet ):
914-
915- permission_classes = (AllowAny , )
916-
917- def scale (self , request , ** kwargs ):
918- token = kwargs ['token' ]
919- print (request .body .decode ("utf8" ))
920- data = json .loads (request .body .decode ("utf8" ))["request" ]
921- if settings .DRYCC_ADMISSION_WEBHOOK_TOKEN == token :
922- allowed = True
923- app_id = data ["object" ]["metadata" ]["namespace" ]
924- app = models .App .objects .filter (id = app_id ).first ()
925- replicas = data ["object" ]["spec" ].get ("replicas" , 0 )
926- container_type = data ["object" ]["metadata" ]["name" ].replace (f"{ app_id } -" , "" , 1 )
927- if app and app .structure .get (container_type ) != replicas : # sync replicas
928- app .structure [container_type ] = replicas
929- super (models .App , app ).save (update_fields = ["structure" , ])
930- else :
931- allowed = False
932- return Response ({
933- "apiVersion" : "admission.k8s.io/v1" ,
934- "kind" : "AdmissionReview" ,
935- "response" : {
936- "uid" : data ["uid" ],
937- "allowed" : allowed ,
938- }
939- })
0 commit comments