Skip to content

Commit

Permalink
Merge pull request kubernetes-sigs#342 from akutz/bugfix/status-patches
Browse files Browse the repository at this point in the history
Fixes failed JSON patches
  • Loading branch information
k8s-ci-robot authored Jun 24, 2019
2 parents c3d8e84 + 22472b2 commit 1b1e31e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 24 deletions.
24 changes: 12 additions & 12 deletions pkg/cloud/vsphere/context/cluster_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,21 +301,19 @@ func (c *ClusterContext) ControlPlaneEndpoint() (string, error) {

// Patch updates the cluster on the API server.
func (c *ClusterContext) Patch() error {
ext, err := v1alpha1.EncodeClusterSpec(c.ClusterConfig)
// Ensure the provider spec is encoded.
newProviderSpec, err := v1alpha1.EncodeClusterSpec(c.ClusterConfig)
if err != nil {
return errors.Wrapf(err, "failed encoding cluster spec for cluster %q", c)
}
newStatus, err := v1alpha1.EncodeClusterStatus(c.ClusterStatus)
if err != nil {
return errors.Wrapf(err, "failed encoding cluster status for cluster %q", c)
}
ext.Object = nil
newStatus.Object = nil
c.Cluster.Spec.ProviderSpec.Value = newProviderSpec

c.Cluster.Spec.ProviderSpec.Value = ext
// Make sure the status isn't part of the JSON patch.
newStatus := c.Cluster.Status.DeepCopy()
c.Cluster.Status = clusterv1.ClusterStatus{}
c.ClusterCopy.Status.DeepCopyInto(&c.Cluster.Status)

// Build a patch and marshal that patch to something the client will
// understand.
// Build and marshal a patch for the cluster object, minus the status.
p, err := patch.NewJSONPatch(c.ClusterCopy, c.Cluster)
if err != nil {
return errors.Wrapf(err, "failed to create new JSONPatch for cluster %q", c)
Expand All @@ -332,7 +330,6 @@ func (c *ClusterContext) Patch() error {
c.Logger.V(6).Info("generated json patch for cluster", "json-patch", string(pb))

result, err := c.ClusterClient.Patch(c.Cluster.Name, types.JSONPatchType, pb)
//result, err := c.ClusterClient.Update(c.Cluster)
if err != nil {
record.Warnf(c.Cluster, updateFailure, "failed to update cluster config %q: %v", c, err)
return errors.Wrapf(err, "failed to patch cluster %q", c)
Expand All @@ -344,7 +341,10 @@ func (c *ClusterContext) Patch() error {
c.Cluster.ResourceVersion = result.ResourceVersion
}

c.Cluster.Status.ProviderStatus = newStatus
// Put the status back.
c.Cluster.Status = clusterv1.ClusterStatus{}
newStatus.DeepCopyInto(&c.Cluster.Status)

if !reflect.DeepEqual(c.Cluster.Status, c.ClusterCopy.Status) {
c.Logger.V(1).Info("updating cluster status")
if _, err := c.ClusterClient.UpdateStatus(c.Cluster); err != nil {
Expand Down
23 changes: 11 additions & 12 deletions pkg/cloud/vsphere/context/machine_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,21 +223,19 @@ func (c *MachineContext) ControlPlaneEndpoint() (string, error) {
// Patch updates the machine on the API server.
func (c *MachineContext) Patch() error {

ext, err := v1alpha1.EncodeMachineSpec(c.MachineConfig)
// Ensure the provider spec is encoded.
newProviderSpec, err := v1alpha1.EncodeMachineSpec(c.MachineConfig)
if err != nil {
return errors.Wrapf(err, "failed encoding machine spec for machine %q", c)
}
newStatus, err := v1alpha1.EncodeMachineStatus(c.MachineStatus)
if err != nil {
return errors.Wrapf(err, "failed encoding machine status for machine %q", c)
}
ext.Object = nil
newStatus.Object = nil
c.Machine.Spec.ProviderSpec.Value = newProviderSpec

c.Machine.Spec.ProviderSpec.Value = ext
// Make sure the status isn't part of the JSON patch.
newStatus := c.Machine.Status.DeepCopy()
c.Machine.Status = clusterv1.MachineStatus{}
c.MachineCopy.Status.DeepCopyInto(&c.Machine.Status)

// Build a patch and marshal that patch to something the client will
// understand.
// Build and marshal a patch for the machine object, minus the status.
p, err := patch.NewJSONPatch(c.MachineCopy, c.Machine)
if err != nil {
return errors.Wrapf(err, "failed to create new JSONPatch for machine %q", c)
Expand All @@ -254,7 +252,6 @@ func (c *MachineContext) Patch() error {
c.Logger.V(6).Info("generated json patch for machine", "json-patch", string(pb))

result, err := c.MachineClient.Patch(c.Machine.Name, apitypes.JSONPatchType, pb)
//result, err := c.MachineClient.Update(c.Machine)
if err != nil {
record.Warnf(c.Machine, updateFailure, "failed to update machine config %q: %v", c, err)
return errors.Wrapf(err, "failed to patch machine %q", c)
Expand All @@ -266,7 +263,9 @@ func (c *MachineContext) Patch() error {
c.Machine.ResourceVersion = result.ResourceVersion
}

c.Machine.Status.ProviderStatus = newStatus
// Put the status back.
c.Machine.Status = clusterv1.MachineStatus{}
newStatus.DeepCopyInto(&c.Machine.Status)

if !reflect.DeepEqual(c.Machine.Status, c.MachineCopy.Status) {
c.Logger.V(1).Info("updating machine status")
Expand Down

0 comments on commit 1b1e31e

Please sign in to comment.