-
Notifications
You must be signed in to change notification settings - Fork 4.6k
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
Create YAML or JSON Clusterspec without creating the cluster #2954
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,13 +18,13 @@ package main | |
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
"strings" | ||
|
||
"io" | ||
|
||
"github.com/spf13/cobra" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/util/sets" | ||
"k8s.io/kops/cmd/kops/util" | ||
api "k8s.io/kops/pkg/apis/kops" | ||
|
@@ -133,7 +133,7 @@ func RunGetClusters(context Factory, out io.Writer, options *GetClusterOptions) | |
} | ||
|
||
if len(clusters) == 0 { | ||
return fmt.Errorf("No clusters found") | ||
return fmt.Errorf("no clusters found") | ||
} | ||
|
||
if options.FullSpec { | ||
|
@@ -146,14 +146,20 @@ func RunGetClusters(context Factory, out io.Writer, options *GetClusterOptions) | |
fmt.Fprint(out, get_cluster_full_warning) | ||
} | ||
|
||
var obj []runtime.Object | ||
if options.output != OutputTable { | ||
for _, c := range clusters { | ||
obj = append(obj, c) | ||
} | ||
} | ||
|
||
switch options.output { | ||
case OutputTable: | ||
return clusterOutputTable(clusters, out) | ||
case OutputYaml: | ||
return clusterOutputYAML(clusters, out) | ||
return fullOutputYAML(out, obj...) | ||
case OutputJSON: | ||
return clusterOutputJson(clusters, out) | ||
|
||
return fullOutputJSON(out, obj...) | ||
default: | ||
return fmt.Errorf("Unknown output format: %q", options.output) | ||
} | ||
|
@@ -206,23 +212,47 @@ func clusterOutputTable(clusters []*api.Cluster, out io.Writer) error { | |
return t.Render(clusters, out, "NAME", "CLOUD", "ZONES") | ||
} | ||
|
||
func clusterOutputJson(clusters []*api.Cluster, out io.Writer) error { | ||
for _, cluster := range clusters { | ||
if err := marshalToWriter(cluster, marshalJSON, out); err != nil { | ||
// fullOutputJson outputs the marshalled JSON of a list of clusters and instance groups. It will handle | ||
// nils for clusters and instanceGroups slices. | ||
func fullOutputJSON(out io.Writer, args ...runtime.Object) error { | ||
argsLen := len(args) | ||
|
||
if argsLen > 1 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should always write the array, even if it is empty. I don't think an empty string is valid JSON? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. JSON object is not an array. For instance when we create an ig, we just get a single object There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can always write an array, but at least with a single JSON object create -f works, with a JSON array it bombs There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We discussed this; you are right - better to only write the array if there are multiple members. This way a single object is an object |
||
if _, err := fmt.Fprint(out, "["); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
for i, arg := range args { | ||
if i != 0 { | ||
if _, err := fmt.Fprint(out, ","); err != nil { | ||
return err | ||
} | ||
} | ||
if err := marshalToWriter(arg, marshalJSON, out); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
if argsLen > 1 { | ||
if _, err := fmt.Fprint(out, "]"); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func clusterOutputYAML(clusters []*api.Cluster, out io.Writer) error { | ||
for i, cluster := range clusters { | ||
// fullOutputJson outputs the marshalled JSON of a list of clusters and instance groups. It will handle | ||
// nils for clusters and instanceGroups slices. | ||
func fullOutputYAML(out io.Writer, args ...runtime.Object) error { | ||
for i, obj := range args { | ||
if i != 0 { | ||
if err := writeYAMLSep(out); err != nil { | ||
return fmt.Errorf("error writing to stdout: %v", err) | ||
} | ||
} | ||
if err := marshalToWriter(cluster, marshalYaml, out); err != nil { | ||
if err := marshalToWriter(obj, marshalYaml, out); err != nil { | ||
return err | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean that targetName is always
!= ""
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is targetName ever == "". I think it is always set, and the if statement is not needed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you're right, but separate PR probably
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you pull this comment out of this PR?