I have some code in golang which connects to a Kubernetes cluster and prints a list of pods ,and a list of configmaps.
Below are two similar functions:
func (k *kubeEntity) getpods(ns string, cs kubernetes.Clientset) {
pods, err := cs.CoreV1().Pods(ns).List(metav1.ListOptions{})
if err != nil {
panic(err.Error())
}
for i, pod := range pods.Items {
fmt.Println(i, ":", pod.Name, "|", pod.Status.Phase)
}
}
func (k *kubeEntity) getcm(ns string, cs kubernetes.Clientset) {
cms, err := cs.CoreV1().ConfigMaps(ns).List(metav1.ListOptions{})
if err != nil {
panic(err.Error())
}
for i, cm := range cms.Items {
fmt.Println(i, ":", cm.Name)
}
}
How can I write one global/generic function that will do the task for both cases?
Thanks in advance
There are no generics in go http://golang.org/doc/faq#generics for the moment.
It supposed to arrive in go 2.0, you can see feedbacks of generics in the draft version here : https://github.com/golang/go/wiki/Go2GenericsFeedback