@rancher/shell 3.0.0-rc.4 → 3.0.0-rc.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
@@ -94,7 +94,7 @@ export function abbreviateClusterName(input) {
94
94
 
95
95
  export function labelForAddon(store, name, configuration = true) {
96
96
  const addon = camelToTitle(name.replace(/^(rke|rke2|rancher)-/, ''));
97
- const fallback = `${ addon } ${ configuration ? 'Configuration' : '' }`;
97
+ const fallback = `${ configuration ? '' : 'Add-on: ' }${ addon }`;
98
98
  const key = `cluster.addonChart."${ name }"${ configuration ? '.configuration' : '.label' }`;
99
99
 
100
100
  return store.getters['i18n/withFallback'](key, null, fallback);
@@ -0,0 +1,240 @@
1
+ import merge from 'lodash/merge';
2
+ import { SECRET } from '@shell/config/types';
3
+
4
+ type Rke2Component = {
5
+ versionInfo: any;
6
+ userChartValues: any;
7
+ chartVersionKey: (chartName: string) => string;
8
+ value: any;
9
+ isEdit: boolean;
10
+ $store: any,
11
+ }
12
+
13
+ type SecretDetails = {
14
+ generateName: string,
15
+ upstreamClusterName: string,
16
+ upstreamNamespace: string,
17
+ downstreamName: string,
18
+ downstreamNamespace: string,
19
+ json?: object,
20
+ }
21
+ type Values = any;
22
+
23
+ type ChartValues = {
24
+ defaultValues: Values,
25
+ userValues: Values,
26
+ combined: Values,
27
+ };
28
+
29
+ const rootGenerateName = 'vsphere-secret-';
30
+
31
+ type SecretJson = any;
32
+
33
+ class VSphereUtils {
34
+ private async findSecret(
35
+ { $store }: Rke2Component, {
36
+ generateName, upstreamClusterName, upstreamNamespace, downstreamName, downstreamNamespace
37
+ }: SecretDetails): Promise<SecretJson | undefined> {
38
+ // Fetch secrets in a specific namespace and partially matching the name to the generate name
39
+ const secrets = await $store.dispatch('management/request', { url: `/v1/${ SECRET }/${ upstreamNamespace }?filter=metadata.name=${ generateName }` });
40
+
41
+ // Filter by specific annotations
42
+ const applicableSecret = secrets.data?.filter((s: any) => {
43
+ return s.metadata.annotations['provisioning.cattle.io/sync-target-namespace'] === downstreamNamespace &&
44
+ s.metadata.annotations['provisioning.cattle.io/sync-target-name'] === downstreamName &&
45
+ s.metadata.annotations['rke.cattle.io/object-authorized-for-clusters'].includes(upstreamClusterName);
46
+ });
47
+
48
+ // If there's more than one the user should tidy up... as it'll cause mayhem with the actual sync from local --> downstream cluster
49
+ if (applicableSecret.length > 1) {
50
+ 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`));
51
+ }
52
+
53
+ return applicableSecret[0];
54
+ }
55
+
56
+ private async findOrCreateSecret(
57
+ rke2Component: Rke2Component,
58
+ {
59
+ generateName, upstreamClusterName, upstreamNamespace, downstreamName, downstreamNamespace, json
60
+ }: SecretDetails
61
+ ) {
62
+ const { $store } = rke2Component;
63
+
64
+ const secretJson = await this.findSecret(rke2Component, {
65
+ generateName,
66
+ upstreamClusterName,
67
+ upstreamNamespace,
68
+ downstreamName,
69
+ downstreamNamespace
70
+ }) || json;
71
+
72
+ return await $store.dispatch('management/create', secretJson);
73
+ }
74
+
75
+ private findChartValues({
76
+ versionInfo,
77
+ userChartValues,
78
+ chartVersionKey,
79
+ }: Rke2Component, chartName: string): ChartValues | undefined {
80
+ const chartValues = versionInfo[chartName]?.values;
81
+
82
+ if (!chartValues) {
83
+ return;
84
+ }
85
+ const userValues = userChartValues[chartVersionKey(chartName)];
86
+
87
+ return {
88
+ defaultValues: chartValues,
89
+ userValues,
90
+ combined: merge({}, chartValues || {}, userValues || {})
91
+ };
92
+ }
93
+
94
+ /**
95
+ * Create upstream vsphere cpi secret to sync downstream
96
+ */
97
+ async handleVsphereCpiSecret(rke2Component: Rke2Component) {
98
+ const generateName = `${ rootGenerateName }cpi-`;
99
+ const downstreamName = 'vsphere-cpi-creds';
100
+ const downstreamNamespace = 'kube-system';
101
+ const { value } = rke2Component;
102
+
103
+ // check values for cpi chart has 'use our method' checkbox
104
+ const { userValues, combined } = this.findChartValues(rke2Component, 'rancher-vsphere-cpi') || {};
105
+
106
+ if (!combined?.vCenter?.credentialsSecret?.generate) {
107
+ return;
108
+ }
109
+
110
+ // find values needed in cpi chart value - https://github.com/rancher/vsphere-charts/blob/main/charts/rancher-vsphere-cpi/questions.yaml#L16-L42
111
+ const { username, password, host } = combined.vCenter;
112
+
113
+ if (!username || !password || !host) {
114
+ throw new Error('vSphere CPI username, password and host are all required when generating a new secret');
115
+ }
116
+
117
+ // create secret as per https://github.com/rancher/vsphere-charts/blob/main/charts/rancher-vsphere-cpi/templates/secret.yaml
118
+ const upstreamClusterName = value.metadata.name;
119
+ const upstreamNamespace = value.metadata.namespace;
120
+ const secret = await this.findOrCreateSecret(rke2Component, {
121
+ generateName,
122
+ upstreamClusterName,
123
+ upstreamNamespace,
124
+ downstreamName,
125
+ downstreamNamespace,
126
+ json: {
127
+ type: SECRET,
128
+ metadata: {
129
+ namespace: upstreamNamespace,
130
+ generateName,
131
+ labels: {
132
+ 'vsphere-cpi-infra': 'secret',
133
+ component: 'rancher-vsphere-cpi-cloud-controller-manager'
134
+ },
135
+ annotations: {
136
+ 'provisioning.cattle.io/sync-target-namespace': downstreamNamespace,
137
+ 'provisioning.cattle.io/sync-target-name': downstreamName,
138
+ 'rke.cattle.io/object-authorized-for-clusters': upstreamClusterName,
139
+ 'provisioning.cattle.io/sync-bootstrap': 'true'
140
+ }
141
+ },
142
+ }
143
+ });
144
+
145
+ secret.setData(`${ host }.username`, username);
146
+ secret.setData(`${ host }.password`, password);
147
+
148
+ await secret.save();
149
+
150
+ // reset cpi chart values
151
+ if (!userValues.vCenter.credentialsSecret) {
152
+ userValues.vCenter.credentialsSecret = {};
153
+ }
154
+ userValues.vCenter.credentialsSecret.generate = false;
155
+ userValues.vCenter.credentialsSecret.name = downstreamName;
156
+ userValues.vCenter.username = '';
157
+ userValues.vCenter.password = '';
158
+ }
159
+
160
+ /**
161
+ * Create upstream vsphere csi secret to sync downstream
162
+ */
163
+ async handleVsphereCsiSecret(rke2Component: Rke2Component) {
164
+ const generateName = `${ rootGenerateName }csi-`;
165
+ const downstreamName = 'vsphere-csi-creds';
166
+ const downstreamNamespace = 'kube-system';
167
+ const { value } = rke2Component;
168
+
169
+ // check values for cpi chart has 'use our method' checkbox
170
+ const { userValues, combined } = this.findChartValues(rke2Component, 'rancher-vsphere-csi') || {};
171
+
172
+ if (!combined?.vCenter?.configSecret?.generate) {
173
+ return;
174
+ }
175
+
176
+ // find values needed in cpi chart value - https://github.com/rancher/vsphere-charts/blob/main/charts/rancher-vsphere-csi/questions.yaml#L1-L36
177
+ const {
178
+ username, password, host, datacenters, port, insecureFlag
179
+ } = combined.vCenter;
180
+
181
+ if (!username || !password || !host || !datacenters) {
182
+ throw new Error('vSphere CSI username, password, host and datacenters are all required when generating a new secret');
183
+ }
184
+
185
+ // This is a copy of https://github.com/rancher/vsphere-charts/blob/a5c99d716df960dc50cf417d9ecffad6b55ca0ad/charts/rancher-vsphere-csi/values.yaml#L12-L21
186
+ // 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
187
+ let configTemplateString = ' |\n [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 }}';
188
+
189
+ configTemplateString = configTemplateString.replace('{{ required \".Values.vCenter.clusterId must be provided\" (default .Values.vCenter.clusterId .Values.global.cattle.clusterId) | quote }}', `"{{clusterId}}"`);
190
+ configTemplateString = configTemplateString.replace('{{ .Values.vCenter.username | quote }}', `"${ username }"`);
191
+ configTemplateString = configTemplateString.replace('{{ .Values.vCenter.password | quote }}', `"${ password }"`);
192
+ configTemplateString = configTemplateString.replace('{{ .Values.vCenter.port | quote }}', `"${ port }"`);
193
+ configTemplateString = configTemplateString.replace('{{ .Values.vCenter.insecureFlag | quote }}', `"${ insecureFlag }"`);
194
+ configTemplateString = configTemplateString.replace('{{ .Values.vCenter.host | quote }}', `"${ host }"`);
195
+ configTemplateString = configTemplateString.replace('{{ .Values.vCenter.datacenters | quote }}', `"${ datacenters }"`);
196
+ // create secret as per https://github.com/rancher/vsphere-charts/blob/main/charts/rancher-vsphere-csi/templates/secret.yaml
197
+ const upstreamClusterName = value.metadata.name;
198
+ const upstreamNamespace = value.metadata.namespace;
199
+
200
+ const secret = await this.findOrCreateSecret(rke2Component, {
201
+ generateName,
202
+ upstreamClusterName,
203
+ upstreamNamespace,
204
+ downstreamName,
205
+ downstreamNamespace,
206
+ json: {
207
+ type: SECRET,
208
+ metadata: {
209
+ namespace: upstreamNamespace,
210
+ generateName,
211
+ annotations: {
212
+ 'provisioning.cattle.io/sync-target-namespace': downstreamNamespace,
213
+ 'provisioning.cattle.io/sync-target-name': downstreamName,
214
+ 'rke.cattle.io/object-authorized-for-clusters': upstreamClusterName,
215
+ 'provisioning.cattle.io/sync-bootstrap': 'true'
216
+ }
217
+ },
218
+ }
219
+ });
220
+
221
+ secret.setData(`csi-vsphere.conf`, configTemplateString);
222
+
223
+ await secret.save();
224
+
225
+ // reset csi chart values
226
+ if (!userValues.vCenter.configSecret) {
227
+ userValues.vCenter.configSecret = {};
228
+ }
229
+ userValues.vCenter.configSecret.generate = false;
230
+ userValues.vCenter.configSecret.name = downstreamName;
231
+ userValues.vCenter.username = '';
232
+ userValues.vCenter.password = '';
233
+ userValues.vCenter.host = '';
234
+ userValues.vCenter.datacenters = '';
235
+ }
236
+ }
237
+
238
+ const utils = new VSphereUtils();
239
+
240
+ 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