/wasm-k8s-host-proxy

Kubernetes client Proxy Host Functions for WebAssembly plugin

Primary LanguageGo

WebAssembly Plugin Kubernetes client Proxy Host Functions Library

This is a host functions library, which is build in the client-go Kubernetes style fashion, that allows to embed interaction with the Kubernetes API from the WebAssembly plugins generated by the go-plugin.

Embedding host functions into the application

The host client proxy implementation has the following set of predefined configurations functions for instantiation connection to the Kubernetes cluster, with following access of the cluster configurations:

  1. WithDefaultKubeConfig - when config file is located on the path $HOME\.kube\config.
  2. WithKubeConfig - when config file location is provided with parameter.
  3. WithInClusterConfig - with the config embedded into the Pod service account.
  4. func() (dynamic.Interface, error) - any function that returns a dynamic.Interface client instance.

To embed the library into the host application, it's necessary to add a call of the Instantiate(...) function in the wazero.Runtime configuration, as described below

import "github.com/dmvolod/wasm-k8s-host-proxy/impl/host"

...
p, err := getter.NewGetterPlugin(ctx, getter.WazeroRuntime(func(ctx context.Context) (wazero.Runtime, error) {
	r, err := getter.DefaultWazeroRuntime()(ctx)
	if err != nil {
		return nil, err
	}

	// Register host functions from the Kubernetes client proxy library.
	return r, host.Instantiate(ctx, r, func() (dynamic.Interface, error) {
		return fakeClient, nil
	})
}))

Using the Kubernetes client API in the WebAssembly plugin

It's very easy to use Kubernetes client proxy API, the same as standard client-go dynamic client API. The only difference is that instead of standard k8s.io/api objects binding, generated k8s-objects from the Kubewarden project are used.

import corev1 "github.com/kubewarden/k8s-objects/api/core/v1"
...
cm := &corev1.ConfigMap{}

err := kubeClientProxy.Namespace("default").Get(ctx, "demo", kubernetes.GetOptions{}, cm)
if err != nil {
	return nil, err
}

Seem more details in the examples

Note: This component is under active development and may have a large number of incompatible changes and additions.

Library Development

Note: To regenerate code stubs and examples, at least v0.7.0 go-plugin version is required.

Generating Kubernetes proxy library stubs

protoc --go-plugin_out=. --go-plugin_opt=paths=source_relative internal/host/kubernetes/kubernetes.proto