Skip to content

Commit ed5db3d

Browse files
refactor api and methodes
1 parent eede758 commit ed5db3d

File tree

8 files changed

+32
-33
lines changed

8 files changed

+32
-33
lines changed

chart/goserv/values.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ replicaCount: 1
44
revisionHistoryLimit: 1
55
image:
66
repository: maxencecolmant/goapi
7-
tag: kub-1
7+
tag: iks-new
88
pullPolicy: IfNotPresent
99
resources:
1010
requests:

configkub/config.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
package configkub
2+
23
import (
34
"k8s.io/client-go/kubernetes"
45
"k8s.io/client-go/kubernetes/typed/core/v1"
@@ -10,6 +11,7 @@ import (
1011

1112
func Getconfig() v1.CoreV1Interface {
1213

14+
//config, err := rest.InClusterConfig()
1315
kubeConfigPath, _ := os.LookupEnv("KUBECONFIG")
1416
config, err := clientcmd.BuildConfigFromFlags("", kubeConfigPath)
1517
if err != nil {

exec/routes.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
package exec
22

33
import (
4-
"fmt"
54
"github.com/gorilla/mux"
65
"log"
76
"main/pkg"
87
"net/http"
8+
"encoding/json"
99
)
1010

1111
func GetRoutes() {
1212
router := mux.NewRouter().StrictSlash(true)
1313
router.HandleFunc("/", project)
14-
router.HandleFunc("/api/namespaces", pkg.GetNs)
15-
router.HandleFunc("/api/pods", pkg.GetPods)
16-
router.HandleFunc("/api/nodes", pkg.GetNodes)
17-
router.HandleFunc("/api/services", pkg.GetServices)
18-
router.HandleFunc("/api/namespaces/{nsname}", pkg.GetNsName)
19-
router.HandleFunc("/api/namespaces/create/{nsname}", pkg.CreateNs)
20-
router.HandleFunc("/api/namespaces/delete/{nsname}", pkg.DeleteNs)
14+
router.HandleFunc("/api/namespaces", pkg.GetNs).Methods("GET")
15+
router.HandleFunc("/api/pods", pkg.GetPods).Methods("GET")
16+
router.HandleFunc("/api/nodes", pkg.GetNodes).Methods("GET")
17+
router.HandleFunc("/api/services", pkg.GetServices).Methods("GET")
18+
router.HandleFunc("/api/namespaces/{nsname}", pkg.GetNsName).Methods("GET")
19+
router.HandleFunc("/api/namespaces/{nsname}", pkg.CreateNs).Methods("POST")
20+
router.HandleFunc("/api/namespaces/{nsname}", pkg.DeleteNs).Methods("DELETE")
2121
log.Fatal(http.ListenAndServe(":8080", router))
2222
}
2323

2424
func project(w http.ResponseWriter, r *http.Request) {
2525
project := "api go kubernetes iks"
26-
fmt.Fprint(w, project)
26+
json.NewEncoder(w).Encode(project)
2727
}

main.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,5 @@ import (
44
"main/exec"
55
)
66
func main() {
7-
87
exec.GetRoutes()
9-
}
10-
11-
12-
13-
8+
}

pkg/namespaces.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package pkg
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"github.com/gorilla/mux"
67
v1 "k8s.io/api/core/v1"
@@ -31,7 +32,7 @@ func GetNs(w http.ResponseWriter, r *http.Request) {
3132
log.Fatal(err)
3233
}
3334

34-
fmt.Fprint( w, ns )
35+
json.NewEncoder(w).Encode(ns)
3536
}
3637

3738

@@ -46,11 +47,11 @@ func GetNsName(w http.ResponseWriter, r *http.Request) {
4647
//listOptions := m
4748
ns, err := api.Namespaces().Get(nsname,getoption )
4849
if err != nil {
49-
fmt.Fprint(w,"This namespace doesn't exist")
50+
json.NewEncoder(w).Encode("This Namespace doesn't exist")
5051
}
5152

5253

53-
fmt.Fprint( w, ns)
54+
json.NewEncoder(w).Encode(ns)
5455

5556
}
5657

@@ -74,6 +75,7 @@ func CreateNs(w http.ResponseWriter, r *http.Request) {
7475
fmt.Fprint(w,"The namespace ", nsname," already exist")
7576
}else {
7677
fmt.Fprint( w, "The namespace : ",ns.Name," have been created")
78+
json.NewEncoder(w).Encode(ns)
7779
}
7880
}
7981

@@ -93,5 +95,4 @@ func DeleteNs(w http.ResponseWriter, r *http.Request) {
9395
fmt.Fprint(w ,"The namespace ", nsname, " have been deleted")
9496
}
9597

96-
9798
}

pkg/nodes.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
package pkg
22

3-
43
import (
5-
"fmt"
6-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
7-
"log"
8-
"main/configkub"
9-
"net/http"
4+
"encoding/json"
5+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
6+
"log"
7+
"main/configkub"
8+
"net/http"
109
)
1110

1211
func GetNodes(w http.ResponseWriter, r *http.Request) {
1312
api := configkub.Getconfig()
1413
listOptions := metav1.ListOptions{}
15-
pods, err := api.Nodes().List(listOptions)
14+
nodes, err := api.Nodes().List(listOptions)
1615
if err != nil {
1716
log.Fatal(err)
1817
}
19-
fmt.Fprint( w, pods )
20-
}
18+
json.NewEncoder(w).Encode(nodes.Items)
19+
20+
}

pkg/pods.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package pkg
22

33
import (
4-
"fmt"
4+
"encoding/json"
55
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
66
"log"
77
"main/configkub"
@@ -15,7 +15,8 @@ func GetPods(w http.ResponseWriter, r *http.Request) {
1515
if err != nil {
1616
log.Fatal(err)
1717
}
18-
fmt.Fprint( w, pods )
18+
json.NewEncoder(w).Encode(pods.Items)
19+
1920

2021

2122
}

pkg/services.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package pkg
22

33
import (
4+
"encoding/json"
45
_ "encoding/json"
5-
"fmt"
66
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
77
"log"
88
"main/configkub"
@@ -26,7 +26,7 @@ func GetServices(w http.ResponseWriter, r *http.Request) {
2626
if err != nil {
2727
log.Fatal(err)
2828
}
29+
json.NewEncoder(w).Encode(services.Items)
2930

30-
fmt.Fprint(w, services.Items)
3131

3232
}

0 commit comments

Comments
 (0)