forked from rancher/dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcatalog.cattle.io.clusterrepo.js
More file actions
258 lines (211 loc) · 6.38 KB
/
Copy pathcatalog.cattle.io.clusterrepo.js
File metadata and controls
258 lines (211 loc) · 6.38 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import { parse } from '@shell/utils/url';
import { CATALOG } from '@shell/config/labels-annotations';
import { insertAt } from '@shell/utils/array';
import { CLUSTER_REPO_APPCO_AUTH_GENERATE_NAME, CATALOG as CATALOG_TYPE } from '@shell/config/types';
import { colorForState, stateDisplay } from '@shell/plugins/dashboard-store/resource-class';
import { _CREATE } from '@shell/config/query-params';
import SteveModel from '@shell/plugins/steve/steve-class';
export default class ClusterRepo extends SteveModel {
applyDefaults() {
if ( !this.spec ) {
this['spec'] = { url: '' };
}
}
get _isClusterRepoDisabled() {
return this.spec?.enabled === false;
}
get _availableActions() {
const out = super._availableActions;
insertAt(out, 0, { divider: true });
if (this._isClusterRepoDisabled) {
insertAt(out, 1, {
action: 'enableClusterRepo',
label: this.t('action.enable'),
icon: 'icon icon-play',
enabled: true,
bulkable: true,
});
} else {
insertAt(out, 1, {
action: 'disableClusterRepo',
label: this.t('action.disable'),
icon: 'icon icon-pause',
enabled: true,
bulkable: true,
});
insertAt(out, 0, {
action: 'refresh',
label: this.t('action.refresh'),
icon: 'icon icon-refresh',
enabled: !!this.links.update,
bulkable: true,
});
}
return out;
}
async refresh() {
const now = (new Date()).toISOString().replace(/\.\d+Z$/, 'Z');
this.spec.forceUpdate = now;
await this.save();
await this.waitForState('active', 10000, 1000);
this.$dispatch('catalog/load', { force: true, reset: true }, { root: true });
}
async disableClusterRepo() {
this.spec.enabled = false;
await this.save();
}
async enableClusterRepo() {
this.spec.enabled = true;
await this.save();
}
get isGit() {
return !!this.spec?.gitRepo;
}
get isOciType() {
const hasExplicitOciUrl = this.spec.url?.split(':')[0] === 'oci';
// insecurePlainHttp is only valid for OCI URL's and allows insecure connections to registries without enforcing TLS checks
const hasInsecurePlainHttp = Object.prototype.hasOwnProperty.call(this.spec, ('insecurePlainHttp'));
return hasExplicitOciUrl || hasInsecurePlainHttp;
}
get isRancherSource() {
let parsed;
if ( this.spec?.url && this.spec?.gitRepo ) {
// Well that's suspicious...
return false;
}
if ( this.spec?.url ) {
parsed = parse(this.spec.url);
if ( parsed && ok(parsed.host) ) {
return true;
}
}
if ( this.spec?.gitRepo ) {
parsed = parse(this.spec.gitRepo);
if ( parsed && ok(parsed.host) ) {
return true;
}
}
return false;
function ok(host) {
host = (host || '').toLowerCase();
return host === 'rancher.io' || host.endsWith('.rancher.io');
}
}
get isRancher() {
return this.isRancherSource && this.metadata.name === 'rancher-charts';
}
get isPartner() {
return this.isRancherSource && this.metadata.name === 'rancher-partner-charts';
}
get color() {
if ( this.isRancher ) {
return 'rancher';
} else if ( this.isPartner ) {
return 'partner';
} else {
const color = parseInt(this.metadata?.annotations?.[CATALOG.COLOR], 10);
if ( isNaN(color) || color <= 0 || color > 8 ) {
return null;
}
return `color${ color }`;
}
}
get canLoad() {
return this.metadata?.state?.name === 'active';
}
get isSuseAppCollectionFromUI() {
return this.metadata?.annotations?.[CATALOG.SUSE_APP_COLLECTION];
}
get isSuseAppCollection() {
// Check annotation set by the UI or if the URL points to the SUSE App Collection registry
return this.isSuseAppCollectionFromUI || this.spec?.url?.startsWith('oci://dp.apps.rancher.io/charts');
}
get typeDisplay() {
if (this.isSuseAppCollectionFromUI) {
return 'SUSE AppCo';
}
if ( this.spec.gitRepo ) {
return 'git';
}
if ( this.spec.url ) {
return this.isOciType ? 'oci' : 'http';
}
return '?';
}
get nameDisplay() {
const name = this.metadata?.name;
const key = `catalog.repo.name."${ name }"`;
return this.$rootGetters['i18n/withFallback'](key, null, name);
}
detailPageHeaderActionOverride(realMode) {
if (realMode === _CREATE) {
return this.t('catalog.repo.add');
}
return null;
}
get urlDisplay() {
return this.status?.url || this.spec.gitRepo || this.spec.url;
}
get branchDisplay() {
return this.spec?.gitBranch || '(default)';
}
get details() {
return [
{
label: 'Type',
content: this.typeDisplay,
},
{
label: 'Downloaded',
content: this.status.downloadTime,
formatter: 'LiveDate',
formatterOpts: { addSuffix: true },
},
];
}
get stateObj() {
return this.metadata?.state ? {
...this.metadata.state,
transitioning: this.metadata.generation > this.status?.observedGeneration ? false : this.metadata.state.transitioning
} : undefined;
}
get stateDisplay() {
if (this._isClusterRepoDisabled) {
return this.t('generic.disabled');
} else {
return stateDisplay(this.state);
}
}
get stateBackground() {
if (this._isClusterRepoDisabled) {
return 'badge-disabled';
} else {
return colorForState(this.state, this.stateObj?.error, this.stateObj?.transitioning).replace('text-', 'bg-');
}
}
waitForOperation(operationId, timeout, interval = 2000) {
return this.waitForTestFn(() => {
if (!this.$getters['schemaFor'](CATALOG_TYPE.OPERATION)) {
return false;
}
if (this.$getters['byId'](CATALOG_TYPE.OPERATION, operationId)) {
return true;
}
this.$dispatch('find', {
type: CATALOG_TYPE.OPERATION,
id: operationId
});
}, `catalog operation fetch`, timeout, interval);
}
async save() {
// Add annotation only if the type is SUSE_APP_COLLECTION
if (this.spec.clientSecret?.name?.search(CLUSTER_REPO_APPCO_AUTH_GENERATE_NAME) === 0) {
if (!this.metadata.annotations) {
this.metadata.annotations = {};
}
this.metadata.annotations[CATALOG.SUSE_APP_COLLECTION] = 'true';
}
// Call the parent save method
return super.save();
}
}