Skip to content

Commit

Permalink
A huge migration to a newer swagger. (#3481)
Browse files Browse the repository at this point in the history
* Huge migration to a newer swagger.
  • Loading branch information
vburenin authored Dec 13, 2016
1 parent 511497f commit f57cd75
Show file tree
Hide file tree
Showing 2,036 changed files with 679,454 additions and 139,413 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -294,11 +294,11 @@ PORTLAYER_DEPS ?= lib/apiservers/portlayer/swagger.json \

$(portlayerapi-client): $(PORTLAYER_DEPS) $(SWAGGER)
@echo regenerating swagger models and operations for Portlayer API client...
@$(SWAGGER) generate client -A PortLayer --template-dir lib/apiservers/templates -t $(realpath $(dir $<)) -f $<
@$(SWAGGER) generate client -A PortLayer --target lib/apiservers/portlayer -f lib/apiservers/portlayer/swagger.json

$(portlayerapi-server): $(PORTLAYER_DEPS) $(SWAGGER)
@echo regenerating swagger models and operations for Portlayer API server...
@$(SWAGGER) generate server --exclude-main -A PortLayer --template-dir lib/apiservers/templates -t $(realpath $(dir $<)) -f $<
@$(SWAGGER) generate server --exclude-main -A PortLayer --target lib/apiservers/portlayer -f lib/apiservers/portlayer/swagger.json

$(portlayerapi): $$(call godeps,cmd/port-layer-server/*.go) $(portlayerapi-server) $(portlayerapi-client)
@echo building Portlayer API server...
Expand Down
28 changes: 19 additions & 9 deletions cmd/port-layer-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@ import (
"os/signal"
"syscall"

spec "github.com/go-swagger/go-swagger/spec"
flags "github.com/jessevdk/go-flags"
"github.com/go-openapi/loads"
"github.com/jessevdk/go-flags"

"github.com/vmware/vic/lib/apiservers/portlayer/restapi"
"github.com/vmware/vic/lib/apiservers/portlayer/restapi/operations"
"github.com/vmware/vic/lib/pprof"

"github.com/vmware/vic/lib/dns"
"github.com/vmware/vic/lib/pprof"
)

var (
Expand All @@ -39,7 +38,8 @@ func init() {
}

func main() {
swaggerSpec, err := spec.New(restapi.SwaggerJSON, "")

swaggerSpec, err := loads.Analyzed(restapi.SwaggerJSON, "")
if err != nil {
log.Fatalln(err)
}
Expand All @@ -53,19 +53,30 @@ func main() {
parser.LongDescription = `Port Layer API`

server.ConfigureFlags()

for _, optsGroup := range api.CommandLineOptionsGroups {
parser.AddGroup(optsGroup.ShortDescription, optsGroup.LongDescription, optsGroup.Options)
_, err := parser.AddGroup(optsGroup.ShortDescription, optsGroup.LongDescription, optsGroup.Options)
if err != nil {
log.Fatalln(err)
}
}

if _, err := parser.Parse(); err != nil {
os.Exit(1)
code := 1
if fe, ok := err.(*flags.Error); ok {
if fe.Type == flags.ErrHelp {
code = 0
}
}
os.Exit(code)
}

server.ConfigureAPI()

// BEGIN
// Set the Interface name to instruct listeners to bind on this interface
options.Interface = "bridge"

// Start the DNS Server
dnsserver := dns.NewServer(options)
if dnsserver != nil {
Expand All @@ -80,8 +91,7 @@ func main() {
<-sig

dnsserver.Stop()

server.Stop()
restapi.StopAPIServers()
}()

go func() {
Expand Down
41 changes: 21 additions & 20 deletions lib/apiservers/engine/backends/backends.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ import (

log "github.com/Sirupsen/logrus"
"github.com/docker/docker/registry"
httptransport "github.com/go-swagger/go-swagger/httpkit/client"
"github.com/go-swagger/go-swagger/swag"
rc "github.com/go-openapi/runtime/client"
"github.com/go-openapi/swag"

"github.com/vmware/vic/lib/apiservers/engine/backends/cache"
"github.com/vmware/vic/lib/apiservers/engine/backends/container"
"github.com/vmware/vic/lib/apiservers/portlayer/client"
apiclient "github.com/vmware/vic/lib/apiservers/portlayer/client"
"github.com/vmware/vic/lib/apiservers/portlayer/client/containers"
"github.com/vmware/vic/lib/apiservers/portlayer/client/misc"
"github.com/vmware/vic/lib/apiservers/portlayer/client/scopes"
Expand All @@ -52,7 +52,7 @@ const (
)

var (
portLayerClient *client.PortLayer
portLayerClient *apiclient.PortLayer
portLayerServerAddr string
portLayerName string
productName string
Expand Down Expand Up @@ -88,8 +88,9 @@ func Init(portLayerAddr, product string, config *config.VirtualContainerHostConf
portLayerName = product + " Backend Engine"
}

t := httptransport.New(portLayerAddr, "/", []string{"http"})
portLayerClient = client.New(t, nil)
t := rc.New(portLayerAddr, "/", []string{"http"})

portLayerClient = apiclient.New(t, nil)
portLayerServerAddr = portLayerAddr

// block indefinitely while waiting on the portlayer to respond to pings
Expand Down Expand Up @@ -123,52 +124,52 @@ func hydrateCaches() error {

wg := sync.WaitGroup{}
wg.Add(waiters)
errors := make(chan error, waiters)
errChan := make(chan error, waiters)

go func() {
defer wg.Done()
if err := imagec.InitializeLayerCache(portLayerClient); err != nil {
errors <- fmt.Errorf("Failed to initialize layer cache: %s", err)
errChan <- fmt.Errorf("Failed to initialize layer cache: %s", err)
return
}
log.Info("Layer cache initialized successfully")
errors <- nil
errChan <- nil
}()

go func() {
defer wg.Done()
if err := cache.InitializeImageCache(portLayerClient); err != nil {
errors <- fmt.Errorf("Failed to initialize image cache: %s", err)
errChan <- fmt.Errorf("Failed to initialize image cache: %s", err)
return
}
log.Info("Image cache initialized successfully")

// container cache relies on image cache so we share a goroutine to update
// them serially
if err := syncContainerCache(); err != nil {
errors <- fmt.Errorf("Failed to update container cache: %s", err)
errChan <- fmt.Errorf("Failed to update container cache: %s", err)
return
}
log.Info("Container cache updated successfully")
errors <- nil
errChan <- nil
}()

go func() {
log.Info("Refreshing repository cache")
defer wg.Done()
if err := cache.NewRepositoryCache(portLayerClient); err != nil {
errors <- fmt.Errorf("Failed to create repository cache: %s", err.Error())
errChan <- fmt.Errorf("Failed to create repository cache: %s", err.Error())
return
}
errors <- nil
errChan <- nil
log.Info("Repository cache updated successfully")
}()

wg.Wait()
close(errors)
close(errChan)

var errs []string
for err := range errors {
for err := range errChan {
if err != nil {
// accumulate all errors into one
errs = append(errs, err.Error())
Expand All @@ -182,7 +183,7 @@ func hydrateCaches() error {
return e
}

func PortLayerClient() *client.PortLayer {
func PortLayerClient() *apiclient.PortLayer {
return portLayerClient
}

Expand Down Expand Up @@ -285,12 +286,12 @@ func syncContainerCache() error {
}

func setPortMapping(info *models.ContainerInfo, backend *Container, container *container.VicContainer) error {
if info.ContainerConfig.State == nil {
if info.ContainerConfig.State == "" {
log.Infof("container state is nil")
return nil
}
if *info.ContainerConfig.State != "Running" || len(container.HostConfig.PortBindings) == 0 {
log.Infof("Container info state: %s", *info.ContainerConfig.State)
if info.ContainerConfig.State != "Running" || len(container.HostConfig.PortBindings) == 0 {
log.Infof("Container info state: %s", info.ContainerConfig.State)
log.Infof("container portbinding: %+v", container.HostConfig.PortBindings)
return nil
}
Expand Down
60 changes: 36 additions & 24 deletions lib/apiservers/engine/backends/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package backends

import (
"context"
"fmt"
"io"
"math/rand"
Expand All @@ -25,8 +26,6 @@ import (
"sync"
"time"

"context"

log "github.com/Sirupsen/logrus"
"github.com/docker/docker/api/types/backend"
derr "github.com/docker/docker/errors"
Expand Down Expand Up @@ -934,22 +933,25 @@ func (c *Container) ContainerInspect(name string, size bool, version version.Ver
}
var started time.Time
var stopped time.Time
if results.Payload.ProcessConfig.StartTime != nil && *results.Payload.ProcessConfig.StartTime > 0 {
started = time.Unix(*results.Payload.ProcessConfig.StartTime, 0)

if results.Payload.ProcessConfig.StartTime > 0 {
started = time.Unix(results.Payload.ProcessConfig.StartTime, 0)
}
if results.Payload.ProcessConfig.StopTime != nil && *results.Payload.ProcessConfig.StopTime > 0 {
stopped = time.Unix(*results.Payload.ProcessConfig.StopTime, 0)
if results.Payload.ProcessConfig.StopTime > 0 {
stopped = time.Unix(results.Payload.ProcessConfig.StopTime, 0)
}

// call to the dockerStatus function to retrieve the docker friendly exitCode
exitCode, status := dockerStatus(int(*results.Payload.ProcessConfig.ExitCode),
*results.Payload.ProcessConfig.Status,
*results.Payload.ContainerConfig.State,
exitCode, status := dockerStatus(
int(results.Payload.ProcessConfig.ExitCode),
results.Payload.ProcessConfig.Status,
results.Payload.ContainerConfig.State,
started, stopped)

// set the payload values
exit := int32(exitCode)
results.Payload.ProcessConfig.ExitCode = &exit
results.Payload.ProcessConfig.Status = &status
results.Payload.ProcessConfig.ExitCode = exit
results.Payload.ProcessConfig.Status = status

inspectJSON, err := ContainerInfoToDockerContainerInspect(vc, results.Payload, PortLayerName())
if err != nil {
Expand Down Expand Up @@ -1047,14 +1049,22 @@ func (c *Container) Containers(config *types.ContainerListOptions) ([]*types.Con
}
var started time.Time
var stopped time.Time
if t.ProcessConfig.StartTime != nil && *t.ProcessConfig.StartTime > 0 {
started = time.Unix(*t.ProcessConfig.StartTime, 0)

if t.ProcessConfig.StartTime > 0 {
started = time.Unix(t.ProcessConfig.StartTime, 0)
}
if t.ProcessConfig.StopTime != nil && *t.ProcessConfig.StopTime > 0 {
stopped = time.Unix(*t.ProcessConfig.StopTime, 0)

if t.ProcessConfig.StopTime > 0 {
stopped = time.Unix(t.ProcessConfig.StopTime, 0)
}

// get the docker friendly status
_, status := dockerStatus(int(*t.ProcessConfig.ExitCode), *t.ProcessConfig.Status, *t.ContainerConfig.State, started, stopped)
_, status := dockerStatus(
int(t.ProcessConfig.ExitCode),
t.ProcessConfig.Status,
t.ContainerConfig.State,
started,
stopped)

ips, err := publicIPv4Addrs()
var ports []types.Port
Expand All @@ -1072,7 +1082,7 @@ func (c *Container) Containers(config *types.ContainerListOptions) ([]*types.Con
imageID, err := cache.RepositoryCache().Get(ref)
if err != nil && err == cache.ErrDoesNotExist {
// the tag has been removed, so we need to show the truncated imageID
imageID = cache.RepositoryCache().GetImageID(*t.ContainerConfig.LayerID)
imageID = cache.RepositoryCache().GetImageID(t.ContainerConfig.LayerID)
if imageID != "" {
id := uid.Parse(imageID)
repo = id.Truncate().String()
Expand All @@ -1081,13 +1091,13 @@ func (c *Container) Containers(config *types.ContainerListOptions) ([]*types.Con
}

c := &types.Container{
ID: *t.ContainerConfig.ContainerID,
ID: t.ContainerConfig.ContainerID,
Image: repo,
Created: *t.ContainerConfig.CreateTime,
Created: t.ContainerConfig.CreateTime,
Status: status,
Names: names,
Command: cmd,
SizeRw: *t.ContainerConfig.StorageSize,
SizeRw: t.ContainerConfig.StorageSize,
Ports: ports,
}
containers = append(containers, c)
Expand Down Expand Up @@ -1470,17 +1480,19 @@ func portInformation(t *models.ContainerInfo, ips []netlink.Addr) []types.Port {
// (works with both IPv4 and IPv6 addresses)
var ports []types.Port

container := cache.ContainerCache().GetContainer(*t.ContainerConfig.ContainerID)
if container == nil {
log.Errorf("Could not find container with ID %s", *t.ContainerConfig.ContainerID)
cid := t.ContainerConfig.ContainerID
c := cache.ContainerCache().GetContainer(cid)

if c == nil {
log.Errorf("Could not find container with ID %s", cid)
return ports
}

for _, ip := range ips {
ports = append(ports, types.Port{IP: ip.IP.String()})
}

portBindings := container.HostConfig.PortBindings
portBindings := c.HostConfig.PortBindings
var resultPorts []types.Port
var err error

Expand Down
Loading

0 comments on commit f57cd75

Please sign in to comment.