Skip to content

Commit

Permalink
enable etcd lock
Browse files Browse the repository at this point in the history
  • Loading branch information
wanyaoqi committed Apr 28, 2020
1 parent 38c1c70 commit 5158a50
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 2 deletions.
4 changes: 4 additions & 0 deletions pkg/apis/identity/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,7 @@ type CertificateDetails struct {
CaCertificate string `json:"ca_certificate"`
CaPrivateKey string `json:"ca_private_key"`
}

const (
ENDPOINT_ETCD_INTERNAL = "etcd-internal"
)
20 changes: 20 additions & 0 deletions pkg/cloudcommon/app/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@ import (
"time"

"yunion.io/x/log"
"yunion.io/x/pkg/errors"
"yunion.io/x/pkg/utils"

"yunion.io/x/onecloud/pkg/apis/identity"
"yunion.io/x/onecloud/pkg/cloudcommon/consts"
"yunion.io/x/onecloud/pkg/cloudcommon/notifyclient"
common_options "yunion.io/x/onecloud/pkg/cloudcommon/options"
"yunion.io/x/onecloud/pkg/cloudcommon/policy"
"yunion.io/x/onecloud/pkg/httperrors"
"yunion.io/x/onecloud/pkg/mcclient/auth"
"yunion.io/x/onecloud/pkg/mcclient/modules"
)

func InitAuth(options *common_options.CommonOptions, authComplete auth.AuthCompletedCallback) {
Expand Down Expand Up @@ -90,3 +94,19 @@ func InitBaseAuth(options *common_options.BaseOptions) {
}
consts.SetNonDefaultDomainProjects(options.NonDefaultDomainProjects)
}

func FetchEtcdServiceInfo() (*identity.EndpointDetails, error) {
s := auth.GetAdminSession(context.Background(), "", "")
ret, err := modules.EndpointsV3.GetByName(s, identity.ENDPOINT_ETCD_INTERNAL, nil)
if err != nil && errors.Cause(err) == httperrors.ErrNotFound {
return nil, nil
} else if err != nil {
return nil, err
}
endpoint := new(identity.EndpointDetails)
err = ret.Unmarshal(endpoint)
if err != nil {
return nil, errors.Wrap(err, "unmarshal endpoint")
}
return endpoint, nil
}
1 change: 1 addition & 0 deletions pkg/cloudcommon/etcd/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"google.golang.org/grpc"

"yunion.io/x/log"

"yunion.io/x/onecloud/pkg/util/seclib2"
)

Expand Down
4 changes: 2 additions & 2 deletions pkg/cloudcommon/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ type DBOptions struct {
LockmanMethod string `help:"method for lock synchronization" choices:"inmemory|etcd" default:"inmemory"`

EtcdOptions
EtcdLockPrefix string `help:"prefix of etcd lock records"`
EtcdLockTTL int `help:"ttl of etcd lock records"`
EtcdLockPrefix string `help:"prefix of etcd lock records" default:"/onecloud/lockman"`
EtcdLockTTL int `help:"ttl of etcd lock records" default:"5"`
}

type EtcdOptions struct {
Expand Down
2 changes: 2 additions & 0 deletions pkg/compute/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ type ComputeOptions struct {
EnableHostHealthCheck bool `help:"enable host health check"`
HostHealthTimeout int `help:"second of wait host reconnect" default:"60"`

FetchEtcdServiceInfoAndUseEtcdLock bool `default:"true" help:"fetch etcd service info and use etcd lock"`

SCapabilityOptions
SASControllerOptions
common_options.CommonOptions
Expand Down
45 changes: 45 additions & 0 deletions pkg/compute/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ package service

import (
"context"
"io/ioutil"
"os"
"path/filepath"
"time"

_ "github.com/go-sql-driver/mysql"
Expand Down Expand Up @@ -60,6 +62,13 @@ func StartService() {
log.Infof("Auth complete!!")
})

if opts.FetchEtcdServiceInfoAndUseEtcdLock {
err := initEtcdLockOpts(opts)
if err != nil {
log.Fatalln(err)
}
}

app := app_common.InitApp(baseOpts, true)
InitHandlers(app)

Expand Down Expand Up @@ -162,3 +171,39 @@ func initDefaultEtcdClient(opts *common_options.DBOptions) error {
}
return nil
}

func initEtcdLockOpts(opts *options.ComputeOptions) error {
etcdEndpoint, err := app_common.FetchEtcdServiceInfo()
if err != nil {
return errors.Wrap(err, "fetch etcd service info")
}
if etcdEndpoint != nil {
opts.EtcdEndpoints = []string{etcdEndpoint.Url}
opts.LockmanMethod = common_options.LockMethodEtcd
if len(etcdEndpoint.CertId) > 0 {
dir, err := ioutil.TempDir("", "etcd-cluster-tls")
if err != nil {
return errors.Wrap(err, "create dir etcd cluster tls")
}
opts.EtcdCert, err = writeFile(dir, "etcd.crt", []byte(etcdEndpoint.Certificate))
if err != nil {
return errors.Wrap(err, "write file certificate")
}
opts.EtcdKey, err = writeFile(dir, "etcd.key", []byte(etcdEndpoint.PrivateKey))
if err != nil {
return errors.Wrap(err, "write file private key")
}
opts.EtcdCacert, err = writeFile(dir, "etcd-ca.crt", []byte(etcdEndpoint.CaCertificate))
if err != nil {
return errors.Wrap(err, "write file cacert")
}
opts.EtcdUseTLS = true
}
}
return nil
}

func writeFile(dir, file string, data []byte) (string, error) {
p := filepath.Join(dir, file)
return p, ioutil.WriteFile(p, data, 0600)
}

0 comments on commit 5158a50

Please sign in to comment.