-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(container): Added inCluster related API and xenon checker
- Loading branch information
1 parent
75a0b9b
commit 4b06b91
Showing
5 changed files
with
210 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
checks=[ "all", "-ST1000", "-ST1003", "-ST1016", "-ST1020", "-ST1021", "-ST1022", "-ST1001"] |
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,88 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"os" | ||
|
||
. "github.com/radondb/radondb-mysql-kubernetes/utils" | ||
) | ||
|
||
var ( | ||
ns string | ||
podName string | ||
) | ||
|
||
func init() { | ||
ns = os.Getenv("NAMESPACE") | ||
podName = os.Getenv("POD_NAME") | ||
} | ||
|
||
func main() { | ||
switch os.Args[1] { | ||
case "leaderStart": | ||
if err := leaderStart(); err != nil { | ||
log.Fatalf("leaderStart failed: %v", err) | ||
} | ||
case "leaderStop": | ||
if err := leaderStop(); err != nil { | ||
log.Fatalf("leaderStop failed: %v", err) | ||
} | ||
case "liveness": | ||
if err := liveness(); err != nil { | ||
log.Fatalf("liveness failed: %v", err) | ||
} | ||
case "readiness": | ||
if err := readiness(); err != nil { | ||
log.Fatalf("readiness failed: %v", err) | ||
} | ||
case "postStart": | ||
if err := postStart(); err != nil { | ||
log.Fatalf("postStart failed: %v", err) | ||
} | ||
case "preStop": | ||
if err := preStop(); err != nil { | ||
log.Fatalf("postStop failed: %v", err) | ||
} | ||
default: | ||
log.Fatalf("Usage: %s leaderStart|leaderStop|liveness|readiness|postStart|preStop", os.Args[0]) | ||
} | ||
} | ||
|
||
// TODO | ||
func leaderStart() error { | ||
return nil | ||
} | ||
|
||
func leaderStop() error { | ||
return PatchRoleLabelTo(myself(string(Follower))) | ||
} | ||
|
||
func liveness() error { | ||
return XenonPingMyself() | ||
} | ||
|
||
func readiness() error { | ||
role := GetRole() | ||
if role != string(Leader) { | ||
return PatchRoleLabelTo(myself(role)) | ||
} | ||
return nil | ||
} | ||
|
||
// TODO | ||
func postStart() error { | ||
return nil | ||
} | ||
|
||
// TODO | ||
func preStop() error { | ||
return nil | ||
} | ||
|
||
func myself(role string) MySQLNode { | ||
return MySQLNode{ | ||
PodName: podName, | ||
Namespace: ns, | ||
Role: role, | ||
} | ||
} |
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,81 @@ | ||
package utils | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
"os/exec" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/rest" | ||
) | ||
|
||
type raftStatus struct { | ||
Leader string `json:"leader"` | ||
State string `json:"state"` | ||
Nodes []string `json:"nodes"` | ||
} | ||
|
||
type MySQLNode struct { | ||
PodName string | ||
Namespace string | ||
Role string | ||
} | ||
|
||
func GetClientSet() (*kubernetes.Clientset, error) { | ||
// Creates the in-cluster config | ||
config, err := rest.InClusterConfig() | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create in-cluster config: %v", err) | ||
} | ||
// Creates the clientset | ||
clientset, err := kubernetes.NewForConfig(config) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create clientset: %v", err) | ||
} | ||
return clientset, nil | ||
} | ||
|
||
func PatchRoleLabelTo(x MySQLNode) error { | ||
// Creates the clientset | ||
clientset, err := GetClientSet() | ||
if err != nil { | ||
return fmt.Errorf("failed to create clientset: %v", err) | ||
} | ||
patch := fmt.Sprintf(`{"metadata":{"labels":{"role":"%s"}}}`, x.Role) | ||
_, err = clientset.CoreV1().Pods(x.Namespace).Patch(context.TODO(), x.PodName, types.MergePatchType, []byte(patch), metav1.PatchOptions{}) | ||
if err != nil { | ||
return fmt.Errorf("failed to patch pod role label: %v", err) | ||
} | ||
return nil | ||
} | ||
|
||
func XenonPingMyself() error { | ||
args := []string{"xenon", "ping"} | ||
cmd := exec.Command("xenoncli", args...) | ||
if err := cmd.Run(); err != nil { | ||
return fmt.Errorf("failed to exec xenoncli xenon ping: %v", err) | ||
} | ||
return nil | ||
} | ||
|
||
func GetRaftStatus() *raftStatus { | ||
args := []string{"raft", "status"} | ||
cmd := exec.Command("xenoncli", args...) | ||
res, err := cmd.Output() | ||
if err != nil { | ||
log.Fatalf("failed to exec xenoncli raft status: %v", err) | ||
} | ||
raftStatus := raftStatus{} | ||
if err := json.Unmarshal(res, &raftStatus); err != nil { | ||
log.Fatalf("failed to unmarshal raft status: %v", err) | ||
} | ||
return &raftStatus | ||
} | ||
|
||
func GetRole() string { | ||
return GetRaftStatus().State | ||
} |