/
role.ts
130 lines (126 loc) · 5.04 KB
/
role.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
/*
* Copyright © 2020 Atomist, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { logger } from "@atomist/automation-client/lib/util/logger";
import * as k8s from "@kubernetes/client-node";
import * as _ from "lodash";
import { k8sErrMsg } from "../support/error";
import { logRetry } from "../support/retry";
import { applicationLabels } from "./labels";
import { metadataTemplate } from "./metadata";
import { patchHeaders } from "./patch";
import {
appName,
KubernetesApplication,
KubernetesResourceRequest,
KubernetesSdm,
} from "./request";
import { logObject } from "./resource";
/**
* Create or patch role or cluster role.
*
* @param req Kubernetes application request
* @return Kubernetes resource spec used to create/update the resource
*/
export async function upsertRole(req: KubernetesResourceRequest): Promise<k8s.V1Role | k8s.V1ClusterRole> {
const slug = appName(req);
if (req.roleSpec.kind === "ClusterRole") {
const spec = await clusterRoleTemplate(req);
try {
await req.clients.rbac.readClusterRole(spec.metadata.name);
} catch (e) {
logger.debug(`Failed to read cluster role ${slug}, creating: ${k8sErrMsg(e)}`);
logger.info(`Creating cluster role ${slug} using '${logObject(spec)}'`);
await logRetry(() => req.clients.rbac.createClusterRole(spec), `create cluster role ${slug}`);
return spec;
}
logger.info(`Cluster role ${slug} exists, patching using '${logObject(spec)}'`);
await logRetry(() => req.clients.rbac.patchClusterRole(spec.metadata.name, spec,
undefined, undefined, undefined, undefined, patchHeaders(req)), `patch cluster role ${slug}`);
return spec;
} else {
const spec = await roleTemplate(req);
try {
await req.clients.rbac.readNamespacedRole(spec.metadata.name, spec.metadata.namespace);
} catch (e) {
logger.debug(`Failed to read role ${slug}, creating: ${k8sErrMsg(e)}`);
logger.info(`Creating role ${slug} using '${logObject(spec)}'`);
await logRetry(() => req.clients.rbac.createNamespacedRole(spec.metadata.namespace, spec), `create role ${slug}`);
return spec;
}
logger.info(`Role ${slug} exists, patching using '${logObject(spec)}'`);
await logRetry(() => req.clients.rbac.patchNamespacedRole(spec.metadata.name, spec.metadata.namespace, spec,
undefined, undefined, undefined, undefined, patchHeaders(req)), `patch role ${slug}`);
return spec;
}
}
/**
* Create role spec for a Kubernetes application. The
* `req.rbac.roleSpec` is merged into the spec created by this
* function using `lodash.merge(default, req.rbac.roleSpec)`.
*
* It is possible to override the role name using the
* [[KubernetesApplication.roleSpec]]. If you do this, make sure you
* know what you are doing and also override it in the
* [[KubernetesApplication.roleBindingSpec]].
*
* @param req application request
* @return role resource specification
*/
export async function roleTemplate(req: KubernetesApplication & KubernetesSdm): Promise<k8s.V1Role> {
const labels = applicationLabels(req);
const metadata = metadataTemplate({
name: req.name,
namespace: req.ns,
labels,
});
const apiVersion = "rbac.authorization.k8s.io/v1";
const kind = "Role";
const r: Partial<k8s.V1Role> = {
metadata,
rules: [],
};
_.merge(r, req.roleSpec, { apiVersion, kind });
r.metadata.namespace = req.ns;
return r as k8s.V1Role;
}
/**
* Create role spec for a Kubernetes application. The
* `req.rbac.roleSpec` is merged into the spec created by this
* function using `lodash.merge(default, req.rbac.roleSpec)`.
*
* It is possible to override the cluster role name using the
* [[KubernetesApplication.roleSpec]]. If you do this, make sure you
* know what you are doing and also override it in the
* [[KubernetesApplication.roleBindingSpec]].
*
* @param req application request
* @return role resource specification
*/
export async function clusterRoleTemplate(req: KubernetesApplication & KubernetesSdm): Promise<k8s.V1ClusterRole> {
const labels = applicationLabels(req);
const metadata = metadataTemplate({
name: req.name,
labels,
});
const apiVersion = "rbac.authorization.k8s.io/v1";
const kind = "ClusterRole";
const r: Partial<k8s.V1ClusterRole> = {
metadata,
rules: [],
};
_.merge(r, req.roleSpec, { apiVersion, kind });
return r as k8s.V1ClusterRole;
}