spring cloud + kubeedge

序言

最近公司在做边缘计算相关的事情,让我负责云平台的开发。云边协同使用的是华为开源的kubeedge,为了更好的支持云平台开发,我开始自己开发基础框架:spring cloud kubeedge。

第一节 :为何要开发这个框架

华为的kubeedge开源的时间并不长,spring cloud 官方还没有集成它。但是spring cloud 家族已经有spring cloud k8s了,该框架封装了io.fabric8。该开源框架对kubernetes的 API server跟随性不强,华为的kubeedge却紧跟kubernetes的版本。所以我决定自己封装另一个开源组件kubernetes client。

第二节  : spring cloud kubeedge 的功能

提供标准接口,屏蔽复杂的函数调用和参数设置。

第三节 : 成果

第四节: 实战使用

子项目spring cloud kubeedge web 实际上是一个测试项目,使用集成的spring-cloud-kubeedge-core来访问搭建好的kubeedge平台。

 部分代码如下:

配置文件:

package com.miller.springcloudkubeedgeweb.config;

import com.miller.springcloudkubeedgeweb.common.StandardNamespaceUtils;
import com.miller.springcloudkubeedgeweb.util.ResourceRenderer;
import io.kubernetes.client.ApiClient;
import io.kubernetes.client.apis.CoreV1Api;
import io.kubernetes.client.util.Config;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;

import io.kubernetes.client.Configuration;

import java.io.IOException;
import java.io.InputStream;

/**
 * @program: spring-cloud-kubeedge
 * @description: cfg
 * @author: Miller.FAN
 * @create: 2019-12-09 15:32
 **/

@org.springframework.context.annotation.Configuration
@EnableAutoConfiguration
public class NeedConfig {

    @Bean
    public CoreV1Api coreV1Api() {
        String fileName = "classpath:/k8s/controller-manager.conf";
        InputStream inputStream = null;
        try {
            inputStream = ResourceRenderer.resourceLoader(fileName);
        } catch (
                IOException e) {
            e.printStackTrace();
        }
        ApiClient client = null;
        try {
            client = Config.fromConfig(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        client.setConnectTimeout(5 * 60 * 1000);
        Configuration.setDefaultApiClient(client);
        return new CoreV1Api();
    }
    @Bean
    public StandardNamespaceUtils standardNamespaceUtils() {
        return new StandardNamespaceUtils();
    }
    
}

 service层:

package com.miller.springcloudkubeedgeweb.service;

import com.miller.springcloudkubeedgecore.namespace.StandardNamespaceUtils;
import io.kubernetes.client.apis.CoreV1Api;
import io.kubernetes.client.models.V1Namespace;
import io.kubernetes.client.models.V1NamespaceList;
import org.springframework.stereotype.Service;

/**
 * @program: spring-cloud-kubeedge
 * @description: namespace
 * @author: Miller.FAN
 * @create: 2019-12-09 11:58
 **/
@Service
public class NamespaceService implements NamespaceRepository{

/*    @Autowired
    StandardNamespaceUtils standardNamespaceUtils;*/

    private CoreV1Api coreV1Api = new CoreV1Api();

    private StandardNamespaceUtils standardNamespaceUtils = new StandardNamespaceUtils(coreV1Api);

    //返回分区列表
    public V1NamespaceList getNamespace(){
        return standardNamespaceUtils.getNamespaceList();
    }

    //创建分区
    public V1Namespace createNamespace() {
        return standardNamespaceUtils.createNamespace();
    }

    //删除分区
    public Boolean deleteNamespace(String namespace) {
        return standardNamespaceUtils.deleteNamespace(namespace);
    }

    //查询指定的分区信息
    public V1Namespace queryNamespace(String namespace) {
        return standardNamespaceUtils.queryNamespace(namespace);
    }

    //替换分区内容
    public V1Namespace replaceNamespace(String name, V1Namespace body) {
        return standardNamespaceUtils.replaceNamespace(name,body);
    }
}

controller层:

package com.miller.springcloudkubeedgeweb.controller;

import com.miller.springcloudkubeedgeweb.service.NamespaceService;
import io.kubernetes.client.models.V1Namespace;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

import static com.miller.springcloudkubeedgeweb.common.Commons.SUCCESS;

/**
 * @program: spring-cloud-kubeedge
 * @description: c
 * @author: Miller.FAN
 * @create: 2019-12-09 14:26
 **/
@RestController
@RequestMapping(path = "/namespace")
@Api(value = "NamespaceController", description = "命名空间信息接口")
public class NamespaceController {

    @Autowired
    private NamespaceService namespaceService;

    @RequestMapping(value ="/get/list" , method = RequestMethod.GET)
    @ApiOperation(value = "get/list", notes = "getNamespaceList")
    public HashMap<String,Object> getNamespaceList() {
        HashMap<String,Object> ret = new HashMap<>();
        ret.put(SUCCESS,namespaceService.getNamespace());
        return ret;
    }

    @RequestMapping(value ="/creat" , method = RequestMethod.POST)
    @ApiOperation(value = "creat", notes = "creatNamespace")
    public HashMap<String,Object> creatNamespace() {
        HashMap<String,Object> ret = new HashMap<>();
        ret.put(SUCCESS,namespaceService.createNamespace());
        return ret;
    }

    @RequestMapping(value ="/delete/{namespace}" , method = RequestMethod.DELETE)
    @ApiOperation(value = "delete", notes = "deleteNamespace")
    public HashMap<String,Object> deleteNamespace(@PathVariable String namespace) {
        HashMap<String,Object> ret = new HashMap<>();
        ret.put(SUCCESS,namespaceService.deleteNamespace(namespace));
        return ret;
    }

    @RequestMapping(value ="/query/{namespace}" , method = RequestMethod.GET)
    @ApiOperation(value = "query", notes = "queryNamespace")
    public HashMap<String,Object> queryNamespace(@PathVariable String namespace) {
        HashMap<String,Object> ret = new HashMap<>();
        ret.put(SUCCESS,namespaceService.queryNamespace(namespace));
        return ret;
    }

    @RequestMapping(value ="/update/{namespace}" , method = RequestMethod.PUT)
    @ApiOperation(value = "update", notes = "creatNamespace")
    public HashMap<String,Object> creatNamespace(@PathVariable String namespace, @RequestBody V1Namespace body) {
        HashMap<String,Object> ret = new HashMap<>();
        ret.put(SUCCESS,namespaceService.replaceNamespace(namespace,body));
        return ret;
    }

}

第五节 : 启动项目,测试

在swagger的UI页面测试:

返回的结果:

{
  "ok": {
    "apiVersion": "v1",
    "items": [
      {
        "apiVersion": null,
        "kind": null,
        "metadata": {
          "annotations": null,
          "clusterName": null,
          "creationTimestamp": {
            "year": 2019,
            "dayOfMonth": 10,
            "dayOfWeek": 2,
            "era": 1,
            "dayOfYear": 344,
            "hourOfDay": 19,
            "yearOfCentury": 19,
            "weekyear": 2019,
            "centuryOfEra": 20,
            "yearOfEra": 2019,
            "monthOfYear": 12,
            "minuteOfHour": 0,
            "weekOfWeekyear": 50,
            "millisOfSecond": 0,
            "secondOfMinute": 30,
            "millisOfDay": 68430000,
            "minuteOfDay": 1140,
            "secondOfDay": 68430,
            "millis": 1575975630000,
            "zone": {
              "fixed": false,
              "uncachedZone": {
                "fixed": false,
                "cachable": true,
                "id": "Asia/Shanghai"
              },
              "id": "Asia/Shanghai"
            },
            "chronology": {
              "zone": {
                "fixed": false,
                "uncachedZone": {
                  "fixed": false,
                  "cachable": true,
                  "id": "Asia/Shanghai"
                },
                "id": "Asia/Shanghai"
              }
            },
            "beforeNow": true,
            "equalNow": false,
            "afterNow": false
          },
          "deletionGracePeriodSeconds": null,
          "deletionTimestamp": null,
          "finalizers": null,
          "generateName": null,
          "generation": null,
          "initializers": null,
          "labels": null,
          "managedFields": null,
          "name": "default",
          "namespace": null,
          "ownerReferences": null,
          "resourceVersion": "148",
          "selfLink": "/api/v1/namespaces/default",
          "uid": "9c1a0018-ce47-4229-a32e-b675b33d7056"
        },
        "spec": {
          "finalizers": [
            "kubernetes"
          ]
        },
        "status": {
          "phase": "Active"
        }
      },
      {
        "apiVersion": null,
        "kind": null,
        "metadata": {
          "annotations": null,
          "clusterName": null,
          "creationTimestamp": {
            "year": 2019,
            "dayOfMonth": 10,
            "dayOfWeek": 2,
            "era": 1,
            "dayOfYear": 344,
            "hourOfDay": 19,
            "yearOfCentury": 19,
            "weekyear": 2019,
            "centuryOfEra": 20,
            "yearOfEra": 2019,
            "monthOfYear": 12,
            "minuteOfHour": 0,
            "weekOfWeekyear": 50,
            "millisOfSecond": 0,
            "secondOfMinute": 27,
            "millisOfDay": 68427000,
            "minuteOfDay": 1140,
            "secondOfDay": 68427,
            "millis": 1575975627000,
            "zone": {
              "fixed": false,
              "uncachedZone": {
                "fixed": false,
                "cachable": true,
                "id": "Asia/Shanghai"
              },
              "id": "Asia/Shanghai"
            },
            "chronology": {
              "zone": {
                "fixed": false,
                "uncachedZone": {
                  "fixed": false,
                  "cachable": true,
                  "id": "Asia/Shanghai"
                },
                "id": "Asia/Shanghai"
              }
            },
            "beforeNow": true,
            "equalNow": false,
            "afterNow": false
          },
          "deletionGracePeriodSeconds": null,
          "deletionTimestamp": null,
          "finalizers": null,
          "generateName": null,
          "generation": null,
          "initializers": null,
          "labels": null,
          "managedFields": null,
          "name": "kube-node-lease",
          "namespace": null,
          "ownerReferences": null,
          "resourceVersion": "38",
          "selfLink": "/api/v1/namespaces/kube-node-lease",
          "uid": "2fa58c84-63ed-4b66-9a1b-896da7d8a7d0"
        },
        "spec": {
          "finalizers": [
            "kubernetes"
          ]
        },
        "status": {
          "phase": "Active"
        }
      },
      {
        "apiVersion": null,
        "kind": null,
        "metadata": {
          "annotations": null,
          "clusterName": null,
          "creationTimestamp": {
            "year": 2019,
            "dayOfMonth": 10,
            "dayOfWeek": 2,
            "era": 1,
            "dayOfYear": 344,
            "hourOfDay": 19,
            "yearOfCentury": 19,
            "weekyear": 2019,
            "centuryOfEra": 20,
            "yearOfEra": 2019,
            "monthOfYear": 12,
            "minuteOfHour": 0,
            "weekOfWeekyear": 50,
            "millisOfSecond": 0,
            "secondOfMinute": 27,
            "millisOfDay": 68427000,
            "minuteOfDay": 1140,
            "secondOfDay": 68427,
            "millis": 1575975627000,
            "zone": {
              "fixed": false,
              "uncachedZone": {
                "fixed": false,
                "cachable": true,
                "id": "Asia/Shanghai"
              },
              "id": "Asia/Shanghai"
            },
            "chronology": {
              "zone": {
                "fixed": false,
                "uncachedZone": {
                  "fixed": false,
                  "cachable": true,
                  "id": "Asia/Shanghai"
                },
                "id": "Asia/Shanghai"
              }
            },
            "beforeNow": true,
            "equalNow": false,
            "afterNow": false
          },
          "deletionGracePeriodSeconds": null,
          "deletionTimestamp": null,
          "finalizers": null,
          "generateName": null,
          "generation": null,
          "initializers": null,
          "labels": null,
          "managedFields": null,
          "name": "kube-public",
          "namespace": null,
          "ownerReferences": null,
          "resourceVersion": "37",
          "selfLink": "/api/v1/namespaces/kube-public",
          "uid": "0964a321-07c2-45ff-8ffa-f3438e9d2d4a"
        },
        "spec": {
          "finalizers": [
            "kubernetes"
          ]
        },
        "status": {
          "phase": "Active"
        }
      },
      {
        "apiVersion": null,
        "kind": null,
        "metadata": {
          "annotations": null,
          "clusterName": null,
          "creationTimestamp": {
            "year": 2019,
            "dayOfMonth": 10,
            "dayOfWeek": 2,
            "era": 1,
            "dayOfYear": 344,
            "hourOfDay": 19,
            "yearOfCentury": 19,
            "weekyear": 2019,
            "centuryOfEra": 20,
            "yearOfEra": 2019,
            "monthOfYear": 12,
            "minuteOfHour": 0,
            "weekOfWeekyear": 50,
            "millisOfSecond": 0,
            "secondOfMinute": 27,
            "millisOfDay": 68427000,
            "minuteOfDay": 1140,
            "secondOfDay": 68427,
            "millis": 1575975627000,
            "zone": {
              "fixed": false,
              "uncachedZone": {
                "fixed": false,
                "cachable": true,
                "id": "Asia/Shanghai"
              },
              "id": "Asia/Shanghai"
            },
            "chronology": {
              "zone": {
                "fixed": false,
                "uncachedZone": {
                  "fixed": false,
                  "cachable": true,
                  "id": "Asia/Shanghai"
                },
                "id": "Asia/Shanghai"
              }
            },
            "beforeNow": true,
            "equalNow": false,
            "afterNow": false
          },
          "deletionGracePeriodSeconds": null,
          "deletionTimestamp": null,
          "finalizers": null,
          "generateName": null,
          "generation": null,
          "initializers": null,
          "labels": null,
          "managedFields": null,
          "name": "kube-system",
          "namespace": null,
          "ownerReferences": null,
          "resourceVersion": "36",
          "selfLink": "/api/v1/namespaces/kube-system",
          "uid": "5e6cbd2f-a2b5-4315-a50e-6d2fd52ab8b7"
        },
        "spec": {
          "finalizers": [
            "kubernetes"
          ]
        },
        "status": {
          "phase": "Active"
        }
      }
    ],
    "kind": "NamespaceList",
    "metadata": {
      "resourceVersion": "109495",
      "selfLink": "/api/v1/namespaces",
      "continue": null
    }
  }
}

上面的返回数据是json格式的,包含了kubeedge的所有namespace的信息。

大家看到import com.miller.***这样的导入,不要被误导,这是我自己开发的starter,在自己本地仓库中,网上是没有的,我的英文名:miller,所有我没有成立的公司叫com.miller。欢迎各路神仙来访,留言、讨论、学习。

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值