istio.io/istio@v0.0.0-20240520182934-d79c90f27776/operator/cmd/mesh/operator-dump.go (about)

     1  // Copyright Istio Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package mesh
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"github.com/spf13/cobra"
    21  
    22  	"istio.io/istio/operator/pkg/util/clog"
    23  	"istio.io/istio/pkg/config/constants"
    24  	"istio.io/istio/pkg/config/labels"
    25  	buildversion "istio.io/istio/pkg/version"
    26  )
    27  
    28  type operatorDumpArgs struct {
    29  	// common is shared operator args
    30  	common operatorCommonArgs
    31  }
    32  
    33  func addOperatorDumpFlags(cmd *cobra.Command, args *operatorDumpArgs) {
    34  	hub, tag := buildversion.DockerInfo.Hub, buildversion.DockerInfo.Tag
    35  
    36  	cmd.PersistentFlags().StringVar(&args.common.hub, "hub", hub, HubFlagHelpStr)
    37  	cmd.PersistentFlags().StringVar(&args.common.tag, "tag", tag, TagFlagHelpStr)
    38  	cmd.PersistentFlags().StringSliceVar(&args.common.imagePullSecrets, "imagePullSecrets", nil, ImagePullSecretsHelpStr)
    39  	cmd.PersistentFlags().StringVar(&args.common.watchedNamespaces, "watchedNamespaces", constants.IstioSystemNamespace,
    40  		"The namespaces the operator controller watches, could be namespace list separated by comma, eg. 'ns1,ns2'")
    41  	cmd.PersistentFlags().StringVar(&args.common.operatorNamespace, "operatorNamespace", operatorDefaultNamespace, OperatorNamespaceHelpstr)
    42  	cmd.PersistentFlags().StringVarP(&args.common.manifestsPath, "charts", "", "", ChartsDeprecatedStr)
    43  	cmd.PersistentFlags().StringVarP(&args.common.manifestsPath, "manifests", "d", "", ManifestsFlagHelpStr)
    44  	cmd.PersistentFlags().StringVarP(&args.common.revision, "revision", "r", "", OperatorRevFlagHelpStr)
    45  	cmd.PersistentFlags().StringVarP(&args.common.outputFormat, "output", "o", yamlOutput,
    46  		"Output format: one of json|yaml")
    47  }
    48  
    49  func operatorDumpCmd(rootArgs *RootArgs, odArgs *operatorDumpArgs) *cobra.Command {
    50  	return &cobra.Command{
    51  		Use:   "dump",
    52  		Short: "Dumps the Istio operator controller manifest.",
    53  		Long:  "The dump subcommand dumps the Istio operator controller manifest.",
    54  		Args:  cobra.ExactArgs(0),
    55  		PreRunE: func(cmd *cobra.Command, args []string) error {
    56  			if !labels.IsDNS1123Label(odArgs.common.revision) && cmd.PersistentFlags().Changed("revision") {
    57  				return fmt.Errorf("invalid revision specified: %v", odArgs.common.revision)
    58  			}
    59  			return nil
    60  		},
    61  		Run: func(cmd *cobra.Command, args []string) {
    62  			l := clog.NewConsoleLogger(cmd.OutOrStdout(), cmd.ErrOrStderr(), installerScope)
    63  			operatorDump(rootArgs, odArgs, l)
    64  		},
    65  	}
    66  }
    67  
    68  // operatorDump dumps the manifest used to install the operator.
    69  func operatorDump(args *RootArgs, odArgs *operatorDumpArgs, l clog.Logger) {
    70  	if err := validateOperatorOutputFormatFlag(odArgs.common.outputFormat); err != nil {
    71  		l.LogAndFatal(fmt.Errorf("unknown output format: %v", odArgs.common.outputFormat))
    72  	}
    73  
    74  	_, mstr, err := renderOperatorManifest(args, &odArgs.common)
    75  	if err != nil {
    76  		l.LogAndFatal(err)
    77  	}
    78  
    79  	var output string
    80  	if output, err = yamlToFormat(mstr, odArgs.common.outputFormat); err != nil {
    81  		l.LogAndFatal(err)
    82  	}
    83  	l.Print(output)
    84  }
    85  
    86  // validateOperatorOutputFormatFlag validates if the output format is valid.
    87  func validateOperatorOutputFormatFlag(outputFormat string) error {
    88  	switch outputFormat {
    89  	case jsonOutput, yamlOutput:
    90  	default:
    91  		return fmt.Errorf("unknown output format: %s", outputFormat)
    92  	}
    93  	return nil
    94  }