Skip to content

Commit 215d5a1

Browse files
Merge pull request #1226 from spadgett/swagger-storage
Only store swagger.json definitions
2 parents d10fb8b + 08abe43 commit 215d5a1

File tree

4 files changed

+36
-23
lines changed

4 files changed

+36
-23
lines changed

frontend/__tests__/module/k8s/autocomplete.spec.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,22 +85,20 @@ describe('getCompletions', () => {
8585
});
8686

8787
it('invokes callback with appropriate completions for properties', (done) => {
88-
const swagger = {
89-
definitions: {
90-
'io.k8s.api.apps.v1.Deployment': {},
91-
'io.k8s.api.apps.v1.DeploymentSpec': {
92-
properties: {
93-
minReadySeconds: {
94-
description: 'Dummy property',
95-
type: 'integer',
96-
format: 'int32',
97-
},
88+
const definitions = {
89+
'io.k8s.api.apps.v1.Deployment': {},
90+
'io.k8s.api.apps.v1.DeploymentSpec': {
91+
properties: {
92+
minReadySeconds: {
93+
description: 'Dummy property',
94+
type: 'integer',
95+
format: 'int32',
9896
},
9997
},
10098
},
10199
};
102100
sessionMock.getLines = jasmine.createSpy('getLinesSpy').and.returnValue(['kind: Deployment', 'apiVersion: apps/v1']);
103-
spyOn(window.localStorage, 'getItem').and.returnValue(JSON.stringify(swagger));
101+
spyOn(window.sessionStorage, 'getItem').and.returnValue(JSON.stringify(definitions));
104102

105103
getCompletions(editorMock, sessionMock, position, '', (error, results) => {
106104
expect(results.length).toEqual(1);
@@ -116,7 +114,7 @@ describe('getCompletions', () => {
116114

117115
it('does not provide completion for properties if k8s API spec cannot be retrieved', (done) => {
118116
sessionMock.getLines = jasmine.createSpy('getLinesSpy').and.returnValue(['kind: Deployment', 'apiVersion: apps/v1']);
119-
spyOn(window.localStorage, 'getItem').and.returnValue(null);
117+
spyOn(window.sessionStorage, 'getItem').and.returnValue(null);
120118

121119
getCompletions(editorMock, sessionMock, position, '', (error, results) => {
122120
fail('Should not have been called');

frontend/public/components/edit-yaml.jsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { SafetyFirst } from './safety-first';
1818
import { coFetchJSON } from '../co-fetch';
1919
import { ResourceSidebar } from './sidebars/resource-sidebar';
2020
import { yamlTemplates } from '../models/yaml-templates';
21+
import { SWAGGER_SESSION_STORAGE_KEY } from '../const';
2122

2223
const { snippetManager } = ace.acequire('ace/snippets');
2324
snippetManager.register([...snippets.values()], 'yaml');
@@ -72,9 +73,17 @@ export const EditYAML = connect(stateToProps)(
7273
}
7374

7475
// Retrieve k8s API spec for autocompletion
75-
if (!window.sessionStorage.getItem(`${window.SERVER_FLAGS.consoleVersion}--swagger.json`)) {
76-
coFetchJSON('api/kubernetes/swagger.json')
77-
.then(swagger => window.sessionStorage.setItem(`${window.SERVER_FLAGS.consoleVersion}--swagger.json`, JSON.stringify(swagger)));
76+
if (!window.sessionStorage.getItem(SWAGGER_SESSION_STORAGE_KEY)) {
77+
coFetchJSON('api/kubernetes/swagger.json').then(swagger => {
78+
// Only store definitions to reduce the document size.
79+
const json = JSON.stringify(swagger.definitions || {});
80+
try {
81+
window.sessionStorage.setItem(SWAGGER_SESSION_STORAGE_KEY, json);
82+
} catch (e) {
83+
// eslint-disable-next-line no-console
84+
console.error('Could not store swagger.json', e);
85+
}
86+
});
7887
}
7988
}
8089

frontend/public/const.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export const NAMESPACE_LOCAL_STORAGE_KEY = 'dropdown-storage-namespaces';
2626
export const LAST_NAMESPACE_NAME_LOCAL_STORAGE_KEY = `${STORAGE_PREFIX}/last-namespace-name`;
2727
export const API_DISCOVERY_RESOURCES_LOCAL_STORAGE_KEY = `${STORAGE_PREFIX}/api-discovery-resources`;
2828
export const COMMUNITY_PROVIDERS_WARNING_LOCAL_STORAGE_KEY = `${STORAGE_PREFIX}/community-providers-warning`;
29+
export const SWAGGER_SESSION_STORAGE_KEY = `${STORAGE_PREFIX}/${window.SERVER_FLAGS.consoleVersion}/swagger-definitions`;
2930

3031
// Bootstrap user for OpenShift 4.0 clusters
3132
export const KUBE_ADMIN_USERNAME = 'kube:admin';

frontend/public/module/k8s/autocomplete.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { ServiceAccountModel, SecretModel, ServiceModel, ConfigMapModel, Alertma
88
import { K8sKind, K8sResourceKind } from '../../module/k8s';
99
import { k8sListPartialMetadata } from '../../module/k8s/resource';
1010
import { getActiveNamespace } from '../../ui/ui-actions';
11+
import { SWAGGER_SESSION_STORAGE_KEY } from '../../const';
1112

1213
export type AceSnippet = {
1314
content: string;
@@ -139,11 +140,11 @@ export const getPropertyCompletions = async(state: Editor, session: IEditSession
139140

140141
const kind = valueFor('kind');
141142
const apiVersion = valueFor('apiVersion');
142-
const swagger: SwaggerAPISpec = JSON.parse(window.sessionStorage.getItem(`${(window as any).SERVER_FLAGS.consoleVersion}--swagger.json`));
143+
const swaggerDefinitions: SwaggerDefinitions = JSON.parse(window.sessionStorage.getItem(SWAGGER_SESSION_STORAGE_KEY));
143144

144-
if (kind.length && apiVersion.length && swagger) {
145-
const defKey = Object.keys(swagger.definitions).find(key => key.endsWith(`${apiVersion.replace('/', '.')}.${kind}`));
146-
const results = Object.keys(_.get(swagger.definitions, [`${defKey}Spec`, 'properties'], {})).map(prop => ({
145+
if (kind.length && apiVersion.length && swaggerDefinitions) {
146+
const defKey = Object.keys(swaggerDefinitions).find(key => key.endsWith(`${apiVersion.replace('/', '.')}.${kind}`));
147+
const results = Object.keys(_.get(swaggerDefinitions, [`${defKey}Spec`, 'properties'], {})).map(prop => ({
147148
name: prop,
148149
score: 10000,
149150
value: prop,
@@ -205,14 +206,18 @@ export const getCompletions: Completer['getCompletions'] = (editor, session, pos
205206
}
206207
};
207208

209+
export type SwaggerDefinitions = {
210+
[name: string]: {
211+
description: string;
212+
properties: {[prop: string]: {description: string, type: string}};
213+
}
214+
};
215+
208216
export type SwaggerAPISpec = {
209217
swagger: string;
210218
info: {title: string, version: string};
211219
paths: {[path: string]: any};
212-
definitions: {[name: string]: {
213-
description: string;
214-
properties: {[prop: string]: {description: string, type: string}};
215-
}};
220+
definitions: SwaggerDefinitions;
216221
};
217222

218223
// TODO: Remove once https://github.com/DefinitelyTyped/DefinitelyTyped/pull/25337 is merged

0 commit comments

Comments
 (0)