Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kubernetes Mirage Setup #17943

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions ui/mirage/factories/kubernetes-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Factory } from 'ember-cli-mirage';

export default Factory.extend({
kubernetes_host: 'https://192.168.99.100:8443',
kubernetes_ca_cert: '-----BEGIN CERTIFICATE-----\n.....\n-----END CERTIFICATE-----',
disable_local_ca_jwt: false,

// property used only for record lookup and filtered from response payload
path: null,
});
16 changes: 16 additions & 0 deletions ui/mirage/factories/kubernetes-role.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Factory } from 'ember-cli-mirage';

export default Factory.extend({
name: 'default-role',
allowed_kubernetes_namespaces: () => ['*'],
allowed_kubernetes_namespace_selector: '',
token_max_ttl: 86400,
token_default_ttl: 0,
service_account_name: 'default',
kubernetes_role_name: '',
kubernetes_role_type: 'Role',
generated_role_rules: '',
name_template: '',
extra_annotations: null,
extra_labels: null,
});
3 changes: 2 additions & 1 deletion ui/mirage/handlers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ import mfaConfig from './mfa-config';
import mfaLogin from './mfa-login';
import oidcConfig from './oidc-config';
import hcpLink from './hcp-link';
import kubernetes from './kubernetes';

export { base, activity, clients, db, kms, mfaConfig, mfaLogin, oidcConfig, hcpLink };
export { base, activity, clients, db, kms, mfaConfig, mfaLogin, oidcConfig, hcpLink, kubernetes };
68 changes: 68 additions & 0 deletions ui/mirage/handlers/kubernetes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { Response } from 'miragejs';

export default function (server) {
const getRecord = (schema, req, dbKey) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do these methods need to be defined within the server function? This might read a little clearer if they are defined outside of it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

createRecord uses the server instance but I suppose that could be passed in. Not sure if it's worth moving outside to then pass in the server instance though? I'm curious if there is a best practice regarding this or personal preference (thinking about possible eslint warning)?

const { path, name } = req.params;
const findBy = dbKey === 'kubernetesConfigs' ? { path } : { name };
const record = schema.db[dbKey].findBy(findBy);
if (record) {
delete record.path;
delete record.id;
}
return record ? { data: record } : new Response(404, {}, { errors: [] });
};
const createRecord = (req, key) => {
const data = JSON.parse(req.requestBody);
if (key === 'kubernetes-config') {
data.path = req.params.path;
}
server.create(key, data);
return new Response(204);
};
const deleteRecord = (schema, req, dbKey) => {
const record = getRecord(schema, req, dbKey);
if (record) {
schema.db[dbKey].remove(record.id);
}
return new Response(204);
};

server.get('/:path/config', (schema, req) => {
return getRecord(schema, req, 'kubernetesConfigs');
});
server.post('/:path/config', (schema, req) => {
return createRecord(req, 'kubernetes-config');
});
server.delete('/:path/config', (schema, req) => {
return deleteRecord(schema, req, 'kubernetesConfigs');
});
server.get('/:path/roles', (schema) => {
return {
data: {
keys: schema.db.kubernetesRoles.where({}).mapBy('name'),
},
};
});
server.get('/:path/roles/:name', (schema, req) => {
return getRecord(schema, req, 'kubernetesRoles');
});
server.post('/:path/roles/:name', (schema, req) => {
return createRecord(req, 'kubernetes-role');
});
server.delete('/:path/roles/:name', (schema, req) => {
return deleteRecord(schema, req, 'kubernetesRoles');
});
server.post('/:path/creds/:role', (schema, req) => {
const { role } = req.params;
const record = schema.db.kubernetesRoles.findBy({ name: role });
const data = JSON.parse(req.requestBody);
let errors;
if (!record) {
errors = [`role '${role}' does not exist`];
} else if (!data.kubernetes_namespace) {
errors = ["'kubernetes_namespace' is required"];
}
// creds cannot be fetched after creation so we don't need to store them
return errors ? new Response(400, {}, { errors }) : new Response(204);
});
}