github.com/jingruilea/kubeedge@v1.2.0-beta.0.0.20200410162146-4bb8902b3879/cloud/cmd/csidriver/app/server.go (about)

     1  /*
     2  Copyright 2019 The KubeEdge Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package app
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"github.com/spf13/cobra"
    23  	"k8s.io/apiserver/pkg/util/term"
    24  	cliflag "k8s.io/component-base/cli/flag"
    25  	"k8s.io/component-base/cli/globalflag"
    26  	"k8s.io/klog"
    27  
    28  	"github.com/kubeedge/kubeedge/cloud/cmd/csidriver/app/options"
    29  	"github.com/kubeedge/kubeedge/cloud/pkg/csidriver"
    30  	"github.com/kubeedge/kubeedge/pkg/util/flag"
    31  	"github.com/kubeedge/kubeedge/pkg/version"
    32  	"github.com/kubeedge/kubeedge/pkg/version/verflag"
    33  )
    34  
    35  func NewCSIDriverCommand() *cobra.Command {
    36  	opts := options.NewCSIDriverOptions()
    37  	cmd := &cobra.Command{
    38  		Use: "csidriver",
    39  		Long: `CSI Driver from KubeEdge: this is more like CSI Driver proxy,
    40  		and it implements all of the CSI Identity and Controller interfaces.
    41  		It sends messages to CloudHub which will forward to edge. Actually all of the actions
    42  		about the Volume Lifecycle are executed in the CSI Driver from Vendor at edge`,
    43  		Run: func(cmd *cobra.Command, args []string) {
    44  			verflag.PrintAndExitIfRequested()
    45  			flag.PrintFlags(cmd.Flags())
    46  
    47  			// To help debugging, immediately log version
    48  			klog.Infof("Version: %+v", version.Get())
    49  
    50  			// start driver
    51  			driver, err := csidriver.NewCSIDriver(opts)
    52  			if err != nil {
    53  				klog.Errorf("failed to initialize driver: %s", err.Error())
    54  				return
    55  			}
    56  			driver.Run()
    57  		},
    58  	}
    59  	fs := cmd.Flags()
    60  	namedFs := opts.Flags()
    61  	verflag.AddFlags(namedFs.FlagSet("global"))
    62  	globalflag.AddGlobalFlags(namedFs.FlagSet("global"), cmd.Name())
    63  	for _, f := range namedFs.FlagSets {
    64  		fs.AddFlagSet(f)
    65  	}
    66  
    67  	usageFmt := "Usage:\n  %s\n"
    68  	cols, _, _ := term.TerminalSize(cmd.OutOrStdout())
    69  	cmd.SetUsageFunc(func(cmd *cobra.Command) error {
    70  		fmt.Fprintf(cmd.OutOrStderr(), usageFmt, cmd.UseLine())
    71  		cliflag.PrintSections(cmd.OutOrStderr(), namedFs, cols)
    72  		return nil
    73  	})
    74  	cmd.SetHelpFunc(func(cmd *cobra.Command, args []string) {
    75  		fmt.Fprintf(cmd.OutOrStdout(), "%s\n\n"+usageFmt, cmd.Long, cmd.UseLine())
    76  		cliflag.PrintSections(cmd.OutOrStdout(), namedFs, cols)
    77  	})
    78  	return cmd
    79  }