Skip to content
This repository has been archived by the owner on May 15, 2021. It is now read-only.

Commit

Permalink
migrate to /openapi/v2 OpenAPI endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
nailgun committed Aug 7, 2020
1 parent 720deb8 commit 1a3f7a0
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 56 deletions.
9 changes: 8 additions & 1 deletion src/modules/k8s/combo.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ import {
resourceSelect,
} from './resources';

import {
MODELS_GET__S,
MODELS_GET__F,
modelsGet,
} from './models';

import {
ITEMS_GET__S,
ITEMS_GET__F,
Expand Down Expand Up @@ -134,6 +140,7 @@ function* sagaCatalogGet() {
.filter(group => !group[RESOURCE_IDS].length)
.map(group => putTake(resourcesGet(group), [RESOURCES_GET__S, RESOURCES_GET__F]))
);
yield putTake(modelsGet(), [MODELS_GET__S, MODELS_GET__F]);

// namespaces <- [state] <- [storage] <- [api]
let namespaces;
Expand Down Expand Up @@ -176,7 +183,7 @@ function* sagaRbacGet() {
yield all(resources.map(resource =>
putTake(itemsGet(resource), [ITEMS_GET__S, ITEMS_GET__F])
));

//
return {};
},
Expand Down
22 changes: 16 additions & 6 deletions src/modules/k8s/items.js
Original file line number Diff line number Diff line change
Expand Up @@ -496,31 +496,41 @@ function itemGetVersionByApiVersion(apiVersion) {
apiVersion &&
(
apiVersion.includes('/')
? apiVersion.split('/')[1]
: apiVersion
? apiVersion
: ('core/' + apiVersion)
)
);
}

function itemGetModelId(item) {
const { apiVersion, kind } = item;
return `${itemGetVersionByApiVersion(apiVersion)}.${kind}`;
return `${itemGetVersionByApiVersion(apiVersion)}/${kind}`;
}

export function itemRemoveReadonlyProperties(item, models, modelId, forcedKeys) {
export function itemRemoveReadonlyProperties(item, models, defId, forcedKeys) {
if (item && models) {

// remove forced keys
forcedKeys && forcedKeys.forEach(key => delete item[key]);

// remove readonly keys
const model = models[modelId || itemGetModelId(item)];
let model;
if (defId) {
model = models.definitions[defId];
} else {
let modelId = itemGetModelId(item);
model = models.byGVK[modelId];
}

if (model) {
const { properties } = model;
Object.keys(properties).forEach(key => {
const { [IS_READONLY]: readonly, $ref } = properties[key];
if (readonly) delete item[key];
else if ($ref) itemRemoveReadonlyProperties(item[key], models, $ref);
else if ($ref) {
const refDefId = $ref.replace(/^#\/definitions\//, '');
itemRemoveReadonlyProperties(item[key], models, refDefId);
}
});
}
}
Expand Down
63 changes: 26 additions & 37 deletions src/modules/k8s/models.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { all, call } from 'redux-saga/effects';
import update from 'immutability-helper';

import {
NotiErrorApi,
} from '../../middleware/notifications';

import {
toKeysObject,
} from '../../utils';

import {
PREFIX,
ID,
Expand All @@ -17,10 +12,6 @@ import {
takeEveryReq,
} from './shared';

import {
groupGetUrl,
} from './groups';


// action codes
// --------------
Expand All @@ -33,9 +24,8 @@ export const MODELS_GET__F = `${PREFIX}/MODELS_GET/F`;
// action creators
// -----------------

export const modelsGet = (group, version) => ({
export const modelsGet = () => ({
type: MODELS_GET,
payload: { group, version },
});


Expand All @@ -62,42 +52,40 @@ function* sagaModelsGet() {
MODELS_GET__F,
],
function* (action) {
const { group, version } = action.payload;

//
const modelUrl = `/swaggerapi${groupGetUrl(group, version)}`;

// get models
let models;
try { models = (yield call(cacheGet, modelUrl)).models; }
try { models = yield call(cacheGet, '/openapi/v2'); }
catch (e) {
if (!(e instanceof NotiErrorApi && e.code === 404)) throw e;
else {
const error = new Error();
error.title = group.name;
error.message = 'No swagger schemas provided. Removing readonly properties for items in this group won\'t work.';
error.title = 'OpenAPI';
error.message = 'No OpenAPI schema. Removing readonly properties for items won\'t work.';
throw error;
}
}

// process models
models = Object.keys(models).map(key => {
const model = models[key];

// set id
delete model.id;
model[ID] = key;
models.byGVK = {};
Object.keys(models.definitions).forEach(key => {
const def = models.definitions[key];
def.$self = key;

const gvks = def['x-kubernetes-group-version-kind'];
if (gvks) {
gvks.forEach(gvk => {
const key = `${gvk.group || 'core'}/${gvk.version}/${gvk.kind}`;
models.byGVK[key] = def;
def[ID] = key;
});
}

// set readonly flags
const { properties } = model;
Object.keys(properties).forEach(id => {
const property = properties[id];
const { description } = property;
property[IS_READONLY] = description && description.includes('Read-only.');
});

//
return model;
if (def.properties) {
Object.values(def.properties).forEach(prop => {
prop[IS_READONLY] = prop.description && prop.description.includes('Read-only.');
});
}
});

//
Expand All @@ -120,8 +108,9 @@ export const modelsReducer = {

[MODELS_GET__S]: (state, action) => {
const { models } = action.payload;
return update(state, {
models: { $merge: toKeysObject(models, ID) },
});
return {
...state,
models: models,
};
},
};
12 changes: 0 additions & 12 deletions src/modules/k8s/resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,13 @@ import {
URL_PART_GROUP,
URL_PART_RESOURCE,
cacheGet,
putTake,
takeEveryReq,
} from './shared';

import {
groupGetUrl,
} from './groups';

import {
MODELS_GET__S,
MODELS_GET__F,
modelsGet,
} from './models';


// action codes
// --------------
Expand Down Expand Up @@ -132,11 +125,6 @@ function* sagaResourcesGet() {
[],
);

// models
yield all(group[VERSIONS].map(version =>
putTake(modelsGet(group, version), [MODELS_GET__S, MODELS_GET__F])
));

//
return { group, resources };
},
Expand Down

0 comments on commit 1a3f7a0

Please sign in to comment.