@nmg/k8s

20.0.0 • Public • Published

@nmg/k8s

NMG helpers for kubernetes workloads

Installation

$ npm install @nmg/k8s

Usage

Cron:

shouldRun(crontab, timezone)

Returns Boolean if crontab should run

Example
const { cron } = require('@nmg/k8s');
const crontab = '* * * * *';

cron.shouldRun(crontab) ? true : false;

Data Warehouse:

queryDataWarehouse(options, [headers])

Example
const { queryDataWarehouse } = require('@nmg/k8s');
queryDataWarehouse(
  {
    url,
    variables,
    query,
  }, 
  headers
);
Dependent Environment Variables
  • process.env.NMG_K8S_DATA_WAREHOUSE_TOKEN

Email:

sendEmail(send_email_params, [credentials])

Example
const { sendEmail } = require('@nmg/k8s');
sendEmail(
  {
    to,
    subject,
    text,
    html,
    attachments,
  }, 
  { user, password }
); // auth vaules optional
Recommended Environment Variables
  • process.env.NMG_K8S_EMAIL_USER
  • process.env.NMG_K8S_EMAIL_PASSWORD

Encryption:

Encrypt

encrypt(value)

Example
const { encrypt } = require('@nmg/k8s');
encrypt(value);
  • process.env.NMG_K8S_SECRET

Decypt

decrypt(encrypted_value)

Example
const { encrypt } = require('@nmg/k8s');
encrypt(value);
Dependent Environment Variables
  • process.env.NMG_K8S_SECRET

Image:

cropImage(input_filepath, output_filepath, [options])

Example
const { cropImage } = require('@nmg/k8s');
cropImage(
  'path/to/original.png', 
  'path/to/new.png', 
  { color_bands: false }
);

IP:

getIp([debug])

debug: Boolean - if true, will log response status code

Will return an IPv4 address or null if there is an error

Example
const ip_helper = require('@nmg/k8s');
ip_helper.getIp().then((ip) => console.log(ip));

Log:

log(log_params)

Formats a log to show up in nmg-big-data.logging.logs BigQuery table

Example
const { log } = require('@nmg/k8s');
log(
  {
    level,
    error_message,
    message,
  }
);


MongoDB:

connect()

Sets up the connection to the mongo db.

getCollection(collection_name)

Gets the collection object for the name passed in.

create(collection_name, data, account) (recommended)

Creates a collection with data and account information.

insert(collection_name, data)

Inserts raw data into collection.

update(collection_name, data, account)

Updates inserted data.

read(collection_name, options, return_cursor)

Reads from collection based on filter and returns either cursor or data.

remove(collection_name, filter, account)

Removes data from collection based on filter.

disconnect()

Closes the connection to the mongo db.

Example
const MongoDbClient = require('@nmg/k8s');
const client = new MongoDBClient(
  {
    db_name,
    connection_url,
    user,
    password,
    options,
  }
);
const db = await client.connect();
await client.disconnect();
Recommended Environment Variables
  • process.env.MONGODB_DB_NAME
  • process.env.MONGODB_CONNECTION_URL
  • process.env.MONGODB_USER
  • process.env.MONGODB_PASSWORD

Mysql Helper:

setupDB()

Sets the DB connection info and returns it

query(_query)

runs a query and returns the results

getTableStructure(table)

describes the given table

structureToSchema(structure)

writeMysqlRowsToBigQueryJsonFile(table_config, where_mysql, output_filepath, mysql_structure)

Example
const MongoDbClient = require('@nmg/k8s');
setupDB(
  {
    host,
    user,
    password,
    database,
    port,
  }
);
const description = await getTableStructure('table_name');
Recommended Environment Variables
  • process.env.MYSQL_HOST
  • process.env.MYSQL_USER
  • process.env.MYSQL_PASS
  • process.env.MYSQL_NAME
  • process.env.MYSQL_PORT

Netsuite:

netsuite.executeScript(options, [env_overrides])

Execute NetSuite RESTlets using URL

Example
const { netsuite } = require('@nmg/k8s');
const env_overrides = {}
const restlet_response = await netsuite.executeScript(
  {
    url: 'https://5000005.restlets.api.netsuite.com/app/site/hosting/restlet.nl?script=999&deploy=1',
    data: {
      saved_search_id: 'customsearch1',
    },
  },
  env_overrides
)

options will be passed to the request({ ...options }) function with the authentication defaults

Recommended Env Variables
  • process.env.NETSUITE_REALM
  • process.env.NETSUITE_CUSTOMER_KEY
  • process.env.NETSUITE_CUSTOMER_SECRET
  • process.env.NETSUITE_TOKEN_KEY
  • process.env.NETSUITE_TOKEN_SECRET

Product Search (soon to be obsolete)

class ProductSearch({ gcs_keyfile })

