-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow all users to list model registries
- Loading branch information
1 parent
13b5562
commit b6f581f
Showing
12 changed files
with
260 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { k8sCreateResource } from '@openshift/dynamic-plugin-sdk-utils'; | ||
import * as React from 'react'; | ||
import { SelfSubjectRulesReviewModel } from '~/api/models'; | ||
import { SelfSubjectRulesReviewKind } from '~/k8sTypes'; | ||
|
||
const checkAccess = (ns: string): Promise<SelfSubjectRulesReviewKind> => { | ||
const selfSubjectRulesReview: SelfSubjectRulesReviewKind = { | ||
apiVersion: 'authorization.k8s.io/v1', | ||
kind: 'SelfSubjectRulesReview', | ||
spec: { | ||
namespace: ns, | ||
}, | ||
}; | ||
return k8sCreateResource<SelfSubjectRulesReviewKind>({ | ||
model: SelfSubjectRulesReviewModel, | ||
resource: selfSubjectRulesReview, | ||
}); | ||
}; | ||
|
||
export const useRulesReview = ( | ||
namespace: string, | ||
): [boolean, SelfSubjectRulesReviewKind['status']] => { | ||
const [loaded, setLoaded] = React.useState(false); | ||
const [status, setStatus] = React.useState<SelfSubjectRulesReviewKind['status']>(undefined); | ||
|
||
React.useEffect(() => { | ||
checkAccess(namespace) | ||
.then((result) => { | ||
if (!result.status?.incomplete && !result.status?.evaluationError) { | ||
setStatus(result.status); | ||
} | ||
setLoaded(true); | ||
}) | ||
.catch((e) => { | ||
// eslint-disable-next-line no-console | ||
console.warn('SelfSubjectRulesReview failed', e); | ||
setLoaded(true); | ||
}); | ||
}, [namespace]); | ||
|
||
return [loaded, status]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import React from 'react'; | ||
import useFetchState, { FetchState, FetchStateCallbackPromise } from '~/utilities/useFetchState'; | ||
import { ServiceKind } from '~/k8sTypes'; | ||
import { listServices } from './modelRegistry/k8s'; | ||
|
||
export const useServices = (namespace: string): FetchState<ServiceKind[]> => { | ||
const callback = React.useCallback<FetchStateCallbackPromise<ServiceKind[]>>( | ||
() => listServices(namespace), | ||
[namespace], | ||
); | ||
|
||
return useFetchState(callback, [], { | ||
initialPromisePurity: true, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { k8sGetResource } from '@openshift/dynamic-plugin-sdk-utils'; | ||
import React from 'react'; | ||
import { ServiceKind } from '~/k8sTypes'; | ||
import useFetchState, { | ||
FetchState, | ||
FetchStateCallbackPromise, | ||
NotReadyError, | ||
} from '~/utilities/useFetchState'; | ||
import { ServiceModel } from './models'; | ||
|
||
export const getServiceByName = (name: string, namespace: string): Promise<ServiceKind> => | ||
k8sGetResource<ServiceKind>({ | ||
model: ServiceModel, | ||
queryOptions: { name, ns: namespace }, | ||
}); | ||
|
||
const fetchServices = async (names: string[], namespace: string): Promise<ServiceKind[]> => { | ||
if (!namespace) { | ||
throw new NotReadyError('No namespace'); | ||
} | ||
|
||
if (names.length === 0) { | ||
return []; | ||
} | ||
|
||
const servicePromises = names.map((name) => getServiceByName(name, namespace)); | ||
const services = await Promise.all(servicePromises); | ||
|
||
return services; | ||
}; | ||
|
||
export const useServicesByNames = ( | ||
names: string[], | ||
namespace: string, | ||
): FetchState<ServiceKind[]> => { | ||
const callback = React.useCallback<FetchStateCallbackPromise<ServiceKind[]>>( | ||
() => fetchServices(names, namespace), | ||
[names, namespace], | ||
); | ||
|
||
return useFetchState(callback, [], { | ||
initialPromisePurity: true, | ||
}); | ||
}; |
68 changes: 61 additions & 7 deletions
68
frontend/src/concepts/modelRegistry/apiHooks/useModelRegistries.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,65 @@ | ||
import React from 'react'; | ||
import useFetchState, { FetchState } from '~/utilities/useFetchState'; | ||
import { ModelRegistryKind } from '~/k8sTypes'; | ||
import { listModelRegistries } from '~/api'; | ||
import useFetchState, { FetchState, FetchStateCallbackPromise } from '~/utilities/useFetchState'; | ||
import { AccessReviewResourceAttributes, ServiceKind } from '~/k8sTypes'; | ||
import { | ||
ServiceModel, | ||
useAccessReview, | ||
useRulesReview, | ||
useServicesByNames, | ||
useServices, | ||
} from '~/api'; | ||
import { MODEL_REGISTRY_DEFAULT_NAMESPACE } from '~/concepts/modelRegistry/const'; | ||
|
||
const useModelRegistries = (): FetchState<ModelRegistryKind[]> => { | ||
const getModelRegistries = React.useCallback(() => listModelRegistries(), []); | ||
return useFetchState<ModelRegistryKind[]>(getModelRegistries, []); | ||
const accessReviewResource: AccessReviewResourceAttributes = { | ||
group: 'user.openshift.io', | ||
resource: 'services', | ||
verb: 'list', | ||
namespace: MODEL_REGISTRY_DEFAULT_NAMESPACE, | ||
}; | ||
|
||
export default useModelRegistries; | ||
const useModelRegistryServices = (): FetchState<ServiceKind[]> => { | ||
const [allowList, accessReviewLoaded] = useAccessReview(accessReviewResource); | ||
const [loaded, statuses] = useRulesReview(MODEL_REGISTRY_DEFAULT_NAMESPACE); | ||
const serviceNames = React.useMemo(() => { | ||
if (!loaded) { | ||
return []; | ||
} | ||
return ( | ||
statuses?.resourceRules.find( | ||
({ resources, verbs }) => | ||
resources?.includes(ServiceModel.plural) && verbs.some((verb) => verb === 'get'), | ||
)?.resourceNames || [] | ||
); | ||
}, [loaded, statuses]); | ||
|
||
const [nonAdminServices, nonAdminServicesLoaded] = useServicesByNames( | ||
serviceNames, | ||
MODEL_REGISTRY_DEFAULT_NAMESPACE, | ||
); | ||
|
||
const [adminServices, adminServicesLoaded] = useServices(MODEL_REGISTRY_DEFAULT_NAMESPACE); | ||
|
||
const getModelRegistryServices = React.useCallback< | ||
FetchStateCallbackPromise<ServiceKind[]> | ||
>(async () => { | ||
if (!accessReviewLoaded || (!nonAdminServicesLoaded && !adminServicesLoaded)) { | ||
return []; | ||
} | ||
|
||
const services = allowList ? adminServices : nonAdminServices; | ||
return Promise.resolve(services); | ||
}, [ | ||
accessReviewLoaded, | ||
nonAdminServicesLoaded, | ||
adminServicesLoaded, | ||
allowList, | ||
adminServices, | ||
nonAdminServices, | ||
]); | ||
|
||
return useFetchState<ServiceKind[]>(getModelRegistryServices, [], { | ||
initialPromisePurity: true, | ||
}); | ||
}; | ||
|
||
export default useModelRegistryServices; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.