mapper

package
v0.1.8 Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2023 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	NamespacesKey       = "namespaces"
	NamespacesShorthand = "n"
)
View Source
const OutputFormatDefault = OutputFormatYAML
View Source
const OutputFormatJSON = "json"
View Source
const OutputFormatKey = "format"
View Source
const OutputFormatYAML = "yaml"
View Source
const OutputLocationKey = "output"
View Source
const OutputLocationShorthand = "o"
View Source
const OutputTypeDefault = OutputTypeSingleFile
View Source
const OutputTypeDirectory = "dir"
View Source
const OutputTypeKey = "output-type"
View Source
const OutputTypeSingleFile = "single-file"

Variables

View Source
var ExportCmd = &cobra.Command{
	Use:   "export",
	Short: "",
	Long:  ``,
	RunE: func(cmd *cobra.Command, args []string) error {
		return mapperclient.WithClient(func(c *mapperclient.Client) error {
			if viper.GetString(OutputLocationKey) == "" && viper.GetString(OutputTypeKey) != "" {
				return fmt.Errorf("flag --%s requires --%s to specify output path", OutputTypeKey, OutputLocationKey)
			}

			ctxTimeout, cancel := context.WithTimeout(context.Background(), 10*time.Second)
			defer cancel()
			namespacesFilter := viper.GetStringSlice(NamespacesKey)
			intentsFromMapper, err := c.ServiceIntents(ctxTimeout, namespacesFilter)
			if err != nil {
				return err
			}

			outputList := make([]v1alpha2.ClientIntents, 0)

			for _, serviceIntents := range intentsFromMapper {
				intentList := make([]v1alpha2.Intent, 0)

				for _, serviceIntent := range serviceIntents.Intents {
					intent := v1alpha2.Intent{
						Name: serviceIntent.Name,
					}

					if len(serviceIntent.Namespace) != 0 && serviceIntent.Namespace != serviceIntents.Client.Namespace {
						intent.Name = fmt.Sprintf("%s.%s", serviceIntent.Name, serviceIntent.Namespace)
					}
					intentList = append(intentList, intent)
				}

				intentsOutput := v1alpha2.ClientIntents{
					TypeMeta: v1.TypeMeta{
						Kind:       consts.IntentsKind,
						APIVersion: consts.IntentsAPIVersion,
					},
					ObjectMeta: v1.ObjectMeta{
						Name:      serviceIntents.Client.Name,
						Namespace: serviceIntents.Client.Namespace,
					},
					Spec: &v1alpha2.IntentsSpec{Service: v1alpha2.Service{Name: serviceIntents.Client.Name}},
				}

				if len(intentList) != 0 {
					intentsOutput.Spec.Calls = intentList
				}

				outputList = append(outputList, intentsOutput)
			}

			if viper.GetString(OutputLocationKey) != "" {
				switch outputTypeVal := viper.GetString(OutputTypeKey); {
				case outputTypeVal == OutputTypeSingleFile:
					err := writeIntentsFile(viper.GetString(OutputLocationKey), outputList)
					if err != nil {
						return err
					}
					output.PrintStderr("Successfully wrote intents into %s", viper.GetString(OutputLocationKey))
				case outputTypeVal == OutputTypeDirectory:
					err := os.MkdirAll(viper.GetString(OutputLocationKey), 0700)
					if err != nil {
						return fmt.Errorf("could not create dir %s: %w", viper.GetString(OutputLocationKey), err)
					}

					for _, intent := range outputList {
						filePath := fmt.Sprintf("%s.%s.yaml", intent.Name, intent.Namespace)
						if err != nil {
							return err
						}
						filePath = filepath.Join(viper.GetString(OutputLocationKey), filePath)
						err := writeIntentsFile(filePath, []v1alpha2.ClientIntents{intent})
						if err != nil {
							return err
						}
					}
					output.PrintStderr("Successfully wrote intents into %s", viper.GetString(OutputLocationKey))
				default:
					return fmt.Errorf("unexpected output type %s, use one of (%s, %s)", outputTypeVal, OutputTypeSingleFile, OutputTypeDirectory)
				}

			} else {
				formatted, err := getFormattedIntents(outputList)
				if err != nil {
					return err
				}
				output.PrintStdout(formatted)
			}
			return nil
		})
	},
}
View Source
var ListCmd = &cobra.Command{
	Use:   "list",
	Short: "",
	Long:  ``,
	RunE: func(cmd *cobra.Command, args []string) error {
		return mapperclient.WithClient(func(c *mapperclient.Client) error {
			namespacesFilter := viper.GetStringSlice(NamespacesKey)
			servicesIntents, err := c.ServiceIntents(context.Background(), namespacesFilter)
			if err != nil {
				return err
			}
			for _, service := range servicesIntents {
				output.PrintStdout("%s in namespace %s calls:", service.Client.Name, service.Client.Namespace)
				for _, intent := range service.Intents {
					if len(intent.Namespace) != 0 {
						output.PrintStdout("  - %s in namespace %s", intent.Name, intent.Namespace)
						continue
					}

					output.PrintStdout("  - %s", intent.Name)
				}

			}

			if len(servicesIntents) == 0 {
				output.PrintStderr("No connections found. The network mapper detects (1) connections that are currently open and (2) DNS lookups while a connection is being initiated, for connections between pods on this cluster.")
			}
			return nil
		})
	},
}
View Source
var MapperCmd = &cobra.Command{
	Use:   "mapper",
	Short: "",
	Long:  ``,
}
View Source
var ResetCmd = &cobra.Command{
	Use:   "reset",
	Short: "Resets and clears the information the mapper holds, causing it to forget the intents it has discovered.",
	Long:  ``,
	RunE: func(cmd *cobra.Command, args []string) error {
		return mapperclient.WithClient(func(c *mapperclient.Client) error {
			return c.ResetCapture(context.Background())
		})
	},
}

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