Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Add events for container release #1472

Merged
merged 4 commits into from
Nov 8, 2018
Merged

Add events for container release #1472

merged 4 commits into from
Nov 8, 2018

Conversation

lelenanam
Copy link
Contributor

For ReleaseEventMetadata use spec with both types: ReleaseImageSpec and ReleaseContainersSpec.

Need this for weaveworks/service#2277

@lelenanam lelenanam requested review from squaremo and rndstr October 26, 2018 04:15
Copy link
Member

@squaremo squaremo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Elena, I reckon this needs a test that makes sure it will decode old serialisations of release metadata -- even if it's just to prevent regressions. I've elaborated in a comment.

// ReleaseEventMetadata is the metadata for when service(s) are released
type ReleaseEventMetadata struct {
ReleaseEventCommon
Spec update.ReleaseSpec `json:"spec"`
Cause update.Cause `json:"cause"`
Spec ReleaseSpec `json:"spec"`

This comment was marked as abuse.

@lelenanam
Copy link
Contributor Author

Right, but if I use embedded structs there is another issue here: json.Marshal skips embedded fields with the same names like Kind and Force (ambiguous selector?):

type ReleaseImageSpec struct {
    ServiceSpecs    []ResourceSpec
    ImageSpec    ImageSpec
    Kind        ReleaseKind
    Excludes    []flux.ResourceID
    Force        bool
}

type ReleaseContainersSpec struct {
    Kind        ReleaseKind
    ContainerSpecs    map[flux.ResourceID][]ContainerUpdate
    SkipMismatches    bool
    Force        bool
}

So now I tend to use a separate type for ReleaseContainersSpec, like:

type ReleaseContainersEventMetadata struct {
	ReleaseEventCommon
	Spec  update.ReleaseContainersSpec `json:"spec"`
	Cause update.Cause                 `json:"cause"`
}

@squaremo
Copy link
Member

squaremo commented Oct 30, 2018

json.Marshal skips embedded fields with the same names like Kind and Force (ambiguous selector?):

Argh, I knew there was a catch.

How about something like

func (s *ReleaseSpec) UnmarshalJSON(b []byte) error {
  type T ReleaseSpec
  t := (*T)(s)
  if err := json.Unmarshal(b, t); er != nil {
    return err
  }

  switch t.Type {
    case "":
      // it's an old-style inline update.ReleaseSpec
      var r update.ReleaseSpec
      json.Unmarshal(b, &r)
      s.ReleaseImageSpec = &r
      s.Type = ReleaseImageSpecType
    case ReleaseContainersSpecType, ReleaseImageSpecType:
      // great, we got a new style value
    default:
      return errors.New("unknown type")
  }
  return nil
}

EDIT: alias -> type (fingers faster than brain)

@lelenanam
Copy link
Contributor Author

Not sure what it means:

alias T ReleaseSpec
  t := (*T)(s)

but if I marshal a struct with named fields:

type ReleaseSpec struct {
	Type                  string
	ReleaseImageSpec      *update.ReleaseImageSpec
	ReleaseContainersSpec *update.ReleaseContainersSpec
}

and then try to unmarshal it, I have runtime: goroutine stack exceeds 1000000000-byte limit fatal error: stack overflow during unmarshal because of recursion calls in UnmarshalJSON.

@squaremo
Copy link
Member

squaremo commented Oct 31, 2018

and then try to unmarshal it, I have runtime: goroutine stack exceeds 1000000000-byte limit fatal error: stack overflow during unmarshal because of recursion calls in UnmarshalJSON.

For posterity: the purpose of the type alias is to create a type that has the same fields (and can therefore be unmarshalled into) but not the UnmarshalJSON method; it's having the latter that leads to stack overflow.

@lelenanam
Copy link
Contributor Author

@squaremo Added UnmarshalJSON method, PTAL.

Copy link
Member

@squaremo squaremo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a bit of extra decoding which I don't think is necessary (see comments). Also, can you make sure you're using the most recent dep (v0.5.0); I wouldn't expect any changes to Gopkg.lock (and those that are there look like formatting/metadata changes).

event/event.go Outdated
func (s *ReleaseSpec) UnmarshalJSON(b []byte) error {
type T ReleaseSpec
t := (*T)(s)
if err := json.Unmarshal(b, &t); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

t is already a pointer *T, so you don't need to pass its address.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

event/event.go Outdated

case ReleaseImageSpecType, ReleaseContainersSpecType:
r := (*T)(s)
if err := json.Unmarshal(b, &r); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r is already a pointer; but moreover, this decoding has already been done, in line 264. I think this case is a no-op -- all the fields will have been decoded. (Is there a test that doesn't pass unless this line is present?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

For ReleaseEventMetadata use spec with both types: ReleaseImageSpec and ReleaseContainersSpec

Need this to fix weaveworks/service#2277
lelenanam added a commit to weaveworks/service that referenced this pull request Nov 6, 2018
lelenanam added a commit to weaveworks/service that referenced this pull request Nov 6, 2018
lelenanam added a commit to weaveworks/service that referenced this pull request Nov 6, 2018
lelenanam added a commit to weaveworks/service that referenced this pull request Nov 6, 2018
lelenanam added a commit to weaveworks/service that referenced this pull request Nov 6, 2018
lelenanam added a commit to weaveworks/service that referenced this pull request Nov 6, 2018
event/event.go Outdated Show resolved Hide resolved
Copy link
Member

@squaremo squaremo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks Elena!

@squaremo squaremo merged commit 73256e9 into master Nov 8, 2018
lelenanam added a commit to weaveworks/service that referenced this pull request Nov 14, 2018
lelenanam added a commit to weaveworks/service that referenced this pull request Nov 14, 2018
@hiddeco hiddeco deleted the promo-notifs-2 branch April 11, 2019 13:07
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants