Cloud API Overview > @adpt/cloud > k8s > useResources
k8s.useResources() function
This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.
Hook that allows a prop to be either an array of handle to k8s resources and values
Signature:
export declare function useResources<ValT, NotReadyT>({ initial, notReady, kinds, thisResourceName, propName, }: {
initial: ValT[];
notReady: NotReadyT;
kinds: string[];
thisResourceName: string;
propName: string;
}): [
(ValT | NotReadyT)[],
(props: (ValT | Handle)[], f: (e: AdaptMountedElement, props: ResourceProps) => Promise<ValT> | ValT) => void
];
Parameters
Parameter | Type | Description |
---|---|---|
{ initial, notReady, kinds, thisResourceName, propName, } | { initial: ValT[]; notReady: NotReadyT; kinds: string[]; thisResourceName: string; propName: string; } |
Returns:
[ (ValT | NotReadyT)[], (props: (ValT | Handle)[], f: (e: AdaptMountedElement, props: ResourceProps) => Promise<ValT> | ValT) => void ]
A two element array, the first element is the current value, the second the update function
This hook will start by returning the initial value and an update function that updates the value the hook returns. The update function takes 2 arguments - the prop value which is an array with a mix of values and handles to be resolved, and a function that receives the elements that any handles point to along with that elements props. This function can be passed by the caller of update to resolve handles as appropriate for the component.
For example, k8s.ClusterRoleBinding() uses this hook to resolve the subjects prop, which is an array of other objects (typically ServiceAccount
) that a particular k8s.ClusterRole() should point to. When ClusterRoleBinding calls the update method, it passes a function that will convert a k8s.Resource element of Kind ServiceAccount
to the underlying Subject
object that kubernetes expects, namely { apiGroup: "", kind: "ServiceAccount", name: resourceIdToName(elem, deployID), namespace: <element namespace> }
Example
function MyResource({ serviceAccountNames }: { serviceAccountNames: (string | Handle)[]}) {
const { deployID } = useBuildHelpers();
const [ resolvedServiceAccountNames, updateSANs] = useResources({
initial: [],
notReady: null,
kinds: ["ServiceAccount"],
thisResourceName: "MyResources",
propName: "serviceAccountNames",
});
updateSANs(serviceAccountNames, (e, props) => {
return {
apiGroup: (e.metadata.apiVersion?.split("/")[0]) || "",
name: resourceElementToName(e, deployID),
namespace: props.metadata.namespace,
}
});
return null;
}