Commit b3e84c88 authored by Brian Downs's avatar Brian Downs

code clean up, simplification, etc.

parent a846957a
...@@ -53,7 +53,7 @@ func run(app *cli.Context, cfg *cmds.Server) error { ...@@ -53,7 +53,7 @@ func run(app *cli.Context, cfg *cmds.Server) error {
ctx := signals.SetupSignalHandler(context.Background()) ctx := signals.SetupSignalHandler(context.Background())
initialized, err := etcd.NewETCD("").IsInitialized(ctx, &serverConfig.ControlConfig) initialized, err := etcd.NewETCD().IsInitialized(ctx, &serverConfig.ControlConfig)
if err != nil { if err != nil {
return err return err
} }
......
...@@ -6,5 +6,5 @@ import ( ...@@ -6,5 +6,5 @@ import (
) )
func init() { func init() {
managed.RegisterDriver(etcd.NewETCD("")) managed.RegisterDriver(etcd.NewETCD())
} }
...@@ -36,9 +36,6 @@ import ( ...@@ -36,9 +36,6 @@ import (
apierrors "k8s.io/apimachinery/pkg/api/errors" apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilnet "k8s.io/apimachinery/pkg/util/net" utilnet "k8s.io/apimachinery/pkg/util/net"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/transport"
"k8s.io/client-go/util/retry" "k8s.io/client-go/util/retry"
) )
...@@ -56,11 +53,6 @@ const ( ...@@ -56,11 +53,6 @@ const (
defaultKeepAliveTimeout = 10 * time.Second defaultKeepAliveTimeout = 10 * time.Second
maxBackupRetention = 5 maxBackupRetention = 5
defaultRetries = 5
defaultWaitSeconds = 5
defaultTimeout = 30
k8sWrapTransportTimeout = 30
) )
var ( var (
...@@ -79,7 +71,6 @@ type ETCD struct { ...@@ -79,7 +71,6 @@ type ETCD struct {
address string address string
cron *cron.Cron cron *cron.Cron
s3 *s3 s3 *s3
cs *kubernetes.Clientset
} }
type learnerProgress struct { type learnerProgress struct {
...@@ -95,29 +86,11 @@ type Members struct { ...@@ -95,29 +86,11 @@ type Members struct {
Members []*etcdserverpb.Member `json:"members"` Members []*etcdserverpb.Member `json:"members"`
} }
// newClient create a new Kubernetes client from configuration.
func newClient(kubeConfigPath string, k8sWrapTransport transport.WrapperFunc) (*kubernetes.Clientset, error) {
config, err := clientcmd.BuildConfigFromFlags("", kubeConfigPath)
if err != nil {
return nil, err
}
if k8sWrapTransport != nil {
config.WrapTransport = k8sWrapTransport
}
config.Timeout = time.Second * defaultTimeout
return kubernetes.NewForConfig(config)
}
// NewETCD creates a new value of type // NewETCD creates a new value of type
// ETCD with an initialized cron value. // ETCD with an initialized cron value.
func NewETCD(kubeConfigAdmin string) *ETCD { func NewETCD() *ETCD {
cs, err := newClient(kubeConfigAdmin, nil)
if err != nil {
logrus.Fatalf("etcd: new k8s client: %s", err.Error())
}
return &ETCD{ return &ETCD{
cron: cron.New(), cron: cron.New(),
cs: cs,
} }
} }
...@@ -892,19 +865,17 @@ func (e *ETCD) Snapshot(ctx context.Context, config *config.Control) error { ...@@ -892,19 +865,17 @@ func (e *ETCD) Snapshot(ctx context.Context, config *config.Control) error {
return err return err
} }
// store the snapshot data somewhere return e.storeSnapshotData(ctx, snapshots)
_ = snapshots
return nil
} }
// snapshotFile represents a single snapshot and it's // snapshotFile represents a single snapshot and it's
// metadata. // metadata.
type snapshotFile struct { type snapshotFile struct {
Name string `json:"name"` Name string `json:"name"`
NodeName string `json:"nodeName"` Location string `json:"location"`
CreatedAt string `json:"createdAt"` NodeName string `json:"nodeName"`
Size int64 `json:"size"` CreatedAt *metav1.Time `json:"createdAt"`
Size int64 `json:"size"`
} }
// listSnapshots provides a list of the currently stored // listSnapshots provides a list of the currently stored
...@@ -925,10 +896,17 @@ func (e *ETCD) listSnapshots(ctx context.Context, snapshotDir string) ([]snapsho ...@@ -925,10 +896,17 @@ func (e *ETCD) listSnapshots(ctx context.Context, snapshotDir string) ([]snapsho
if obj.Err != nil { if obj.Err != nil {
return nil, obj.Err return nil, obj.Err
} }
ca, err := time.Parse(time.RFC3339, obj.LastModified.Format(time.RFC3339))
if err != nil {
return nil, err
}
snapshots = append(snapshots, snapshotFile{ snapshots = append(snapshots, snapshotFile{
Name: "s3://" + e.config.EtcdS3BucketName + "/" + obj.Key, Name: obj.Key,
CreatedAt: obj.LastModified.String(), Location: "s3://" + e.config.EtcdS3BucketName + "/" + obj.Key,
Size: obj.Size, CreatedAt: &metav1.Time{
Time: ca,
},
Size: obj.Size,
}) })
} }
...@@ -944,18 +922,21 @@ func (e *ETCD) listSnapshots(ctx context.Context, snapshotDir string) ([]snapsho ...@@ -944,18 +922,21 @@ func (e *ETCD) listSnapshots(ctx context.Context, snapshotDir string) ([]snapsho
for _, f := range files { for _, f := range files {
snapshots = append(snapshots, snapshotFile{ snapshots = append(snapshots, snapshotFile{
Name: filepath.Join(snapshotDir, f.Name()), Name: f.Name(),
NodeName: nodeName, Location: "file://" + filepath.Join(snapshotDir, f.Name()),
CreatedAt: f.ModTime().String(), NodeName: nodeName,
Size: f.Size(), CreatedAt: &metav1.Time{
Time: f.ModTime(),
},
Size: f.Size(),
}) })
} }
return snapshots, nil return snapshots, nil
} }
// updateConfigMap // updateSnapshotData
func updateConfigMap(data map[string]string, snapshotFiles []snapshotFile) error { func updateSnapshotData(data map[string]string, snapshotFiles []snapshotFile) error {
for _, v := range snapshotFiles { for _, v := range snapshotFiles {
b, err := json.Marshal(v) b, err := json.Marshal(v)
if err != nil { if err != nil {
...@@ -969,80 +950,31 @@ func updateConfigMap(data map[string]string, snapshotFiles []snapshotFile) error ...@@ -969,80 +950,31 @@ func updateConfigMap(data map[string]string, snapshotFiles []snapshotFile) error
// storeSnapshotData stores the given snapshot data in the "snapshots" ConfigMap. // storeSnapshotData stores the given snapshot data in the "snapshots" ConfigMap.
func (e *ETCD) storeSnapshotData(ctx context.Context, snapshotFiles []snapshotFile) error { func (e *ETCD) storeSnapshotData(ctx context.Context, snapshotFiles []snapshotFile) error {
snapshotConfigMap, err := e.cs.CoreV1().ConfigMaps(metav1.NamespaceSystem).Get(ctx, "snapshots", metav1.GetOptions{}) return retry.OnError(retry.DefaultBackoff, func(err error) bool {
if err != nil { return apierrors.IsConflict(err) || apierrors.IsAlreadyExists(err) || !apierrors.IsNotFound(err)
}, func() error {
data := make(map[string]string, len(snapshotFiles))
if err := updateSnapshotData(data, snapshotFiles); err != nil {
return err
}
snapshotConfigMap, err := e.config.Runtime.Core.Core().V1().ConfigMap().Get(metav1.NamespaceSystem, snapshotConfigMapName, metav1.GetOptions{})
if apierrors.IsNotFound(err) { if apierrors.IsNotFound(err) {
data := make(map[string]string, len(snapshotFiles))
if err := updateConfigMap(data, snapshotFiles); err != nil {
return err
}
cm := v1.ConfigMap{ cm := v1.ConfigMap{
TypeMeta: metav1.TypeMeta{
Kind: "ConfigMap",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: snapshotConfigMapName, Name: snapshotConfigMapName,
Namespace: metav1.NamespaceSystem, Namespace: metav1.NamespaceSystem,
}, },
Data: data, Data: data,
} }
if err := retry.OnError(retry.DefaultBackoff, _, err := e.config.Runtime.Core.Core().V1().ConfigMap().Create(&cm)
func(err error) bool { return err
return !apierrors.IsAlreadyExists(err)
}, func() error {
_, err = e.cs.CoreV1().ConfigMaps(metav1.NamespaceSystem).Create(ctx, &cm, metav1.CreateOptions{})
return err
},
); err != nil && apierrors.IsAlreadyExists(err) {
return retry.RetryOnConflict(retry.DefaultBackoff, func() error {
snapshotConfigMap, err := e.cs.CoreV1().ConfigMaps(metav1.NamespaceSystem).Get(ctx, snapshotConfigMapName, metav1.GetOptions{})
if err != nil {
return err
}
snapshotConfigMap.Data = make(map[string]string, len(snapshotFiles))
if err := updateConfigMap(snapshotConfigMap.Data, snapshotFiles); err != nil {
return err
}
_, err = e.cs.CoreV1().ConfigMaps(metav1.NamespaceSystem).Update(ctx, snapshotConfigMap, metav1.UpdateOptions{})
return err
})
} else if err != nil {
return err
}
return nil
} }
return err
}
if err := retry.OnError(retry.DefaultBackoff, snapshotConfigMap.Data = data
func(err error) bool { _, err = e.config.Runtime.Core.Core().V1().ConfigMap().Update(snapshotConfigMap)
return !apierrors.IsConflict(err)
}, func() error {
snapshotConfigMap.Data = make(map[string]string, len(snapshotFiles))
if err := updateConfigMap(snapshotConfigMap.Data, snapshotFiles); err != nil {
return err
}
_, err = e.cs.CoreV1().ConfigMaps(metav1.NamespaceSystem).Update(ctx, snapshotConfigMap, metav1.UpdateOptions{})
return err
},
); err != nil && apierrors.IsConflict(err) {
return retry.RetryOnConflict(retry.DefaultBackoff, func() error {
snapshotConfigMap, err := e.cs.CoreV1().ConfigMaps(metav1.NamespaceSystem).Get(ctx, snapshotConfigMapName, metav1.GetOptions{})
if err != nil {
return err
}
snapshotConfigMap.Data = make(map[string]string, len(snapshotFiles))
if err := updateConfigMap(snapshotConfigMap.Data, snapshotFiles); err != nil {
return err
}
_, err = e.cs.CoreV1().ConfigMaps(metav1.NamespaceSystem).Update(ctx, snapshotConfigMap, metav1.UpdateOptions{})
return err
})
} else if err != nil {
return err return err
} })
return nil
} }
// setSnapshotFunction schedules snapshots at the configured interval // setSnapshotFunction schedules snapshots at the configured interval
...@@ -1089,12 +1021,14 @@ func (e *ETCD) Restore(ctx context.Context) error { ...@@ -1089,12 +1021,14 @@ func (e *ETCD) Restore(ctx context.Context) error {
// snapshotRetention iterates through the snapshots and removes the oldest // snapshotRetention iterates through the snapshots and removes the oldest
// leaving the desired number of snapshots. // leaving the desired number of snapshots.
func snapshotRetention(retention int, snapshotDir string) error { func snapshotRetention(retention int, snapshotDir string) error {
nodeName := os.Getenv("NODE_NAME")
var snapshotFiles []os.FileInfo var snapshotFiles []os.FileInfo
if err := filepath.Walk(snapshotDir, func(path string, info os.FileInfo, err error) error { if err := filepath.Walk(snapshotDir, func(path string, info os.FileInfo, err error) error {
if err != nil { if err != nil {
return err return err
} }
if strings.HasPrefix(info.Name(), snapshotPrefix) { if strings.HasPrefix(info.Name(), snapshotPrefix+nodeName) {
snapshotFiles = append(snapshotFiles, info) snapshotFiles = append(snapshotFiles, info)
} }
return nil return nil
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment