hydrate

package
v1.18.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 13, 2024 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Cmd = &cobra.Command{
	Use:   "hydrate",
	Short: "Compiles the local repository to the exact form that would be sent to the APIServer.",
	Long: `Compiles the local repository to the exact form that would be sent to the APIServer.

The output directory consists of one directory per declared Cluster, and defaultcluster/ for
clusters without declarations. Each directory holds the full set of configs for a single cluster,
which you could kubectl apply -fR to the cluster, or have Config Sync sync to the cluster.`,
	Args: cobra.ExactArgs(0),
	RunE: func(cmd *cobra.Command, _ []string) error {

		cmd.SilenceUsage = true

		sourceFormat := filesystem.SourceFormat(flags.SourceFormat)
		if sourceFormat == "" {
			sourceFormat = filesystem.SourceFormatHierarchy
		}
		rootDir, needsHydrate, err := hydrate.ValidateHydrateFlags(sourceFormat)
		if err != nil {
			return err
		}

		if needsHydrate {

			if rootDir, err = hydrate.ValidateAndRunKustomize(rootDir.OSPath()); err != nil {
				return err
			}

			defer func() {
				_ = os.RemoveAll(rootDir.OSPath())
			}()
		}

		files, err := nomosparse.FindFiles(rootDir)
		if err != nil {
			return err
		}

		parser := filesystem.NewParser(&reader.File{})

		validateOpts, err := hydrate.ValidateOptions(cmd.Context(), rootDir, flags.APIServerTimeout)
		if err != nil {
			return err
		}

		if sourceFormat == filesystem.SourceFormatHierarchy {
			files = filesystem.FilterHierarchyFiles(rootDir, files)
		} else {

			validateOpts.Scope = declared.RootReconciler
		}

		filePaths := reader.FilePaths{
			RootDir:   rootDir,
			PolicyDir: cmpath.RelativeOS(rootDir.OSPath()),
			Files:     files,
		}

		parseOpts := hydrate.ParseOptions{
			Parser:       parser,
			SourceFormat: sourceFormat,
			FilePaths:    filePaths,
		}

		var allObjects []ast.FileObject
		encounteredError := false
		numClusters := 0
		clusterFilterFunc := func(clusterName string, fileObjects []ast.FileObject, err status.MultiError) {
			clusterEnabled := flags.AllClusters()
			for _, cluster := range flags.Clusters {
				if clusterName == cluster {
					clusterEnabled = true
				}
			}
			if !clusterEnabled {
				return
			}
			numClusters++

			if err != nil {
				if clusterName == "" {
					clusterName = nomosparse.UnregisteredCluster
				}
				util.PrintErrOrDie(fmt.Errorf("errors for Cluster %q: %w", clusterName, err))

				encounteredError = true

				if status.HasBlockingErrors(err) {
					return
				}
			}

			allObjects = append(allObjects, fileObjects...)
		}
		hydrate.ForEachCluster(cmd.Context(), parseOpts, validateOpts, clusterFilterFunc)

		multiCluster := numClusters > 1
		fileObjects := hydrate.GenerateFileObjects(multiCluster, allObjects...)
		if flat {
			err = hydrate.PrintFlatOutput(outPath, flags.OutputFormat, fileObjects)
		} else {
			err = hydrate.PrintDirectoryOutput(outPath, flags.OutputFormat, fileObjects)
		}
		if err != nil {
			return err
		}

		if encounteredError {
			os.Exit(1)
		}

		return nil
	},
}

Cmd is the Cobra object representing the hydrate command.

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL