k8s云原生-Fluid-如何使用Go客户端创建、删除fluid资源

作者: K8S实践指南

下面以创建和删除Dataset和AlluxioRuntime为例,展示如何通过使用Go客户端创建、删除fluid资源。

要创建的对象对应的yaml文件

假定我们要依据dataset.yamlruntime.yaml创建和删除相应的Dataset和AlluxioRuntime对象。 Dataset对应的dataset.yaml文件

apiVersion: data.fluid.io/v1alpha1
kind: Dataset
metadata:
  name: spark
spec:
  mounts:
    - mountPoint: https://mirrors.bit.edu.cn/apache/spark/
      name: spark

AlluxioRuntime对应的runtime.yaml文件

apiVersion: data.fluid.io/v1alpha1
kind: AlluxioRuntime
metadata:
  name: spark
spec:
  replicas: 1
  tieredstore:
    levels:
      - mediumtype: MEM
        path: /dev/shm
        quota: 8Gi
        high: "0.95"
        low: "0.7"

go客户端代码

采用go客户端创建Dataset和AlluxioRuntime。这里有两种方式,一种是借助fluid的api通过结构体来创建Dataset和AlluixoRuntime的manifest,另一种方式是直接通过格式化的字符串来创建Dataset和AlluxioRuntime的manifest。

注意:这里只列举了创建Dataset和Runtime对象的部分必要属性,Dataset和AlluxioRuntime可配置的属性远不只这些,如果想要配置其它更多属性,可仿照以下例子,结合api/v1alpha1/dataset_types.go以及api/v1alpha1/alluxioruntime_types.go文件进行配置,这两个文件中的代码详细写明了Dataset和AlluxioRuntime各个属性的名称及类型。

借助fluid的api来创建Dataset和AlluxioRuntime的manifest

package main
import (
    "context"
    "fmt"
    "github.com/fluid-cloudnative/fluid/api/v1alpha1"
    "k8s.io/apimachinery/pkg/api/resource"
    metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    "k8s.io/apimachinery/pkg/runtime"
    "k8s.io/apimachinery/pkg/runtime/schema"
    "k8s.io/apimachinery/pkg/runtime/serializer/yaml"
    "k8s.io/apimachinery/pkg/util/json"
    "k8s.io/client-go/dynamic"
    "k8s.io/client-go/tools/clientcmd"
    "time"
)
func createObject(dynamicClient dynamic.Interface, gvr schema.GroupVersionResource, gvk schema.GroupVersionKind, namespace string, manifest []byte) error{
    obj := &unstructured.Unstructured
    decoder := yaml.NewDecodingSerializer(unstructured.UnstructuredJSONScheme)
    if _, _, err := decoder.Decode(manifest, &gvk, obj); err != nil {
        return err
    }
    _, err := dynamicClient.Resource(gvr).Namespace(namespace).Create(context.TODO(), obj, metav1.CreateOptions{})
    if err != nil {
        return err
    }
    return nil
}
func deleteObject(dynamicClient dynamic.Interface, gvr schema.GroupVersionResource, namespace string, name string) error{
    return dynamicClient.Resource(gvr).Namespace(namespace).Delete(context.TODO(), name, metav1.DeleteOptions{})
}
func getObject(dynamicClient dynamic.Interface, gvr schema.GroupVersionResource, namespace string, name string, obj runtime.Object) error {
    data, err := dynamicClient.Resource(gvr).Namespace(namespace).Get(context.TODO(), name, metav1.GetOptions{})
    if err != nil {
        return err
    }
    dataJson, err := data.MarshalJSON()
    if err != nil {
        return err
    }
    if err = json.Unmarshal(dataJson, obj); err != nil {
        return err
    }
    return nil
}
func main() {
    // uses the current context in kubeconfig
    // path-to-kubeconfig -- for example, /root/.kube/config
    config, _ := clientcmd.BuildConfigFromFlags("", "/root/.kube/config")
    // creates the client
    dynamicClient, err := dynamic.NewForConfig(config)
    if err != nil {
        panic(err)
    }
    datasetGVR := schema.GroupVersionResource{
        Group: "data.fluid.io",
        Version: "v1alpha1",
        Resource: "datasets",
    }
    datasetGVK := schema.GroupVersionKind{
        Group:   "data.fluid.io",
        Version: "v1alpha1",
        Kind:    "Dataset",
    }
    // Dataset manifest
    dsManifest:= &v1alpha1.Dataset{
        TypeMeta: metav1.TypeMeta{
            Kind:       "Dataset",
            APIVersion: "data.fluid.io/v1alpha1",
        },
        ObjectMeta: metav1.ObjectMeta{
            Name: "spark",
            Namespace: "default",
        },
        Spec: v1alpha1.DatasetSpec{
            Mounts: [] v1alpha1.Mount{
                {
                    MountPoint:     "https://mirrors.bit.edu.cn/apache/spark/",
                    Name:           "spark",
                },
            },
        },
    }
    runtimeGVR := schema.GroupVersionResource{
        Group:    "data.fluid.io",
        Version:  "v1alpha1",
        Resource: "alluxioruntimes",
    }
    runtimeGVK := schema.GroupVersionKind{
        Group:   "data.fluid.io",
        Version: "v1alpha1",
        Kind:    "AlluxioRuntime",
    }
    // AlluxioRuntime manifest
    quantity := resource.MustParse("8Gi")
    arManifest := &v1alpha1.AlluxioRuntime{
        TypeMeta: metav1.TypeMeta{
            Kind:       "AlluxioRuntime",
            APIVersion: "data.fluid.io/v1alpha1",
        },
        ObjectMeta: metav1.ObjectMeta{
            Name: "spark",
            Namespace: "default",
        },
        Spec: v1alpha1.AlluxioRuntimeSpec{
            Replicas: 1,
            Tieredstore: v1alpha1.Tieredstore{
                Levels: []v1alpha1.Level{
                    {
                        MediumType: "MEM",
                        Path:       "/dev/shm",
                        Quota:      &quantity,
                        High:       "0.95",
                        Low:        "0.7",
                    },
                },
            },
        },
    }
    manifest, err := json.Marshal(dsManifest)
    if err != nil {
        panic(err)
    }
    // create the dataset
    if err = createObject(dynamicClient, datasetGVR, datasetGVK, "default", manifest); err != nil{
        panic(err)
        fmt.Println("create the dataset successfully!")
    }
    manifest, err = json.Marshal(arManifest)
    if err != nil {
        panic(err)
    }
    // create the runtime
    if err = createObject(dynamicClient, runtimeGVR, runtimeGVK, "default", manifest); err != nil {
        panic(err)
        fmt.Println("create the runtime successfully!")
    }
    // check whether the dataset is ready
    var ready bool = false
    var dataset v1alpha1.Dataset
    for !ready {
        // get the dataset
        if err = getObject(dynamicClient, datasetGVR, "default", "spark", &dataset); err != nil {
            panic(err)
            status := dataset.Status.Phase
            if status == v1alpha1.BoundDatasetPhase {
                fmt.Println("the dataset is bound.")
                ready = true
                fmt.Println("the dataset is not bound, wait 10 seconds.")
                time.Sleep(10 * time.Second)
            }
        }
    }
    // delete the runtime
    if err = deleteObject(dynamicClient, runtimeGVR, "default", "spark"); err != nil{
        panic(err)
        fmt.Println("delete the runtime successfully!")
    }
    // delete the dataset
    if err = deleteObject(dynamicClient, datasetGVR, "default", "spark"); err != nil{
        panic(err)
        fmt.Println("delete the dataset successfully!")
    }
}

通过格式化的字符串来创建Dataset和AlluxioRuntime的manifest

注意dsManifestrtManifest字符串的格式应该是和yaml文件的格式一致的,空格不能用tab代替!

