Skip to content

Commit

Permalink
Merge branch 'dev' into feature/log
Browse files Browse the repository at this point in the history
  • Loading branch information
lulu010722 authored Apr 20, 2024
2 parents 6881b45 + be49744 commit aabb0d2
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 3 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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
.idea

# windows executable files
**/*.exe
**/*.exe

build
2 changes: 2 additions & 0 deletions cmd/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,5 @@ cd cmd
go run kubectl.go
```

kubectl apply -f \<filename\>
已经可以使用,请正确指定yaml文件的路径
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) {

}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
module minik8s

go 1.22.2
go 1.22

require (
github.com/fatih/color v1.16.0
github.com/spf13/cobra v1.8.0
gopkg.in/yaml.v3 v3.0.1
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
77 changes: 76 additions & 1 deletion pkg/kubectl/cmd/applycmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,28 @@ 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{
Expand All @@ -19,12 +37,69 @@ func ApplyCmd() *cobra.Command {
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
}

fmt.Println("the file you specified is:", path)
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")
}
}
9 changes: 9 additions & 0 deletions pkg/kubectl/cmd/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx

0 comments on commit aabb0d2

Please sign in to comment.