Skip to content

Instantly share code, notes, and snippets.

@jonathanvila
Last active October 10, 2022 17:03
Show Gist options
  • Save jonathanvila/acbbae4ba6a00768b1e7c21db8454d91 to your computer and use it in GitHub Desktop.
Save jonathanvila/acbbae4ba6a00768b1e7c21db8454d91 to your computer and use it in GitHub Desktop.
Create an Istio WasmPlugin using Go lang
/// Copyright (c) Tetrate, Inc 2022 All Rights Reserved.
package main
import (
"context"
"os"
"path"
"github.com/gogo/protobuf/types"
"istio.io/api/extensions/v1alpha1"
apiv1alpha1 "istio.io/client-go/pkg/apis/extensions/v1alpha1"
clientv1alpha1 "istio.io/client-go/pkg/clientset/versioned/typed/extensions/v1alpha1"
istiocollections "istio.io/istio/pkg/config/schema/collections"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/clientcmd"
)
func main() {
// create a wasmplugin
wasmPlugin := &apiv1alpha1.WasmPlugin{
TypeMeta: v1.TypeMeta{
Kind: istiocollections.IstioExtensionsV1Alpha1Wasmplugins.Resource().GroupVersionKind().Kind,
},
ObjectMeta: v1.ObjectMeta{
Name: "pluginname",
Namespace: "default",
Labels: nil,
Annotations: nil,
},
Spec: v1alpha1.WasmPlugin{
Selector: nil,
PluginName: "pluginname",
Priority: &types.Int64Value{Value: int64(999999)},
PluginConfig: nil,
Url: "wasmedge/example-wasi",
Phase: v1alpha1.PluginPhase_AUTHN,
ImagePullPolicy: v1alpha1.PullPolicy_IfNotPresent,
},
}
// Get the client
home, err := os.UserHomeDir()
if err != nil {
panic(err)
}
config, err := clientcmd.BuildConfigFromFlags("", path.Join(home, ".kube/config"))
if err != nil {
panic(err.Error())
}
client, err := clientv1alpha1.NewForConfig(config)
if err != nil {
panic(err.Error())
}
// Debug: Marshalling to JSON
value, err := json.Marshal(wasmPlugin)
if err != nil {
panic(err)
}
fmt.Printf("JSON: %s \n", string(value))
// Apply the creation of the new plugin
_, err = client.WasmPlugins("default").Create(context.TODO(), wasmPlugin, v1.CreateOptions{})
// expect no error
if err != nil {
panic(err)
}
// We will get here this error :
// panic: WasmPlugin.extensions.istio.io "pluginname" is invalid:
// spec.priority: Invalid value: "string": spec.priority in body must be of type integer: "string"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment