This repository has been archived by the owner on Jun 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 64
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 #272 from ibuildthecloud/master
Import cluster logic
- Loading branch information
Showing
593 changed files
with
206,459 additions
and
65 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,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 | ||
} |
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,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 | ||
} |
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 |
---|---|---|
@@ -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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.