Search code examples
gokuberneteskubernetes-go-client

Golang kubernetes go-client cast Deployment to DeploymentList


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.


Solution

  • 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
    }