Skip to content

Commit

Permalink
Update flux package with new listImages fields and query parameters (#…
Browse files Browse the repository at this point in the history
…2045)

* Update flux package with new listImages fields and query parameters

* Update flux package

* Update listImages to use ListImagesWithOptions

* Update flux-api/bus

* Add ListImagesWithOptions for server

* Remove tests for mandatory arguments because we now have defaults
  • Loading branch information
aaron7 authored Jun 13, 2018
1 parent 5164f76 commit 477911e
Show file tree
Hide file tree
Showing 37 changed files with 622 additions and 143 deletions.
5 changes: 3 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 44 additions & 11 deletions flux-api/bus/nats/bus.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/nats-io/go-nats"

"github.com/weaveworks/flux/api"
"github.com/weaveworks/flux/api/v10"
"github.com/weaveworks/flux/api/v6"
"github.com/weaveworks/flux/api/v9"
fluxerr "github.com/weaveworks/flux/errors"
Expand All @@ -29,17 +30,18 @@ const (
presenceTick = 50 * time.Millisecond
encoderType = nats.JSON_ENCODER

methodKick = ".Platform.Kick"
methodPing = ".Platform.Ping"
methodVersion = ".Platform.Version"
methodExport = ".Platform.Export"
methodListServices = ".Platform.ListServices"
methodListImages = ".Platform.ListImages"
methodJobStatus = ".Platform.JobStatus"
methodSyncStatus = ".Platform.SyncStatus"
methodUpdateManifests = ".Platform.UpdateManifests"
methodGitRepoConfig = ".Platform.GitRepoConfig"
methodNotifyChange = ".Platform.NotifyChange"
methodKick = ".Platform.Kick"
methodPing = ".Platform.Ping"
methodVersion = ".Platform.Version"
methodExport = ".Platform.Export"
methodListServices = ".Platform.ListServices"
methodListImages = ".Platform.ListImages"
methodListImagesWithOptions = ".Platform.ListImagesWithOptions"
methodJobStatus = ".Platform.JobStatus"
methodSyncStatus = ".Platform.SyncStatus"
methodUpdateManifests = ".Platform.UpdateManifests"
methodGitRepoConfig = ".Platform.GitRepoConfig"
methodNotifyChange = ".Platform.NotifyChange"
)

