-
Notifications
You must be signed in to change notification settings - Fork 178
/
namespaceUtils.ts
159 lines (152 loc) · 4.95 KB
/
namespaceUtils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import {
PatchUtils,
V1ResourceAttributes,
V1SelfSubjectAccessReview,
} from '@kubernetes/client-node';
import { NamespaceApplicationCase } from './const';
import { K8sStatus, KubeFastifyInstance, OauthFastifyRequest } from '../../../types';
import { createCustomError } from '../../../utils/requestUtils';
import { isK8sStatus, passThroughResource } from '../k8s/pass-through';
export const createSelfSubjectAccessReview = (
fastify: KubeFastifyInstance,
request: OauthFastifyRequest,
resourceAttributes: V1ResourceAttributes,
): Promise<V1SelfSubjectAccessReview | K8sStatus> => {
const kc = fastify.kube.config;
const cluster = kc.getCurrentCluster();
const selfSubjectAccessReviewObject: V1SelfSubjectAccessReview = {
apiVersion: 'authorization.k8s.io/v1',
kind: 'SelfSubjectAccessReview',
spec: { resourceAttributes },
};
return passThroughResource<V1SelfSubjectAccessReview>(fastify, request, {
url: `${cluster.server}/apis/authorization.k8s.io/v1/selfsubjectaccessreviews`,
method: 'POST',
requestData: JSON.stringify(selfSubjectAccessReviewObject),
});
};
const checkAdminNamespacePermission = (
fastify: KubeFastifyInstance,
request: OauthFastifyRequest,
name: string,
): Promise<V1SelfSubjectAccessReview | K8sStatus> =>
createSelfSubjectAccessReview(fastify, request, {
group: 'project.openshift.io',
resource: 'projects',
subresource: '',
verb: 'update',
name,
namespace: name,
});
const checkEditNamespacePermission = (
fastify: KubeFastifyInstance,
request: OauthFastifyRequest,
name: string,
): Promise<V1SelfSubjectAccessReview | K8sStatus> =>
createSelfSubjectAccessReview(fastify, request, {
group: 'serving.kserve.io',
resource: 'servingruntimes',
subresource: '',
verb: 'create',
name,
namespace: name,
});
export const applyNamespaceChange = async (
fastify: KubeFastifyInstance,
request: OauthFastifyRequest,
name: string,
context: NamespaceApplicationCase,
dryRun?: string,
): Promise<{ applied: boolean }> => {
if (name.startsWith('openshift') || name.startsWith('kube')) {
// Kubernetes and OpenShift namespaces are off limits to this flow
throw createCustomError(
'Invalid namespace target',
'Cannot mutate namespaces with "openshift" or "kube"',
400,
);
}
let annotations = {};
let labels = {};
let checkPermissionsFn = null;
switch (context) {
case NamespaceApplicationCase.DSG_CREATION:
{
labels = { 'opendatahub.io/dashboard': 'true' };
checkPermissionsFn = checkAdminNamespacePermission;
}
break;
case NamespaceApplicationCase.MODEL_MESH_PROMOTION:
{
labels = { 'modelmesh-enabled': 'true' };
checkPermissionsFn = checkEditNamespacePermission;
}
break;
case NamespaceApplicationCase.KSERVE_PROMOTION:
{
labels = { 'modelmesh-enabled': 'false' };
checkPermissionsFn = checkEditNamespacePermission;
}
break;
case NamespaceApplicationCase.KSERVE_NIM_PROMOTION:
{
annotations = { 'opendatahub.io/nim-support': 'true' };
labels = { 'modelmesh-enabled': 'false' };
checkPermissionsFn = checkEditNamespacePermission;
}
break;
case NamespaceApplicationCase.RESET_MODEL_SERVING_PLATFORM:
{
annotations = { 'opendatahub.io/nim-support': null };
labels = { 'modelmesh-enabled': null };
checkPermissionsFn = checkEditNamespacePermission;
}
break;
default:
throw createCustomError('Unknown configuration', 'Cannot apply namespace change', 400);
}
if (checkPermissionsFn === null) {
throw createCustomError(
'Invalid backend state -- dev broken workflow',
'checkPermissionsFn is null -- appropriate permissions must be checked for all actions',
500,
);
}
const selfSubjectAccessReview = await checkPermissionsFn(fastify, request, name);
if (isK8sStatus(selfSubjectAccessReview)) {
throw createCustomError(
selfSubjectAccessReview.reason,
selfSubjectAccessReview.message,
selfSubjectAccessReview.code,
);
}
if (!selfSubjectAccessReview.status.allowed) {
fastify.log.error(`Unable to access the namespace, ${selfSubjectAccessReview.status.reason}`);
throw createCustomError(
'Forbidden',
"You don't have permission to update serving platform labels on the current project.",
403,
);
}
return fastify.kube.coreV1Api
.patchNamespace(
name,
{ metadata: { annotations, labels } },
undefined,
dryRun,
undefined,
undefined,
{
headers: { 'Content-type': PatchUtils.PATCH_FORMAT_JSON_MERGE_PATCH },
},
)
.then(() => ({ applied: true }))
.catch((e) => {
fastify.log.error(
`Unable to update Namespace "${name}" with context "${
NamespaceApplicationCase[context]
}". ${e.response?.body?.message || e.message}`,
);
return { applied: false };
});
};