Skip to content

Commit

Permalink
Merge branch 'dev' into feature/pods
Browse files Browse the repository at this point in the history
  • Loading branch information
Doris-xm authored Apr 20, 2024
2 parents 0497851 + be49744 commit 859b60c
Show file tree
Hide file tree
Showing 12 changed files with 468 additions and 212 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/test.yaml
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
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@
.vscode

# jetbrain related files
.idea
.idea

# windows executable files
**/*.exe

build
41 changes: 41 additions & 0 deletions cmd/README.md
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文件的路径
27 changes: 27 additions & 0 deletions cmd/kubectl.go
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)
}
}
9 changes: 9 additions & 0 deletions cmd/kubectl_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

import (
"testing"
)

func TestMain(m *testing.M) {

}
222 changes: 11 additions & 211 deletions go.sum

Large diffs are not rendered by default.

105 changes: 105 additions & 0 deletions pkg/kubectl/cmd/applycmd.go
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")
}
25 changes: 25 additions & 0 deletions pkg/kubectl/cmd/applycmd_test.go
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")
}
}
54 changes: 54 additions & 0 deletions pkg/kubectl/cmd/deletecmd.go
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)
}
65 changes: 65 additions & 0 deletions pkg/kubectl/cmd/describecmd.go
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)
}
Loading

0 comments on commit 859b60c

Please sign in to comment.