var (
Expand Down Expand Up @@ -153,6 +155,12 @@ type ListImagesResponse struct {
ErrorResponse `json:",omitempty"`
}

// ListImagesWithOptionsResponse is the ListImagesWithOptions response.
type ListImagesWithOptionsResponse struct {
Result []v6.ImageStatus
ErrorResponse `json:",omitempty"`
}

// UpdateManifestsResponse is the UpdateManifests response.
type UpdateManifestsResponse struct {
Result job.ID
Expand Down Expand Up @@ -265,6 +273,16 @@ func (r *natsPlatform) ListImages(ctx context.Context, spec update.ResourceSpec)
return response.Result, extractError(response.ErrorResponse)
}

func (r *natsPlatform) ListImagesWithOptions(ctx context.Context, opts v10.ListImagesOptions) ([]v6.ImageStatus, error) {
var response ListImagesWithOptionsResponse
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
if err := r.conn.RequestWithContext(ctx, r.instance+methodListImagesWithOptions, opts, &response); err != nil {
return response.Result, remote.UnavailableError(err)
}
return response.Result, extractError(response.ErrorResponse)
}

func (r *natsPlatform) UpdateManifests(ctx context.Context, u update.Spec) (job.ID, error) {
var response UpdateManifestsResponse
ctx, cancel := context.WithTimeout(ctx, timeout)
Expand Down Expand Up @@ -403,6 +421,8 @@ func (n *NATS) processRequest(ctx context.Context, request *nats.Msg, instID ser
err = n.processListServices(ctx, request, platform)
case strings.HasSuffix(request.Subject, methodListImages):
err = n.processListImages(ctx, request, platform)
case strings.HasSuffix(request.Subject, methodListImagesWithOptions):
err = n.processListImagesWithOptions(ctx, request, platform)
case strings.HasSuffix(request.Subject, methodUpdateManifests):
err = n.processUpdateManifests(ctx, request, platform)
case strings.HasSuffix(request.Subject, methodJobStatus):
Expand Down Expand Up @@ -495,6 +515,19 @@ func (n *NATS) processListImages(ctx context.Context, request *nats.Msg, platfor
return err
}

func (n *NATS) processListImagesWithOptions(ctx context.Context, request *nats.Msg, platform api.UpstreamServer) error {
var (
req v10.ListImagesOptions
res []v6.ImageStatus
)
err := encoder.Decode(request.Subject, request.Data, &req)
if err == nil {
res, err = platform.ListImagesWithOptions(ctx, req)
}
n.enc.Publish(request.Reply, ListImagesWithOptionsResponse{res, makeErrorResponse(err)})
return err
}

func (n *NATS) processUpdateManifests(ctx context.Context, request *nats.Msg, platform api.UpstreamServer) error {
var (
req update.Spec
Expand Down
39 changes: 38 additions & 1 deletion flux-api/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/weaveworks/flux"

fluxapi "github.com/weaveworks/flux/api"
"github.com/weaveworks/flux/api/v10"
"github.com/weaveworks/flux/api/v9"
fluxerr "github.com/weaveworks/flux/errors"
"github.com/weaveworks/flux/event"
Expand Down Expand Up @@ -106,6 +107,8 @@ func (s Server) MakeHandler(r *mux.Router) http.Handler {
transport.SyncStatus: s.syncStatus,
transport.JobStatus: s.jobStatus,
transport.GitRepoConfig: s.gitRepoConfig,
// flux/api/ServerV10
transport.ListImagesWithOptions: s.listImagesWithOptions,
// fluxctl legacy routes
transport.UpdateImages: s.updateImages,
transport.UpdatePolicies: s.updatePolicies,
Expand Down Expand Up @@ -151,7 +154,9 @@ func (s Server) listServices(w http.ResponseWriter, r *http.Request) {

func (s Server) listImages(w http.ResponseWriter, r *http.Request) {
ctx := getRequestContext(r)
service := mux.Vars(r)["service"]
queryValues := r.URL.Query()
service := queryValues.Get("service")

spec, err := update.ParseResourceSpec(service)
if err != nil {
transport.WriteError(w, r, http.StatusBadRequest, errors.Wrapf(err, "parsing service spec %q", service))
Expand All @@ -167,6 +172,38 @@ func (s Server) listImages(w http.ResponseWriter, r *http.Request) {
transport.JSONResponse(w, r, d)
}

func (s Server) listImagesWithOptions(w http.ResponseWriter, r *http.Request) {
var opts v10.ListImagesOptions
ctx := getRequestContext(r)
queryValues := r.URL.Query()

// service - Select services to update.
service := queryValues.Get("service")
if service == "" {
service = string(update.ResourceSpecAll)
}
spec, err := update.ParseResourceSpec(service)
if err != nil {
transport.WriteError(w, r, http.StatusBadRequest, errors.Wrapf(err, "parsing service spec %q", service))
return
}
opts.Spec = spec

// containerFields - Override which fields to return in the container struct.
containerFields := queryValues.Get("containerFields")
if containerFields != "" {
opts.OverrideContainerFields = strings.Split(containerFields, ",")
}

d, err := s.daemonProxy.ListImagesWithOptions(ctx, opts)
if err != nil {
transport.ErrorResponse(w, r, err)
return
}

transport.JSONResponse(w, r, d)
}

func (s Server) updateManifests(w http.ResponseWriter, r *http.Request) {
ctx := getRequestContext(r)

Expand Down
47 changes: 27 additions & 20 deletions flux-api/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"testing"
"time"

"github.com/weaveworks/flux/api/v10"

"github.com/go-kit/kit/log"
"github.com/gorilla/mux"

Expand Down Expand Up @@ -193,20 +195,6 @@ func TestFluxsvc_ListServices(t *testing.T) {
if svcs[0].ID.String() != helloWorldSvc && svcs[1].ID.String() != helloWorldSvc {
t.Errorf("Expected one of the services to be %q", helloWorldSvc)
}

// Test that `namespace` argument is mandatory
u, err := transport.MakeURL(ts.URL, router, "ListServices")
if err != nil {
t.Fatal(err)
}
resp, err := http.Get(u.String())
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusNotFound {
t.Fatalf("Request should result in 404, but got: %q", resp.Status)
}
}

// Note that this test will reach out to docker hub to check the images
Expand Down Expand Up @@ -240,19 +228,38 @@ func TestFluxsvc_ListImages(t *testing.T) {
if len(imgs[0].Containers) == 0 {
t.Error("Expected >1 containers")
}
}

// Note that this test will reach out to docker hub to check the images
// associated with alpine
func TestFluxsvc_ListImagesWithOptions(t *testing.T) {
setup(t)
defer teardown()

// Test that `service` argument is mandatory
u, err := transport.MakeURL(ts.URL, router, "ListImages")
ctx := context.Background()

// Test ListImagesWithOptions
imgs, err := apiClient.ListImagesWithOptions(ctx, v10.ListImagesOptions{Spec: update.ResourceSpecAll})
if err != nil {
t.Fatal(err)
}
resp, err := http.Get(u.String())
if len(imgs) != 2 {
t.Error("Expected there two sets of images")
}
if len(imgs[0].Containers) == 0 && len(imgs[1].Containers) == 0 {
t.Error("Should have been lots of containers")
}

// Test ListImagesWithOptions for specific service
imgs, err = apiClient.ListImagesWithOptions(ctx, v10.ListImagesOptions{Spec: helloWorldSvc})
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusNotFound {
t.Fatalf("Request should result in 404, but got: %s", resp.Status)
if len(imgs) != 2 {
t.Error("Expected two sets of images")
}
if len(imgs[0].Containers) == 0 {
t.Error("Expected >1 containers")
}
}

Expand Down
16 changes: 16 additions & 0 deletions flux-api/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"sync/atomic"
"time"

"github.com/weaveworks/flux/api/v10"

"github.com/go-kit/kit/log"
"github.com/pkg/errors"

Expand Down Expand Up @@ -179,6 +181,20 @@ func (s *Server) ListImages(ctx context.Context, spec update.ResourceSpec) (res
return inst.Platform.ListImages(ctx, spec)
}

// ListImagesWithOptions calls ListImagesWithOptions on the given instance.
func (s *Server) ListImagesWithOptions(ctx context.Context, opts v10.ListImagesOptions) (res []v6.ImageStatus, err error) {
instID, err := getInstanceID(ctx)
if err != nil {
return res, err
}

inst, err := s.instancer.Get(instID)
if err != nil {
return nil, errors.Wrapf(err, "getting instance "+string(instID))
}
return inst.Platform.ListImagesWithOptions(ctx, opts)
}

// UpdateImages updates images on the given instance.
func (s *Server) UpdateImages(ctx context.Context, spec update.ReleaseSpec, cause update.Cause) (job.ID, error) {
instID, err := getInstanceID(ctx)
Expand Down
10 changes: 4 additions & 6 deletions vendor/github.com/weaveworks/flux/api/api.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions vendor/github.com/weaveworks/flux/api/v10/api.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 2 additions & 9 deletions vendor/github.com/weaveworks/flux/api/v6/api.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 477911e

Please sign in to comment.