在Kubernetes中获取入口元数据

huangapple go评论88阅读模式
英文:

Fetch ingress metadata in kubernetes

问题

我有以下的Go代码,用于打印我的Kubernetes部署元数据:

package test

import (
	"context"
	"fmt"
	"os"
	"testing"

	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/client-go/kubernetes"
	"k8s.io/client-go/tools/clientcmd"
)

func TestEksStandaloneModule(t *testing.T) {
	namespace := fmt.Sprintf("%v-%v", "project", "dit")
	kubeconfig := os.Getenv("HOME") + "/KUBECONFIG"

	clientCfg, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
	if err != nil {
		t.Error(err)
	}

	clientset, err := kubernetes.NewForConfig(clientCfg)

	appDeployment, err := clientset.AppsV1().Deployments(namespace).Get(context.TODO(), "deploymentname", v1.GetOptions{})
	fmt.Println(appDeployment)
}

有人可以帮我编写一个打印Ingress元数据的函数吗?我对Go不太熟悉。

英文:

I have below go code that print my kubernetes deployment metadata,

package test

import (
	"context"
	"fmt"
	"os"
	"testing"

	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/client-go/kubernetes"
	"k8s.io/client-go/tools/clientcmd"
)

func TestEksStandaloneModule(t *testing.T) {
	namespace := fmt.Sprintf("%v-%v", "project", "dit")
	kubeconfig := os.Getenv("HOME") + "/KUBECONFIG"

	clientCfg, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
	if err != nil {
		t.Error(err)
	}

	clientset, err := kubernetes.NewForConfig(clientCfg)

	appDeployment, err := clientset.AppsV1().Deployments(namespace).Get(context.TODO(), "deploymentname", v1.GetOptions{})
	fmt.Println(appDeployment)
}

Can any one please help me get a function that prints ingress metadata?
I am new to go.

答案1

得分: 1

我能够通过以下方式使其工作:

appIngress, err := clientset.NetworkingV1().Ingresses(namespace).Get(context.TODO(), "helloworldspringbootssl-ap145385", v1.GetOptions{})
fmt.Println(appIngress)

请注意,这是一个Go语言的代码片段。

英文:

I was able to make it work by

appIngress, err := clientset.NetworkingV1().Ingresses(namespace).Get(context.TODO(), "helloworldspringbootssl-ap145385", v1.GetOptions{})
fmt.Println(appIngress)

huangapple
  • 本文由 发表于 2022年3月24日 17:19:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/71599802.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定