github.com/wmuizelaar/kpt@v0.0.0-20221018115725-bd564717b2ed/site/sdk/ts-guide.md (about)

     1  # Typescript SDK Developer Guide
     2  
     3  This guide will walk you through developing a kpt function using the Typescript SDK.
     4  
     5  ## Setup
     6  
     7  ### System Requirements
     8  
     9  Currently supported platforms: amd64 Linux/Mac
    10  
    11  ### Setting Up Your Local Environment
    12  
    13  Currently supported platforms: amd64 Linux/Mac
    14  
    15  - Install [kpt][download-kpt] and its dependencies
    16  - Install [node][download-node]
    17    - The SDK requires `npm` version 6 or higher.
    18    - If installing node from binaries (i.e. without a package manager), follow
    19      these [installation instructions][install-node].
    20  
    21  ### Your Kubernetes Cluster
    22  
    23  For the type generation functionality to work, you need a Kubernetes cluster
    24  with the [CRD OpenAPI Publishing][crd-openapi] feature which is GA with
    25  Kubernetes 1.16.
    26  
    27  Alternatively, you can use an existing NPM package with pre-generated types
    28  such as the `hello-world` package discussed in the [Quickstart] and skip to
    29  [implementing the function](#implement-the-function).
    30  
    31  ### Working with CRDs
    32  
    33  The SDK uses the k8s server to generate the Typescript classes. If your
    34  function uses a Custom Resource Definition, make sure you apply it to the
    35  cluster used for type generation:
    36  
    37  ```shell
    38  $ kubectl apply -f /path/to/my/crd.yaml
    39  ```
    40  
    41  ## Create the NPM Package
    42  
    43  To initialize a new NPM package, first create a package directory:
    44  
    45  ```shell
    46  $ mkdir my-package
    47  $ cd my-package
    48  ```
    49  
    50  > **Note:** All subsequent commands are run from the `my-package/` directory.
    51  
    52  Run the interactive initializer:
    53  
    54  ```shell
    55  $ npm init kpt-functions
    56  ```
    57  
    58  Follow the instructions and respond to all prompts.
    59  
    60  This process will create the following:
    61  
    62  1. `package.json`: The `kpt-functions` framework library is the only item
    63     declared in `dependencies`. Everything required to compile and test your
    64     config function is declared as `devDependencies`, including the
    65     `create-kpt-functions` CLI discussed later in the `README`.
    66  1. `src/`: Directory containing the source files for all your functions, e.g.:
    67     - `my_func.ts`: Implement your function's interface here.
    68     - `my_func_test.ts`: Unit tests for your function.
    69     - `my_func_run.ts`: The entry point from which your function is run.
    70  1. `src/gen/`: Contains Kubernetes core and CRD types generated from the
    71     OpenAPI spec published by the cluster you selected.
    72  1. `build/`: Contains Dockerfile for each function, e.g.:
    73     - `my_func.Dockerfile`
    74  
    75  Next, install all package dependencies:
    76  
    77  ```shell
    78  $ npm install
    79  ```
    80  
    81  In addition to installation, `install` compiles your function into the `dist/`
    82  directory.
    83  
    84  You can run your function directly:
    85  
    86  ```shell
    87  $ node dist/my_func_run.js --help
    88  ```
    89  
    90  Currently, it simply passes through the input configuration data. Let's remedy
    91  this.
    92  
    93  ## Implement the Function
    94  
    95  You can now start implementing the function using your favorite IDE, e.g.
    96  [VSCode]:
    97  
    98  ```shell
    99  $ code .
   100  ```
   101  
   102  In `src/my_func.ts`, implement the `KptFunc` interface from the [TS SDK API].
   103  
   104  Take a look at these [demo functions] to better understand how
   105  to use the typescript library.
   106  
   107  Once you've written some code, build the package with:
   108  
   109  ```shell
   110  $ npm run build
   111  ```
   112  
   113  Alternatively, run the following in a separate terminal. It will continuously
   114  build your function as you make changes:
   115  
   116  ```shell
   117  $ npm run watch
   118  ```
   119  
   120  To run the tests, use:
   121  
   122  ```shell
   123  $ npm test
   124  ```
   125  
   126  ## Debug and Test the Function
   127  
   128  You may want to run a function developed with one of the config function SDKs
   129  using the exec runtime in order to avoid the overhead associated with running
   130  a container. To run your function in the exec runtime, you will first need to
   131  package your function as an executable.
   132  
   133  The below example shows how to run a typescript function using the kpt exec
   134  runtime.
   135  
   136  ### Prerequisites
   137  
   138  - Install the pkg CLI.
   139  
   140    ```shell
   141    $ npm install -g pkg
   142    ```
   143  
   144  - Install your kpt-functions package module to create your function's
   145    distributable file.
   146  
   147    ```shell
   148    $ npm i
   149    ```
   150  
   151  ### Steps
   152  
   153  1. Pass the path to the appropriate executable for your OS when running kpt
   154     using the exec runtime.
   155  
   156     ```shell
   157     $ kpt fn eval DIR --exec "node dist/my_func_run.js"
   158     ```
   159  
   160  ## Build and push container images
   161  
   162  With your working function in-hand, it's time to package your function into an
   163  executable container image.
   164  
   165  To build the docker image:
   166  
   167  ```shell
   168  $ npm run kpt:docker-build
   169  ```
   170  
   171  You can now run the function container, e.g.:
   172  
   173  ```shell
   174  $ kpt fn eval DIR --image gcr.io/kpt-functions-demo/my-func:dev
   175  ```
   176  
   177  To push the image to your container registry of choice:
   178  
   179  ```shell
   180  $ npm run kpt:docker-push
   181  ```
   182  
   183  You'll need proper authentication/authorization to push to your registry.
   184  
   185  `kpt:docker-push` pushes to the registry specified in the
   186  `kpt.docker_repo_base` field in `package.json`. You can manually edit this
   187  field at any time.
   188  
   189  The default value for the container image tag is `dev`. This can be overridden
   190  using`--tag` flag:
   191  
   192  ```shell
   193  $ npm run kpt:docker-build -- --tag=latest
   194  $ npm run kpt:docker-push -- --tag=latest
   195  ```
   196  
   197  ## Use the SDK CLI
   198  
   199  The `create-kpt-functions` package (installed as `devDependencies`), provides
   200  a CLI for managing the NPM package you created above. The CLI sub-commands can
   201  be invoked via `npm run`. For example, to add a new function to the package:
   202  
   203  ```shell
   204  $ npm run kpt:function-create
   205  ```
   206  
   207  These sub-commands are available:
   208  
   209  ```
   210  kpt:docker-create       Generate Dockerfiles for all functions. Overwrite
   211                          files if they exist.
   212  kpt:docker-build        Build container images for all functions.
   213  kpt:docker-push         Push container images to the registry for all
   214                          functions.
   215  kpt:function-create     Generate stubs for a new function. Overwrites files
   216                          if they exist.
   217  kpt:type-create         Generate classes for core and CRD types. Overwrite
   218                          files if they exist.
   219  ```
   220  
   221  Flags are passed to the CLI after the `--` separator. For example, to pass a
   222  tag when building a container image:
   223  
   224  ```shell
   225  $ npm run kpt:docker-build -- --tag=latest
   226  ```
   227  
   228  [download-kpt]: /book/01-getting-started/01-system-requirements
   229  [download-node]: https://nodejs.org/en/download/
   230  [install-node]: https://github.com/nodejs/help/wiki/Installation/
   231  [install-node]: https://github.com/nodejs/help/wiki/Installation/
   232  [install-docker]: https://docs.docker.com/engine/installation/
   233  [crd-openapi]: https://github.com/kubernetes/kubernetes/blob/master/CHANGELOG/CHANGELOG-1.15.md#customresourcedefinition-openapi-publishing
   234  [quickstart]: ../quickstart/
   235  [vscode]: https://code.visualstudio.com/
   236  [ts sdk api]: https://googlecontainertools.github.io/kpt-functions-sdk/api/
   237  [demo functions]: https://github.com/GoogleContainerTools/kpt-functions-sdk/tree/master/ts/demo-functions/src/