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

Introduce ExtendedObjectMetadata, holding Writes #87

Merged
merged 3 commits into from
Feb 13, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
39 changes: 24 additions & 15 deletions staging/kos/pkg/apis/network/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,19 @@ type ObjectWrite struct {
// GetServerWriteTime returns the server time of the write to the
// given section, or zero if there was none.
func (writes WriteSet) GetServerWriteTime(section string) Timestamp {
return writes.GetWrite(section).ServerTime
wr, _ := writes.GetWrite(section)
return wr.ServerTime
}

// GetWrite returns the write to the given section, or zero if there
// was none.
func (writes WriteSet) GetWrite(section string) ObjectWrite {
// GetWrite returns the write to the given section, and a bool
// indicating whether there is one.
func (writes WriteSet) GetWrite(section string) (ObjectWrite, bool) {
for _, wr := range writes {
if wr.Section == section {
return wr
return wr, true
}
}
return ObjectWrite{}
return ObjectWrite{}, false
}

// SetWrite produces a revised slice that includes the given write.
Expand All @@ -83,7 +84,7 @@ func (writes WriteSet) SetWrite(section string, serverTime Timestamp) WriteSet {
func (writes WriteSet) Diff(others WriteSet) WriteSet {
ans := make(WriteSet, 0, len(writes))
for _, wr := range writes {
owr := others.GetWrite(wr.Section)
owr, _ := others.GetWrite(wr.Section)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Other nit, but the following check could use the bool variable.

if owr == (ObjectWrite{}) {
ans = append(ans, wr)
}
Expand All @@ -96,8 +97,8 @@ func (writes WriteSet) Diff(others WriteSet) WriteSet {
func (writes WriteSet) UnionLeft(others WriteSet) WriteSet {
ans := append(WriteSet{}, writes...)
for _, owr := range others {
wr := ans.GetWrite(owr.Section)
if wr == (ObjectWrite{}) {
_, found := ans.GetWrite(owr.Section)
if !found {
ans = append(ans, owr)
}
}
Expand All @@ -109,9 +110,13 @@ func (writes WriteSet) UnionLeft(others WriteSet) WriteSet {
func (writes WriteSet) UnionMin(others WriteSet) WriteSet {
ans := others.Diff(writes)
for _, wr := range writes {
owr := others.GetWrite(wr.Section)
owr.ServerTime = owr.ServerTime.Min(wr.ServerTime)
ans = append(ans, owr)
owr, found := others.GetWrite(wr.Section)
if found {
owr.ServerTime = owr.ServerTime.Min(wr.ServerTime)
ans = append(ans, owr)
} else {
ans = append(ans, wr)
}
}
return ans
}
Expand All @@ -121,9 +126,13 @@ func (writes WriteSet) UnionMin(others WriteSet) WriteSet {
func (writes WriteSet) UnionMax(others WriteSet) WriteSet {
ans := others.Diff(writes)
for _, wr := range writes {
owr := others.GetWrite(wr.Section)
owr.ServerTime = owr.ServerTime.Max(wr.ServerTime)
ans = append(ans, owr)
owr, found := others.GetWrite(wr.Section)
if found {
owr.ServerTime = owr.ServerTime.Max(wr.ServerTime)
ans = append(ans, owr)
} else {
ans = append(ans, wr)
}
}
return ans
}
Expand Down
39 changes: 24 additions & 15 deletions staging/kos/pkg/apis/network/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,19 @@ type ObjectWrite struct {
// GetServerWriteTime returns the server time of the write to the
// given section, or zero if there was none.
func (writes WriteSet) GetServerWriteTime(section string) Timestamp {
return writes.GetWrite(section).ServerTime
wr, _ := writes.GetWrite(section)
return wr.ServerTime
}

// GetWrite returns the write to the given section, or zero if there
// was none.
func (writes WriteSet) GetWrite(section string) ObjectWrite {
// GetWrite returns the write to the given section, and a bool
// indicating whether there is one.
func (writes WriteSet) GetWrite(section string) (ObjectWrite, bool) {
for _, wr := range writes {
if wr.Section == section {
return wr
return wr, true
}
}
return ObjectWrite{}
return ObjectWrite{}, false
}

// SetWrite produces a revised slice that includes the given write.
Expand All @@ -83,7 +84,7 @@ func (writes WriteSet) SetWrite(section string, serverTime Timestamp) WriteSet {
func (writes WriteSet) Diff(others WriteSet) WriteSet {
ans := make(WriteSet, 0, len(writes))
for _, wr := range writes {
owr := others.GetWrite(wr.Section)
owr, _ := others.GetWrite(wr.Section)
if owr == (ObjectWrite{}) {
ans = append(ans, wr)
}
Expand All @@ -96,8 +97,8 @@ func (writes WriteSet) Diff(others WriteSet) WriteSet {
func (writes WriteSet) UnionLeft(others WriteSet) WriteSet {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This method and the other Union methods are never used (same observation for internal API version types). Do you plan other commits where they are used?

Copy link
Owner Author

Choose a reason for hiding this comment

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

Yes. I have some vague plans that use various of these.

ans := append(WriteSet{}, writes...)
for _, owr := range others {
wr := ans.GetWrite(owr.Section)
if wr == (ObjectWrite{}) {
_, found := ans.GetWrite(owr.Section)
if !found {
ans = append(ans, owr)
}
}
Expand All @@ -109,9 +110,13 @@ func (writes WriteSet) UnionLeft(others WriteSet) WriteSet {
func (writes WriteSet) UnionMin(others WriteSet) WriteSet {
ans := others.Diff(writes)
for _, wr := range writes {
owr := others.GetWrite(wr.Section)
owr.ServerTime = owr.ServerTime.Min(wr.ServerTime)
ans = append(ans, owr)
owr, found := others.GetWrite(wr.Section)
if found {
owr.ServerTime = owr.ServerTime.Min(wr.ServerTime)
ans = append(ans, owr)
} else {
ans = append(ans, wr)
}
}
return ans
}
Expand All @@ -121,9 +126,13 @@ func (writes WriteSet) UnionMin(others WriteSet) WriteSet {
func (writes WriteSet) UnionMax(others WriteSet) WriteSet {
ans := others.Diff(writes)
for _, wr := range writes {
owr := others.GetWrite(wr.Section)
owr.ServerTime = owr.ServerTime.Max(wr.ServerTime)
ans = append(ans, owr)
owr, found := others.GetWrite(wr.Section)
if found {
owr.ServerTime = owr.ServerTime.Max(wr.ServerTime)
ans = append(ans, owr)
} else {
ans = append(ans, wr)
}
}
return ans
}
Expand Down
17 changes: 3 additions & 14 deletions staging/kos/pkg/registry/network/networkattachment/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ func (networkattachmentStrategy) PrepareForUpdate(ctx context.Context, obj, old
newNA := obj.(*network.NetworkAttachment)
newNA.ExtendedObjectMeta = oldNA.ExtendedObjectMeta
now := network.Now()
if oldNA.Spec.Node != newNA.Spec.Node || oldNA.Spec.Subnet != newNA.Spec.Subnet || !SliceOfStringEqual(oldNA.Spec.PostCreateExec, newNA.Spec.PostCreateExec) || !SliceOfStringEqual(oldNA.Spec.PostDeleteExec, newNA.Spec.PostDeleteExec) {
// ValidateUpdate insists that the only Spec field that can change is PostDeleteExec
if !SliceOfStringEqual(oldNA.Spec.PostDeleteExec, newNA.Spec.PostDeleteExec) {
newNA.Writes = newNA.Writes.SetWrite(network.NASectionSpec, now)
}
if oldNA.Status.LockUID != newNA.Status.LockUID || oldNA.Status.AddressVNI != newNA.Status.AddressVNI || oldNA.Status.IPv4 != newNA.Status.IPv4 || !SliceOfStringEqual(oldNA.Status.Errors.IPAM, newNA.Status.Errors.IPAM) {
Expand Down Expand Up @@ -148,20 +149,8 @@ func (networkattachmentStrategy) ValidateUpdate(ctx context.Context, obj, old ru
if newNa.Spec.Subnet != oldNa.Spec.Subnet {
errs = append(errs, field.Forbidden(field.NewPath("spec", "subnet"), immutableFieldMsg))
}
if differ(newNa.Spec.PostCreateExec, oldNa.Spec.PostCreateExec) {
if !SliceOfStringEqual(newNa.Spec.PostCreateExec, oldNa.Spec.PostCreateExec) {
errs = append(errs, field.Forbidden(field.NewPath("spec", "postCreateExec"), immutableFieldMsg))
}
return errs
}

func differ(s1, s2 []string) bool {
if len(s1) != len(s2) {
return true
}
for i := range s1 {
if s1[i] != s2[i] {
return true
}
}
return false
}
4 changes: 1 addition & 3 deletions staging/kos/pkg/registry/network/subnet/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,7 @@ func (*subnetStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Ob
newSubnet := obj.(*network.Subnet)
newSubnet.ExtendedObjectMeta = oldSubnet.ExtendedObjectMeta
now := network.Now()
if newSubnet.Spec != oldSubnet.Spec {
newSubnet.Writes = newSubnet.Writes.SetWrite(network.SubnetSectionSpec, now)
}
// the Spec is immutable, see ValidateUpdate
if newSubnet.Status.Validated != oldSubnet.Status.Validated || !SliceOfStringEqual(newSubnet.Status.Errors, oldSubnet.Status.Errors) {
newSubnet.Writes = newSubnet.Writes.SetWrite(network.SubnetSectionStatus, now)
}
Expand Down