k8s go client: how to pass a v1.Deployment type to a function

10/19/2019

I'm new to go and playing with k8s go-client. I'd like to pass items from deploymentsClient.List(metav1.ListOptions{}) to a funcion. fmt.Printf("%T\n", deploy) says it's type v1.Deployment. So I write a function that takes (deploy *v1.Deployment) and pass it &deploy where deploy is an item in the deploymentsClient.List. This errors with cmd/list.go:136:38: undefined: v1 however. What am I doing wrong?

Here are my imports

import (
    //  "encoding/json"
    "flag"
    "fmt"
    //yaml "github.com/ghodss/yaml"
    "github.com/spf13/cobra"
    // "k8s.io/apimachinery/pkg/api/errors"
    metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    "k8s.io/client-go/kubernetes"
    "k8s.io/client-go/tools/clientcmd"
    "os"
    "path/filepath"
)

Then I get the list of deployments:

    deploymentsClient := clientset.AppsV1().Deployments(ns)
    deployments, err := deploymentsClient.List(metav1.ListOptions{})
    if err != nil {
        panic(err.Error())
    }
    for _, deploy := range deployments.Items {
        fmt.Println(deploy.ObjectMeta.SelfLink)
        //      printDeploymentSpecJson(deploy)
        //      printDeploymentSpecYaml(deploy)

    }
-- user1855481
go
kubernetes

1 Answer

10/20/2019

You need to import "k8s.io/api/apps/v1", Deployment is defined in the package. See https://godoc.org/k8s.io/api/apps/v1.

-- Dagang
Source: StackOverflow