135 lines
3.1 KiB
Go
135 lines
3.1 KiB
Go
package main
|
|
|
|
import (
|
|
"drone-kubernetes/k8s"
|
|
"fmt"
|
|
"github.com/urfave/cli/v2"
|
|
"os"
|
|
)
|
|
|
|
func main() {
|
|
app := &cli.App{
|
|
Name: "drone-kubernetes",
|
|
Usage: "Drone plugin to deploy to kubernetes",
|
|
}
|
|
|
|
app.Action = run
|
|
|
|
app.Flags = []cli.Flag{
|
|
&cli.StringFlag{
|
|
Required: true,
|
|
Name: "k8s_host",
|
|
Usage: "k8s ip+port or domain",
|
|
EnvVars: []string{"PLUGIN_K8S_HOST"},
|
|
},
|
|
&cli.StringFlag{
|
|
Required: true,
|
|
Name: "k8s_auth_type",
|
|
Usage: "k8s auth type example: token,config",
|
|
EnvVars: []string{"PLUGIN_K8S_AUTH_TYPE"},
|
|
},
|
|
&cli.StringFlag{
|
|
Name: "k8s_auth_config",
|
|
Usage: "k8s auth config",
|
|
EnvVars: []string{"PLUGIN_K8S_AUTH_CONFIG"},
|
|
},
|
|
&cli.StringFlag{
|
|
Name: "k8s_auth_token",
|
|
Usage: "k8s auth token",
|
|
EnvVars: []string{"PLUGIN_K8S_AUTH_TOKEN"},
|
|
},
|
|
&cli.StringFlag{
|
|
Name: "k8s_auth_ca_crt",
|
|
Usage: "k8s auth ca crt",
|
|
EnvVars: []string{"PLUGIN_K8S_AUTH_CA_CRT"},
|
|
},
|
|
&cli.BoolFlag{
|
|
Name: "k8s_auth_skip_tls",
|
|
Usage: "k8s auth skip tls",
|
|
EnvVars: []string{"PLUGIN_K8S_AUTH_SKIP_TLS"},
|
|
},
|
|
&cli.StringFlag{
|
|
Required: true,
|
|
Name: "k8s_namespace",
|
|
Usage: "k8s namespace example: default",
|
|
DefaultText: "default",
|
|
EnvVars: []string{"PLUGIN_K8S_NAMESPACE"},
|
|
},
|
|
&cli.StringFlag{
|
|
Required: true,
|
|
Name: "kind",
|
|
Usage: "workload type,example: deployment,service...",
|
|
EnvVars: []string{"PLUGIN_KIND"},
|
|
},
|
|
&cli.StringFlag{
|
|
Required: true,
|
|
Name: "resource_name",
|
|
Usage: "resource name,example: deployments name pod name",
|
|
EnvVars: []string{"PLUGIN_RESOURCE_NAME"},
|
|
},
|
|
&cli.StringFlag{
|
|
Required: true,
|
|
Name: "action_method",
|
|
Aliases: []string{"acm"},
|
|
Usage: "workload action method,example: create,update,delete,restart...",
|
|
EnvVars: []string{"PLUGIN_ACTION_METHOD"},
|
|
},
|
|
&cli.StringFlag{
|
|
Name: "repo",
|
|
Usage: "repo",
|
|
EnvVars: []string{"PLUGIN_REPO"},
|
|
},
|
|
}
|
|
|
|
// 启动
|
|
err := app.Run(os.Args)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
}
|
|
|
|
func run(c *cli.Context) error {
|
|
|
|
authType := c.String("k8s_auth_type")
|
|
authConfig := c.String("k8s_auth_config")
|
|
authToken := c.String("k8s_auth_token")
|
|
|
|
if (authType != "") && (authConfig == "" && authToken == "") {
|
|
return cli.Exit("auth token or auth config not empty", 1)
|
|
}
|
|
|
|
if c.String("action_method") == "update" {
|
|
if c.String("repo") == "" {
|
|
return cli.Exit("repo not empty", 1)
|
|
}
|
|
}
|
|
|
|
k8sConf, err := k8s.NewConfig(
|
|
k8s.WithHost(c.String("k8s_host")),
|
|
k8s.WithAuthType(c.String("k8s_auth_type")),
|
|
k8s.WithConfig(c.String("k8s_auth_config")),
|
|
k8s.WithToken(c.String("k8s_auth_token")),
|
|
k8s.WithCaCrt(c.String("k8s_auth_ca_crt")),
|
|
k8s.WithIsSkipTls(c.Bool("k8s_auth_skip_tls")),
|
|
)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
client, err := k8sConf.Client()
|
|
|
|
if err != nil {
|
|
fmt.Println("k8s client error: ", err.Error())
|
|
return cli.Exit("k8s client error", 1)
|
|
}
|
|
|
|
if err = client.Do(c.String("k8s_namespace"), c.String("kind"), c.String("resource_name"), c.String("action_method"), c.String("repo")); err != nil {
|
|
return cli.Exit(err.Error(), 1)
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|