fluid icon indicating copy to clipboard operation
fluid copied to clipboard

Generate fluid-related clients to facilitate go client calls and watch-related resources

Open lgy1027 opened this issue 1 year ago • 7 comments

Add related clients for easy operation. If you make changes and need to regenerate, just execute the script. Some modifications need to be made in the API structure.

Example: `import ( "context" "fmt" "github.com/fluid-cloudnative/fluid/api/v1alpha1" "github.com/fluid-cloudnative/fluid/pkg/apis/client/clientset/versioned" "github.com/fluid-cloudnative/fluid/pkg/apis/client/informers/externalversions" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/client-go/tools/cache" "k8s.io/client-go/tools/clientcmd" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" "time" )

const defaultResync = 600 * time.Second

var ( finalizerName = "data.fluid.io/finalizer" )

func main() { config, err := clientcmd.BuildConfigFromFlags("", "config") if err != nil { panic(err) } config.QPS = 1e6 config.Burst = 1e6 client, err := versioned.NewForConfig(config) if err != nil { panic(err) } informerFactory := externalversions.NewSharedInformerFactory(client, defaultResync)

informer := informerFactory.Data().V1alpha1().Datasets().Informer()
lister := informerFactory.Data().V1alpha1().Datasets().Lister()

informer.AddEventHandlerWithResyncPeriod(cache.ResourceEventHandlerFuncs{
	AddFunc: func(obj interface{}) {
		dataset, ok := obj.(*v1alpha1.Dataset)
		if !ok {
			return
		}

		fmt.Println(dataset.Name)
	},

	DeleteFunc: func(obj interface{}) {
		dataset, ok := obj.(*v1alpha1.Dataset)
		if !ok {
			return
		}

		fmt.Println(dataset.DeletionTimestamp == nil)
	},
}, 0)

ch := make(chan struct{})
informerFactory.Start(ch)
informerFactory.WaitForCacheSync(ch)

list, err := lister.Datasets("default").List(labels.Everything())
if err != nil {
	panic(err)
}
fmt.Println(list)
ds := &v1alpha1.Dataset{
	ObjectMeta: metav1.ObjectMeta{
		Name:      "test",
		Namespace: "default",
	},
	Spec: v1alpha1.DatasetSpec{
		SharedOptions: map[string]string{
			"alluxio.underfs.s3.endpoint":            "http://127.0.0.1:32000",
			"alluxio.underfs.s3.disable.dns.buckets": "true",
			"alluxio.underfs.s3.inherit.acl":         "false",
		},
		SharedEncryptOptions: []v1alpha1.EncryptOption{
			{Name: "aws.accessKeyId", ValueFrom: v1alpha1.EncryptOptionSource{SecretKeyRef: v1alpha1.SecretKeySelector{Name: "minio", Key: "accessKeyId"}}},
			{Name: "aws.secretKey", ValueFrom: v1alpha1.EncryptOptionSource{SecretKeyRef: v1alpha1.SecretKeySelector{Name: "minio", Key: "secretKey"}}},
		},
		PlacementMode: v1alpha1.ShareMode,
		AccessModes: []corev1.PersistentVolumeAccessMode{
			corev1.ReadWriteMany,
		},
		Mounts: []v1alpha1.Mount{{
			MountPoint: "dataset://default/s3/test",
			Name:       "test",
		}},
	},
}
_, err = client.DataV1alpha1().Datasets(ds.Namespace).Create(context.Background(), ds, metav1.CreateOptions{})
if err != nil {
	panic(err)
}

ds, err = client.DataV1alpha1().Datasets(ds.Namespace).Get(context.Background(), ds.Name, metav1.GetOptions{})
if err != nil {
	panic(err)
}

if !controllerutil.ContainsFinalizer(ds, finalizerName) {
	controllerutil.AddFinalizer(ds, finalizerName)
}
_, err = client.DataV1alpha1().Datasets(ds.Namespace).Update(context.Background(), ds, metav1.UpdateOptions{})
if err != nil {
	panic(err)
}
propagationPolicy := metav1.DeletePropagationBackground
err = client.DataV1alpha1().Datasets(ds.Namespace).Delete(context.Background(), ds.Name, metav1.DeleteOptions{
	PropagationPolicy: &propagationPolicy,
})
if err != nil {
	panic(err)
}
<-ch

}`

lgy1027 avatar Sep 25 '23 02:09 lgy1027

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: Once this PR has been reviewed and has the lgtm label, please assign cheyang for approval by writing /assign @cheyang in a comment. For more information see:The Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment Approvers can cancel approval by writing /approve cancel in a comment

fluid-e2e-bot[bot] avatar Sep 25 '23 02:09 fluid-e2e-bot[bot]

Hi @lgy1027. Thanks for your PR.

I'm waiting for a fluid-cloudnative member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

fluid-e2e-bot[bot] avatar Sep 25 '23 02:09 fluid-e2e-bot[bot]

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 1 Code Smell

No Coverage information No Coverage information
0.0% 0.0% Duplication

sonarqubecloud[bot] avatar Sep 25 '23 02:09 sonarqubecloud[bot]

Codecov Report

Merging #3468 (3aec4bd) into master (208b672) will decrease coverage by 0.02%. The diff coverage is 0.00%.

:exclamation: Current head 3aec4bd differs from pull request most recent head 171ab4a. Consider uploading reports for the commit 171ab4a to get more accurate results

@@            Coverage Diff             @@
##           master    #3468      +/-   ##
==========================================
- Coverage   64.29%   64.28%   -0.02%     
==========================================
  Files         442      443       +1     
  Lines       26452    26454       +2     
==========================================
- Hits        17007    17005       -2     
- Misses       7440     7443       +3     
- Partials     2005     2006       +1     
Files Changed Coverage Δ
api/v1alpha1/register.go 0.00% <0.00%> (ø)

... and 1 file with indirect coverage changes

codecov[bot] avatar Sep 25 '23 03:09 codecov[bot]

/test fluid-e2e

cheyang avatar Sep 25 '23 13:09 cheyang

Fluid is built by controller runtime and code generation with kubebuilder. I'm wondering if this change can still be compatible with kubebuilder.

cheyang avatar Sep 25 '23 14:09 cheyang

Fluid is built by controller runtime and code generation with kubebuilder. I'm wondering if this change can still be compatible with kubebuilder.

This modification is only to facilitate Go to operate fluid, and also uses code-generator to generate client-related code. In my opinion, it is not intrusive to the original code. As for the submitted script, I can actually delete it. I feel that the official https:// github.com/fluid-cloudnative/fluid/blob/master/docs/zh/dev/use_go_create_resource.md is a bit troublesome to operate, including resource watch

lgy1027 avatar Sep 26 '23 01:09 lgy1027