Skip to content

Commit 4225d14

Browse files
authored
Merge pull request #7 from kalgurn/feat/refactoring
Command module refactored for #6
2 parents c5a830f + 07f5741 commit 4225d14

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1289
-376
lines changed

.github/dependabot.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
version: 2
2+
updates:
3+
- package-ecosystem: "gomod"
4+
directory: "/"
5+
schedule:
6+
interval: "daily"

.github/workflows/release.yaml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
env:
2424
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2525
WITH_V: true
26-
DRY_RUN: false
26+
DRY_RUN: true
2727

2828
test:
2929
name: Test
@@ -130,6 +130,14 @@ jobs:
130130
- name: Release Notes
131131
run:
132132
git log $(git describe HEAD~ --tags --abbrev=0)..HEAD --pretty='format:* %h %s%n * %an <%ae>' --no-merges >> ".github/RELEASE_TEMPLATE.md"
133+
134+
- name: Bump version and push tag
135+
uses: anothrNick/github-tag-action@1.35.0
136+
id: tag
137+
env:
138+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
139+
WITH_V: true
140+
DRY_RUN: false
133141

134142
- name: Download artifacts
135143
id: download

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,6 @@ vendor/
1717
*-test.yaml
1818

1919
# Intermediate binary
20-
kubeconfig-manager
20+
kubeconfig-manager
21+
22+
.vscode/

Dockerfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FROM golang:latest
2+
3+
COPY . /app
4+
WORKDIR /app
5+
6+
RUN go mod download
7+
RUN go test -v ./...

build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
PACKAGE_NAME="kubeconfig-manager"
44
MAIN_PACKAGE="cmd/${PACKAGE_NAME}/main.go"
5-
COMMAND_PACKAGE="github.com/kalgurn/${PACKAGE_NAME}/internal/command"
5+
COMMAND_PACKAGE="github.com/kalgurn/${PACKAGE_NAME}/internal/cmd"
66

77
BINARY_NAME="${PACKAGE_NAME}-${GOOS}-${GOARCH}"
88

cmd/kubeconfig-manager/main.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package main
22

33
import (
4-
"os"
5-
6-
"github.com/kalgurn/kubeconfig-manager/internal/command"
4+
"github.com/kalgurn/kubeconfig-manager/internal/cmd"
75
)
86

97
func main() {
108

11-
command.Run(os.Args)
9+
cmd.Execute()
1210

1311
}

go.mod

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ module github.com/kalgurn/kubeconfig-manager
33
go 1.16
44

55
require (
6-
github.com/pborman/getopt/v2 v2.1.0
6+
facette.io/natsort v0.0.0-20181210072756-2cd4dd1e2dcb // indirect
7+
github.com/sirupsen/logrus v1.8.1 // indirect
8+
github.com/spf13/cobra v1.2.1
79
k8s.io/client-go v0.21.2
810
)

go.sum

Lines changed: 275 additions & 13 deletions
Large diffs are not rendered by default.

internal/cmd/add.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/kalgurn/kubeconfig-manager/internal/kubeconfig"
8+
log "github.com/kalgurn/kubeconfig-manager/internal/logger"
9+
"github.com/spf13/cobra"
10+
"k8s.io/client-go/tools/clientcmd/api"
11+
)
12+
13+
func init() {
14+
rootCmd.AddCommand(addCmd)
15+
}
16+
17+
func AddComposer(cmd *cobra.Command, args []string) error {
18+
path := args[0]
19+
if _, err := os.Stat(path); os.IsNotExist(err) {
20+
return fmt.Errorf("no kubeconfig at path %s", path)
21+
}
22+
importedConfig := kubeconfig.Load(path)
23+
err := Add(importedConfig)
24+
return err
25+
26+
}
27+
func Add(cfg *api.Config) error {
28+
kubeconfigPath, err := kubeconfig.GetConfigPath()
29+
if err != nil {
30+
return fmt.Errorf("%s", err)
31+
}
32+
defaultCfg := kubeconfig.Load(kubeconfigPath)
33+
logger = log.NewLogger(Verbose)
34+
logger.Debug("merging config from ", cfg, " with ", kubeconfigPath)
35+
kubeconfig.Merge(cfg, defaultCfg)
36+
logger.Debug("saving config to ", kubeconfigPath)
37+
kubeconfig.Save(kubeconfigPath, defaultCfg)
38+
logger.Info("config saved")
39+
return nil
40+
}
41+
42+
var addCmd = &cobra.Command{
43+
Use: "add [path to kubeconfig]",
44+
Short: "adding kubeconfig context, cluster and user from external to current",
45+
Long: "adding kubeconfig context, cluster and user from external to current",
46+
Args: cobra.MinimumNArgs(1),
47+
RunE: func(cmd *cobra.Command, args []string) error {
48+
return AddComposer(cmd, args)
49+
},
50+
}

internal/cmd/addRancher.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/kalgurn/kubeconfig-manager/internal/kubeconfig"
8+
log "github.com/kalgurn/kubeconfig-manager/internal/logger"
9+
"github.com/kalgurn/kubeconfig-manager/internal/rancherClient"
10+
"github.com/spf13/cobra"
11+
)
12+
13+
var (
14+
URL string
15+
Token string
16+
Cluster string
17+
)
18+
19+
func init() {
20+
addRancherCmd.Flags().StringVarP(&URL, "url", "u", "", "URL to a Rancher")
21+
addRancherCmd.Flags().StringVarP(&Cluster, "cluster", "c", "", "URL to a Rancher")
22+
addRancherCmd.Flags().StringVarP(&Token, "token", "t", "", "token to a Rancher")
23+
addRancherCmd.MarkFlagRequired("url")
24+
addRancherCmd.MarkFlagRequired("cluster")
25+
addCmd.AddCommand(addRancherCmd)
26+
}
27+
28+
func AddRancherComposer(cmd *cobra.Command, args []string) error {
29+
URL, _ = cmd.Flags().GetString("url")
30+
Token, _ = cmd.Flags().GetString("token")
31+
Cluster, _ = cmd.Flags().GetString("cluter")
32+
33+
err := AddRancher(URL, Cluster, Token)
34+
35+
return err
36+
}
37+
func AddRancher(url string, cluster string, token string) error {
38+
kubeconfigPath, err := kubeconfig.GetConfigPath()
39+
if err != nil {
40+
return fmt.Errorf("%s", err)
41+
}
42+
defaultCfg := kubeconfig.Load(kubeconfigPath)
43+
logger = log.NewLogger(Verbose)
44+
logger.Debug("downloading kubeconfig for a cluster", Cluster, "from", URL)
45+
rancherCfg, err := rancherClient.GetRancherConfig(url, cluster, token)
46+
if err != nil {
47+
fmt.Println(err)
48+
os.Exit(1)
49+
}
50+
logger.Debug("merging config with ", kubeconfigPath)
51+
kubeconfig.Merge(rancherCfg, defaultCfg)
52+
logger.Debug("saving config to ", kubeconfigPath)
53+
kubeconfig.Save(kubeconfigPath, defaultCfg)
54+
logger.Info("config saved")
55+
return nil
56+
}
57+
58+
var addRancherCmd = &cobra.Command{
59+
Use: "rancher --url=[rancher url] --token=[rancher token]",
60+
Short: "adding kubeconfig downloaded from a specific rancher installation",
61+
Long: "adding kubeconfig downloaded from a specific rancher installation",
62+
RunE: func(cmd *cobra.Command, args []string) error {
63+
return AddRancherComposer(cmd, args)
64+
},
65+
}

0 commit comments

Comments
 (0)