package main
import (
    "context"
    "fmt"
    "github.com/fluid-cloudnative/fluid/api/v1alpha1"
    metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    "k8s.io/apimachinery/pkg/runtime"
    "k8s.io/apimachinery/pkg/runtime/schema"
    "k8s.io/apimachinery/pkg/runtime/serializer/yaml"
    "k8s.io/apimachinery/pkg/util/json"
    "k8s.io/client-go/dynamic"
    "k8s.io/client-go/tools/clientcmd"
    "time"
)
// dataset manifest
const dsManifest = `
apiVersion: data.fluid.io/v1alpha1
kind: Dataset
metadata:
  name: spark
spec:
  mounts:
    - mountPoint: https://mirrors.bit.edu.cn/apache/spark/
      name: spark
`
// runtime manifest
const rtManifest = `
apiVersion: data.fluid.io/v1alpha1
kind: AlluxioRuntime
metadata:
  name: spark
spec:
  replicas: 1
  tieredstore:
    levels:
      - mediumtype: MEM
        path: /dev/shm
        quota: 8Gi
        high: "0.95"
        low: "0.7"
`
func createObject(dynamicClient dynamic.Interface, gvr schema.GroupVersionResource, gvk schema.GroupVersionKind, namespace string, manifest []byte) error{
    obj := &unstructured.Unstructured
    decoder := yaml.NewDecodingSerializer(unstructured.UnstructuredJSONScheme)
    if _, _, err := decoder.Decode(manifest, &gvk, obj); err != nil {
        return err
    }
    _, err := dynamicClient.Resource(gvr).Namespace(namespace).Create(context.TODO(), obj, metav1.CreateOptions{})
    if err != nil {
        return err
    }
    return nil
}
func deleteObject(dynamicClient dynamic.Interface, gvr schema.GroupVersionResource, namespace string, name string) error{
    return dynamicClient.Resource(gvr).Namespace(namespace).Delete(context.TODO(), name, metav1.DeleteOptions{})
}
func getObject(dynamicClient dynamic.Interface, gvr schema.GroupVersionResource, namespace string, name string, obj runtime.Object) error {
    data, err := dynamicClient.Resource(gvr).Namespace(namespace).Get(context.TODO(), name, metav1.GetOptions{})
    if err != nil {
        return err
    }
    dataJson, err := data.MarshalJSON()
    if err != nil {
        return err
    }
    if err = json.Unmarshal(dataJson, obj); err != nil {
        return err
    }
    return nil
}
func main() {
    // uses the current context in kubeconfig
    // path-to-kubeconfig -- for example, /root/.kube/config
    config, _ := clientcmd.BuildConfigFromFlags("", "/root/.kube/config")
    // creates the client
    dynamicClient, err := dynamic.NewForConfig(config)
    if err != nil {
        panic(err)
    }
    datasetGVR := schema.GroupVersionResource{
        Group: "data.fluid.io",
        Version: "v1alpha1",
        Resource: "datasets",
    }
    datasetGVK := schema.GroupVersionKind{
        Group:   "data.fluid.io",
        Version: "v1alpha1",
        Kind:    "Dataset",
    }

    runtimeGVR := schema.GroupVersionResource{
        Group:    "data.fluid.io",
        Version:  "v1alpha1",
        Resource: "alluxioruntimes",
    }
    runtimeGVK := schema.GroupVersionKind{
        Group:   "data.fluid.io",
        Version: "v1alpha1",
        Kind:    "AlluxioRuntime",
    }
    // create the dataset
    if err = createObject(dynamicClient, datasetGVR, datasetGVK, "default", []byte(dsManifest)); err != nil{
        panic(err)
        fmt.Println("create the dataset successfully!")
    }
    // create the runtime
    if err = createObject(dynamicClient, runtimeGVR, runtimeGVK, "default", []byte(rtManifest)); err != nil {
        panic(err)
        fmt.Println("create the runtime successfully!")
    }
    // check whether the dataset is ready
    var ready bool = false
    var dataset v1alpha1.Dataset
    for !ready {
        // get the dataset
        if err = getObject(dynamicClient, datasetGVR, "default", "spark", &dataset); err != nil {
            panic(err)
            status := dataset.Status.Phase
            if status == v1alpha1.BoundDatasetPhase {
                fmt.Println("the dataset is bound.")
                ready = true
                fmt.Println("the dataset is not bound, wait 10 seconds.")
                time.Sleep(10 * time.Second)
            }
        }
    }
    // delete the runtime
    if err = deleteObject(dynamicClient, runtimeGVR, "default", "spark"); err != nil{
        panic(err)
        fmt.Println("delete the runtime successfully!")
    }
    // delete the dataset
    if err = deleteObject(dynamicClient, datasetGVR, "default", "spark"); err != nil{
        panic(err)
        fmt.Println("delete the dataset successfully!")
    }
}

