...

Source file src/sigs.k8s.io/cli-utils/cmd/diff/cmddiff.go

Documentation: sigs.k8s.io/cli-utils/cmd/diff

     1  // Copyright 2020 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package diff
     5  
     6  import (
     7  	"os"
     8  
     9  	"github.com/spf13/cobra"
    10  	"k8s.io/cli-runtime/pkg/genericclioptions"
    11  	"k8s.io/cli-runtime/pkg/resource"
    12  	"k8s.io/klog/v2"
    13  	"k8s.io/kubectl/pkg/cmd/diff"
    14  	"k8s.io/kubectl/pkg/cmd/util"
    15  	"k8s.io/kubectl/pkg/util/i18n"
    16  	"sigs.k8s.io/cli-utils/pkg/common"
    17  )
    18  
    19  const tmpDirPrefix = "diff-cmd"
    20  
    21  // NewCommand returns cobra command to implement client-side diff of package
    22  // directory. For each local config file, get the resource in the cluster
    23  // and diff the local config resource against the resource in the cluster.
    24  func NewCommand(f util.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command {
    25  	options := diff.NewDiffOptions(ioStreams)
    26  	cmd := &cobra.Command{
    27  		Use:                   "diff (DIRECTORY | STDIN)",
    28  		DisableFlagsInUseLine: true,
    29  		Short:                 i18n.T("Diff local config against cluster applied version"),
    30  		Args:                  cobra.MaximumNArgs(1),
    31  		Run: func(cmd *cobra.Command, args []string) {
    32  			cleanupFunc, err := Initialize(options, f, args)
    33  			defer cleanupFunc()
    34  			util.CheckErr(err)
    35  			util.CheckErr(options.Run())
    36  		},
    37  	}
    38  
    39  	return cmd
    40  }
    41  
    42  // Initialize fills in the DiffOptions in preparation for DiffOptions.Run().
    43  // Returns a cleanup function for removing temp files after expanding stdin, or
    44  // error if there is an error filling in the options or if there
    45  // is not one argument that is a directory.
    46  func Initialize(o *diff.DiffOptions, f util.Factory, args []string) (func(), error) {
    47  	cleanupFunc := func() {}
    48  	// Validate the only argument is a (package) directory path.
    49  	filenameFlags, err := common.DemandOneDirectory(args)
    50  	if err != nil {
    51  		return cleanupFunc, err
    52  	}
    53  	// Process input from stdin
    54  	if len(args) == 0 {
    55  		tmpDir, err := createTempDir()
    56  		if err != nil {
    57  			return cleanupFunc, err
    58  		}
    59  		cleanupFunc = func() {
    60  			os.RemoveAll(tmpDir)
    61  		}
    62  		filenameFlags.Filenames = &[]string{tmpDir}
    63  		klog.V(6).Infof("stdin diff command temp dir: %s", tmpDir)
    64  		if err := common.FilterInputFile(os.Stdin, tmpDir); err != nil {
    65  			return cleanupFunc, err
    66  		}
    67  	} else {
    68  		// We do not want to diff the inventory object. So we expand
    69  		// the config file paths, excluding the inventory object.
    70  		filenameFlags, err = common.ExpandPackageDir(filenameFlags)
    71  		if err != nil {
    72  			return cleanupFunc, err
    73  		}
    74  	}
    75  	o.FilenameOptions = filenameFlags.ToOptions()
    76  
    77  	o.OpenAPISchema, err = f.OpenAPISchema()
    78  	if err != nil {
    79  		return cleanupFunc, err
    80  	}
    81  
    82  	o.DynamicClient, err = f.DynamicClient()
    83  	if err != nil {
    84  		return cleanupFunc, err
    85  	}
    86  
    87  	o.DryRunVerifier = resource.NewQueryParamVerifier(o.DynamicClient, f.OpenAPIGetter(), resource.QueryParamDryRun)
    88  
    89  	o.CmdNamespace, o.EnforceNamespace, err = f.ToRawKubeConfigLoader().Namespace()
    90  	if err != nil {
    91  		return cleanupFunc, err
    92  	}
    93  
    94  	o.Builder = f.NewBuilder()
    95  
    96  	// We don't support server-side apply diffing yet.
    97  	o.ServerSideApply = false
    98  	o.ForceConflicts = false
    99  
   100  	return cleanupFunc, nil
   101  }
   102  
   103  func createTempDir() (string, error) {
   104  	// Create a temporary file with the passed prefix in
   105  	// the default temporary directory.
   106  	tmpDir, err := os.MkdirTemp("", tmpDirPrefix)
   107  	if err != nil {
   108  		return "", err
   109  	}
   110  	return tmpDir, nil
   111  }
   112  

View as plain text