This documentation is for a release that is no longer maintained
See documentation for the latest supported version 3 or the latest supported version 4.6.12. 関数開発リファレンスガイド
OpenShift Serverless Functions は、基本的な関数を作成するために使用できるテンプレートを提供します。テンプレートは、関数プロジェクトのボイラープレートを開始し、kn func
ツールで使用できるように準備します。各関数テンプレートは特定のランタイム用に調整されており、その規則に従います。テンプレートを使用すると、関数プロジェクトを自動的に開始できます。
次のランタイムのテンプレートが利用可能です。
6.12.1. Node.js コンテキストオブジェクトのリファレンス リンクのコピーリンクがクリップボードにコピーされました!
context
オブジェクトには、関数開発者が利用可能なプロパティーが複数あります。これらのプロパティーにアクセスすると、HTTP 要求に関する情報が提供され、出力がクラスターログに書き込まれます。
6.12.1.1. log リンクのコピーリンクがクリップボードにコピーされました!
出力をクラスターロギングに書き込むために使用可能なロギングオブジェクトを提供します。ログは Pino logging API に準拠します。
ログの例
function handle(context) { context.log.info(“Processing customer”); }
function handle(context) {
context.log.info(“Processing customer”);
}
kn func invoke
コマンドを使用して、この関数にアクセスできます。
コマンドの例
kn func invoke --target 'http://example.function.com'
$ kn func invoke --target 'http://example.function.com'
出力例
{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"Processing customer"}
{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"Processing customer"}
ログレベルは、fatal
、error
、warn
、info
、debug
、trace
、または silent
のいずれかに設定できます。これを実行するには、config
コマンドを使用してこれらの値のいずれかを環境変数 FUNC_LOG_LEVEL
に割り当てて、logLevel
の値を変更します。
6.12.1.2. query リンクのコピーリンクがクリップボードにコピーされました!
要求のクエリー文字列 (ある場合) をキーと値のペアとして返します。これらの属性はコンテキストオブジェクト自体にも表示されます。
サンプルクエリー
kn func invoke
コマンドを使用して、この関数にアクセスできます。
コマンドの例
kn func invoke --target 'http://example.com?name=tiger'
$ kn func invoke --target 'http://example.com?name=tiger'
出力例
{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"tiger"}
{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"tiger"}
6.12.1.3. ボディー リンクのコピーリンクがクリップボードにコピーされました!
要求ボディー (ある場合) を返します。要求ボディーに JSON コードが含まれる場合には、属性は直接利用できるように解析されます。
ボディーの例
function handle(context) { // log the incoming request body's 'hello' parameter context.log.info(context.body.hello); }
function handle(context) {
// log the incoming request body's 'hello' parameter
context.log.info(context.body.hello);
}
curl
コマンドを使用してこの関数を呼び出すことができます。
コマンドの例
kn func invoke -d '{"Hello": "world"}'
$ kn func invoke -d '{"Hello": "world"}'
出力例
{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"world"}
{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"world"}
6.12.1.4. ヘッダー リンクのコピーリンクがクリップボードにコピーされました!
HTTP 要求ヘッダーをオブジェクトとして返します。
ヘッダーの例
function handle(context) { context.log.info(context.headers["custom-header"]); }
function handle(context) {
context.log.info(context.headers["custom-header"]);
}
kn func invoke
コマンドを使用して、この関数にアクセスできます。
コマンドの例
kn func invoke --target 'http://example.function.com'
$ kn func invoke --target 'http://example.function.com'
出力例
{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"some-value"}
{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"some-value"}
6.12.1.5. HTTP 要求 リンクのコピーリンクがクリップボードにコピーされました!
- 方法
- HTTP 要求メソッドを文字列として返します。
- httpVersion
- HTTP バージョンを文字列として返します。
- httpVersionMajor
- HTTP メジャーバージョン番号を文字列として返します。
- httpVersionMinor
- HTTP マイナーバージョン番号を文字列として返します。
6.12.2. Typescript コンテキストオブジェクトの参照 リンクのコピーリンクがクリップボードにコピーされました!
context
オブジェクトには、関数開発者が利用可能なプロパティーが複数あります。これらのプロパティーにアクセスすると、着信 HTTP 要求に関する情報が提供され、出力がクラスターログに書き込まれます。
6.12.2.1. log リンクのコピーリンクがクリップボードにコピーされました!
出力をクラスターロギングに書き込むために使用可能なロギングオブジェクトを提供します。ログは Pino logging API に準拠します。
ログの例
kn func invoke
コマンドを使用して、この関数にアクセスできます。
コマンドの例
kn func invoke --target 'http://example.function.com'
$ kn func invoke --target 'http://example.function.com'
出力例
{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"Processing customer"}
{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"Processing customer"}
ログレベルは、fatal
、error
、warn
、info
、debug
、trace
、または silent
のいずれかに設定できます。これを実行するには、config
コマンドを使用してこれらの値のいずれかを環境変数 FUNC_LOG_LEVEL
に割り当てて、logLevel
の値を変更します。
6.12.2.2. query リンクのコピーリンクがクリップボードにコピーされました!
要求のクエリー文字列 (ある場合) をキーと値のペアとして返します。これらの属性はコンテキストオブジェクト自体にも表示されます。
サンプルクエリー
kn func invoke
コマンドを使用して、この関数にアクセスできます。
コマンドの例
kn func invoke --target 'http://example.function.com' --data '{"name": "tiger"}'
$ kn func invoke --target 'http://example.function.com' --data '{"name": "tiger"}'
出力例
{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"tiger"} {"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"tiger"}
{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"tiger"}
{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"tiger"}
6.12.2.3. ボディー リンクのコピーリンクがクリップボードにコピーされました!
要求ボディー (ある場合) を返します。要求ボディーに JSON コードが含まれる場合には、属性は直接利用できるように解析されます。
ボディーの例
kn func invoke
コマンドを使用して、この関数にアクセスできます。
コマンドの例
kn func invoke --target 'http://example.function.com' --data '{"hello": "world"}'
$ kn func invoke --target 'http://example.function.com' --data '{"hello": "world"}'
出力例
{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"world"}
{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"world"}
6.12.2.4. ヘッダー リンクのコピーリンクがクリップボードにコピーされました!
HTTP 要求ヘッダーをオブジェクトとして返します。
ヘッダーの例
curl
コマンドを使用してこの関数を呼び出すことができます。
コマンドの例
curl -H'x-custom-header: some-value’' http://example.function.com
$ curl -H'x-custom-header: some-value’' http://example.function.com
出力例
{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"some-value"}
{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"some-value"}
6.12.2.5. HTTP 要求 リンクのコピーリンクがクリップボードにコピーされました!
- 方法
- HTTP 要求メソッドを文字列として返します。
- httpVersion
- HTTP バージョンを文字列として返します。
- httpVersionMajor
- HTTP メジャーバージョン番号を文字列として返します。
- httpVersionMinor
- HTTP マイナーバージョン番号を文字列として返します。