Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update client to use BearerTokenFile when needed #15

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions cli/rg-client-test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,22 @@ import (
)

func main() {
cli, err := rgclient.CreateUnified()
MustafaSaber marked this conversation as resolved.
Show resolved Hide resolved
cli, err := rgclient.CreateUnifiedWithOptions(&rgclient.Options{TokenFile: "/tmp/k8s_token"})
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you want to specify a const for this and I don't see how a fixed file can help in a test as you need to write the valid token into the file somehow

// cli, err := rgclient.CreateUnified()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drop this comment

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{})
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drop this comment

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))

Expand Down
36 changes: 31 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
}
Expand All @@ -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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to create client via CreateUnified() and then modify its rest.Config?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively we can also consider using functional options, see https://golang.cafe/blog/golang-functional-options-pattern.html

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, it returns the clientset at this level we only can call resources.

config, err := getRestConfigWithOptions(opts)
if err != nil {
return nil, err
}
Expand All @@ -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 {
Expand Down