github.com/kubernetes-incubator/kube-aws@v0.16.4/cmd/validate.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/kubernetes-incubator/kube-aws/core/root"
     7  	"github.com/kubernetes-incubator/kube-aws/logger"
     8  	"github.com/spf13/cobra"
     9  )
    10  
    11  var (
    12  	cmdValidate = &cobra.Command{
    13  		Use:          "validate",
    14  		Short:        "Validate cluster assets",
    15  		Long:         ``,
    16  		RunE:         runCmdValidate,
    17  		SilenceUsage: true,
    18  	}
    19  
    20  	validateOpts = struct {
    21  		awsDebug, skipWait bool
    22  		profile            string
    23  		targets            []string
    24  	}{}
    25  )
    26  
    27  func init() {
    28  	RootCmd.AddCommand(cmdValidate)
    29  	cmdValidate.Flags().BoolVar(
    30  		&validateOpts.awsDebug,
    31  		"aws-debug",
    32  		false,
    33  		"Log debug information from aws-sdk-go library",
    34  	)
    35  	cmdValidate.Flags().StringVar(&validateOpts.profile, "profile", "", "The AWS profile to use from credentials file")
    36  	cmdValidate.Flags().StringSliceVar(
    37  		&validateOpts.targets,
    38  		"targets",
    39  		root.AllOperationTargetsAsStringSlice(),
    40  		"Validate nothing but specified sub-stacks. Specify `all` or any combination of `etcd`, `control-plane`, and node pool names. Defaults to `all`")
    41  }
    42  
    43  func runCmdValidate(_ *cobra.Command, _ []string) error {
    44  	opts := root.NewOptions(validateOpts.awsDebug, validateOpts.skipWait, validateOpts.profile)
    45  
    46  	cluster, err := root.LoadClusterFromFile(configPath, opts, validateOpts.awsDebug)
    47  	if err != nil {
    48  		return fmt.Errorf("failed to initialize cluster driver: %v", err)
    49  	}
    50  
    51  	logger.Info("Validating UserData and stack template...\n")
    52  
    53  	targets := root.OperationTargetsFromStringSlice(validateOpts.targets)
    54  
    55  	report, err := cluster.ValidateStack(targets)
    56  	if report != "" {
    57  		logger.Infof("Validation Report: %s\n", report)
    58  	}
    59  	if err != nil {
    60  		return err
    61  	}
    62  
    63  	logger.Info("stack template is valid.\n\n")
    64  	logger.Info("Validation OK!")
    65  	return nil
    66  }