-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support to install, remove and search apps (#2)
Signed-off-by: Prasad Ghangal <[email protected]>
- Loading branch information
1 parent
55e699e
commit f850739
Showing
9 changed files
with
587 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
.DEFAULT_GOAL := build | ||
.PHONY: build pre-build system-check cli | ||
|
||
#Build the binary | ||
build: pre-build | ||
@cd cmd/cli;GOOS_VAL=$(shell go env GOOS) GOARCH_VAL=$(shell go env GOARCH) go build -o $(shell go env GOPATH)/bin/kbrew | ||
@echo "Build completed successfully" | ||
|
||
cli: | ||
@cd cmd/cli;GOOS_VAL=$(shell go env GOOS) GOARCH_VAL=$(shell go env GOARCH) go build -o $(shell go env GOPATH)/bin/kbrew | ||
@echo "cli generated successfully" | ||
|
||
#system checks | ||
system-check: | ||
@echo "Checking system information" | ||
@if [ -z "$(shell go env GOOS)" ] || [ -z "$(shell go env GOARCH)" ] ; \ | ||
then \ | ||
echo 'ERROR: Could not determine the system architecture.' && exit 1 ; \ | ||
else \ | ||
echo 'GOOS: $(shell go env GOOS)' ; \ | ||
echo 'GOARCH: $(shell go env GOARCH)' ; \ | ||
echo 'System information checks passed.'; \ | ||
fi ; | ||
|
||
#Pre-build checks | ||
pre-build: system-check |
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,131 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
"text/tabwriter" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/vishal-biyani/kbrew/pkg/apps" | ||
"github.com/vishal-biyani/kbrew/pkg/apps/raw" | ||
) | ||
|
||
type method string | ||
|
||
const ( | ||
create method = "create" | ||
delete method = "delete" | ||
) | ||
|
||
var ( | ||
rootCmd = &cobra.Command{ | ||
Use: "kbrew", | ||
Short: "Homebrew for your Kubernetes applications", | ||
Long: `TODO: Long description`, | ||
} | ||
|
||
installCmd = &cobra.Command{ | ||
Use: "install [NAME]", | ||
Short: "Install application", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return manageApp(create, args) | ||
}, | ||
} | ||
|
||
removeCmd = &cobra.Command{ | ||
Use: "remove [NAME]", | ||
Short: "Remove application", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return manageApp(delete, args) | ||
}, | ||
} | ||
|
||
searchCmd = &cobra.Command{ | ||
Use: "search [NAME]", | ||
Short: "Search application", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return search(args) | ||
}, | ||
} | ||
) | ||
|
||
func init() { | ||
rootCmd.AddCommand(installCmd) | ||
rootCmd.AddCommand(removeCmd) | ||
rootCmd.AddCommand(searchCmd) | ||
} | ||
|
||
func main() { | ||
Execute() | ||
} | ||
|
||
// Execute executes the main command | ||
func Execute() error { | ||
return rootCmd.Execute() | ||
} | ||
|
||
func manageApp(m method, args []string) error { | ||
var app apps.App | ||
var err error | ||
for _, a := range args { | ||
app, err = raw.New(a) | ||
if err != nil { | ||
// TODO: Check other app types | ||
return err | ||
} | ||
data, err := app.Manifest(context.Background(), nil) | ||
if err != nil { | ||
return err | ||
} | ||
//fmt.Printf("Data:: \n%s\n", data) | ||
if err := executeCommand(m, data); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func search(args []string) error { | ||
ctx := context.Background() | ||
// List raw apps | ||
rawApps, err := raw.List(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
//TODO: Support for other app types | ||
if len(args) == 0 { | ||
printList(rawApps) | ||
return nil | ||
} | ||
result := []string{} | ||
for _, a := range rawApps { | ||
if strings.HasPrefix(a, args[0]) { | ||
result = append(result, a) | ||
} | ||
} | ||
printList(result) | ||
return nil | ||
} | ||
|
||
func printList(list []string) { | ||
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.TabIndent) | ||
fmt.Fprintln(w, "NAME\tVERSION") | ||
for _, l := range list { | ||
fmt.Fprintln(w, fmt.Sprintf("%s\t%s", l, "NA")) | ||
} | ||
w.Flush() | ||
|
||
} | ||
|
||
func executeCommand(m method, data []byte) error { | ||
// Generate code | ||
c := exec.Command("kubectl", string(m), "-f", "-") | ||
c.Stdin = strings.NewReader(string(data)) | ||
c.Stdout = os.Stdout | ||
c.Stderr = os.Stderr | ||
return c.Run() | ||
} |
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,10 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func main() { | ||
matchVersionKubeConfigFlags := cmdutil.NewMatchVersionFlags(kubeConfigFlags) | ||
f := cmdutil.NewFactory(matchVersionKubeConfigFlags) | ||
} |
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,23 @@ | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: nginx | ||
spec: | ||
replicas: 3 | ||
selector: | ||
matchLabels: | ||
app: nginx | ||
template: | ||
metadata: | ||
labels: | ||
app: nginx | ||
spec: | ||
containers: | ||
- image: nginx:1.12 | ||
imagePullPolicy: IfNotPresent | ||
name: web | ||
ports: | ||
- containerPort: 80 | ||
name: http | ||
protocol: TCP | ||
|
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,14 @@ | ||
apiVersion: v1 | ||
kind: Service | ||
metadata: | ||
name: nginx-service | ||
labels: | ||
app: nginx | ||
spec: | ||
ports: | ||
- name: server | ||
port: 80 | ||
protocol: TCP | ||
type: ClusterIP | ||
selector: | ||
app: nginx |
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,10 @@ | ||
module github.com/vishal-biyani/kbrew | ||
|
||
go 1.15 | ||
|
||
require ( | ||
github.com/pkg/errors v0.9.1 | ||
github.com/spf13/cobra v1.1.1 | ||
github.com/urfave/cli v1.22.5 | ||
github.com/urfave/cli/v2 v2.3.0 | ||
) |
Oops, something went wrong.