文章列表

更多推荐

更多
  • Kubenetes扩展教程-三、API 流扩展 忽必烈 API Flow,身份验证网页挂钩,授权网页挂钩,动态准入控制器,关键要点,证明,服务器 API 配置库,服务器 API 配置库,Webhook 配置资源,> —埃隆·马斯克 >> 商业巨头、工业设计师、工程师Kuber
    Apache CN

  • Kubenetes扩展教程-四、扩展 Kubernetes API kuble API 概述,自定义资源定义和控制器,聚合的 API 和扩展服务器,关键要点,API 版本控制,API 组,库 API 中的扩展点,Kubernetes 客户库,Kubernetes 中的运算符模式,kubebuilder
    Apache CN

  • Kubenetes扩展教程-七、即将到来的扩展点 服务目录,集群 API,关键要点,结论,> —西奥多·罗斯福 >> 美国第 26 任总统Kubernetes 凭借其在云原生世界中的强大基础获得了成功。它提供了一组丰富的特性来管理容器化的应用,并提供了各种扩展点来添加新的功能。
    Apache CN

  • Kubenetes扩展教程-六、基础设施扩展 云原生基础架构,存储插件,网络插件,设备插件,关键要点,容器存储接口,kubernetes 中的 CSI 插件,库柏网络模型,容器网络接口(CNI),设备插件 API,设备插件的开发和部署,> —马克·扎克伯格 >> 美国企业家、脸
    Apache CN

  • Kubenetes扩展教程-五、调度扩展 库调度程序概述,配置和管理多个调度程序,调度程序扩展器,关键要点,调度框架,配置详细信息,> —圣雄甘地 >> 印度律师、政治家、社会活动家、作家 调度器是 Kubernetes 的核心部分,用于将工作负载分配给集群中的节点。分
    Apache CN

  • Kubenetes扩展教程-二、`kubectl`插件 二、kubectl插件kubectl 安装和使用,立方插件设计,创建您的第一个 kubectl 插件,插件库:krew,关键要点,> —马歇尔·麦克卢汉 >> 媒体学者和评论家 命令行工具是开发人员的瑞士军刀。您可以连接到后端系
    Apache CN

  • Kubenetes扩展教程-一、简介 忽必烈再世,Kubernetes 扩展模式,关键要点,控制平面组件,节点组件,配置 Kubernetes 集群,控制器,web 手册,二进制插件,立方扩展点,> 卡尔沙堡 >> 美国诗人、传记作家、记者和编辑,三次获得普利策奖K
    Apache CN

  • Azure Kubernetes微服务教程-六、AK 的 CI/CD 介绍,快速浏览 DevOps,本次练习的目标,使用 Azure 门户创建 Azure Kubernetes 服务,使用 Azure 门户创建 Azure 容器实例,使用 Visual Studio 构建示例应用,使用 Azure Dev
    Apache CN

  • Azure Kubernetes微服务教程-五、保护和监控 AKS 上运行的应用 介绍,安全概念,蓝色库柏服务清单,监控概念,摘要,主安全,节点安全性,集群升级,网络安全性,不可告人的秘密,安全概念:结论,容器洞察,Azure 监视器功能,介绍首先,祝贺你完成了本书的 60%。在阅读完架构设计和模式之后,
    Apache CN

  • Azure Kubernetes微服务教程-二、微服务:架构和设计考虑 介绍,微服务架构优势,衍生商业价值,定义面向目标的分层方法,应用面向目标的分层方法,微服务设计,微服务的系统方法,微服务设计流程,目标和原则,平台,文化,摘要,附录,模块性,粘结性,相互关系,微服务架构目标和优势的成熟度模型,服务,解决
    Apache CN

  • 近期文章

    更多
    文章目录

      推荐作者

      更多