github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/sealer/cmd/upgrade.go (about)

     1  // Copyright © 2021 Alibaba Group Holding Ltd.
     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 cmd
    16  
    17  import (
    18  	"fmt"
    19  	"os"
    20  
    21  	"github.com/alibaba/sealer/pkg/clusterfile"
    22  
    23  	"github.com/alibaba/sealer/apply"
    24  	"github.com/spf13/cobra"
    25  )
    26  
    27  var upgradeClusterName string
    28  
    29  const (
    30  	clusterfilepath = `%s/.sealer/%s/Clusterfile`
    31  )
    32  
    33  // upgradeCmd represents the upgrade command
    34  var upgradeCmd = &cobra.Command{
    35  	Use:     "upgrade",
    36  	Short:   "upgrade your kubernetes cluster",
    37  	Long:    `sealer upgrade imagename --cluster clustername`,
    38  	Example: `sealer upgrade kubernetes:v1.19.9 --cluster my-cluster`,
    39  	Args:    cobra.ExactArgs(1),
    40  	RunE: func(cmd *cobra.Command, args []string) error {
    41  		var err error
    42  		//get clustername
    43  		if upgradeClusterName == "" {
    44  			upgradeClusterName, err = clusterfile.GetDefaultClusterName()
    45  			if err != nil {
    46  				return err
    47  			}
    48  		}
    49  		//get Clusterfile
    50  		userHome, _ := os.UserHomeDir()
    51  		var filepath = fmt.Sprintf(clusterfilepath, userHome, upgradeClusterName)
    52  		desiredCluster, err := clusterfile.GetClusterFromFile(filepath)
    53  		if err != nil {
    54  			return err
    55  		}
    56  		applier, err := apply.NewApplier(desiredCluster)
    57  		if err != nil {
    58  			return err
    59  		}
    60  		return applier.Upgrade(args[0])
    61  	},
    62  }
    63  
    64  func init() {
    65  	rootCmd.AddCommand(upgradeCmd)
    66  	upgradeCmd.Flags().StringVarP(&upgradeClusterName, "cluster", "c", "", "The name of your cluster to upgrade")
    67  }