-
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.
Merge branch 'dev' into feature/pods
- Loading branch information
Showing
12 changed files
with
468 additions
and
212 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,27 @@ | ||
name: test all cases and build | ||
|
||
on: | ||
push: | ||
branches: ["master", "dev", "feature/*"] | ||
|
||
pull_request: | ||
branches: ["master", "dev", "feature/*"] | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v2 | ||
|
||
- name: Set up Go | ||
uses: actions/setup-go@v2 | ||
with: | ||
go-version: "1.17" | ||
|
||
- name: Run tests | ||
run: go test ./... | ||
|
||
- name: Build | ||
run: go build -o ./build/kubectl ./cmd |
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 |
---|---|---|
|
@@ -2,4 +2,9 @@ | |
.vscode | ||
|
||
# jetbrain related files | ||
.idea | ||
.idea | ||
|
||
# windows executable files | ||
**/*.exe | ||
|
||
build |
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,41 @@ | ||
# kubectl接口 | ||
|
||
|
||
|
||
目前支持的接口如下 | ||
|
||
kubectl apply -f \<filename\> (可带-a或--all标志) | ||
|
||
kubectl get pod | ||
|
||
kubectl get deployment | ||
|
||
kubectl get service | ||
|
||
kubectl get node | ||
|
||
kebectl describe pod \<pod_name\> | ||
|
||
kubectl describe deployment \<deployment_name\> | ||
|
||
kubectl describe service \<service_name\> | ||
|
||
kubectl describe node \<node_name\> | ||
|
||
kebectl delete pod \<pod_name\> | ||
|
||
kubectl delete deployment \<deployment_name\> | ||
|
||
kubectl delete service \<service_name\> | ||
|
||
|
||
|
||
如要使用上述命令,需要先构建所有文件 | ||
|
||
``` | ||
cd cmd | ||
go run kubectl.go | ||
``` | ||
|
||
kubectl apply -f \<filename\> | ||
已经可以使用,请正确指定yaml文件的路径 |
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,27 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"minik8s/pkg/kubectl/cmd" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
func main() { | ||
rootCmd := &cobra.Command{ | ||
Use: "kubectl", | ||
Long: "Welcome to use kubectl CLI tool!", | ||
Run: nil, | ||
} | ||
|
||
rootCmd.AddCommand(cmd.GetCmd()) | ||
rootCmd.AddCommand(cmd.ApplyCmd()) | ||
rootCmd.AddCommand(cmd.DeleteCmd()) | ||
rootCmd.AddCommand(cmd.DescribeCmd()) | ||
|
||
if err := rootCmd.Execute(); err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
} |
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,9 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
|
||
} |
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,105 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
type Resource struct { | ||
ApiVersion string `yaml:"apiVersion"` | ||
Kind string `yaml:"kind"` | ||
Metadata struct { | ||
Name string `yaml:"name"` | ||
} `yaml:"metadata"` | ||
Spec struct { | ||
Replicas int `yaml:"replicas"` | ||
Selector struct { | ||
MatchLabels struct { | ||
App string `yaml:"app"` | ||
} `yaml:"matchLabels"` | ||
} `yaml:"selector"` | ||
} `yaml:"spec"` | ||
} | ||
|
||
func ApplyCmd() *cobra.Command { | ||
|
||
applyCmd := &cobra.Command{ | ||
Use: "apply", | ||
Short: "apply a yaml file to create a resource", | ||
Run: applyCmdHandler, | ||
} | ||
|
||
applyCmd.Flags().StringP("file", "f", "", "specify a file name") | ||
|
||
return applyCmd | ||
} | ||
|
||
func parseYamlFileToResource(file *os.File) *Resource { | ||
decoder := yaml.NewDecoder(file) | ||
resource := &Resource{} | ||
|
||
err := decoder.Decode(resource) | ||
if err != nil { | ||
fmt.Println("Error decoding yaml:", err) | ||
return nil | ||
} | ||
fmt.Printf("Decode yaml successfully, resource:%+v\n", resource) | ||
return resource | ||
} | ||
|
||
func applyCmdHandler(cmd *cobra.Command, args []string) { | ||
path, err := cmd.Flags().GetString("file") | ||
if err != nil { | ||
fmt.Println("Error getting flags:", err) | ||
return | ||
} | ||
|
||
file, err := os.Open(path) | ||
if err != nil { | ||
fmt.Println("Error opening file:", err) | ||
return | ||
} | ||
|
||
defer file.Close() | ||
|
||
resource := parseYamlFileToResource(file) | ||
|
||
switch resource.Kind { | ||
case "Pod": | ||
applyPodHandler(resource) | ||
case "Service": | ||
applyServiceHandler(resource) | ||
case "Deployment": | ||
applyDeploymentHandler(resource) | ||
case "ReplicaSet": | ||
applyReplicaSetHandler(resource) | ||
case "StatefulSet": | ||
applyStatefulSetHandler(resource) | ||
default: | ||
fmt.Println("Unknown resource kind") | ||
} | ||
|
||
} | ||
|
||
func applyPodHandler(resource *Resource) { | ||
fmt.Println("creating or updating pod") | ||
} | ||
|
||
func applyServiceHandler(resource *Resource) { | ||
fmt.Println("creating or updating service") | ||
} | ||
|
||
func applyDeploymentHandler(resource *Resource) { | ||
fmt.Println("creating or updating deployment") | ||
} | ||
|
||
func applyReplicaSetHandler(resource *Resource) { | ||
fmt.Println("creating or updating replicaset") | ||
} | ||
|
||
func applyStatefulSetHandler(resource *Resource) { | ||
fmt.Println("creating or updating statefulset") | ||
} |
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,25 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestParseYamlFileToResource(t *testing.T) { | ||
|
||
file, err := os.Open("test.yaml") | ||
if err != nil { | ||
fmt.Println("err opening yaml file") | ||
} | ||
|
||
resource := parseYamlFileToResource(file) | ||
|
||
if resource.ApiVersion != "v1" || | ||
resource.Kind != "Pod" || | ||
resource.Metadata.Name != "nginx" || | ||
resource.Spec.Replicas != 3 || | ||
resource.Spec.Selector.MatchLabels.App != "nginx" { | ||
t.Errorf("err parse yaml file\n") | ||
} | ||
} |
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,54 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
func DeleteCmd() *cobra.Command { | ||
|
||
var deleteCmd, deletePodCmd, deleteDeploymentCmd, deleteServiceCmd *cobra.Command | ||
|
||
deleteCmd = &cobra.Command{ | ||
Use: "delete", | ||
Short: "delete a resourse", | ||
Run: nil, | ||
} | ||
|
||
deletePodCmd = &cobra.Command{ | ||
Use: "pod", | ||
Short: "delete pod", | ||
Run: deletePodCmdHandler, | ||
} | ||
|
||
deleteDeploymentCmd = &cobra.Command{ | ||
Use: "deployment", | ||
Short: "delete deployment", | ||
Run: deleteDeploymentCmdHandler, | ||
} | ||
|
||
deleteServiceCmd = &cobra.Command{ | ||
Use: "service", | ||
Short: "delete service", | ||
Run: deleteServiceCmdHandler, | ||
} | ||
|
||
deleteCmd.AddCommand(deletePodCmd) | ||
deleteCmd.AddCommand(deleteDeploymentCmd) | ||
deleteCmd.AddCommand(deleteServiceCmd) | ||
|
||
return deleteCmd | ||
} | ||
|
||
func deletePodCmdHandler(cmd *cobra.Command, args []string) { | ||
fmt.Println("pod name:", args) | ||
} | ||
|
||
func deleteDeploymentCmdHandler(cmd *cobra.Command, args []string) { | ||
fmt.Println("deployment name:", args) | ||
} | ||
|
||
func deleteServiceCmdHandler(cmd *cobra.Command, args []string) { | ||
fmt.Println("service name:", args) | ||
} |
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,65 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
func DescribeCmd() *cobra.Command { | ||
|
||
var describeCmd, describePodCmd, describeDeploymentCmd, describeServiceCmd, describeNodeCmd *cobra.Command | ||
|
||
describeCmd = &cobra.Command{ | ||
Use: "describe", | ||
Short: "describe the config of some resourse", | ||
Run: nil, | ||
} | ||
|
||
describePodCmd = &cobra.Command{ | ||
Use: "pod", | ||
Short: "describe a pod", | ||
Run: describePodCmdHandler, | ||
} | ||
|
||
describeDeploymentCmd = &cobra.Command{ | ||
Use: "deployment", | ||
Short: "describe a deployment", | ||
Run: describeDeploymentCmdHandler, | ||
} | ||
|
||
describeServiceCmd = &cobra.Command{ | ||
Use: "service", | ||
Short: "describe a service", | ||
Run: describeServiceCmdHandler, | ||
} | ||
|
||
describeNodeCmd = &cobra.Command{ | ||
Use: "node", | ||
Short: "describe a node", | ||
Run: describeNodeCmdHandler, | ||
} | ||
|
||
describeCmd.AddCommand(describePodCmd) | ||
describeCmd.AddCommand(describeDeploymentCmd) | ||
describeCmd.AddCommand(describeServiceCmd) | ||
describeCmd.AddCommand(describeNodeCmd) | ||
|
||
return describeCmd | ||
} | ||
|
||
func describePodCmdHandler(cmd *cobra.Command, args []string) { | ||
fmt.Println("pod name:", args) | ||
} | ||
|
||
func describeDeploymentCmdHandler(cmd *cobra.Command, args []string) { | ||
fmt.Println("deployment name:", args) | ||
} | ||
|
||
func describeServiceCmdHandler(cmd *cobra.Command, args []string) { | ||
fmt.Println("service name:", args) | ||
} | ||
|
||
func describeNodeCmdHandler(cmd *cobra.Command, args []string) { | ||
fmt.Println("node name:", args) | ||
} |
Oops, something went wrong.