-
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 pull request #7 from CloudOS-Group3/feature/etcd
Feature/etcd
- Loading branch information
Showing
8 changed files
with
277 additions
and
4 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
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
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
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 etcd | ||
|
||
import ( | ||
"context" | ||
clientv3 "go.etcd.io/etcd/client/v3" | ||
"minik8s/pkg/util" | ||
"minik8s/util/log" | ||
) | ||
|
||
type Store struct { | ||
etcdClient *clientv3.Client | ||
} | ||
|
||
func NewStore() *Store { | ||
cli, err := util.GetEtcdClient() | ||
if err != nil { | ||
log.Fatal("Failed to connect to etcd: %v", err.Error()) | ||
return nil | ||
} | ||
|
||
return &Store{etcdClient: cli} | ||
} | ||
|
||
func (store Store) PutEtcdPair(key, value string) bool { | ||
// get an interface to access kv store | ||
kv := clientv3.NewKV(store.etcdClient) | ||
|
||
// then put the pair to kv store | ||
_, err := kv.Put(context.TODO(), key, value, clientv3.WithPrevKV()) | ||
if err != nil { | ||
log.Fatal("Failed to put pair in kv-store: %v", err.Error()) | ||
return false | ||
} | ||
|
||
return true | ||
} | ||
|
||
func (store Store) GetEtcdPair(key string) (value string) { | ||
kv := clientv3.NewKV(store.etcdClient) | ||
|
||
resp, err := kv.Get(context.TODO(), key) | ||
if err != nil { | ||
log.Fatal("Failed to get pair in kv-store: %v", err.Error()) | ||
return "" | ||
} | ||
|
||
if len(resp.Kvs) == 0 { | ||
log.Warn("key %s not found in kv-store", key) | ||
return "" | ||
} | ||
|
||
return string(resp.Kvs[0].Value) | ||
} | ||
|
||
func (store Store) DeleteEtcdPair(key string) bool { | ||
kv := clientv3.NewKV(store.etcdClient) | ||
|
||
_, err := kv.Delete(context.TODO(), key) | ||
if err != nil { | ||
log.Fatal("Failed to delete pair in kv-store: %v", err.Error()) | ||
return false | ||
} | ||
|
||
return true | ||
} |
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,81 @@ | ||
package etcd | ||
|
||
import ( | ||
"encoding/json" | ||
"minik8s/pkg/api" | ||
"minik8s/util/log" | ||
) | ||
|
||
func (store Store) PutPod(value api.Pod) bool { | ||
// generate key | ||
key := "" | ||
if value.Metadata.NameSpace == "" { | ||
key = "/registry/pods/default/" + value.Metadata.Name | ||
} else { | ||
key = "/registry/pods/" + value.Metadata.NameSpace + "/" + value.Metadata.Name | ||
} | ||
|
||
// check whether the name is already exist | ||
res := store.GetEtcdPair(key) | ||
if len(res) != 0 { | ||
log.Info("Pod name %s already exists", key) | ||
return false | ||
} | ||
|
||
// marshal pod as a json | ||
jsonValue, err := json.Marshal(value) | ||
if err != nil { | ||
log.Error("Error marshalling pod json %v", err) | ||
return false | ||
} | ||
|
||
// put json in etcd | ||
return store.PutEtcdPair(key, string(jsonValue)) | ||
} | ||
|
||
func (store Store) GetPod(namespace, name string) (api.Pod, bool) { | ||
// generate key | ||
key := "" | ||
if namespace == "" { | ||
key = "/registry/pods/default/" + name | ||
} else { | ||
key = "/registry/pods/" + namespace + "/" + name | ||
} | ||
|
||
// get json in etcd | ||
res := store.GetEtcdPair(key) | ||
|
||
// if not exist | ||
if len(res) == 0 { | ||
log.Info("Pod %s not found", key) | ||
return api.Pod{}, false | ||
} | ||
|
||
// unmarshal json | ||
var pod api.Pod | ||
err := json.Unmarshal([]byte(res), &pod) | ||
if err != nil { | ||
log.Error("Error unmarshalling pod json %v", err) | ||
return api.Pod{}, false | ||
} | ||
return pod, true | ||
} | ||
|
||
func (store Store) DeletePod(namespace, name string) bool { | ||
// generate key | ||
key := "" | ||
if namespace == "" { | ||
key = "/registry/pods/default/" + name | ||
} else { | ||
key = "/registry/pods/" + namespace + "/" + name | ||
} | ||
|
||
// check whether key is exist | ||
res := store.GetEtcdPair(key) | ||
if len(res) == 0 { | ||
log.Info("Pod %s not found", key) | ||
return false | ||
} | ||
|
||
return store.DeleteEtcdPair(key) | ||
} |
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 etcd | ||
|
||
var EtcdStore *Store = nil | ||
|
||
func init() { | ||
// create an etcd store | ||
newStore := NewStore() | ||
EtcdStore = newStore | ||
} |
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,14 @@ | ||
package util | ||
|
||
import ( | ||
clientv3 "go.etcd.io/etcd/client/v3" | ||
"time" | ||
) | ||
|
||
func GetEtcdClient() (*clientv3.Client, error) { | ||
return clientv3.New( | ||
clientv3.Config{ | ||
Endpoints: []string{"http://localhost:2379"}, | ||
DialTimeout: 2 * time.Second, | ||
}) | ||
} |
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,48 @@ | ||
package etcd | ||
|
||
import ( | ||
"minik8s/pkg/api" | ||
"minik8s/pkg/etcd" | ||
"testing" | ||
) | ||
|
||
func TestEtcd(t *testing.T) { | ||
newPod := &api.Pod{ | ||
Metadata: api.ObjectMeta{ | ||
Name: "test-pod", | ||
NameSpace: "default", | ||
}, | ||
Spec: api.PodSpec{ | ||
Containers: []api.Container{ | ||
{ | ||
Name: "test-container1", | ||
Image: "docker.io/library/nginx:latest", | ||
ImagePullPolicy: api.PullPolicyIfNotPresent, | ||
}, | ||
{ | ||
Name: "test-container2", | ||
Image: "docker.io/library/nginx:latest", | ||
ImagePullPolicy: api.PullPolicyIfNotPresent, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
res := etcd.EtcdStore.PutPod(*newPod) | ||
if res != true { | ||
t.Errorf("etcd put pod fail") | ||
} | ||
|
||
pod, res := etcd.EtcdStore.GetPod("default", "test-pod") | ||
if res != true { | ||
t.Errorf("etcd get pod fail") | ||
} | ||
if pod.Metadata.Name != "test-pod" && pod.Metadata.NameSpace != "default" { | ||
t.Errorf("etcd get pod fail") | ||
} | ||
|
||
res = etcd.EtcdStore.DeletePod("default", "test-pod") | ||
if res != true { | ||
t.Errorf("etcd delete pod fail") | ||
} | ||
} |