diff --git a/glide.lock b/glide.lock index 6e2e42a..a291a55 100644 --- a/glide.lock +++ b/glide.lock @@ -1,5 +1,5 @@ -hash: dd80df76b62073b5846624df90929f50c8d8ded4aad953864b41bb929cad8ad4 -updated: 2019-03-29T13:02:31.745505+11:00 +hash: 3db3288a056b65e1736f52faf757d1a393b0af3d0ebbe5b63bdeab60b37e1da0 +updated: 2019-04-01T09:42:56.599946+11:00 imports: - name: github.com/aymerick/raymond version: b565731e1464263de0bda75f2e45d97b54b60110 @@ -7,10 +7,6 @@ imports: - ast - lexer - parser -- name: github.com/joho/godotenv - version: 5c0e6c6ab1a0a9ef0a8822cba3a05d62f7dad941 -- name: github.com/urfave/cli - version: cfb38830724cc34fedffe9a2a29fb54fa9169cd1 - name: k8s.io/client-go version: e64494209f554a6723674bd494d69445fb76a1d4 testImports: [] diff --git a/glide.yaml b/glide.yaml index cd26900..e6b2e3b 100644 --- a/glide.yaml +++ b/glide.yaml @@ -2,7 +2,4 @@ package: github.com/danielgormly/kubano import: - package: k8s.io/client-go version: v10.0.0 -- package: github.com/urfave/cli - version: v1 -- package: github.com/joho/godotenv -- package: github.com/aymerick/raymond \ No newline at end of file +- package: github.com/aymerick/raymond diff --git a/kubano b/kubano index 951963a..52c5e8e 100755 Binary files a/kubano and b/kubano differ diff --git a/main.go b/main.go index a195d95..531ec22 100644 --- a/main.go +++ b/main.go @@ -3,61 +3,20 @@ package main import ( "log" "os" - - "github.com/joho/godotenv" - "github.com/urfave/cli" ) func main() { - app := cli.NewApp() - app.Name = "Kubano" - app.Usage = "Use with Drone CI" - app.Version = "0.0.1" - app.Action = run - app.Flags = []cli.Flag{ - cli.StringFlag{ - Name: "ca", - Usage: "Certificate Authority cert to use (Base-64 encoded)", - EnvVar: "PLUGIN_CA", - }, - cli.StringFlag{ - Name: "token", - Usage: "Kubernetes service token", - EnvVar: "PLUGIN_TOKEN", - }, - cli.StringFlag{ - Name: "server", - Usage: "Kubernetes server address", - EnvVar: "PLUGIN_SERVER", - }, - cli.StringFlag{ - Name: "namespace", - Usage: "namespace to use: 'default' is the default", - EnvVar: "PLUGIN_NAMESPACE", - }, - cli.StringFlag{ - Name: "template", - Usage: "template file to use for deployment e.g. deployment.yaml", - EnvVar: "PLUGIN_TEMPLATE", + plugin := Plugin{ + Template: os.Getenv("PLUGIN_TEMPLATE"), + KubeConfig: KubeConfig{ + Token: os.Getenv("PLUGIN_TOKEN"), + Endpoint: os.Getenv("PLUGIN_ENDPOINT"), + Ca: os.Getenv("PLUGIN_CA"), + Namespace: os.Getenv("PLUGIN_NAMESPACE"), }, } - if err := app.Run(os.Args); err != nil { + err := plugin.Exec() + if err != nil { log.Fatal(err) } } - -func run(c *cli.Context) error { - if c.String("env-file") != "" { - _ = godotenv.Load(c.String("env-file")) - } - plugin := Plugin{ - Template: c.String("template"), - KubeConfig: KubeConfig{ - Token: c.String("token"), - Server: c.String("server"), - Ca: c.String("ca"), - Namespace: c.String("namespace"), - }, - } - return plugin.Exec() -} diff --git a/plugin.go b/plugin.go index a95d8e3..80ec064 100644 --- a/plugin.go +++ b/plugin.go @@ -13,7 +13,7 @@ import ( type ( KubeConfig struct { Ca string - Server string + Endpoint string Token string Namespace string Template string @@ -25,22 +25,21 @@ type ( ) func (p Plugin) Exec() error { - if p.KubeConfig.Server == "" { - log.Fatal("KUBE_SERVER is not defined") + if p.KubeConfig.Endpoint == "" { + log.Fatal("PLUGIN_ENDPOINT is not defined") } if p.KubeConfig.Token == "" { - log.Fatal("KUBE_TOKEN is not defined") + log.Fatal("PLUGIN_TOKEN is not defined") } if p.KubeConfig.Ca == "" { - log.Fatal("KUBE_CA is not defined") + log.Fatal("PLUGIN_CA is not defined") } if p.KubeConfig.Namespace == "" { p.KubeConfig.Namespace = "default" } if p.Template == "" { - log.Fatal("KUBE_TEMPLATE, or template must be defined") + log.Fatal("PLUGIN_TEMPLATE, or template must be defined") } - // // connect to Kubernetes // clientset, err := p.createKubeClient() // if err != nil { @@ -49,6 +48,7 @@ func (p Plugin) Exec() error { raw, err := ioutil.ReadFile(p.Template) if err != nil { + log.Print("Error reading template file:") return err } @@ -56,8 +56,9 @@ func (p Plugin) Exec() error { ctx := make(map[string]string) ctx["KUBE_CA"] = p.KubeConfig.Ca - ctx["KUBE_TOKEN"] = p.KubeConfig.Ca - ctx["KUBE_SERVER"] = p.KubeConfig.Ca + ctx["KUBE_TOKEN"] = p.KubeConfig.Token + ctx["KUBE_ENDPOINT"] = p.KubeConfig.Endpoint + ctx["KUBE_NAMESPACE"] = p.KubeConfig.Namespace droneEnv := os.Environ() for _, value := range droneEnv { re := regexp.MustCompile(`^(DRONE_.*)=(.*)`) diff --git a/test.sh b/test.sh index 048a50d..9d0908a 100755 --- a/test.sh +++ b/test.sh @@ -1,5 +1,12 @@ #!/bin/bash +export DRONE_COMMIT_SHA=1234567 export DRONE_BRANCH=test +export PLUGIN_CA=test +export PLUGIN_TOKEN=test +export PLUGIN_ENDPOINT=test +export PLUGIN_NAMESPACE=test +export PLUGIN_TEMPLATE=test/deployment.yaml + go build -./kubano --server=go --token=lol --ca=ho --template=test/deployment.yaml +./kubano diff --git a/test/deployment.yaml b/test/deployment.yaml index ed9684e..03689b7 100644 --- a/test/deployment.yaml +++ b/test/deployment.yaml @@ -10,11 +10,11 @@ spec: template: metadata: labels: - app: {{commit.branch}} + app: {{DRONE_COMMIT_BRANCH}} spec: containers: - name: nginx - image: 10.0.0.24:443/test:{{DRONE_BRANCH}}.{{[commit.sha]}} + image: 10.0.0.24:443/test:{{DRONE_BRANCH}}.{{DRONE_COMMIT_SHA}} ports: - containerPort: 80 imagePullSecrets: