diff --git a/cli/rg-client-test/main.go b/cli/rg-client-test/main.go index 60802d4..bb9bdbe 100644 --- a/cli/rg-client-test/main.go +++ b/cli/rg-client-test/main.go @@ -12,17 +12,22 @@ import ( ) func main() { - cli, err := rgclient.CreateUnified() + cli, err := rgclient.CreateUnifiedWithOptions(&rgclient.Options{TokenFile: "/tmp/k8s_token"}) + // cli, err := rgclient.CreateUnified() if err != nil { log.Fatalf("Failed to create unified RouteGroup client: %v", err) } log.Println("have cli") // example kubernetes.Interface access - ings, err := cli.ExtensionsV1beta1().Ingresses("").List(context.TODO(), metav1.ListOptions{}) - //ings, err := cli.NetworkingV1().Ingress("").List(context.TODO(), metav1.ListOptions{}) + // ings, err := cli.ExtensionsV1beta1().Ingresses("").List(context.TODO(), metav1.ListOptions{}) + ings, err := cli.NetworkingV1().Ingresses("").List(context.TODO(), metav1.ListOptions{}) + if err != nil { + log.Fatalf("Failed to get Ingress list: %v", err) + } + for _, ing := range ings.Items { - log.Printf("ing NAmespace/Name: %s/%s", ing.Namespace, ing.Name) + log.Printf("ing Namespace/Name: %s/%s\n", ing.Namespace, ing.Name) } log.Printf("have ing %d", len(ings.Items)) diff --git a/client.go b/client.go index c3e1f4d..ac1435d 100644 --- a/client.go +++ b/client.go @@ -34,6 +34,10 @@ type Clientset struct { zclient *zclient.Clientset } +type Options struct { + TokenFile string +} + // ZalandoV1 implements ZalandoInterface func (cs *Clientset) ZalandoV1() zalandov1.ZalandoV1Interface { return cs.zclient.ZalandoV1() @@ -60,7 +64,7 @@ func NewClientset(config *rest.Config) (*Clientset, error) { // Create returns the Zalandointerface client func Create() (ZalandoInterface, error) { - config, err := getRestConfig() + config, err := getRestConfigWithOptions(nil) if err != nil { return nil, err } @@ -70,7 +74,22 @@ func Create() (ZalandoInterface, error) { // CreateUnified returns the unified client func CreateUnified() (Interface, error) { - config, err := getRestConfig() + config, err := getRestConfigWithOptions(nil) + if err != nil { + return nil, err + } + + client, err := NewClientset(config) + if err != nil { + return nil, fmt.Errorf("unable to create a unified client: %v", err) + } + + return client, nil +} + +// CreateUnifiedWithOptions returns the unified client that +func CreateUnifiedWithOptions(opts *Options) (Interface, error) { + config, err := getRestConfigWithOptions(opts) if err != nil { return nil, err } @@ -83,12 +102,19 @@ func CreateUnified() (Interface, error) { return client, nil } -func getRestConfig() (*rest.Config, error) { +func getRestConfigWithOptions(opts *Options) (*rest.Config, error) { config, err := rest.InClusterConfig() if err != nil { if errors.Is(err, rest.ErrNotInCluster) { - config = &rest.Config{ - Host: LocalAPIServer, + if opts == nil { + config = &rest.Config{ + Host: LocalAPIServer, + } + } else { + config = &rest.Config{ + Host: LocalAPIServer, + BearerTokenFile: opts.TokenFile, + } } err = nil } else {