Skip to content
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

Update flux package with new listImages fields and query parameters #2045

Merged
merged 6 commits into from
Jun 13, 2018
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
36 changes: 36 additions & 0 deletions 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 @@ -152,6 +155,7 @@ func (s Server) listServices(w http.ResponseWriter, r *http.Request) {
func (s Server) listImages(w http.ResponseWriter, r *http.Request) {

This comment was marked as abuse.

ctx := getRequestContext(r)
service := mux.Vars(r)["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 +171,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
49 changes: 49 additions & 0 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 @@ -256,6 +258,53 @@ func TestFluxsvc_ListImages(t *testing.T) {
}
}

// 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()

ctx := context.Background()

// Test ListImagesWithOptions
imgs, err := apiClient.ListImagesWithOptions(ctx, v10.ListImagesOptions{Spec: update.ResourceSpecAll})
if err != nil {
t.Fatal(err)
}
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)
}
if len(imgs) != 2 {
t.Error("Expected two sets of images")
}
if len(imgs[0].Containers) == 0 {
t.Error("Expected >1 containers")
}

// Test that `service` argument is mandatory
u, err := transport.MakeURL(ts.URL, router, "ListImagesWithOptions")
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: %s", resp.Status)
}
}

func TestFluxsvc_Release(t *testing.T) {
setup(t)
defer teardown()
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