Skip to content
This repository has been archived by the owner on Jun 18, 2022. It is now read-only.

Commit

Permalink
Merge pull request #272 from ibuildthecloud/master
Browse files Browse the repository at this point in the history
Import cluster logic
  • Loading branch information
ibuildthecloud authored Feb 20, 2018
2 parents e5c0ac5 + 19aef18 commit f6754c4
Show file tree
Hide file tree
Showing 593 changed files with 206,459 additions and 65 deletions.
86 changes: 86 additions & 0 deletions cluster/cluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package cluster

import (
"encoding/base64"
"fmt"
"io/ioutil"
"os"
"path"

"k8s.io/client-go/rest"
)

const (
rancherCredentialsFolder = "/cattle-credentials"
urlFilename = "url"
tokenFilename = "token"

kubernetesServiceHostKey = "KUBERNETES_SERVICE_HOST"
kubernetesServicePortKey = "KUBERNETES_SERVICE_PORT"
)

func TokenAndURL() (string, string, error) {
return getRancherClient()
}

func Params() (map[string]interface{}, error) {
cfg, err := rest.InClusterConfig()
if err != nil {
return nil, err
}

if err := populateCAData(cfg); err != nil {
return nil, err
}

kubernetesServiceHost, err := getenv(kubernetesServiceHostKey)
if err != nil {
return nil, err
}
kubernetesServicePort, err := getenv(kubernetesServicePortKey)
if err != nil {
return nil, err
}

return map[string]interface{}{
"cluster": map[string]interface{}{
"address": fmt.Sprintf("%s:%s", kubernetesServiceHost, kubernetesServicePort),
"token": cfg.BearerToken,
"caCert": base64.StdEncoding.EncodeToString(cfg.CAData),
},
}, nil
}

func getenv(env string) (string, error) {
value := os.Getenv(env)
if value == "" {
return "", fmt.Errorf("%s is empty", env)
}
return value, nil
}

func populateCAData(cfg *rest.Config) error {
bytes, err := ioutil.ReadFile(cfg.CAFile)
if err != nil {
return err
}
cfg.CAData = bytes
return nil
}

func getRancherClient() (string, string, error) {
url, err := readKey(urlFilename)
if err != nil {
return "", "", err
}
token, err := readKey(tokenFilename)
return token, url, err
}

func readKey(key string) (string, error) {
bytes, err := ioutil.ReadFile(path.Join(rancherCredentialsFolder, key))
if err != nil {
return "", err
}
return string(bytes), nil
}
62 changes: 24 additions & 38 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"net/http"
"net/url"
"os"
"strings"

"github.com/rancher/norman/types/slice"
"github.com/rancher/agent/cluster"
"github.com/rancher/agent/node"
"github.com/rancher/rancher/pkg/remotedialer"
"github.com/sirupsen/logrus"
)
Expand All @@ -30,48 +30,48 @@ func main() {
}
}

