apply

package
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: May 2, 2023 License: Apache-2.0 Imports: 25 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 fmt.Errorf("failed to get kube config: %w", err)
		}

		controllerNs, err = cmd.Flags().GetString("controller-ns")
		if err != nil {
			return fmt.Errorf("failed to get controller namespace flag: %w", err)
		}

		controllerAddr, err := cmd.Flags().GetString("controller")
		if err != nil {
			return fmt.Errorf("failed to get controller address flag: %w", err)
		}

		kube, err := cmd.Flags().GetBool("kube")
		if err != nil {
			return fmt.Errorf("failed to get kube flag: %w", err)
		}

		if controllerAddr == "" && !kube {
			err = cmd.Flags().Set("kube", "true")
			if err != nil {
				return fmt.Errorf("failed to set kube flag: %w", err)
			}
		}

		err = controller.PreRunE(cmd, args)
		if err != nil {
			return fmt.Errorf("failed to run controller pre-run: %w", err)
		}

		client, err = controller.Client()
		if err != nil {
			return fmt.Errorf("failed to get controller client: %w", err)
		}

		return nil
	},
	PersistentPostRun: controller.PostRun,
}

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 := k8sclient.New(kubeRestConfig, k8sclient.Options{
			Scheme: scheme.Scheme,
		})
		if err != nil {
			return fmt.Errorf("failed to create Kubernetes client: %w", err)
		}

		deployment, err := utils.GetControllerDeployment(kubeRestConfig, controllerNs)
		if err != nil {
			return 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 := &policyv1alpha1.Policy{}
		err = c.Get(context.Background(), k8sclient.ObjectKey{
			Namespace: deployment.Namespace,
			Name:      policyName,
		}, policy)
		if err != nil {
			if apimeta.IsNoMatchError(err) {
				var dynamicConfigStruct *structpb.Struct
				dynamicConfigStruct, err = structpb.NewStruct(dynamicConfigYAML)
				if err != nil {
					return fmt.Errorf("failed to parse DynamicConfig Struct: %w", err)
				}
				request := languagev1.PostDynamicConfigRequest{
					PolicyName:    policyName,
					DynamicConfig: dynamicConfigStruct,
				}
				_, err = client.PostDynamicConfig(context.Background(), &request)
				if err != nil {
					return fmt.Errorf("failed to update DynamicConfig: %w", err)
				}
			} else {
				return fmt.Errorf("failed to get Policy '%s': %w", policyName, err)
			}
		} else {
			policy.DynamicConfig.Raw = dynamicConfigBytes
			err = c.Update(context.Background(), policy)
			if err != nil {
				return fmt.Errorf("failed to update DynamicConfig for 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