Skip to content

Commit

Permalink
PatchEnvelopes: URL exposed to app instance reflects API
Browse files Browse the repository at this point in the history
Signed-off-by: Pavel Abramov <[email protected]>
  • Loading branch information
uncleDecart committed Oct 19, 2023
1 parent 26f847f commit 1a126a1
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 11 deletions.
9 changes: 8 additions & 1 deletion pkg/pillar/cmd/zedrouter/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ const SignerMaxSize = 65535
// DiagMaxSize is the max returned size for diag
const DiagMaxSize = 65535

// PatchEnvelopeURLPath is route used for patch envelopes
// it is used in URL composing of patch envelopes
const PatchEnvelopeURLPath = "/eve/v1/patch/"

// MetaDataServerIP is IP of meta data server
const MetaDataServerIP = "169.254.169.254"

func (z *zedrouter) makeMetadataHandler() http.Handler {
r := chi.NewRouter()

Expand Down Expand Up @@ -87,7 +94,7 @@ func (z *zedrouter) makeMetadataHandler() http.Handler {
diagHandler := &diagHandler{zedrouter: z}
r.Get("/eve/v1/diag", diagHandler.ServeHTTP)

r.Route("/eve/v1/patch/", func(r chi.Router) {
r.Route(PatchEnvelopeURLPath, func(r chi.Router) {

Check warning on line 97 in pkg/pillar/cmd/zedrouter/metadata.go

View check run for this annotation

Codecov / codecov/patch

pkg/pillar/cmd/zedrouter/metadata.go#L97

Added line #L97 was not covered by tests
r.Use(WithPatchEnvelopesByIP(z))

r.Get("/description.json", HandlePatchDescription(z))
Expand Down
6 changes: 3 additions & 3 deletions pkg/pillar/cmd/zedrouter/metadata_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,8 +517,8 @@ func (hdl AppInfoHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {

blob := types.AppBlobsAvailable{
CustomMeta: st.CustomMeta,
DownloadURL: fmt.Sprintf("http://169.254.169.254/eve/app-custom-blobs/%s",
st.DisplayName),
DownloadURL: fmt.Sprintf("http://%s/eve/app-custom-blobs/%s",
MetaDataServerIP, st.DisplayName),

Check warning on line 521 in pkg/pillar/cmd/zedrouter/metadata_handlers.go

View check run for this annotation

Codecov / codecov/patch

pkg/pillar/cmd/zedrouter/metadata_handlers.go#L520-L521

Added lines #L520 - L521 were not covered by tests
}

appInfo.AppBlobs = append(appInfo.AppBlobs, blob)
Expand Down Expand Up @@ -583,7 +583,7 @@ func HandlePatchDescription(z *zedrouter) func(http.ResponseWriter, *http.Reques
// WithPatchEnvelopesByIP middleware returns envelopes which are more than 0
envelopes := r.Context().Value(patchEnvelopesContextKey).(types.PatchEnvelopeInfoList)

Check warning on line 584 in pkg/pillar/cmd/zedrouter/metadata_handlers.go

View check run for this annotation

Codecov / codecov/patch

pkg/pillar/cmd/zedrouter/metadata_handlers.go#L584

Added line #L584 was not covered by tests

b, err := PatchEnvelopesJSONForAppInstance(envelopes)
b, err := patchEnvelopesJSONForAppInstance(envelopes)

Check warning on line 586 in pkg/pillar/cmd/zedrouter/metadata_handlers.go

View check run for this annotation

Codecov / codecov/patch

pkg/pillar/cmd/zedrouter/metadata_handlers.go#L586

Added line #L586 was not covered by tests
if err != nil {
http.Error(w, err.Error(), http.StatusUnprocessableEntity)
return
Expand Down
28 changes: 22 additions & 6 deletions pkg/pillar/cmd/zedrouter/patchenvelopes.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ import (
// both inline binary artifacts which are ready to be downloaded by app instances
// and volume references, which are handled by volumemgr. So PatchEnvelopes struct has
// VolumeStatusCh and PatchEnvelopeInfoCh to process volumes and patch envelopes.
// NewPatchEnvelopes() starts coroutine processMessages which reads from both channels
// NewPatchEnvelopes() starts goroutine processMessages which reads from both channels
// and stores latest state of patch envelopes exposed to app instances via Get()
//
// That means that VolumeStatusCh and PatchEnvelopeInfoCh should not be read from anywhere
// except processMessages(). so there is no deadlocks.
// except processMessages(), also it will not wait/hang by sending information to other channels
// so there is no deadlocks.
type PatchEnvelopes struct {
sync.Mutex
VolumeStatusCh chan PatchEnvelopesVsCh
Expand Down Expand Up @@ -57,7 +58,7 @@ type PatchEnvelopesVsCh struct {
Action PatchEnvelopesVsChAction
}

// NewPatchEnvelopes returns PatchEnvelopes structure and starts coroutine
// NewPatchEnvelopes returns PatchEnvelopes structure and starts goroutine
// to process messages from channels. Note that we create buffered channels
// to avoid unbounded processing time in writing to channel
func NewPatchEnvelopes(log *base.LogObject) *PatchEnvelopes {
Expand Down Expand Up @@ -91,6 +92,8 @@ func (pes *PatchEnvelopes) Get(appUUID string) types.PatchEnvelopeInfoList {
}
}

// processMessages and the function it calls not block, hence
// they can not send to any channels
func (pes *PatchEnvelopes) processMessages() {
for {
select {
Expand Down Expand Up @@ -222,15 +225,28 @@ type peInfoToDisplay struct {
VolumeRefs []types.BinaryBlobVolumeRef
}

// PatchEnvelopesJSONForAppInstance returns json representation
// patchEnvelopesJSONForAppInstance returns json representation
// of Patch Envelopes list which are shown to app instances
func PatchEnvelopesJSONForAppInstance(pe types.PatchEnvelopeInfoList) ([]byte, error) {
func patchEnvelopesJSONForAppInstance(pe types.PatchEnvelopeInfoList) ([]byte, error) {
toDisplay := make([]peInfoToDisplay, len(pe.Envelopes))

for i, envelope := range pe.Envelopes {

var binaryBlobs []types.BinaryBlobCompleted
binaryBlobs = nil
if envelope.BinaryBlobs != nil {
binaryBlobs = make([]types.BinaryBlobCompleted, len(envelope.BinaryBlobs))
copy(binaryBlobs, envelope.BinaryBlobs)
}

Check warning on line 240 in pkg/pillar/cmd/zedrouter/patchenvelopes.go

View check run for this annotation

Codecov / codecov/patch

pkg/pillar/cmd/zedrouter/patchenvelopes.go#L230-L240

Added lines #L230 - L240 were not covered by tests

for j := range binaryBlobs {
url := fmt.Sprintf("http://%s%sdownload/%s/%s", MetaDataServerIP, PatchEnvelopeURLPath, envelope.PatchID, binaryBlobs[j].FileName)
binaryBlobs[j].URL = url
}

Check warning on line 245 in pkg/pillar/cmd/zedrouter/patchenvelopes.go

View check run for this annotation

Codecov / codecov/patch

pkg/pillar/cmd/zedrouter/patchenvelopes.go#L242-L245

Added lines #L242 - L245 were not covered by tests

toDisplay[i] = peInfoToDisplay{
PatchID: envelope.PatchID,
BinaryBlobs: envelope.BinaryBlobs,
BinaryBlobs: binaryBlobs,
VolumeRefs: envelope.VolumeRefs,
}

Check warning on line 251 in pkg/pillar/cmd/zedrouter/patchenvelopes.go

View check run for this annotation

Codecov / codecov/patch

pkg/pillar/cmd/zedrouter/patchenvelopes.go#L247-L251

Added lines #L247 - L251 were not covered by tests
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/pillar/types/patchenvelopestypes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestPatchEnvelopeInfoList(t *testing.T) {

pe.Envelopes = append(pe.Envelopes, peInfo)

g.Expect(pe.Get(uuidString)).To(gomega.BeEquivalentTo([]types.PatchEnvelopeInfo{peInfo}))
g.Expect(pe.Get(uuidString).Envelopes).To(gomega.BeEquivalentTo([]types.PatchEnvelopeInfo{peInfo}))
}

func TestFindPatchEnvelopeById(t *testing.T) {
Expand Down

0 comments on commit 1a126a1

Please sign in to comment.