mirror of https://github.com/kubernetes/kops.git
Minor fix for json response to keep it consistent for single or multiple clusters
Fix tests another fix to json marshalling fixes to return a single json object if a specific object is selected Add changes to docs
This commit is contained in:
parent
f40bdef005
commit
2124c4cf85
|
|
@ -720,7 +720,7 @@ func RunCreateCluster(ctx context.Context, f *util.Factory, out io.Writer, c *Cr
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
case OutputJSON:
|
case OutputJSON:
|
||||||
if err := fullOutputJSON(out, obj...); err != nil {
|
if err := fullOutputJSON(out, true, obj...); err != nil {
|
||||||
return fmt.Errorf("error writing cluster json to stdout: %v", err)
|
return fmt.Errorf("error writing cluster json to stdout: %v", err)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|
|
||||||
|
|
@ -221,7 +221,7 @@ func RunCreateInstanceGroup(ctx context.Context, f *util.Factory, out io.Writer,
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
case OutputJSON:
|
case OutputJSON:
|
||||||
if err := fullOutputJSON(out, ig); err != nil {
|
if err := fullOutputJSON(out, true, ig); err != nil {
|
||||||
return fmt.Errorf("error writing cluster json to stdout: %v", err)
|
return fmt.Errorf("error writing cluster json to stdout: %v", err)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|
|
||||||
|
|
@ -158,7 +158,7 @@ func RunGet(ctx context.Context, f commandutils.Factory, out io.Writer, options
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
case OutputJSON:
|
case OutputJSON:
|
||||||
if err := fullOutputJSON(out, allObjects...); err != nil {
|
if err := fullOutputJSON(out, false, allObjects...); err != nil {
|
||||||
return fmt.Errorf("error writing json to stdout: %v", err)
|
return fmt.Errorf("error writing json to stdout: %v", err)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|
|
||||||
|
|
@ -123,17 +123,11 @@ func RunGetClusters(ctx context.Context, f commandutils.Factory, out io.Writer,
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
singleClusterSelected := false
|
||||||
var clusterList []*kopsapi.Cluster
|
var clusterList []*kopsapi.Cluster
|
||||||
if len(options.ClusterNames) != 1 {
|
if len(options.ClusterNames) == 1 {
|
||||||
list, err := client.ListClusters(ctx, metav1.ListOptions{})
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
for i := range list.Items {
|
|
||||||
clusterList = append(clusterList, &list.Items[i])
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Optimization - avoid fetching all clusters if we're only querying one
|
// Optimization - avoid fetching all clusters if we're only querying one
|
||||||
|
singleClusterSelected = true
|
||||||
cluster, err := client.GetCluster(ctx, options.ClusterNames[0])
|
cluster, err := client.GetCluster(ctx, options.ClusterNames[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !apierrors.IsNotFound(err) {
|
if !apierrors.IsNotFound(err) {
|
||||||
|
|
@ -142,6 +136,14 @@ func RunGetClusters(ctx context.Context, f commandutils.Factory, out io.Writer,
|
||||||
} else {
|
} else {
|
||||||
clusterList = append(clusterList, cluster)
|
clusterList = append(clusterList, cluster)
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
list, err := client.ListClusters(ctx, metav1.ListOptions{})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for i := range list.Items {
|
||||||
|
clusterList = append(clusterList, &list.Items[i])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
clusters, err := filterClustersByName(options.ClusterNames, clusterList)
|
clusters, err := filterClustersByName(options.ClusterNames, clusterList)
|
||||||
|
|
@ -176,7 +178,11 @@ func RunGetClusters(ctx context.Context, f commandutils.Factory, out io.Writer,
|
||||||
case OutputYaml:
|
case OutputYaml:
|
||||||
return fullOutputYAML(out, obj...)
|
return fullOutputYAML(out, obj...)
|
||||||
case OutputJSON:
|
case OutputJSON:
|
||||||
return fullOutputJSON(out, obj...)
|
// if singleClusterSelected is true, only a single object is returned
|
||||||
|
// otherwise to keep it consistent, always returns an array.
|
||||||
|
// Ex: kops get clusters -ojson should will always return an array (even if 1 cluster is available)
|
||||||
|
// kops get cluster test.example.com -o json will return a single object (since a specific cluster is selected)
|
||||||
|
return fullOutputJSON(out, singleClusterSelected, obj...)
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("Unknown output format: %q", options.Output)
|
return fmt.Errorf("Unknown output format: %q", options.Output)
|
||||||
}
|
}
|
||||||
|
|
@ -227,12 +233,10 @@ func clusterOutputTable(clusters []*kopsapi.Cluster, out io.Writer) error {
|
||||||
return t.Render(clusters, out, "NAME", "CLOUD", "ZONES")
|
return t.Render(clusters, out, "NAME", "CLOUD", "ZONES")
|
||||||
}
|
}
|
||||||
|
|
||||||
// fullOutputJson outputs the marshalled JSON of a list of clusters and instance groups. It will handle
|
// fullOutputJSON outputs the marshalled JSON of a list of clusters and instance groups. It will handle
|
||||||
// nils for clusters and instanceGroups slices.
|
// nils for clusters and instanceGroups slices.
|
||||||
func fullOutputJSON(out io.Writer, args ...runtime.Object) error {
|
func fullOutputJSON(out io.Writer, singleObject bool, args ...runtime.Object) error {
|
||||||
argsLen := len(args)
|
if !singleObject {
|
||||||
|
|
||||||
if argsLen > 1 {
|
|
||||||
if _, err := fmt.Fprint(out, "["); err != nil {
|
if _, err := fmt.Fprint(out, "["); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -249,7 +253,7 @@ func fullOutputJSON(out io.Writer, args ...runtime.Object) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if argsLen > 1 {
|
if !singleObject {
|
||||||
if _, err := fmt.Fprint(out, "]"); err != nil {
|
if _, err := fmt.Fprint(out, "]"); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -258,7 +262,7 @@ func fullOutputJSON(out io.Writer, args ...runtime.Object) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// fullOutputJson outputs the marshalled JSON of a list of clusters and instance groups. It will handle
|
// fullOutputYAML outputs the marshalled JSON of a list of clusters and instance groups. It will handle
|
||||||
// nils for clusters and instanceGroups slices.
|
// nils for clusters and instanceGroups slices.
|
||||||
func fullOutputYAML(out io.Writer, args ...runtime.Object) error {
|
func fullOutputYAML(out io.Writer, args ...runtime.Object) error {
|
||||||
for i, obj := range args {
|
for i, obj := range args {
|
||||||
|
|
|
||||||
|
|
@ -113,8 +113,12 @@ func RunGetInstanceGroups(ctx context.Context, f commandutils.Factory, out io.Wr
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
singleObject := false
|
||||||
|
|
||||||
if len(instancegroups) == 0 {
|
if len(instancegroups) == 0 {
|
||||||
return fmt.Errorf("no InstanceGroup objects found")
|
return fmt.Errorf("no InstanceGroup objects found")
|
||||||
|
} else if len(instancegroups) == 1 {
|
||||||
|
singleObject = true
|
||||||
}
|
}
|
||||||
|
|
||||||
var obj []runtime.Object
|
var obj []runtime.Object
|
||||||
|
|
@ -130,7 +134,7 @@ func RunGetInstanceGroups(ctx context.Context, f commandutils.Factory, out io.Wr
|
||||||
case OutputYaml:
|
case OutputYaml:
|
||||||
return fullOutputYAML(out, obj...)
|
return fullOutputYAML(out, obj...)
|
||||||
case OutputJSON:
|
case OutputJSON:
|
||||||
return fullOutputJSON(out, obj...)
|
return fullOutputJSON(out, singleObject, obj...)
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("unknown output format: %q", options.Output)
|
return fmt.Errorf("unknown output format: %q", options.Output)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -332,7 +332,7 @@ func RunToolboxInstanceSelector(ctx context.Context, f *util.Factory, out io.Wri
|
||||||
return fmt.Errorf("error writing cluster yaml to stdout: %v", err)
|
return fmt.Errorf("error writing cluster yaml to stdout: %v", err)
|
||||||
}
|
}
|
||||||
case OutputJSON:
|
case OutputJSON:
|
||||||
if err := fullOutputJSON(out, ig); err != nil {
|
if err := fullOutputJSON(out, true, ig); err != nil {
|
||||||
return fmt.Errorf("error writing cluster json to stdout: %v", err)
|
return fmt.Errorf("error writing cluster json to stdout: %v", err)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
|
|
|
||||||
|
|
@ -64,6 +64,8 @@ It is recommended to keep using the `v1alpha2` API version.
|
||||||
|
|
||||||
* The `kops rolling-update cluster` command has a new `--drain-timeout` flag for specifying the maximum amount of time to wait when attempting to drain a node. Previously, rolling-updates would attempt to drain a node for an indefinite amount of time. If `--drain-timeout` is not specified, a default of 15 minutes is applied.
|
* The `kops rolling-update cluster` command has a new `--drain-timeout` flag for specifying the maximum amount of time to wait when attempting to drain a node. Previously, rolling-updates would attempt to drain a node for an indefinite amount of time. If `--drain-timeout` is not specified, a default of 15 minutes is applied.
|
||||||
|
|
||||||
|
* Fix inconsistent output of `kops get clusters -ojson`. This will now always return a list (irrespective of a single or multiple clusters) to keep the format consistent. However, note that `kops get cluster dev.example.com -ojson` will continue to work as previously, and will return a single object.
|
||||||
|
|
||||||
# Full change list since 1.22.0 release
|
# Full change list since 1.22.0 release
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue