-
Notifications
You must be signed in to change notification settings - Fork 174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
A huge migration to a newer swagger. #3481
Changes from 1 commit
922b081
a233d31
7ede85f
7b49636
d360ed4
7988621
737c885
3445742
402830e
8110091
b352166
142f93b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -52,7 +52,7 @@ const ( | |
) | ||
|
||
var ( | ||
portLayerClient *client.PortLayer | ||
portLayerClient *apiclient.PortLayer | ||
portLayerServerAddr string | ||
portLayerName string | ||
productName string | ||
|
@@ -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 | ||
|
@@ -123,52 +124,52 @@ func hydrateCaches() error { | |
|
||
wg := sync.WaitGroup{} | ||
wg.Add(waiters) | ||
errors := make(chan error, waiters) | ||
errChan := make(chan error, waiters) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment as above. This isn't strictly necessary to migrate to the new swagger version. Please remove it from your change and we can review these cosmetic changes in a different PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the cosmetic change and the only one that you can say "big". |
||
|
||
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()) | ||
|
@@ -182,7 +183,7 @@ func hydrateCaches() error { | |
return e | ||
} | ||
|
||
func PortLayerClient() *client.PortLayer { | ||
func PortLayerClient() *apiclient.PortLayer { | ||
return portLayerClient | ||
} | ||
|
||
|
@@ -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 | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
package backends | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"math/rand" | ||
|
@@ -25,8 +26,6 @@ import ( | |
"sync" | ||
"time" | ||
|
||
"context" | ||
|
||
log "github.com/Sirupsen/logrus" | ||
"github.com/cenkalti/backoff" | ||
"github.com/docker/docker/api/types/backend" | ||
|
@@ -985,22 +984,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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this purely formatting? Unnecessary churn to migrate to the new swagger. Encapsulate these cosmetic changes in their own PR and we can review them later. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not a cosmetic change. it a result of a variable becoming a value not a pointer during migration. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @fdawg4l There are a few cosmetic changes, but I would gloss over them and focus on the pointer issue. Vast majority of the changes I've seen so far are due to Swagger and not cosmetic. |
||
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 { | ||
|
@@ -1098,14 +1100,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 | ||
|
@@ -1123,7 +1133,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() | ||
|
@@ -1132,13 +1142,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) | ||
|
@@ -1521,17 +1531,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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1. I really hate go programmer's habit of overloading one line with a bazillion operations. They need to remember the compiler doesn't care, and readability is more important. |
||
|
||
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 | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
apiclient
isn't really necessary to migrate from 1 swagger version to another. We can debate the naming of this later. Lets keep this change restricted only only the required changes to include the new swagger version. The unnecessary churn makes reviewing harder.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used tools to deal with imports, all automatic changes are not easy to manage manually.