@rancher/shell 1.2.5-rc.1 → 1.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/utils/cluster.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import semver from 'semver';
2
+ import { camelToTitle } from '@shell/utils/string';
2
3
  import { CAPI } from '@shell/config/labels-annotations';
3
4
  import { MANAGEMENT, VIRTUAL_HARVESTER_PROVIDER } from '@shell/config/types';
4
5
  import { SETTING } from '@shell/config/settings';
@@ -90,3 +91,11 @@ export function abbreviateClusterName(input) {
90
91
 
91
92
  return result;
92
93
  }
94
+
95
+ export function labelForAddon(name, configuration = true) {
96
+ const addon = camelToTitle(name.replace(/^(rke|rke2|rancher)-/, ''));
97
+ const fallback = `${ configuration ? '' : 'Add-on: ' }${ addon }`;
98
+ const key = `cluster.addonChart."${ name }"${ configuration ? '.configuration' : '.label' }`;
99
+
100
+ return this.$store.getters['i18n/withFallback'](key, null, fallback);
101
+ }
@@ -0,0 +1,277 @@
1
+ import merge from 'lodash/merge';
2
+ import { SECRET } from '@shell/config/types';
3
+ import { PROVISIONING_PRE_BOOTSTRAP } from '@shell/store/features';
4
+
5
+ export const VMWARE_VSPHERE = 'vmwarevsphere';
6
+
7
+ type Rke2Component = {
8
+ versionInfo: any;
9
+ userChartValues: any;
10
+ chartVersionKey: (chartName: string) => string;
11
+ value: any;
12
+ isEdit: boolean;
13
+ provider: string;
14
+ $store: any,
15
+ }
16
+
17
+ type SecretDetails = {
18
+ generateName: string,
19
+ upstreamClusterName: string,
20
+ upstreamNamespace: string,
21
+ downstreamName: string,
22
+ downstreamNamespace: string,
23
+ json?: object,
24
+ }
25
+ type Values = any;
26
+
27
+ type ChartValues = {
28
+ defaultValues: Values,
29
+ userValues: Values,
30
+ combined: Values,
31
+ };
32
+
33
+ const rootGenerateName = 'vsphere-secret-';
34
+
35
+ type SecretJson = any;
36
+
37
+ class VSphereUtils {
38
+ private async findSecret(
39
+ { $store }: Rke2Component, {
40
+ generateName, upstreamClusterName, upstreamNamespace, downstreamName, downstreamNamespace
41
+ }: SecretDetails): Promise<SecretJson | undefined> {
42
+ const secrets = await $store.dispatch('management/request', { url: `/v1/${ SECRET }/${ upstreamNamespace }?filter=metadata.name=${ generateName }` });
43
+
44
+ const applicableSecret = secrets.data?.filter((s: any) => {
45
+ return s.metadata.annotations['provisioning.cattle.io/sync-target-namespace'] === downstreamNamespace &&
46
+ s.metadata.annotations['provisioning.cattle.io/sync-target-name'] === downstreamName &&
47
+ s.metadata.annotations['rke.cattle.io/object-authorized-for-clusters'].includes(upstreamClusterName);
48
+ });
49
+
50
+ if (applicableSecret.length > 1) {
51
+ return Promise.reject(new Error(`Found multiple matching secrets (${ upstreamNamespace }/${ upstreamNamespace } for ${ upstreamClusterName }), this will cause synchronizing mishaps. Consider removing stale secrets from old clusters`));
52
+ }
53
+
54
+ return applicableSecret[0];
55
+ }
56
+
57
+ private async findOrCreateSecret(
58
+ rke2Component: Rke2Component,
59
+ {
60
+ generateName, upstreamClusterName, upstreamNamespace, downstreamName, downstreamNamespace, json
61
+ }: SecretDetails
62
+ ) {
63
+ const { $store } = rke2Component;
64
+
65
+ const secretJson = await this.findSecret(rke2Component, {
66
+ generateName,
67
+ upstreamClusterName,
68
+ upstreamNamespace,
69
+ downstreamName,
70
+ downstreamNamespace
71
+ }) || json;
72
+
73
+ return await $store.dispatch('management/create', secretJson);
74
+ }
75
+
76
+ private findChartValues({
77
+ versionInfo,
78
+ userChartValues,
79
+ chartVersionKey,
80
+ }: Rke2Component, chartName: string): ChartValues | undefined {
81
+ const chartValues = versionInfo[chartName]?.values;
82
+
83
+ if (!chartValues) {
84
+ return;
85
+ }
86
+ const userValues = userChartValues[chartVersionKey(chartName)];
87
+
88
+ return {
89
+ defaultValues: chartValues,
90
+ userValues,
91
+ combined: merge({}, chartValues || {}, userValues || {})
92
+ };
93
+ }
94
+
95
+ private handleVsphereSecret({ $store, provider }: { $store: any, provider: string}): boolean {
96
+ if (provider !== VMWARE_VSPHERE) {
97
+ return false;
98
+ }
99
+
100
+ const isPrebootstrapEnabled = $store.getters['features/get'](PROVISIONING_PRE_BOOTSTRAP);
101
+
102
+ if (!isPrebootstrapEnabled) {
103
+ return false;
104
+ }
105
+
106
+ return true;
107
+ }
108
+
109
+ /**
110
+ * Create upstream vsphere cpi secret to sync downstream
111
+ */
112
+ async handleVsphereCpiSecret(rke2Component: Rke2Component) {
113
+ if (!this.handleVsphereSecret(rke2Component)) {
114
+ return;
115
+ }
116
+
117
+ const generateName = `${ rootGenerateName }cpi-`;
118
+ const downstreamName = 'rancher-vsphere-cpi-credentials';
119
+ const downstreamNamespace = 'kube-system';
120
+ const { value } = rke2Component;
121
+
122
+ // check values for cpi chart has 'use our method' checkbox
123
+ const { userValues, combined } = this.findChartValues(rke2Component, 'rancher-vsphere-cpi') || {};
124
+
125
+ if (!combined?.vCenter?.credentialsSecret?.generate) {
126
+ if (userValues?.vCenter?.username) {
127
+ userValues.vCenter.username = '';
128
+ }
129
+ if (userValues?.vCenter?.password) {
130
+ userValues.vCenter.password = '';
131
+ }
132
+
133
+ return;
134
+ }
135
+
136
+ // find values needed in cpi chart value - https://github.com/rancher/vsphere-charts/blob/main/charts/rancher-vsphere-cpi/questions.yaml#L16-L42
137
+ const { username, password, host } = combined.vCenter;
138
+
139
+ if (!username || !password || !host) {
140
+ throw new Error('vSphere CPI username, password and host are all required when generating a new secret');
141
+ }
142
+
143
+ // create secret as per https://github.com/rancher/vsphere-charts/blob/main/charts/rancher-vsphere-cpi/templates/secret.yaml
144
+ const upstreamClusterName = value.metadata.name;
145
+ const upstreamNamespace = value.metadata.namespace;
146
+ const secret = await this.findOrCreateSecret(rke2Component, {
147
+ generateName,
148
+ upstreamClusterName,
149
+ upstreamNamespace,
150
+ downstreamName,
151
+ downstreamNamespace,
152
+ json: {
153
+ type: SECRET,
154
+ metadata: {
155
+ namespace: upstreamNamespace,
156
+ generateName,
157
+ labels: {
158
+ 'vsphere-cpi-infra': 'secret',
159
+ component: 'rancher-vsphere-cpi-cloud-controller-manager'
160
+ },
161
+ annotations: {
162
+ 'provisioning.cattle.io/sync-target-namespace': downstreamNamespace,
163
+ 'provisioning.cattle.io/sync-target-name': downstreamName,
164
+ 'rke.cattle.io/object-authorized-for-clusters': upstreamClusterName,
165
+ 'provisioning.cattle.io/sync-bootstrap': 'true'
166
+ }
167
+ },
168
+ }
169
+ });
170
+
171
+ secret.setData(`${ host }.username`, username);
172
+ secret.setData(`${ host }.password`, password);
173
+
174
+ await secret.save();
175
+
176
+ // reset cpi chart values
177
+ if (!userValues.vCenter.credentialsSecret) {
178
+ userValues.vCenter.credentialsSecret = {};
179
+ }
180
+ userValues.vCenter.credentialsSecret.generate = false;
181
+ userValues.vCenter.credentialsSecret.name = downstreamName;
182
+ userValues.vCenter.username = '';
183
+ userValues.vCenter.password = '';
184
+ }
185
+
186
+ /**
187
+ * Create upstream vsphere csi secret to sync downstream
188
+ */
189
+ async handleVsphereCsiSecret(rke2Component: Rke2Component) {
190
+ if (!this.handleVsphereSecret(rke2Component)) {
191
+ return;
192
+ }
193
+
194
+ const generateName = `${ rootGenerateName }csi-`;
195
+ const downstreamName = 'rancher-vsphere-csi-credentials';
196
+ const downstreamNamespace = 'kube-system';
197
+ const { value } = rke2Component;
198
+
199
+ // check values for cpi chart has 'use our method' checkbox
200
+ const { userValues, combined } = this.findChartValues(rke2Component, 'rancher-vsphere-csi') || {};
201
+
202
+ if (!combined?.vCenter?.configSecret?.generate) {
203
+ if (userValues?.vCenter?.username) {
204
+ userValues.vCenter.username = '';
205
+ }
206
+ if (userValues?.vCenter?.password) {
207
+ userValues.vCenter.password = '';
208
+ }
209
+
210
+ return;
211
+ }
212
+
213
+ // find values needed in cpi chart value - https://github.com/rancher/vsphere-charts/blob/main/charts/rancher-vsphere-csi/questions.yaml#L1-L36
214
+ const {
215
+ username, password, host, datacenters, port, insecureFlag
216
+ } = combined.vCenter;
217
+
218
+ if (!username || !password || !host || !datacenters) {
219
+ throw new Error('vSphere CSI username, password, host and datacenters are all required when generating a new secret');
220
+ }
221
+
222
+ // This is a copy of https://github.com/rancher/vsphere-charts/blob/a5c99d716df960dc50cf417d9ecffad6b55ca0ad/charts/rancher-vsphere-csi/values.yaml#L12-L21
223
+ // Which makes it's way into the secret via https://github.com/rancher/vsphere-charts/blob/main/charts/rancher-vsphere-csi/templates/secret.yaml#L8
224
+ let configTemplateString = ' [Global]\n cluster-id = {{ required \".Values.vCenter.clusterId must be provided\" (default .Values.vCenter.clusterId .Values.global.cattle.clusterId) | quote }}\n user = {{ .Values.vCenter.username | quote }}\n password = {{ .Values.vCenter.password | quote }}\n port = {{ .Values.vCenter.port | quote }}\n insecure-flag = {{ .Values.vCenter.insecureFlag | quote }}\n\n [VirtualCenter {{ .Values.vCenter.host | quote }}]\n datacenters = {{ .Values.vCenter.datacenters | quote }}';
225
+
226
+ configTemplateString = configTemplateString.replace('{{ required \".Values.vCenter.clusterId must be provided\" (default .Values.vCenter.clusterId .Values.global.cattle.clusterId) | quote }}', `"{{clusterId}}"`);
227
+ configTemplateString = configTemplateString.replace('{{ .Values.vCenter.username | quote }}', `"${ username }"`);
228
+ configTemplateString = configTemplateString.replace('{{ .Values.vCenter.password | quote }}', `"${ password }"`);
229
+ configTemplateString = configTemplateString.replace('{{ .Values.vCenter.port | quote }}', `"${ port }"`);
230
+ configTemplateString = configTemplateString.replace('{{ .Values.vCenter.insecureFlag | quote }}', `"${ insecureFlag }"`);
231
+ configTemplateString = configTemplateString.replace('{{ .Values.vCenter.host | quote }}', `"${ host }"`);
232
+ configTemplateString = configTemplateString.replace('{{ .Values.vCenter.datacenters | quote }}', `"${ datacenters }"`);
233
+ // create secret as per https://github.com/rancher/vsphere-charts/blob/main/charts/rancher-vsphere-csi/templates/secret.yaml
234
+ const upstreamClusterName = value.metadata.name;
235
+ const upstreamNamespace = value.metadata.namespace;
236
+
237
+ const secret = await this.findOrCreateSecret(rke2Component, {
238
+ generateName,
239
+ upstreamClusterName,
240
+ upstreamNamespace,
241
+ downstreamName,
242
+ downstreamNamespace,
243
+ json: {
244
+ type: SECRET,
245
+ metadata: {
246
+ namespace: upstreamNamespace,
247
+ generateName,
248
+ annotations: {
249
+ 'provisioning.cattle.io/sync-target-namespace': downstreamNamespace,
250
+ 'provisioning.cattle.io/sync-target-name': downstreamName,
251
+ 'rke.cattle.io/object-authorized-for-clusters': upstreamClusterName,
252
+ 'provisioning.cattle.io/sync-bootstrap': 'true'
253
+ }
254
+ },
255
+ }
256
+ });
257
+
258
+ secret.setData(`csi-vsphere.conf`, configTemplateString);
259
+
260
+ await secret.save();
261
+
262
+ // reset csi chart values
263
+ if (!userValues.vCenter.configSecret) {
264
+ userValues.vCenter.configSecret = {};
265
+ }
266
+ userValues.vCenter.configSecret.generate = false;
267
+ userValues.vCenter.configSecret.name = downstreamName;
268
+ userValues.vCenter.username = '';
269
+ userValues.vCenter.password = '';
270
+ userValues.vCenter.host = '';
271
+ userValues.vCenter.datacenters = '';
272
+ }
273
+ }
274
+
275
+ const utils = new VSphereUtils();
276
+
277
+ export default utils;
@@ -1,2 +0,0 @@
1
- // Auto-generated type definitions for shell
2
- // Do not modify this file as changes will get overwritten