apply

package
v1.3.0-rc.2 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2023 License: Apache-2.0 Imports: 24 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ApplyCmd = &cobra.Command{
	Use:   "apply",
	Short: "Apply Aperture Policies",
	Long: `
Use this command to apply the Aperture Policies.`,
	SilenceErrors: true,
	PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
		var err error
		kubeRestConfig, err = utils.GetKubeConfig(kubeConfig)
		if err != nil {
			return err
		}
		return nil
	},
}

ApplyCmd is the command to apply a policy to the cluster.

View Source
var ApplyDynamicConfigCmd = &cobra.Command{
	Use:           "dynamic-config",
	Short:         "Apply Aperture DynamicConfig to a Policy",
	Long:          `Use this command to apply the Aperture DynamicConfig to a Policy.`,
	SilenceErrors: true,
	Example:       `aperturectl apply dynamic-config --policy=static-rate-limiting --file=dynamic-config.yaml`,
	PreRunE: func(_ *cobra.Command, _ []string) error {
		if policyName == "" {
			return errors.New("policy name is required")
		}
		if dynamicConfigFile == "" {
			return errors.New("dynamic config file is required")
		}
		// read the dynamic config file
		var err error
		dynamicConfigBytes, err = os.ReadFile(dynamicConfigFile)
		if err != nil {
			return err
		}
		return nil
	},
	RunE: func(_ *cobra.Command, _ []string) error {
		err := api.AddToScheme(scheme.Scheme)
		if err != nil {
			return fmt.Errorf("failed to connect to Kubernetes: %w", err)
		}

		c, err := client.New(kubeRestConfig, client.Options{
			Scheme: scheme.Scheme,
		})
		if err != nil {
			return fmt.Errorf("failed to create Kubernetes client: %w", err)
		}

		deployment, err := getControllerDeployment()
		if err != nil {
			return err
		}

		policy := &policyv1alpha1.Policy{}
		err = c.Get(context.Background(), client.ObjectKey{
			Namespace: deployment.Namespace,
			Name:      policyName,
		}, policy)
		if err != nil {
			return fmt.Errorf("failed to get Policy '%s': %w", policyName, err)
		}

		dynamicConfigYAML := make(map[string]interface{})
		err = yaml.Unmarshal(dynamicConfigBytes, &dynamicConfigYAML)
		if err != nil {
			return fmt.Errorf("failed to parse DynamicConfig YAML: %w", err)
		}
		dynamicConfigBytes, err := json.Marshal(dynamicConfigYAML)
		if err != nil {
			return fmt.Errorf("failed to parse DynamicConfig JSON: %w", err)
		}

		policy.DynamicConfig.Raw = dynamicConfigBytes
		err = c.Update(context.Background(), policy)
		if err != nil {
			return fmt.Errorf("failed to update Policy '%s': %w", policyName, err)
		}

		log.Info().Str("policy", policyName).Str("namespace", deployment.Namespace).Msg("Updated DynamicConfig successfully")

		return nil
	},
}

ApplyDynamicConfigCmd is the command to apply DynamicConfig to a Policy.

View Source
var ApplyPolicyCmd = &cobra.Command{
	Use:           "policy",
	Short:         "Apply Aperture Policy to the cluster",
	Long:          `Use this command to apply the Aperture Policy to the cluster.`,
	SilenceErrors: true,
	Example: `aperturectl apply policy --file=policies/static-rate-limiting.yaml

aperturectl apply policy --dir=policies`,
	RunE: func(_ *cobra.Command, _ []string) error {
		if file != "" {
			return applyPolicy(file)
		} else if dir != "" {
			policies, err := getPolicies(dir)
			if err != nil {
				return err
			}

			model := tui.InitialCheckboxModel(policies, "Which policies to apply?")
			p := tea.NewProgram(model)
			if _, err := p.Run(); err != nil {
				return err
			}

			for policyIndex := range model.Selected {
				fileName := policies[policyIndex]
				if err := applyPolicy(fileName); err != nil {
					log.Error().Err(err).Msgf("failed to apply policy '%s' on Kubernetes.", fileName)
				}
			}
			return nil
		} else {
			return errors.New("either --file or --dir must be provided")
		}
	},
}

ApplyPolicyCmd is the command to apply a policy to the cluster.

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