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

Generate clientset for vm crds #481

Closed
Haleygo opened this issue May 19, 2022 · 6 comments
Closed

Generate clientset for vm crds #481

Haleygo opened this issue May 19, 2022 · 6 comments
Labels
question Further information is requested

Comments

@Haleygo
Copy link
Contributor

Haleygo commented May 19, 2022

Hi Team,
I'm trying to operate some crud in vm crd like vmrules, but can't find any client available now in project.
So I'm wondering why you guys not use tool like code-generator to generate clientset for them.
Thanks.

@Haleygo Haleygo changed the title Add client for vm crds Generate clientset for vm crds May 19, 2022
@f41gh7
Copy link
Collaborator

f41gh7 commented May 19, 2022

Hello, you can use kubernetes controller runtime client

Example in pseudo-code, it may throw errors, it didn't test it. But it shows main concept.

go.mod

module my-module
go 1.18

require (
k8s.io/apimachinery v0.23.6
sigs.k8s.io/controller-runtime v0.11.2
github.com/VictoriaMetrics/operator/api v0.0.0-20220413082921-eef8e2eece26
k8s.io/client-go v12.0.0+incompatible
k8s.io/utils v0.0.0-20220210201930-3a6ce19ff2f9
)

replace (
	k8s.io/client-go => k8s.io/client-go v0.23.6
)

usage

package main

import (
  "sigs.k8s.io/controller-runtime/pkg/client"
   operator "github.com/VictoriaMetrics/operator/api/v1beta1"
  "k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/clientcmd"
)

func setupRuntime() *runtime.Scheme {
	s := scheme.Scheme
	s.AddKnownTypes(operator.GroupVersion,
		&operator.VMRule{},
		&operator.VMRuleList{},
	)

	if err := operator.AddToScheme(s); err != nil {
		logger.Fatalf("BUG, cannot add scheme to client")
	}
	return s
}
func main(){
  
        lr := clientcmd.NewDefaultClientConfigLoadingRules()
	cfg := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(lr, &clientcmd.ConfigOverrides{})
	restCfg, err := cfg.ClientConfig()
	if err != nil {
		return nil, fmt.Errorf("cannot read client cfg from kubeconfig: %w", err)
	}

	c, err := client.NewWithWatch(restCfg, client.Options{Scheme: setupRuntime()})
	if err != nil {
		return nil, fmt.Errorf("cannot build client: %w", err)
	}
      var rules operator.VMRuleList
     if err := c.List(context.Background(), &rules); err != nil {
           logger.Fatalf("cannot list rules: %s",err)
     }
    for _, rule := range rules.Items{}
}

@f41gh7 f41gh7 added the question Further information is requested label May 19, 2022
@Haleygo
Copy link
Contributor Author

Haleygo commented May 19, 2022

Hello, you can use kubernetes controller runtime client

Example in pseudo-code, it may throw errors, it didn't test it. But it shows main concept.

go.mod

module my-module
go 1.18

require (
k8s.io/apimachinery v0.23.6
sigs.k8s.io/controller-runtime v0.11.2
github.com/VictoriaMetrics/operator/api v0.0.0-20220413082921-eef8e2eece26
k8s.io/client-go v12.0.0+incompatible
k8s.io/utils v0.0.0-20220210201930-3a6ce19ff2f9
)

replace (
	k8s.io/client-go => k8s.io/client-go v0.23.6
)

usage

package main

import (
  "sigs.k8s.io/controller-runtime/pkg/client"
   operator "github.com/VictoriaMetrics/operator/api/v1beta1"
  "k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/clientcmd"
)

func setupRuntime() *runtime.Scheme {
	s := scheme.Scheme
	s.AddKnownTypes(operator.GroupVersion,
		&operator.VMRule{},
		&operator.VMRuleList{},
	)

	if err := operator.AddToScheme(s); err != nil {
		logger.Fatalf("BUG, cannot add scheme to client")
	}
	return s
}
func main(){
  
        lr := clientcmd.NewDefaultClientConfigLoadingRules()
	cfg := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(lr, &clientcmd.ConfigOverrides{})
	restCfg, err := cfg.ClientConfig()
	if err != nil {
		return nil, fmt.Errorf("cannot read client cfg from kubeconfig: %w", err)
	}

	c, err := client.NewWithWatch(restCfg, client.Options{Scheme: setupRuntime()})
	if err != nil {
		return nil, fmt.Errorf("cannot build client: %w", err)
	}
      var rules operator.VMRuleList
     if err := c.List(context.Background(), &rules); err != nil {
           logger.Fatalf("cannot list rules: %s",err)
     }
    for _, rule := range rules.Items{}
}

Thank you for the example :)
Actually I'm using dynamicClient to operate them now, and all of these work.
I just open this issue to ask that is there any thought to generate clientset for those crd(like prometheus operator did), it will be much more convenient to use in another project.

@f41gh7
Copy link
Collaborator

f41gh7 commented May 23, 2022

It shouldn't be hard to implement it with https://github.com/kubernetes/code-generator

Will add it to backlog.

f41gh7 added a commit that referenced this issue Jun 13, 2022
It should simplify API usage for typed clients
#481
@Haleygo
Copy link
Contributor Author

Haleygo commented Jun 21, 2022

@f41gh7 Thanks for adding it.
I'm testing it now and found a bug that can't get all vm resources by client now. Because the request path prefix should be /apis instead of /api .
image
image
/api is only for k8s core [resource] (https://github.com/kubernetes/code-generator/blob/5a93c8bd8f9a2797d0fc9331b37614e7a9b149e4/cmd/client-gen/generators/generator_for_group.go#L75)
And I think the mistake is made by the input directory parameters when execute client-gen, code generator will use the directory path as api resource group.
image
Change file directory name from "core" to "vm" can fix it.

@f41gh7
Copy link
Collaborator

f41gh7 commented Jun 21, 2022

Thanks! Will fix it soon.

I've just added generation and didn't test it yet.

f41gh7 added a commit that referenced this issue Jun 23, 2022
@f41gh7
Copy link
Collaborator

f41gh7 commented Jul 25, 2022

v0.26.0 release contains versioned client. It's moved into separate go module, which should reduce dependency blob.

Feel free to re-open, if it's not working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants