Skip to content

Commit

Permalink
Update LXD Client Config
Browse files Browse the repository at this point in the history
This commit updates the LXD discovery plugin to support the new LXD
client API.
  • Loading branch information
jtopjian authored and illarion committed Aug 25, 2017
1 parent 5c51b0c commit 153afc7
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 45 deletions.
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ deps: clean-deps
golang.org/x/sys/windows \
github.com/inconshreveable/mousetrap \
github.com/gin-contrib/cors \
github.com/lxc/lxd \
github.com/lxc/lxd/client \
github.com/lxc/lxd/lxc/config \
github.com/jtopjian/lxdhelpers

clean-dist:
Expand Down Expand Up @@ -98,7 +99,7 @@ dist:
else \
cd $$distpath && zip -r ../../${NAME}_${VERSION}_$$1_$$2.zip . && cd - ;\
fi \
done
done

build-container-latest: build
@echo Building docker container LATEST
Expand Down
96 changes: 53 additions & 43 deletions src/discovery/lxd.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import (

"github.com/jtopjian/lxdhelpers"

"github.com/lxc/lxd"
lxd "github.com/lxc/lxd/client"
lxd_config "github.com/lxc/lxd/lxc/config"
"github.com/lxc/lxd/shared"
)

Expand Down Expand Up @@ -53,16 +54,27 @@ func lxdFetch(cfg config.DiscoveryConfig) (*[]core.Backend, error) {
return nil, err
}

/* Get an LXD config */
lxdConfig, err := lxdBuildConfig(cfg)
if err != nil {
return nil, err
}

/* Set the timeout for the client */
client.Http.Timeout = utils.ParseDurationOrDefault(cfg.Timeout, lxdTimeout)
httpClient, err := client.GetHTTPClient()
if err != nil {
return nil, err
}

httpClient.Timeout = utils.ParseDurationOrDefault(cfg.Timeout, lxdTimeout)

log.Debug("Fetching containers from ", client.Config.Remotes[cfg.LXDServerRemoteName].Addr)
log.Debug("Fetching containers from ", lxdConfig.Remotes[cfg.LXDServerRemoteName].Addr)

/* Create backends from response */
backends := []core.Backend{}

/* Fetch containers */
containers, err := client.ListContainers()
containers, err := client.GetContainers()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -140,11 +152,11 @@ func lxdFetch(cfg config.DiscoveryConfig) (*[]core.Backend, error) {
/**
* Create new LXD Client
*/
func lxdBuildClient(cfg config.DiscoveryConfig) (*lxd.Client, error) {
func lxdBuildClient(cfg config.DiscoveryConfig) (lxd.ContainerServer, error) {
log := logging.For("lxdBuildClient")

/* Make a client to pass around */
var client *lxd.Client
var client lxd.ContainerServer

/* Build a configuration with the requested options */
lxdConfig, err := lxdBuildConfig(cfg)
Expand All @@ -162,28 +174,18 @@ func lxdBuildClient(cfg config.DiscoveryConfig) (*lxd.Client, error) {
/* Validate or accept certificates on the server side (LXD) */
serverCertf := lxdConfig.ServerCertPath(cfg.LXDServerRemoteName)
if !shared.PathExists(serverCertf) {

/* If the server certificate was not found, either gobetween and the LXD server are set
* up for PKI, or gobetween must authenticate with the LXD server and accept its server
* certificate.
*
* First, create a simple LXD client
* First, see if communication with the LXD server is possible.
*/
client, err = lxd.NewClient(&lxdConfig, cfg.LXDServerRemoteName)
_, err := lxdConfig.GetContainerServer(cfg.LXDServerRemoteName)
if err != nil {
return nil, err
}

/* Next, check if the client is able to communicate with the LXD server. If it can,
* this means that gobetween and the LXD server are configured with PKI certificates
* from a private CA.
*
* But if there's an error, then gobetween will try to download the server's cert.
*/
if _, err := client.GetServerConfig(); err != nil {
/* If there was an error, then gobetween will try to download the server's cert. */
if cfg.LXDAcceptServerCert {
var err error
client, err = lxdhelpers.GetRemoteCertificate(client, cfg.LXDServerRemoteName)
client, err = lxdhelpers.GetRemoteCertificate(lxdConfig, cfg.LXDServerRemoteName)
if err != nil {
return nil, fmt.Errorf("Could not add the LXD server: %s", err)
}
Expand All @@ -194,33 +196,38 @@ func lxdBuildClient(cfg config.DiscoveryConfig) (*lxd.Client, error) {
return nil, err
}
}
}

/*
* Finally, check and see if gobetween needs to authenticate with the LXD server.
* Authentication happens only once. After that, gobetween will be a trusted client
* as long as the exchanged certificates to not change.
*
* Authentication must happen even if PKI is in use.
*/
log.Info("Attempting to authenticate")
err = lxdhelpers.ValidateRemoteConnection(client, cfg.LXDServerRemoteName, cfg.LXDServerRemotePassword)
if err != nil {
log.Info("Authentication unsuccessful")
return nil, err
}
/*
* Finally, check and see if gobetween needs to authenticate with the LXD server.
* Authentication happens only once. After that, gobetween will be a trusted client
* as long as the exchanged certificates to not change.
*
* Authentication must happen even if PKI is in use.
*/
client, err = lxdConfig.GetContainerServer(cfg.LXDServerRemoteName)
if err != nil {
return nil, err
}

log.Info("Authentication successful")
log.Info("Attempting to authenticate")
err = lxdhelpers.ValidateRemoteConnection(client, cfg.LXDServerRemoteName, cfg.LXDServerRemotePassword)
if err != nil {
log.Info("Authentication unsuccessful")
return nil, err
}

log.Info("Authentication successful")
}

/* Build a new client */
client, err = lxd.NewClient(&lxdConfig, cfg.LXDServerRemoteName)
client, err = lxdConfig.GetContainerServer(cfg.LXDServerRemoteName)
if err != nil {
return nil, err
}

/* Validate the client config and connectivity */
if _, err := client.GetServerConfig(); err != nil {
if _, _, err := client.GetServer(); err != nil {
return nil, err
}

Expand All @@ -230,25 +237,28 @@ func lxdBuildClient(cfg config.DiscoveryConfig) (*lxd.Client, error) {
/**
* Create LXD Client Config
*/
func lxdBuildConfig(cfg config.DiscoveryConfig) (lxd.Config, error) {
func lxdBuildConfig(cfg config.DiscoveryConfig) (*lxd_config.Config, error) {
log := logging.For("lxdBuildConfig")

log.Debug("Using API: ", cfg.LXDServerAddress)

/* Build an LXD configuration that will connect to the requested LXD server */
config := lxd.Config{
ConfigDir: cfg.LXDConfigDirectory,
Remotes: make(map[string]lxd.RemoteConfig),
var config *lxd_config.Config
if conf, err := lxd_config.LoadConfig(cfg.LXDConfigDirectory); err != nil {
config = &lxd_config.DefaultConfig
config.ConfigDir = cfg.LXDConfigDirectory
} else {
config = conf
}
config.Remotes[cfg.LXDServerRemoteName] = lxd.RemoteConfig{Addr: cfg.LXDServerAddress}

config.Remotes[cfg.LXDServerRemoteName] = lxd_config.Remote{Addr: cfg.LXDServerAddress}
return config, nil
}

/**
* Get container IP address depending on network interface and address type
*/
func lxdDetermineContainerIP(client *lxd.Client, container, iface, addrType string) (string, error) {
func lxdDetermineContainerIP(client lxd.ContainerServer, container, iface, addrType string) (string, error) {
var containerIP string

/* Convert addrType to inet */
Expand All @@ -260,7 +270,7 @@ func lxdDetermineContainerIP(client *lxd.Client, container, iface, addrType stri
inet = "inet6"
}

cstate, err := client.ContainerState(container)
cstate, _, err := client.GetContainerState(container)
if err != nil {
return "", err
}
Expand Down

0 comments on commit 153afc7

Please sign in to comment.