2020-02-11 23:27:43 +00:00
|
|
|
package nodeconfig
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/base32"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
2022-03-02 23:47:27 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/configfilearg"
|
|
|
|
"github.com/k3s-io/k3s/pkg/version"
|
2020-02-11 23:27:43 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
corev1 "k8s.io/api/core/v1"
|
|
|
|
)
|
|
|
|
|
2020-05-05 22:09:04 +00:00
|
|
|
var (
|
|
|
|
NodeArgsAnnotation = version.Program + ".io/node-args"
|
|
|
|
NodeEnvAnnotation = version.Program + ".io/node-env"
|
|
|
|
NodeConfigHashAnnotation = version.Program + ".io/node-config-hash"
|
2022-04-04 21:54:50 +00:00
|
|
|
ClusterEgressLabel = "egress." + version.Program + ".io/cluster"
|
2020-05-05 22:09:04 +00:00
|
|
|
)
|
|
|
|
|
2020-02-11 23:27:43 +00:00
|
|
|
const (
|
2020-05-05 22:09:04 +00:00
|
|
|
OmittedValue = "********"
|
2020-02-11 23:27:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func getNodeArgs() (string, error) {
|
|
|
|
nodeArgsList := []string{}
|
2021-05-08 07:32:35 +00:00
|
|
|
for _, arg := range configfilearg.MustParse(os.Args[1:]) {
|
2020-07-27 19:09:21 +00:00
|
|
|
if strings.HasPrefix(arg, "--") && strings.Contains(arg, "=") {
|
2020-02-11 23:27:43 +00:00
|
|
|
parsedArg := strings.SplitN(arg, "=", 2)
|
|
|
|
nodeArgsList = append(nodeArgsList, parsedArg...)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
nodeArgsList = append(nodeArgsList, arg)
|
|
|
|
}
|
|
|
|
for i, arg := range nodeArgsList {
|
|
|
|
if isSecret(arg) {
|
|
|
|
if i+1 < len(nodeArgsList) {
|
2020-03-12 18:18:56 +00:00
|
|
|
nodeArgsList[i+1] = OmittedValue
|
2020-02-11 23:27:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nodeArgs, err := json.Marshal(nodeArgsList)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "Failed to retrieve argument list for node")
|
|
|
|
}
|
|
|
|
return string(nodeArgs), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getNodeEnv() (string, error) {
|
|
|
|
k3sEnv := make(map[string]string)
|
|
|
|
for _, v := range os.Environ() {
|
|
|
|
keyValue := strings.SplitN(v, "=", 2)
|
2020-05-05 22:09:04 +00:00
|
|
|
if strings.HasPrefix(keyValue[0], version.ProgramUpper+"_") {
|
2020-02-11 23:27:43 +00:00
|
|
|
k3sEnv[keyValue[0]] = keyValue[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for key := range k3sEnv {
|
|
|
|
if isSecret(key) {
|
2020-03-12 18:18:56 +00:00
|
|
|
k3sEnv[key] = OmittedValue
|
2020-02-11 23:27:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
k3sEnvJSON, err := json.Marshal(k3sEnv)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "Failed to retrieve environment map for node")
|
|
|
|
}
|
|
|
|
return string(k3sEnvJSON), nil
|
|
|
|
}
|
|
|
|
|
2022-04-04 21:54:50 +00:00
|
|
|
// SetNodeConfigAnnotations stores a redacted version of the k3s cli args and
|
|
|
|
// environment variables as annotations on the node object. It also stores a
|
|
|
|
// hash of the combined args + variables. These are used by other components
|
|
|
|
// to determine if the node configuration has been changed.
|
2020-02-11 23:27:43 +00:00
|
|
|
func SetNodeConfigAnnotations(node *corev1.Node) (bool, error) {
|
|
|
|
nodeArgs, err := getNodeArgs()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
nodeEnv, err := getNodeEnv()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
h := sha256.New()
|
|
|
|
_, err = h.Write([]byte(nodeArgs + nodeEnv))
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("Failed to hash the node config: %v", err)
|
|
|
|
}
|
|
|
|
if node.Annotations == nil {
|
|
|
|
node.Annotations = make(map[string]string)
|
|
|
|
}
|
|
|
|
configHash := h.Sum(nil)
|
|
|
|
encoded := base32.StdEncoding.EncodeToString(configHash[:])
|
|
|
|
if node.Annotations[NodeConfigHashAnnotation] == encoded {
|
|
|
|
return false, nil
|
|
|
|
}
|
2020-07-27 19:09:21 +00:00
|
|
|
|
2020-02-11 23:27:43 +00:00
|
|
|
node.Annotations[NodeEnvAnnotation] = nodeEnv
|
|
|
|
node.Annotations[NodeArgsAnnotation] = nodeArgs
|
|
|
|
node.Annotations[NodeConfigHashAnnotation] = encoded
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2022-04-04 21:54:50 +00:00
|
|
|
// SetNodeConfigLabels adds labels for functionality flags
|
|
|
|
// that may not be present on down-level or up-level nodes.
|
|
|
|
// These labels are used by other components to determine whether
|
|
|
|
// or not a node supports particular functionality.
|
|
|
|
func SetNodeConfigLabels(node *corev1.Node) (bool, error) {
|
|
|
|
if node.Labels == nil {
|
|
|
|
node.Labels = make(map[string]string)
|
|
|
|
}
|
|
|
|
if _, ok := node.Labels[ClusterEgressLabel]; !ok {
|
|
|
|
node.Labels[ClusterEgressLabel] = "true"
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2020-02-11 23:27:43 +00:00
|
|
|
func isSecret(key string) bool {
|
|
|
|
secretData := []string{
|
2020-05-05 22:09:04 +00:00
|
|
|
version.ProgramUpper + "_TOKEN",
|
|
|
|
version.ProgramUpper + "_DATASTORE_ENDPOINT",
|
|
|
|
version.ProgramUpper + "_AGENT_TOKEN",
|
|
|
|
version.ProgramUpper + "_CLUSTER_SECRET",
|
2021-04-12 21:27:57 +00:00
|
|
|
"AWS_ACCESS_KEY_ID",
|
|
|
|
"AWS_SECRET_ACCESS_KEY",
|
2020-02-11 23:27:43 +00:00
|
|
|
"--token",
|
|
|
|
"-t",
|
|
|
|
"--agent-token",
|
|
|
|
"--datastore-endpoint",
|
|
|
|
"--cluster-secret",
|
2021-04-12 21:27:57 +00:00
|
|
|
"--etcd-s3-access-key",
|
|
|
|
"--etcd-s3-secret-key",
|
2020-02-11 23:27:43 +00:00
|
|
|
}
|
|
|
|
for _, secret := range secretData {
|
|
|
|
if key == secret {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|