-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
318 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
VERSION=v0.2.6 | ||
VERSION=v0.3.0 | ||
|
||
tag: | ||
@git tag -a ${VERSION} -m "version ${VERSION}" && git push origin ${VERSION} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/moonwalker/comet/internal/exec" | ||
"github.com/moonwalker/comet/internal/log" | ||
"github.com/moonwalker/comet/internal/parser" | ||
) | ||
|
||
var ( | ||
kubeCmd = &cobra.Command{ | ||
Use: "kube [stack]", | ||
Short: "Kubeconfig for stack", | ||
Aliases: []string{"kubeconfig"}, | ||
RunE: kube, | ||
Args: cobra.ExactArgs(1), | ||
} | ||
) | ||
|
||
func init() { | ||
rootCmd.AddCommand(kubeCmd) | ||
} | ||
|
||
func kube(cmd *cobra.Command, args []string) error { | ||
executor, err := exec.GetExecutor(config) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
stacks, err := parser.LoadStacks(config.StacksDir) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
stack, err := stacks.GetStack(args[0]) | ||
if stack == nil { | ||
return err | ||
} | ||
|
||
kubeconfig, err := stack.Kubeconfig.Render(config, stacks, executor, stack.Name) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Println(kubeconfig) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package schema | ||
|
||
import ( | ||
"bytes" | ||
"text/template" | ||
) | ||
|
||
type ( | ||
Kubeconfig struct { | ||
Current int `json:"current"` | ||
Clusters []*KubeconfgCluster `json:"clusters"` | ||
} | ||
|
||
KubeconfgCluster struct { | ||
Context string `json:"context"` | ||
Host string `json:"host"` | ||
Cert string `json:"cert"` | ||
ExecApiVersion string `json:"exec_apiversion"` | ||
ExecCommand string `json:"exec_command"` | ||
ExecArgs []string `json:"exec_args"` | ||
} | ||
) | ||
|
||
const ( | ||
KubeconfigDefaultApiVersion = "client.authentication.k8s.io/v1beta1" | ||
) | ||
|
||
func (k *Kubeconfig) Render(config *Config, stacks *Stacks, executor Executor, stackName string) (string, error) { | ||
if len(k.Clusters) == 0 { | ||
return "", nil | ||
} | ||
|
||
if k.Current < 0 || k.Current >= len(k.Clusters) { | ||
k.Current = 0 | ||
} | ||
|
||
for _, c := range k.Clusters { | ||
if len(c.ExecApiVersion) == 0 { | ||
c.ExecApiVersion = KubeconfigDefaultApiVersion | ||
} | ||
} | ||
|
||
t, err := NewTemplater(config, stacks, executor, stackName) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
err = t.Any(k, nil) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
tmpl, err := template.New("k").Parse(kubeconfigTemplate) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
var b bytes.Buffer | ||
err = tmpl.Execute(&b, k) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return b.String(), nil | ||
} | ||
|
||
const kubeconfigTemplate = `apiVersion: v1 | ||
kind: Config | ||
current-context: {{ (index .Clusters .Current).Context }} | ||
contexts: | ||
{{- range .Clusters }} | ||
- name: {{ .Context }} | ||
context: | ||
cluster: {{ .Context }} | ||
user: {{ .Context }} | ||
{{- end }} | ||
clusters: | ||
{{- range .Clusters }} | ||
- name: {{ .Context }} | ||
cluster: | ||
server: {{ .Host }} | ||
certificate-authority-data: {{ .Cert }} | ||
{{- end }} | ||
users: | ||
{{- range .Clusters }} | ||
- name: {{ .Context }} | ||
user: | ||
exec: | ||
apiVersion: {{ .ExecApiVersion }} | ||
command: {{ .ExecCommand }} | ||
{{- if .ExecArgs }} | ||
args: | ||
{{- range .ExecArgs }} | ||
- {{ . }} | ||
{{- end }} | ||
{{- end }} | ||
{{- end }}` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.