@@ -86,6 +86,17 @@ func manageResource(
8686 return fmt .Errorf ("failed to unmarshal resource: %w" , err )
8787 }
8888
89+ // Check if ClusterRoleBinding references a ClusterRole that exists
90+ if u .GetKind () == "ClusterRoleBinding" {
91+ if shouldSkip , err := CheckClusterRoleExists (ctx , cli , u ); err != nil {
92+ return fmt .Errorf ("failed to check ClusterRole existence: %w" , err )
93+ } else if shouldSkip {
94+ log .FromContext (ctx ).V (1 ).Info ("Skipping ClusterRoleBinding - referenced ClusterRole not found" ,
95+ "clusterRoleBinding" , u .GetName ())
96+ return nil
97+ }
98+ }
99+
89100 kGvk := res .GetGvk ()
90101 gvk := schema.GroupVersionKind {
91102 Group : kGvk .Group ,
@@ -204,6 +215,18 @@ func applyPlugins(resMap *resmap.ResMap, ownerInstance *llamav1alpha1.LlamaStack
204215 TargetKind : "PersistentVolumeClaim" ,
205216 CreateIfNotExists : true ,
206217 },
218+ {
219+ SourceValue : ownerInstance .GetNamespace (),
220+ TargetField : "subjects[0].namespace" ,
221+ TargetKind : "ClusterRoleBinding" ,
222+ CreateIfNotExists : true ,
223+ },
224+ {
225+ SourceValue : ownerInstance .GetName () + "-sa" ,
226+ TargetField : "subjects[0].name" ,
227+ TargetKind : "ClusterRoleBinding" ,
228+ CreateIfNotExists : true ,
229+ },
207230 },
208231 })
209232 if err := fieldTransformerPlugin .Transform (* resMap ); err != nil {
@@ -233,3 +256,33 @@ func FilterExcludeKinds(resMap *resmap.ResMap, kindsToExclude []string) (*resmap
233256 }
234257 return & filteredResMap , nil
235258}
259+
260+ // CheckClusterRoleExists checks if a ClusterRoleBinding should be skipped due to missing ClusterRole.
261+ func CheckClusterRoleExists (ctx context.Context , cli client.Client , crb * unstructured.Unstructured ) (bool , error ) {
262+ roleRef , found , _ := unstructured .NestedMap (crb .Object , "roleRef" )
263+ if ! found {
264+ return false , nil // No roleRef, don't skip
265+ }
266+
267+ roleName , _ , _ := unstructured .NestedString (roleRef , "name" )
268+ if roleName == "" {
269+ return false , nil // Empty roleName, don't skip
270+ }
271+
272+ // Check if the referenced ClusterRole exists
273+ clusterRole := & unstructured.Unstructured {}
274+ clusterRole .SetGroupVersionKind (schema.GroupVersionKind {
275+ Group : "rbac.authorization.k8s.io" ,
276+ Version : "v1" ,
277+ Kind : "ClusterRole" ,
278+ })
279+ clusterRole .SetName (roleName )
280+
281+ err := cli .Get (ctx , client.ObjectKey {Name : roleName }, clusterRole )
282+ if err != nil && k8serr .IsNotFound (err ) {
283+ return true , nil
284+ } else if err != nil {
285+ return false , err
286+ }
287+ return false , nil
288+ }
0 commit comments