[英]Create/Get a custom kubernetes resource
I want to create a custom kubernetes resource with go.我想用 go 创建一个自定义 kubernetes 资源。 The application is deployed in the kubernetes cluster.
该应用程序部署在 kubernetes 集群中。 I want to create eg the followng resource:
我想创建例如以下资源:
apiVersion: configuration.konghq.com/v1
kind: KongPlugin
metadata:
name: add-response-header
config:
add:
headers:
- "demo: injected-by-kong"
plugin: response-transformer
So far I always created the 'standard' resources eg a secret with the following code:到目前为止,我总是使用以下代码创建“标准”资源,例如秘密:
CreateSecret(name string, data map[string]string) error {
confs, err := rest.InClusterConfig()
if err != nil {
panic(err)
}
clientset, err = kubernetes.NewForConfig(confs)
i := clientset.CoreV1()
if _, err := i.Secrets(namespace).Create(&v1.Secret{
TypeMeta: metav1.TypeMeta{
Kind: "Secret",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
StringData: data,
Type: "Opaque",
}); err != nil {
return err
}
}
In addition I tried to get a resource with the following code:此外,我尝试使用以下代码获取资源:
b, err := clientset.RESTClient().Get().Namespace(namespace).Resource("KongPlugin").DoRaw()
I get the following err:我得到以下错误:
the server could not find the requested resource (get KongPlugin)
If I make a request at the command line k get KongPlugin
I can see all the resources.如果我在命令行发出请求
k get KongPlugin
我可以看到所有资源。
NAME PLUGIN-TYPE AGE
add-proxy-headers request-transformer 3h34m
So how can view the custom resoources?那么如何查看自定义资源呢?
For RESTClient
对于
RESTClient
Get:
得到:
You have to fully specify path to you custom resource.您必须完全指定自定义资源的路径。 Use
fluent
interface使用
fluent
界面
data, err := clientset.RESTClient().
Get().
AbsPath("/apis/<api>/<version>").
Namespace("<namespace>").
Resource("kongplugins").
Name("kongplugin-sample").
DoRaw(context.TODO())
or specify manually或手动指定
data, err := clientset.RESTClient().
Get().
AbsPath("/apis/<api>/<version>/namespaces/<namespace>/kongplugins/kongplugin-sample").
DoRaw(context.TODO())
You can find AbsPath
in selfLink
of custom resource.您可以在自定义资源的
selfLink
中找到AbsPath
。
Create:
创造:
For example, you can post
marshaled data use AbsPath
例如,您可以使用
AbsPath
post
封送数据
kongPlugin := &KongPlugin{
TypeMeta: metav1.TypeMeta{
APIVersion: "<api>/<version>",
Kind: "KongPlugin",
},
ObjectMeta: metav1.ObjectMeta{
Name: "kongplugin-sample",
Namespace: "<namespace>",
},
...}}
body, err := json.Marshal(kongPlugin)
data, err := clientset.RESTClient().
Post().
AbsPath("/apis/<api>/<version>/namespaces/<namespace>/kongplugins").
Body(body).
DoRaw(context.TODO())
because arg
of the method Body(obj interface{})
is an empty interface, you can use different types of arguments according to documentation: k8s.io/client-go/rest - func (*Request) Body因为方法
Body(obj interface{})
的arg
是一个空接口,您可以根据文档使用不同类型的 arguments: k8s.io/client-go/rest - func (*Request) Body
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.