Example
const { ProductSearch } = require('@nmg/k8s')
const product_search = new ProductSearch({
  keyfile: path.resolve(process.env.GOOGLE_APPLICATION_CREDENTIALS),
})
await product_search.setupCache();

const searches_results = await product_search.multiProductSearch({
  searches: [
    { pn_search: 'GSS25GMHES' },
    { pn_search: 'HBLP651RUC' },
    { pn_search: 'GSS25GGHWW' },
  ],
  num_possible_results: 1,
  cutoff: 0.9,
})
Environment Variables
  • process.env.NMG_K8S_DATA_WAREHOUSE_TOKEN

Queue:

Queue(size)

Example
const { Queue } = require('@nmg/k8s');
const queue = new Queue(10);

for (const page of response.pageRanges) {
  const _page = page;
  console.log(`QUEUE - ${_page.index}`);
  queue.push(
    async () => {
      console.log(`RUN - ${_page.index}`);
      return executeScript({
        ...options,
        page_index: _page.index,
      }).then((page_results) => {
        console.log(`RETURNED - ${_page.index}`)
        results.push(...page_results)
      });
    }
  );
}
queue.start()
await new Promise((resolve, reject) => {
  const resolveIfEmpty = () => {
    if (queue.isEmpty()) {
      resolve();
    }
    else {
      setTimeout(resolveIfEmpty, 100)
    }
  }
  resolveIfEmpty();
})
queue.stop();
}

Rate Limiting:

RateLimit(options)

Example
const { RateLimit } = require('@nmg/k8s');

const rate_limiter = new RateLimiter({
  shouldRequeueOnError(error) {
    if (error.message === 'THIS NEEDS EXPONENTIAL BACKOFF') {
      rate_limiter.exponentiallyBackoff()
    }
    return /(Quota|exponential)/ig.test(error.message)
  }
});
rate_limiter.on('request_processed', () => {
  rate_limiter.resetExponentialBackoff(); //always reset
  console.log(`REMAINING REQUESTS - ${rate_limiter.requests.length}`)
});
rate_limiter.queueRequest(
  async () => {
    return Promise.resolve();
  }
);
rate_limiter.startProcessing();
await new Promise((resolve) => {
  rate_limiter.once('queue_empty', resolve));
}

options

limits = [
  // limit to 3 requests for 1000ms
  {
    type: 'RATE_LIMIT',
    requests: 3,
    requests_timespan: 1000, // milliseconds
  },
  // AND limit to 1000 requests for 100000ms
  {
    type: 'RATE_LIMIT',
    requests: 1000,
    requests_timespan: 100000, // milliseconds
  },
  // AND limit to 5 concurrent requests
  {
    type: 'CONCURRENCY',
    requests: 5,
  },
],
requests = [], // intialize with requests
log = false,
shouldRequeueOnError = null, // return true to retry

constants

returns a list of constants used in NMG projects

Example
const { nmg_constants } = require('@nmg/k8s');

//gets the collection name for member_sale_outs
nmg_constants.collections.member_sale_outs;

Tests

The Jest testing framework has been implemented. To run these tests run: npm run test

Contributing

Automated deploy is set up on the pipelines/npm branch

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
20.0.01latest

Version History

VersionDownloads (Last 7 Days)Published
20.0.01
19.2.10
19.2.00
19.1.00
19.0.30
19.0.20
19.0.10
19.0.00
18.3.20
18.3.10
18.3.02
18.2.50
18.2.40
18.2.30
18.1.30
18.1.20
18.1.10
18.1.00
18.0.60
18.0.50
18.0.40
18.0.30
18.0.20
18.0.10
17.7.00
17.6.60
17.6.50
17.6.40
17.6.20
17.6.11
17.6.00
17.5.30
17.5.10
17.5.00
17.0.40
17.0.30
17.0.20
17.0.10
17.0.06
16.0.00
15.1.100
15.1.90
15.1.80
15.1.70
15.1.60
15.1.50
15.1.41
15.1.30
15.1.20
15.1.10
15.1.01
15.0.20
15.0.10
15.0.00
14.0.00
13.2.10
13.2.00
13.1.30
13.1.20
13.1.10
13.1.00
13.0.00
12.3.40
12.3.30
12.3.20
12.3.10
12.3.00
12.1.10
12.1.00
12.0.10
12.0.00
11.0.10
10.0.10
9.0.20
9.0.10
8.0.00
7.1.40
7.1.30
7.0.30
7.0.20
7.0.10
7.0.00
6.0.00
5.0.10
4.0.10
4.0.00
3.0.10
2.0.11
1.0.10
1.0.00

Package Sidebar

Install

npm i @nmg/k8s

Weekly Downloads

9

Version

20.0.0

License

ISC

Unpacked Size

571 kB

Total Files

54

Last publish

Collaborators

  • stevenkaspar
  • nmg-technology-service-account