英文:
Golang kubernetes go-client cast Deployment to DeploymentList
问题
我正在创建一个程序,从Kubernetes获取所有部署的列表,类型为*v1.DeploymentList
。我已经成功实现了这一点,并且它可以正常工作。然后,我对这个列表进行一些处理,并执行许多操作。现在,我有一个新的要求:需要能够仅获取一个部署,并对其应用相同的逻辑。问题是,当我获取部署时,得到的是*v1.Deployment
,这当然与*v1.DeploymentList
不同,因为它是一个列表。现在,这个DeploymentList不是一个切片,所以我不能只是使用append
,也不知道如何转换/转型。作为一个"实用主义"的解决方案,我正在尝试将该部署转换为DeploymentList,然后将剩下的逻辑应用于它作为一个部署,因为在这一点上改变其他一切将意味着很大的负担。
我有以下代码:
func listK8sDeployments(the_clientset *kubernetes.Clientset, mirrorDeploy *string) *v1.DeploymentList {
if mirrorDeploy != nil {
tmp_deployments, err := the_clientset.AppsV1().Deployments(apiv1.NamespaceDefault).Get(context.TODO(), *mirrorDeploy, metav1.GetOptions{})
if err != nil {
panic(err.Error())
}
// 在这里需要将*v1.Deployment转换为*v1.DeploymentList,即一个包含一个部署的列表,以便根据我的现有逻辑返回它。如果我能做到这一点,我就不需要改变程序中的其他任何东西。
// 返回只包含一个部署的部署列表并结束。
}
deployments_list, err := the_clientset.AppsV1().Deployments(apiv1.NamespaceDefault).List(context.TODO(), metav1.ListOptions{})
if err != nil {
panic(err.Error())
}
return deployments_list
}
它返回一个*v1.Deployment
,但是我需要将这个数据作为一个列表,即使它是*v1.DeploymentList
。我尝试过使用append
,但是*v1.DeploymentList
不是一个切片,所以我不能这样做。有什么办法可以实现这个目标,或者我应该改变做事情的方式吗?请解释一下。顺便说一句:我对Go和与k8s相关的编程都很新。
英文:
I am creating a program that gets a list of all deployments from Kubernetes as a *v1.DeploymentList
. I managed to do that and it works. Then I do some processing of this list and execute many actions afterwards. Now, I have a new requirement; need to also be able to pull just ONE deployment and apply the same logic to it. The problem is when I use get the deployment what I get is *v1.Deployment
which of course is different from *v1.DeploymentList
as this is a list. Now, this DeploymentList is not a slice, so I can NOT just use append
and do not know how to convert/cast. As a "pragmatic" solution, what I am trying to do it to just convert that Deployment into DeploymentList and then apply the rest of my logic as just a deployment as changing everything else would imply a lot of burden at this point.
I have the following code:
func listK8sDeployments(the_clientset *kubernetes.Clientset, mirrorDeploy *string) *v1.DeploymentList {
if mirrorDeploy != nil {
tmp_deployments, err := the_clientset.AppsV1().Deployments(apiv1.NamespaceDefault).Get(context.TODO(), *mirrorDeploy, metav1.GetOptions{})
if err != nil {
panic(err.Error())
}
// Here would need to convert the *v1.Deployment into *v1.DeploymentList a list to retun it according to my EXISTING logic. If I can do this, I do not need to change anything else on the program.
// return the Deployment list with one single deployment inside and finish.
}
deployments_list, err := the_clientset.AppsV1().Deployments(apiv1.NamespaceDefault).List(context.TODO(), metav1.ListOptions{})
if err != nil {
panic(err.Error())
}
return deployments_list
}
It returns a *v1.Deployment
, but I need this data as a list even if it *v1.DeploymentList
I have tried to append, but the *v1.DeploymentList
is not a slice, so I can not do it. Any ideas as to how to achieve this or should I change the way things are done? Please explain. FYI: I am new to Go and to programming k8s related things too.
答案1
得分: 4
当你查看v1.DeploymentList的定义时,你可以看到Deployment的位置:
// DeploymentList is a list of Deployments.
type DeploymentList struct {
metav1.TypeMeta `json:",inline"`
// Standard list metadata.
// +optional
metav1.ListMeta `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`
// Items is the list of Deployments.
Items []Deployment `json:"items" protobuf:"bytes,2,rep,name=items"`
}
然后你可以使用你的值轻松创建一个新的实例:
func listK8sDeployments(the_clientset *kubernetes.Clientset, mirrorDeploy *string) *v1.DeploymentList {
if *mirrorDeploy != "" {
tmp_deployments, err := the_clientset.AppsV1().Deployments(apiv1.NamespaceDefault).Get(context.TODO(), *mirrorDeploy, metav1.GetOptions{})
if err != nil {
panic(err.Error())
}
// create a new list with your deployment and return it
deployments_list := v1.DeploymentList{Items: []v1.Deployment{*tmp_deployments}}
return &deployments_list
}
deployments_list, err := the_clientset.AppsV1().Deployments(apiv1.NamespaceDefault).List(context.TODO(), metav1.ListOptions{})
if err != nil {
panic(err.Error())
}
return deployments_list
}
英文:
when you look at the definition of v1.DeploymentList you can see where the Deployment is located:
// DeploymentList is a list of Deployments.
type DeploymentList struct {
metav1.TypeMeta `json:",inline"`
// Standard list metadata.
// +optional
metav1.ListMeta `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`
// Items is the list of Deployments.
Items []Deployment `json:"items" protobuf:"bytes,2,rep,name=items"`
}
then you can easily create a new instance of it with your value:
func listK8sDeployments(the_clientset *kubernetes.Clientset, mirrorDeploy *string) *v1.DeploymentList {
if *mirrorDeploy != "" {
tmp_deployments, err := the_clientset.AppsV1().Deployments(apiv1.NamespaceDefault).Get(context.TODO(), *mirrorDeploy, metav1.GetOptions{})
if err != nil {
panic(err.Error())
}
// create a new list with your deployment and return it
deployments_list := v1.DeploymentList{Items: []v1.Deployment{*tmp_deployments}}
return &deployments_list
}
deployments_list, err := the_clientset.AppsV1().Deployments(apiv1.NamespaceDefault).List(context.TODO(), metav1.ListOptions{})
if err != nil {
panic(err.Error())
}
return deployments_list
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论