Skip to content

Commit

Permalink
fix: oc idle: marshal versioned objects
Browse files Browse the repository at this point in the history
In Kubernetes 1.6, the internal version objects lack the appropriate
annotations to marshal, so we have to convert to versioned objects
before marshaling when creating a patch in `oc idle`.
  • Loading branch information
DirectXMan12 authored and deads2k committed Apr 27, 2017
1 parent 3068df0 commit 35b0c46
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions pkg/cmd/cli/cmd/idle.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,12 +485,16 @@ func setIdleAnnotations(serviceName types.NamespacedName, annotations map[string

// patchObj patches calculates a patch between the given new object and the existing marshaled object
func patchObj(obj runtime.Object, metadata metav1.Object, oldData []byte, mapping *meta.RESTMapping, f *clientcmd.Factory) (runtime.Object, error) {
newData, err := json.Marshal(obj)
versionedObj, err := mapping.ObjectConvertor.ConvertToVersion(obj, schema.GroupVersions{mapping.GroupVersionKind.GroupVersion()})
if err != nil {
return nil, err
}
newData, err := json.Marshal(versionedObj)
if err != nil {
return nil, err
}

patchBytes, err := strategicpatch.CreateTwoWayMergePatch(oldData, newData, obj)
patchBytes, err := strategicpatch.CreateTwoWayMergePatch(oldData, newData, versionedObj)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -597,14 +601,22 @@ func (o *IdleOptions) RunIdle(f *clientcmd.Factory) error {
hadError = true
continue
}
oldData, err := json.Marshal(info.obj)
// we need a versioned obj to properly marshal to JSON, so that we can compute the patch
mapping, err := mapper.RESTMapping(gvks[0].GroupKind(), gvks[0].Version)
if err != nil {
fmt.Fprintf(o.errOut, "error: unable to mark service %q as idled: %v", serviceName.String(), err)
hadError = true
continue
}

mapping, err := mapper.RESTMapping(gvks[0].GroupKind(), gvks[0].Version)
versionedObj, err := mapping.ObjectConvertor.ConvertToVersion(info.obj, schema.GroupVersions{gvks[0].GroupVersion()})
if err != nil {
fmt.Fprintf(o.errOut, "error: unable to mark service %q as idled: %v", serviceName.String(), err)
hadError = true
continue
}

oldData, err := json.Marshal(versionedObj)
if err != nil {
fmt.Fprintf(o.errOut, "error: unable to mark service %q as idled: %v", serviceName.String(), err)
hadError = true
Expand Down

0 comments on commit 35b0c46

Please sign in to comment.