func run() error {
token := os.Getenv("CATTLE_TOKEN")
roles := split(os.Getenv("CATTLE_ROLE"))
params := map[string]interface{}{
"customConfig": map[string]interface{}{
"address": os.Getenv("CATTLE_ADDRESS"),
"internalAddress": os.Getenv("CATTLE_INTERNAL_ADDRESS"),
"roles": split(os.Getenv("CATTLE_ROLE")),
},
"etcd": slice.ContainsString(roles, "etcd"),
"controlPlane": slice.ContainsString(roles, "controlplane"),
"worker": slice.ContainsString(roles, "worker"),
"requestedHostname": os.Getenv("CATTLE_NODE_NAME"),
func getParams() (map[string]interface{}, error) {
if os.Getenv("CATTLE_CLUSTER") == "true" {
return cluster.Params()
}
return node.Params(), nil
}

for k, v := range params {
if m, ok := v.(map[string]string); ok {
for k, v := range m {
logrus.Infof("Option %s=%s", k, v)
}
} else {
logrus.Infof("Option %s=%v", k, v)
}
func getTokenAndURL() (string, string, error) {
if os.Getenv("CATTLE_CLUSTER") == "true" {
return cluster.TokenAndURL()
}
return node.TokenAndURL()
}

func run() error {
params, err := getParams()
if err != nil {
return err
}

bytes, err := json.Marshal(params)
if err != nil {
return err
}

token, server, err := getTokenAndURL()
if err != nil {
return err
}

headers := map[string][]string{
Token: {token},
Params: {base64.StdEncoding.EncodeToString(bytes)},
}

server := os.Getenv("CATTLE_SERVER")
serverURL, err := url.Parse(server)
if err != nil {
return err
}

wsURL := fmt.Sprintf("wss://%s/v3/connect", serverURL.Host)
logrus.Infof("Connecting to %s with token %s", wsURL, token)
remotedialer.ClientConnect(wsURL, http.Header(headers), nil, func(proto, address string) bool {
switch proto {
case "tcp":
Expand All @@ -84,17 +84,3 @@ func run() error {

return nil
}

func split(s string) []string {
var result []string
for _, part := range strings.Split(s, ",") {
p := strings.TrimSpace(part)
if p != "" {
result = append(result, p)
}
}
if len(result) == 1 && result[0] == "" {
return nil
}
return result
}
56 changes: 56 additions & 0 deletions node/node.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package node

import (
"os"
"strings"

"github.com/rancher/norman/types/slice"
"github.com/sirupsen/logrus"
)

func TokenAndURL() (string, string, error) {
return os.Getenv("CATTLE_TOKEN"), os.Getenv("CATTLE_SERVER"), nil
}

func Params() map[string]interface{} {
roles := split(os.Getenv("CATTLE_ROLE"))
params := map[string]interface{}{
"customConfig": map[string]interface{}{
"address": os.Getenv("CATTLE_ADDRESS"),
"internalAddress": os.Getenv("CATTLE_INTERNAL_ADDRESS"),
"roles": split(os.Getenv("CATTLE_ROLE")),
},
"etcd": slice.ContainsString(roles, "etcd"),
"controlPlane": slice.ContainsString(roles, "controlplane"),
"worker": slice.ContainsString(roles, "worker"),
"requestedHostname": os.Getenv("CATTLE_NODE_NAME"),
}

for k, v := range params {
if m, ok := v.(map[string]string); ok {
for k, v := range m {
logrus.Infof("Option %s=%s", k, v)
}
} else {
logrus.Infof("Option %s=%v", k, v)
}
}

return map[string]interface{}{
"node": params,
}
}

func split(s string) []string {
var result []string
for _, part := range strings.Split(s, ",") {
p := strings.TrimSpace(part)
if p != "" {
result = append(result, p)
}
}
if len(result) == 1 && result[0] == "" {
return nil
}
return result
}
34 changes: 19 additions & 15 deletions package/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ while true; do
-d | --debug) DEBUG=true ;;
-s | --server) shift; CATTLE_SERVER=$1 ;;
-t | --token) shift; CATTLE_TOKEN=$1 ;;
-c | --ca-checksum) shift; CA_CHECKSUM=$1 ;;
-c | --ca-checksum) shift; CATTLE_CA_CHECKSUM=$1 ;;
-a | --all-roles) ALL=true ;;
-e | --etcd) ETCD=true ;;
-w | --worker) WORKER=true ;;
Expand All @@ -37,10 +37,12 @@ if [ "$DEBUG" = true ]; then
set -x
fi

if [ ! -w /var/run/docker.sock ] || [ ! -S /var/run/docker.sock ]; then
error Please bind mount in the docker socket to /var/run/docker.sock
error example: docker run -v /var/run/docker.sock:/var/run/docker.sock ...
exit 1
if [ "$CATTLE_CLUSTER" != "true" ]; then
if [ ! -w /var/run/docker.sock ] || [ ! -S /var/run/docker.sock ]; then
error Please bind mount in the docker socket to /var/run/docker.sock
error example: docker run -v /var/run/docker.sock:/var/run/docker.sock ...
exit 1
fi
fi

if [ -z "$CATTLE_NODE_NAME" ]; then
Expand Down Expand Up @@ -71,13 +73,13 @@ else
fi
fi

if [ -n "$CA_CHECKSUM" ]; then
if [ -n "$CATTLE_CA_CHECKSUM" ]; then
temp=$(mktemp)
curl --insecure -s -fL $CATTLE_SERVER/v3/settings/cacerts | jq -r .value > $temp
cat $temp
if [ "$(sha256sum $temp | awk '{print $1}')" != $CA_CHECKSUM ]; then
if [ "$(sha256sum $temp | awk '{print $1}')" != $CATTLE_CA_CHECKSUM ]; then
rm -f $temp
error $CATTLE_SERVER/v3/settings/cacerts does not match $CA_CHECKSUM
error $CATTLE_SERVER/v3/settings/cacerts does not match $CATTLE_CA_CHECKSUM
exit 1
fi
mkdir -p certs
Expand All @@ -90,14 +92,16 @@ if [ -z "$CATTLE_SERVER" ]; then
exit 1
fi

if [ -z "$CATTLE_TOKEN" ]; then
error -- --token is a required option
exit 1
fi
if [ "$CATTLE_CLUSTER" != "true" ]; then
if [ -z "$CATTLE_TOKEN" ]; then
error -- --token is a required option
exit 1
fi

if [ -z "$CATTLE_ADDRESS" ]; then
error -- --address is a required option
exit 1
if [ -z "$CATTLE_ADDRESS" ]; then
error -- --address is a required option
exit 1
fi
fi

exec agent
2 changes: 1 addition & 1 deletion vendor.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# package
github.com/rancher/agent

github.com/rancher/rancher f81cb41617ada783f8be617762ecd21b127feba7 transitive=true
github.com/rancher/rancher 5034831c009dfee14df7ec6684a208c3137b8ec7 transitive=true
github.com/sirupsen/logrus 89742aefa4b206dcf400792f3bd35b542998eb3b
5 changes: 5 additions & 0 deletions vendor/github.com/PuerkitoBio/purell/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions vendor/github.com/PuerkitoBio/purell/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions vendor/github.com/PuerkitoBio/purell/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f6754c4

Please sign in to comment.