mirror of https://github.com/linkerd/linkerd2.git
feat(cli): Add json output to link, unlink, allow, and allow-scrapes commands (#12658)
We add an -o/--output flag to the remaining commands which render kubernetes resources and do not yet have this flag. The supported values for this flag are "yaml" (default) and "json". The commands are: linkerd mulitcluster allow linkerd multicluster link linkerd multicluster unlink linkerd viz allow-scrapes Signed-off-by: Alex Leong <alex@buoyant.io>
This commit is contained in:
parent
80a1803c7f
commit
26eba44ac3
|
@ -24,6 +24,7 @@ type (
|
|||
namespace string
|
||||
serviceAccountName string
|
||||
ignoreCluster bool
|
||||
output string
|
||||
}
|
||||
)
|
||||
|
||||
|
@ -31,6 +32,7 @@ func newAllowCommand() *cobra.Command {
|
|||
opts := allowOptions{
|
||||
namespace: defaultMulticlusterNamespace,
|
||||
ignoreCluster: false,
|
||||
output: "yaml",
|
||||
}
|
||||
|
||||
cmd := &cobra.Command{
|
||||
|
@ -69,16 +71,15 @@ func newAllowCommand() *cobra.Command {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
stdout.Write(buf.Bytes())
|
||||
stdout.Write([]byte("---\n"))
|
||||
|
||||
return nil
|
||||
return pkgcmd.RenderYAMLAs(&buf, stdout, opts.output)
|
||||
},
|
||||
}
|
||||
|
||||
cmd.Flags().StringVar(&opts.namespace, "namespace", defaultMulticlusterNamespace, "The destination namespace for the service account.")
|
||||
cmd.Flags().BoolVar(&opts.ignoreCluster, "ignore-cluster", false, "Ignore cluster configuration")
|
||||
cmd.Flags().StringVar(&opts.serviceAccountName, "service-account-name", "", "The name of the multicluster access service account")
|
||||
cmd.PersistentFlags().StringVarP(&opts.output, "output", "o", "yaml", "Output format. One of: json|yaml")
|
||||
|
||||
pkgcmd.ConfigureNamespaceFlagCompletion(
|
||||
cmd, []string{"namespace"},
|
||||
|
|
|
@ -2,6 +2,7 @@ package cmd
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
|
@ -56,6 +57,7 @@ type (
|
|||
gatewayPort uint32
|
||||
ha bool
|
||||
enableGateway bool
|
||||
output string
|
||||
}
|
||||
)
|
||||
|
||||
|
@ -181,9 +183,19 @@ A full list of configurable values can be found at https://github.com/linkerd/li
|
|||
},
|
||||
}
|
||||
|
||||
credsOut, err := yaml.Marshal(creds)
|
||||
if err != nil {
|
||||
return err
|
||||
var credsOut []byte
|
||||
if opts.output == "yaml" {
|
||||
credsOut, err = yaml.Marshal(creds)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else if opts.output == "json" {
|
||||
credsOut, err = json.Marshal(creds)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
return fmt.Errorf("output format %s not supported", opts.output)
|
||||
}
|
||||
|
||||
destinationCreds := corev1.Secret{
|
||||
|
@ -204,9 +216,20 @@ A full list of configurable values can be found at https://github.com/linkerd/li
|
|||
k8s.ConfigKeyName: kubeconfig,
|
||||
},
|
||||
}
|
||||
destinationCredsOut, err := yaml.Marshal(destinationCreds)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
var destinationCredsOut []byte
|
||||
if opts.output == "yaml" {
|
||||
destinationCredsOut, err = yaml.Marshal(destinationCreds)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else if opts.output == "json" {
|
||||
destinationCredsOut, err = json.Marshal(destinationCreds)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
return fmt.Errorf("output format %s not supported", opts.output)
|
||||
}
|
||||
|
||||
remoteDiscoverySelector, err := metav1.ParseToLabelSelector(opts.remoteDiscoverySelector)
|
||||
|
@ -287,9 +310,20 @@ A full list of configurable values can be found at https://github.com/linkerd/li
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
linkOut, err := yaml.Marshal(obj.Object)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
var linkOut []byte
|
||||
if opts.output == "yaml" {
|
||||
linkOut, err = yaml.Marshal(obj.Object)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else if opts.output == "json" {
|
||||
linkOut, err = json.Marshal(obj.Object)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
return fmt.Errorf("output format %s not supported", opts.output)
|
||||
}
|
||||
|
||||
values, err := buildServiceMirrorValues(opts)
|
||||
|
@ -313,19 +347,23 @@ A full list of configurable values can be found at https://github.com/linkerd/li
|
|||
return err
|
||||
}
|
||||
}
|
||||
serviceMirrorOut, err := renderServiceMirror(values, valuesOverrides, opts.namespace)
|
||||
serviceMirrorOut, err := renderServiceMirror(values, valuesOverrides, opts.namespace, opts.output)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
separator := []byte("---\n")
|
||||
if opts.output == "json" {
|
||||
separator = []byte("\n")
|
||||
}
|
||||
stdout.Write(credsOut)
|
||||
stdout.Write([]byte("---\n"))
|
||||
stdout.Write(separator)
|
||||
stdout.Write(destinationCredsOut)
|
||||
stdout.Write([]byte("---\n"))
|
||||
stdout.Write(separator)
|
||||
stdout.Write(linkOut)
|
||||
stdout.Write([]byte("---\n"))
|
||||
stdout.Write(separator)
|
||||
stdout.Write(serviceMirrorOut)
|
||||
stdout.Write([]byte("---\n"))
|
||||
stdout.Write(separator)
|
||||
|
||||
return nil
|
||||
},
|
||||
|
@ -350,6 +388,7 @@ A full list of configurable values can be found at https://github.com/linkerd/li
|
|||
cmd.Flags().Uint32Var(&opts.gatewayPort, "gateway-port", opts.gatewayPort, "If specified, overwrites gateway port when gateway service is not type LoadBalancer")
|
||||
cmd.Flags().BoolVar(&opts.ha, "ha", opts.ha, "Enable HA configuration for the service-mirror deployment (default false)")
|
||||
cmd.Flags().BoolVar(&opts.enableGateway, "gateway", opts.enableGateway, "If false, allows a link to be created against a cluster that does not have a gateway service")
|
||||
cmd.Flags().StringVarP(&opts.output, "output", "o", "yaml", "Output format. One of: json|yaml")
|
||||
|
||||
pkgcmd.ConfigureNamespaceFlagCompletion(
|
||||
cmd, []string{"namespace", "gateway-namespace"},
|
||||
|
@ -357,7 +396,7 @@ A full list of configurable values can be found at https://github.com/linkerd/li
|
|||
return cmd
|
||||
}
|
||||
|
||||
func renderServiceMirror(values *multicluster.Values, valuesOverrides map[string]interface{}, namespace string) ([]byte, error) {
|
||||
func renderServiceMirror(values *multicluster.Values, valuesOverrides map[string]interface{}, namespace string, format string) ([]byte, error) {
|
||||
files := []*chartloader.BufferedFile{
|
||||
{Name: chartutil.ChartfileName},
|
||||
{Name: "templates/service-mirror.yaml"},
|
||||
|
@ -420,14 +459,19 @@ func renderServiceMirror(values *multicluster.Values, valuesOverrides map[string
|
|||
}
|
||||
|
||||
// Merge templates and inject
|
||||
var out bytes.Buffer
|
||||
var yamlBytes bytes.Buffer
|
||||
for _, tmpl := range chart.Templates {
|
||||
t := path.Join(chart.Metadata.Name, tmpl.Name)
|
||||
if _, err := out.WriteString(renderedTemplates[t]); err != nil {
|
||||
if _, err := yamlBytes.WriteString(renderedTemplates[t]); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
var out bytes.Buffer
|
||||
err = pkgcmd.RenderYAMLAs(&yamlBytes, &out, format)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out.Bytes(), nil
|
||||
}
|
||||
|
||||
|
@ -450,6 +494,7 @@ func newLinkOptionsWithDefault() (*linkOptions, error) {
|
|||
gatewayPort: 0,
|
||||
ha: false,
|
||||
enableGateway: true,
|
||||
output: "yaml",
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
|
|
@ -34,7 +34,7 @@ func TestServiceMirrorRender(t *testing.T) {
|
|||
for i, tc := range testCases {
|
||||
tc := tc
|
||||
t.Run(fmt.Sprintf("%d: %s", i, tc.goldenFileName), func(t *testing.T) {
|
||||
out, err := renderServiceMirror(tc.serviceMirrorValues, charts.MergeMaps(defaultValues, tc.overrides), "test")
|
||||
out, err := renderServiceMirror(tc.serviceMirrorValues, charts.MergeMaps(defaultValues, tc.overrides), "test", "yaml")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to render templates: %v", err)
|
||||
}
|
||||
|
|
|
@ -105,8 +105,16 @@ func newUnlinkCommand() *cobra.Command {
|
|||
}
|
||||
|
||||
for _, r := range resources {
|
||||
if err := r.RenderResource(stdout); err != nil {
|
||||
log.Errorf("failed to render resource %s: %s", r.Name, err)
|
||||
if opts.output == "yaml" {
|
||||
if err := r.RenderResource(stdout); err != nil {
|
||||
log.Errorf("failed to render resource %s: %s", r.Name, err)
|
||||
}
|
||||
} else if opts.output == "json" {
|
||||
if err := r.RenderResourceJSON(stdout); err != nil {
|
||||
log.Errorf("failed to render resource %s: %s", r.Name, err)
|
||||
}
|
||||
} else {
|
||||
return fmt.Errorf("unsupported format: %s", opts.output)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -116,6 +124,7 @@ func newUnlinkCommand() *cobra.Command {
|
|||
|
||||
cmd.Flags().StringVar(&opts.namespace, "namespace", defaultMulticlusterNamespace, "The namespace for the service account")
|
||||
cmd.Flags().StringVar(&opts.clusterName, "cluster-name", "", "Cluster name")
|
||||
cmd.Flags().StringVarP(&opts.output, "output", "o", "yaml", "Output format. One of: json|yaml")
|
||||
|
||||
pkgcmd.ConfigureNamespaceFlagCompletion(
|
||||
cmd, []string{"namespace"},
|
||||
|
|
|
@ -76,4 +76,3 @@ subjects:
|
|||
- kind: ServiceAccount
|
||||
name: {{ .AccountName }}
|
||||
namespace: linkerd-multicluster
|
||||
---
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"os"
|
||||
"bytes"
|
||||
"text/template"
|
||||
|
||||
pkgcmd "github.com/linkerd/linkerd2/pkg/cmd"
|
||||
|
@ -118,6 +118,7 @@ type templateOptions struct {
|
|||
|
||||
// newCmdAllowScrapes creates a new cobra command `allow-scrapes`
|
||||
func newCmdAllowScrapes() *cobra.Command {
|
||||
output := "yaml"
|
||||
options := templateOptions{
|
||||
ExtensionName: ExtensionName,
|
||||
ChartName: vizChartName,
|
||||
|
@ -136,10 +137,17 @@ linkerd viz allow-scrapes --namespace emojivoto | kubectl apply -f -`,
|
|||
},
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
t := template.Must(template.New("allow-scrapes").Parse(allowScrapePolicy))
|
||||
return t.Execute(os.Stdout, options)
|
||||
var buf bytes.Buffer
|
||||
err := t.Execute(&buf, options)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return pkgcmd.RenderYAMLAs(&buf, stdout, output)
|
||||
},
|
||||
}
|
||||
cmd.Flags().StringVarP(&options.TargetNs, "namespace", "n", options.TargetNs, "The namespace in which to authorize Prometheus scrapes.")
|
||||
cmd.Flags().StringVarP(&output, "output", "o", output, "Output format. One of: json|yaml")
|
||||
|
||||
pkgcmd.ConfigureNamespaceFlagCompletion(
|
||||
cmd, []string{"n", "namespace"},
|
||||
|
|
Loading…
Reference in New Issue