@@ -56,6 +56,13 @@ func HandleKubernetesComponents(workspace *common.DevWorkspaceWithConfig, api sy
5656 return nil
5757 }
5858
59+ var validatedK8sComponents []string
60+ if data , ok := workspace .Annotations [constants .DevWorkspaceValidatedK8sResourcesAnnotation ]; ok {
61+ if err := json .Unmarshal ([]byte (data ), & validatedK8sComponents ); err != nil {
62+ return fmt .Errorf ("failed to parse %s annotation: %w" , constants .DevWorkspaceValidatedK8sResourcesAnnotation , err )
63+ }
64+ }
65+
5966 for _ , component := range kubeComponents {
6067 // Ignore error as we filtered list above
6168 k8sLikeComponent , _ := getK8sLikeComponent (component )
@@ -64,7 +71,7 @@ func HandleKubernetesComponents(workspace *common.DevWorkspaceWithConfig, api sy
6471 return & dwerrors.FailError {Message : fmt .Sprintf ("could not process component %s" , component .Name ), Err : err }
6572 }
6673
67- err = restrictK8sComponent (workspace , obj )
74+ err = restrictK8sComponent (workspace , obj , validatedK8sComponents )
6875 if err != nil {
6976 return & dwerrors.FailError {Message : fmt .Sprintf ("could not process component %s" , component .Name ), Err : err }
7077 }
@@ -138,14 +145,7 @@ func addMetadata(obj client.Object, workspace *common.DevWorkspaceWithConfig, ap
138145 return nil
139146}
140147
141- func restrictK8sComponent (workspace * common.DevWorkspaceWithConfig , obj client.Object ) error {
142- var validatedK8sComponents []string
143- if raw := workspace .Annotations [constants .DevWorkspaceValidatedK8sResourcesAnnotation ]; raw != "" {
144- if err := json .Unmarshal ([]byte (raw ), & validatedK8sComponents ); err != nil {
145- return fmt .Errorf ("failed to parse %s annotation: %w" , constants .DevWorkspaceValidatedK8sResourcesAnnotation , err )
146- }
147- }
148-
148+ func restrictK8sComponent (workspace * common.DevWorkspaceWithConfig , obj client.Object , validatedK8sComponents []string ) error {
149149 gvk := obj .GetObjectKind ().GroupVersionKind ()
150150 switch gvk {
151151 case
@@ -159,11 +159,9 @@ func restrictK8sComponent(workspace *common.DevWorkspaceWithConfig, obj client.O
159159 dw .SchemeGroupVersion .WithKind ("DevWorkspaceTemplate" ):
160160 return fmt .Errorf ("DevWorkspace objects are not permitted within DevWorkspace components" )
161161 default :
162- // For backward compatibility, skip the validation for already-running workspaces since the
163- // annotation may not be present on workspaces created before this check was introduced.
164- // Note: workspaces that are being started just after DWO is updated may fail to start
165- // if the webhook has not re-validated them, as the annotation will be absent.
166- if workspace .Status .Phase != dw .DevWorkspaceStatusRunning {
162+ // For backward compatibility, skip the validation when the annotation is absent, as it
163+ // may not be present on workspaces created before this check was introduced.
164+ if validatedK8sComponents != nil {
167165 if ! slices .Contains (validatedK8sComponents , gvk .String ()) {
168166 return fmt .Errorf ("user is not authorized to create %s resources" , gvk .Kind )
169167 }
0 commit comments