forked from k3s-io/k3s
-
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.
Add etcd s3 config secret implementation
Signed-off-by: Brad Davidson <brad.davidson@rancher.com>
- Loading branch information
Showing
10 changed files
with
236 additions
and
63 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
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,117 @@ | ||
package etcd | ||
|
||
import ( | ||
"encoding/base64" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"github.com/k3s-io/k3s/pkg/daemons/config" | ||
"github.com/sirupsen/logrus" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
// withS3ConfigSecret returns a modified ETCD struct that is overriden | ||
// with etcd S3 snapshot config from the secret. | ||
// If the secret does not exist, the etcd struct is returned unmodified. | ||
func (e *ETCD) withS3ConfigSecret() *ETCD { | ||
secretName := e.config.EtcdS3ConfigSecret | ||
if secretName == "" { | ||
return e | ||
} | ||
|
||
if e.hasS3Config() { | ||
logrus.Infof("Ignoring S3 config from etcd-s3-config-secret %s: S3 configuration is already present", secretName) | ||
return e | ||
} | ||
|
||
secret, err := e.config.Runtime.Core.Core().V1().Secret().Get("kube-system", secretName, metav1.GetOptions{}) | ||
if err != nil { | ||
if !apierrors.IsNotFound(err) { | ||
logrus.Warnf("Failed to get Secret for etcd-s3-config-secret %s: %v", secretName, err) | ||
} | ||
return e | ||
} | ||
|
||
logrus.Infof("Using S3 config from etcd-config-secret %s", secretName) | ||
caBundles := []string{} | ||
re := &ETCD{ | ||
client: e.client, | ||
config: &config.Control{ | ||
CriticalControlArgs: e.config.CriticalControlArgs, | ||
Runtime: e.config.Runtime, | ||
DataDir: e.config.DataDir, | ||
Datastore: e.config.Datastore, | ||
EtcdSnapshotCompress: e.config.EtcdSnapshotCompress, | ||
EtcdSnapshotDir: e.config.EtcdSnapshotDir, | ||
EtcdSnapshotName: e.config.EtcdSnapshotName, | ||
EtcdSnapshotRetention: e.config.EtcdSnapshotRetention, | ||
}, | ||
name: e.name, | ||
address: e.address, | ||
cron: e.cron, | ||
cancel: e.cancel, | ||
snapshotSem: e.snapshotSem, | ||
} | ||
re.config.EtcdS3AccessKey = string(secret.Data["etcd-s3-access-key"]) | ||
re.config.EtcdS3BucketName = string(secret.Data["etcd-s3-bucket"]) | ||
re.config.EtcdS3Endpoint = string(secret.Data["etcd-s3-endpoint"]) | ||
re.config.EtcdS3Folder = string(secret.Data["etcd-s3-folder"]) | ||
re.config.EtcdS3Proxy = string(secret.Data["etcd-s3-proxy"]) | ||
re.config.EtcdS3Region = string(secret.Data["etcd-s3-region"]) | ||
re.config.EtcdS3SecretKey = string(secret.Data["etcd-s3-secret-key"]) | ||
|
||
re.config.EtcdS3SkipSSLVerify, err = strconv.ParseBool(string(secret.Data["etcd-s3-skip-ssl-verify"])) | ||
if err != nil { | ||
logrus.Warnf("Failed to parse etcd-s3-skip-ssl-verify value from S3 config secret %s: %v", secretName, err) | ||
} | ||
|
||
re.config.EtcdS3Insecure, err = strconv.ParseBool(string(secret.Data["etcd-s3-insecure"])) | ||
if err != nil { | ||
logrus.Warnf("Failed to parse etcd-s3-insecure value from S3 config secret %s: %v", secretName, err) | ||
} | ||
|
||
re.config.EtcdS3Timeout, err = time.ParseDuration(string(secret.Data["etcd-s3-timeout"])) | ||
if err != nil { | ||
logrus.Warnf("Failed to parse etcd-s3-timeout value from S3 config secret %s: %v", secretName, err) | ||
} | ||
|
||
// Add inline CA bundle if set | ||
if len(secret.Data["etcd-s3-endpoint-ca"]) > 0 { | ||
caBundles = append(caBundles, base64.StdEncoding.EncodeToString(secret.Data["etcd-s3-endpoint-ca"])) | ||
} | ||
|
||
// Add CA bundles from named configmap if set | ||
if caConfigMapName := string(secret.Data["etcd-s3-endpoint-ca-name"]); caConfigMapName != "" { | ||
configMap, err := e.config.Runtime.Core.Core().V1().ConfigMap().Get("kube-system", caConfigMapName, metav1.GetOptions{}) | ||
if err != nil { | ||
logrus.Warnf("Failed to get ConfigMap %s for etcd-s3-endpoint-ca-name value from S3 config secret %s: %v", caConfigMapName, secretName, err) | ||
} else { | ||
for _, v := range configMap.Data { | ||
caBundles = append(caBundles, base64.StdEncoding.EncodeToString([]byte(v))) | ||
} | ||
for _, v := range configMap.BinaryData { | ||
caBundles = append(caBundles, base64.StdEncoding.EncodeToString(v)) | ||
} | ||
} | ||
} | ||
|
||
// Concatenate all requested CA bundle strings into config var | ||
re.config.EtcdS3EndpointCA = strings.Join(caBundles, " ") | ||
|
||
return re | ||
} | ||
|
||
func (e *ETCD) hasS3Config() bool { | ||
return e.config.EtcdS3Insecure == true || | ||
e.config.EtcdS3SkipSSLVerify == true || | ||
e.config.EtcdS3AccessKey != "" || | ||
e.config.EtcdS3BucketName != "" || | ||
e.config.EtcdS3Endpoint != "" || | ||
e.config.EtcdS3EndpointCA != "" || | ||
e.config.EtcdS3Folder != "" || | ||
e.config.EtcdS3Proxy != "" || | ||
e.config.EtcdS3Region != "" || | ||
e.config.EtcdS3SecretKey != "" | ||
} |
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
Oops, something went wrong.