[v2.12] add more redis endpoints

This commit is contained in:
Divyansh Manchanda 2019-08-05 11:58:35 +05:30
Родитель beb2ea0e79
Коммит 24a6bd6132
4 изменённых файлов: 63 добавлений и 6 удалений

Просмотреть файл

@ -14,7 +14,7 @@ rbac:
image:
repository: divyanshm/k8s-poolprovider
tag: v2.11
tag: v2.12
pullPolicy: IfNotPresent
vsts:

Просмотреть файл

@ -68,7 +68,7 @@ spec:
spec:
serviceAccountName: pipelineprovider
containers:
- image: divyanshm/k8s-poolprovider:v2.11
- image: divyanshm/k8s-poolprovider:v2.12
name: k8s-poolprovider
command: ["/app/main"]
ports:

39
main.go
Просмотреть файл

@ -4,6 +4,7 @@ import (
"fmt"
"log"
"net/http"
"strings"
)
const (
@ -23,8 +24,10 @@ func main() {
s.HandleFunc("/delete", func(w http.ResponseWriter, r *http.Request) { KubernetesDeleteHandler(w, r) })
// Test redis
s.HandleFunc("/storageget", storageGetHandler(storage))
s.HandleFunc("/storageset", storageSetHandler(storage))
s.HandleFunc("/storageget", StorageGetHandler(storage))
s.HandleFunc("/storageset", StorageSetHandler(storage))
s.HandleFunc("/storageping", PingHandler(storage))
s.HandleFunc("/storagegetkeys", GetKeysHandler(storage))
// Start HTTP Server with request logging
log.Fatal(http.ListenAndServe(":8082", s))
@ -54,7 +57,7 @@ func KubernetesDeleteHandler(resp http.ResponseWriter, req *http.Request) {
fmt.Fprintf(resp, "Response: %s", pods)
}
func storageGetHandler(s Storage) http.HandlerFunc {
func StorageGetHandler(s Storage) http.HandlerFunc {
return func(resp http.ResponseWriter, req *http.Request) {
key := req.URL.Query()["key"]
res, err := s.Get(key[0])
@ -68,7 +71,7 @@ func storageGetHandler(s Storage) http.HandlerFunc {
}
}
func storageSetHandler(s Storage) http.HandlerFunc {
func StorageSetHandler(s Storage) http.HandlerFunc {
return func(resp http.ResponseWriter, req *http.Request) {
key := req.URL.Query()["key"]
value := req.URL.Query()["value"]
@ -81,4 +84,32 @@ func storageSetHandler(s Storage) http.HandlerFunc {
resp.WriteHeader(http.StatusOK)
fmt.Fprintln(resp, "Value set")
}
}
func PingHandler(s Storage) http.HandlerFunc {
return func(resp http.ResponseWriter, req *http.Request) {
_, err := s.Ping()
if err != nil {
resp.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(resp, err.Error())
return
}
res := "pong"
resp.WriteHeader(http.StatusOK)
fmt.Fprintln(resp, res)
}
}
func GetKeysHandler(s Storage) http.HandlerFunc {
return func(resp http.ResponseWriter, req *http.Request) {
res, err := s.GetKeys("*")
if err != nil {
resp.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(resp, err.Error())
return
}
resp.WriteHeader(http.StatusOK)
fmt.Fprintln(resp, strings.Join(res, ", "))
}
}

Просмотреть файл

@ -10,6 +10,7 @@ type Storage interface {
Ping() (string, error)
Get(string) (string, error)
Set(string, string) error
GetKeys(string) ([]string, error)
}
type RedisStorage struct {
@ -61,4 +62,29 @@ func (r *RedisStorage) Set(key string, value string) error {
_, err := conn.Do("SET", key, value)
return err
}
func (r *RedisStorage) GetKeys(pattern string) ([]string, error) {
conn := r.connectionPool.Get()
defer conn.Close()
iter := 0
keys := []string{}
for {
arr, err := redis.Values(conn.Do("SCAN", iter, "MATCH", pattern))
if err != nil {
return keys, err
}
iter, _ = redis.Int(arr[0], nil)
k, _ := redis.Strings(arr[1], nil)
keys = append(keys, k...)
if iter == 0 {
break
}
}
return keys, nil
}