Fn Projectについて調べて見た。②
前回はInstall Fnまでやりました。
今回はJavaのIntroductionをやります。
まずは Fn CLI の更新
fn run
を実行してもエラーになってしまいます。
そもそも何のコマンドだったか思い出せずGitHubを確認したら、run
コマンドは壊れてるらしいです。
約3ヶ月ぶりなので、まずはFn CLIを更新します。
(2018/10/6時点でver0.5.8)
$ brew upgrade fn
Functionの作成
$ fn init --runtime java --trigger http fntest Creating function at: /fntest Function boilerplate generated. func.yaml created.
実行後、以下の様にディレクトリ、ファイルが作成されます。
fntest ├── func.yaml ├── pom.xml └── src ├── main │ └── java │ └── com │ └── example │ └── fn │ └── HelloFunction.java └── test └── java └── com └── example └── fn └── HelloFunctionTest.java
func.yamlの中身
schema_version: 20180708 name: fntest version: 0.0.1 runtime: java build_image: fnproject/fn-java-fdk-build:jdk9-1.0.72 run_image: fnproject/fn-java-fdk:jdk9-1.0.72 cmd: com.example.fn.HelloFunction::handleRequest format: http-stream triggers: - name: fntest-trigger type: http source: /fntest-trigger
yamlにはFunctionに関するメタデータが含まれています。
Fn Introductionより抜粋
- schema_version–identifies the version of the schema for this function file.
- version–the version of the function.
- runtime–the language used for this function.
- cmd–the cmd property is set to the fully qualified name of the function class and the method that should be invoked when your javafn function is called.
- build_image–the image used to build your function’s image.
- run_image–the image your function runs in.
- format–the function uses JSON as its input/output method (see: Open Function Format).
- triggers–identifies the automatically generated trigger name and source. For example, this function would be executed from the URL http://localhost:8080/t/appname/gofn-trigger. Where appname is the name of the app chosen for your function when it is deployed.
Functionのバージョンや言語のほか、Functionが呼び出された時に実行されるメソッドなどが記載されています。
FunctionをDeploy。しかし、失敗。
$ fn deploy --app myapp --local Deploying fntest to app: myapp Bumped to version 0.0.5 Building image fntest:0.0.5 Updating function fntest using image fntest:0.0.5... Fn: Invalid format on Fn See 'fn <command> --help' for more information. Client version: 0.5.8
なぜかデプロイができません。
ビルドしてみます。
$ fn build Building image fntest:0.0.5 Function fntest:0.0.5 built successfully.
ビルドは成功します。
func.yamlで問題の切り分けをする。
以前(2018/7/2時点)のyamlとの違う点をいじってみました。
format http-stream
の箇所をformat http
に変更しています。
(versionは試行錯誤の跡ですw)
schema_version: 20180708 name: fntest version: 0.0.11 runtime: java build_image: fnproject/fn-java-fdk-build:jdk9-1.0.72 run_image: fnproject/fn-java-fdk:jdk9-1.0.72 cmd: com.example.fn.HelloFunction::handleRequest format: http triggers: - name: fntest-trigger type: http source: /fntest-trigger
デプロイしてみます。
$ fn deploy --app myapp --local Deploying fntest to app: myapp Bumped to version 0.0.11 Building image fntest:0.0.11 Updating function fntest using image fntest:0.0.11... Successfully created function: fntest with fntest:0.0.11 Successfully created trigger: fntest-trigger
成功しました。
format http-stream
に問題があるのか、やり方やインストール時に問題があったのかはわかりません。
チュートリアル通りにfn init
するとやはりformat http-stream
でfunc.yaml
が作成されます。
(client ver0.5.15で再確認済み)
Functionを呼び出す。しかし、失敗。
$ fn invoke myapp fntest Fn: Fn invoke url annotation not present, fnproject.io/fn/invokeEndpoint See 'fn <command> --help' for more information. Client version: 0.5.15
なぜすんなり行かないのか。
7月上旬にチュートリアルを一通りやった時には、こんな困らなかったのですが。
ソース読んでエラー発生箇所は分かりましたが、どうしたらいいのかサッパリです。
もう少し調べます。
備忘
fn invoke
format http-stream