美文网首页
go 脚本优化多个集群浏览器访问信息显示

go 脚本优化多个集群浏览器访问信息显示

作者: for笑 | 来源:发表于2022-03-17 20:20 被阅读0次
package main

import (
    "github.com/gin-gonic/gin"
    "fmt"
    "os/exec"
    "strings"
    "reflect"
)

var envs = [...]string {"dev","qa","pre","prod"}

var dev_conf string = "/abc/def/aaa_kubeconfig"
var qa_conf string = "/abc/def/bbb_conf"
var pre_conf string = "/abc/def/ccc-kubeconfig"
var prod_conf string = "/abc/def/ddd.conf"

// 根据环境标识获取环境pod状态
func execCmd(kubeconf_path string,c chan string){
    var str []byte
    var cmd *exec.Cmd
    var err error

    cmd_str1 := "kubectl get pod -n son  --kubeconfig "
    cmd_str2 := cmd_str1 + kubeconf_path
    println(cmd_str2)
    cmd = exec.Command("sh","-c",cmd_str2)
    str,err = cmd.Output()
    if err != nil{
        fmt.Println(err)
        c <- "环境标识错误"
    }
    c <- string(str)
}


// 查看指定环境pod状态
func get_pod(env string)(abde string){
    c := make(chan string)
    var kubeconf_path string
    fmt.Println("++++++++++++++++查看"+env+"环境pod+++++++++++++++++++")
    switch env{
        case envs[0]:
            kubeconf_path = dev_conf
        case envs[1]:
            kubeconf_path = qa_conf
        case envs[2]:
            kubeconf_path = pro_conf
        case envs[3]:
            kubeconf_path = prod_conf
        default:
            kubeconf_path = "aa"
    }
// 采用了go并发
    go execCmd(kubeconf_path,c)
    f := <-c
    return string(f)
}

// 查看描述指定环境的pod信息
func cmd_desc(env string,pod string)(aa string){
    var kubeconf_path string
    switch env{
        case envs[0]:
            kubeconf_path = dev_conf
        case envs[1]:
            kubeconf_path = qa_conf
        case envs[2]:
            kubeconf_path = pre_conf
        case envs[3]:
            kubeconf_path = prod_conf
        default:
            kubeconf_path = "aa"
    }

    var str []byte
    var cmd *exec.Cmd
    var err error
    cmd_str1 := "kubectl describe pod -n son  --kubeconfig "
    cmd_str2 := cmd_str1 + kubeconf_path +" "+pod
    println(cmd_str2)
    cmd = exec.Command("sh","-c",cmd_str2)
    str,err = cmd.Output()
    if err != nil{
        fmt.Println(err)
        return "1,检查uri格式是否正确(http:/127.0.0.1:8080/{env}/desc/{pod_name};\n2,确认pod名是否存在"
    }
    return string(str)
}

// 查看描述指定环境的pod日志
func cmd_logs(env string,pod string)(aa string){
    var kubeconf_path string
    switch env{
        case envs[0]:
            kubeconf_path = dev_conf
        case envs[1]:
            kubeconf_path = qa_conf
        case envs[2]:
            kubeconf_path = pre_conf
        case envs[3]:
            kubeconf_path = prod_conf
        default:
            kubeconf_path = "aa"
    }

    var str []byte
    var cmd *exec.Cmd
    var err error
    cmd_str1 := "kubectl logs --since=1h -n son  --kubeconfig "
    cmd_str2 := cmd_str1 + kubeconf_path +" "+pod
    println(cmd_str2)
    cmd = exec.Command("sh","-c",cmd_str2)
    str,err = cmd.Output()
    if err != nil{
        fmt.Println(err)
        return "1,检查uri格式是否正确(http:/127.0.0.1:8080/{env}/logs/{pod_name};\n2,确认pod名是否存在;\n3,目前仅支持1个小时内的日志查询,暂不支持动态查询"
    }
    println(string(str))
    return string(str)
}

// 查看描述指定环境的ingress
func cmd_ingress(env string)(aa string){
    var kubeconf_path string
    switch env{
        case envs[0]:
            kubeconf_path = dev_conf
        case envs[1]:
            kubeconf_path = qa_conf
        case envs[2]:
            kubeconf_path = pre_conf
        case envs[3]:
            kubeconf_path = prod_conf
        default:
            kubeconf_path = "aa"
    }

    var str []byte
    var cmd *exec.Cmd
    var err error
    cmd_str1 := "kubectl get ing -n son  --kubeconfig "
    cmd_str2 := cmd_str1 + kubeconf_path
    println(cmd_str2)
    cmd = exec.Command("sh","-c",cmd_str2)
    str,err = cmd.Output()
    if err != nil{
        fmt.Println(err)
        return "1,检查uri格式是否正确(http:/127.0.0.1:8080/{env}/ingress)\n"
    }
    println(string(str))
    return string(str)
}

func main() {
// 字符串相加(官方推荐方式)
    s1 := "aaaa"
    s2 := "bbbb"
    var  build strings.Builder
    build.WriteString(s1)
    build.WriteString(s2)
    s3 := build.String()
    fmt.Println(s3)
    fmt.Println("Hello, World!")


    fmt.Println("**************程序启动*****************")
    // 创建一个默认的路由引擎
    router := gin.Default()
    // GET:请求方式;/hello:请求的路径
    // 当客户端以GET方法请求/hello路径时,会执行后面的匿名函数
    router.GET("/hello/:env", func(c *gin.Context) {
        env := c.Param("env")
        // c.JSON:返回JSON格式的数据
        c.JSON(200, gin.H{
            "message": env,
        })
    })

    router.GET("/:env/desc/:pod", func(c *gin.Context) {
        env := c.Param("env")
        pod := c.Param("pod")
        // c.String 返回String格式的数据
        c.String(200,string(cmd_desc(env,pod)))
    })

    router.GET("/:env/logs/:pod", func(c *gin.Context) {
        env := c.Param("env")
        pod := c.Param("pod")
        // c.String 返回String格式的数据
        c.String(200, string(cmd_logs(env,pod)))
    })

    router.GET("/:env", func(c *gin.Context) {
        env := c.Param("env")
        // 打印对象类型
        fmt.Println(reflect.TypeOf(get_pod(env)))
        c.String
    // 启动HTTP服务,默认在0.0.0.0:8080启动服务
    router.Run(":8001")
}

相关文章

网友评论

      本文标题:go 脚本优化多个集群浏览器访问信息显示

      本文链接:https://www.haomeiwen.com/subject/qzhbdrtx.html