dynamicconfig

package
v2.34.0 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ApplyCmd = &cobra.Command{
	Use:           "apply",
	Short:         "Apply Aperture DynamicConfig to a Policy",
	Long:          `Use this command to apply the Aperture DynamicConfig to a Policy.`,
	SilenceErrors: true,
	Example:       `aperturectl dynamic-config apply --policy=rate-limiting --file=dynamic-config.yaml`,
	PreRunE: func(_ *cobra.Command, _ []string) error {
		var err error
		dynamicConfigBytes, err = utils.GetDynamicConfigBytes(policyName, 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)
		}

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

		if controller.IsKube() {
			var kubeClient k8sclient.Client
			kubeClient, err = k8sclient.New(controller.GetKubeRestConfig(), k8sclient.Options{
				Scheme: scheme.Scheme,
			})
			if err != nil {
				return fmt.Errorf("failed to create Kubernetes client: %w", err)
			}

			var deployment *appsv1.Deployment
			deployment, err = utils.GetControllerDeployment(controller.GetKubeRestConfig(), controllerNs)
			if err != nil {
				return err
			}

			dynamicConfigBytes, err = json.Marshal(dynamicConfigYAML)
			if err != nil {
				return fmt.Errorf("failed to parse DynamicConfig JSON: %w", err)
			}

			policy := &policyv1alpha1.Policy{}
			err = kubeClient.Get(context.Background(), k8sclient.ObjectKey{
				Namespace: deployment.Namespace,
				Name:      policyName,
			}, policy)
			if err != nil {
				if utils.IsNoMatchError(err) {
					err = utils.ApplyDynamicConfigUsingAPI(client, dynamicConfigYAML, policyName)
					if err != nil {
						return err
					}
				} else {
					return fmt.Errorf("failed to get Policy '%s': %w", policyName, err)
				}
			} else {
				policy.DynamicConfig.Raw = dynamicConfigBytes
				err = kubeClient.Update(context.Background(), policy)
				if err != nil {
					return fmt.Errorf("failed to update DynamicConfig for policy '%s': %w", policyName, err)
				}
			}
		} else {
			err = utils.ApplyDynamicConfigUsingAPI(client, dynamicConfigYAML, policyName)
			if err != nil {
				return err
			}
		}

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

		return nil
	},
}

ApplyCmd is the command to apply DynamicConfig to a Policy.

View Source
var DelCmd = &cobra.Command{
	Use:           "delete POLICY_NAME",
	Short:         "Delete Aperture DynamicConfig of a Policy.",
	Long:          "Use this command to delete the Aperture DynamicConfig to a Policy.",
	Example:       "aperture dynamic-config delete rate-limiting",
	SilenceErrors: true,
	Args:          cobra.ExactArgs(1),
	RunE: func(_ *cobra.Command, args []string) error {
		policyName := args[0]
		if controller.IsKube() {
			err := api.AddToScheme(scheme.Scheme)
			if err != nil {
				return fmt.Errorf("failed to connect to Kubernetes: %w", err)
			}

			var kubeClient k8sclient.Client
			kubeClient, err = k8sclient.New(controller.GetKubeRestConfig(), k8sclient.Options{
				Scheme: scheme.Scheme,
			})
			if err != nil {
				return fmt.Errorf("failed to create Kubernetes client: %w", err)
			}

			var deployment *appsv1.Deployment
			deployment, err = utils.GetControllerDeployment(controller.GetKubeRestConfig(), controllerNs)
			if err != nil {
				return err
			}

			policy := &policyv1alpha1.Policy{}
			err = kubeClient.Get(context.Background(), k8sclient.ObjectKey{
				Namespace: deployment.Namespace,
				Name:      policyName,
			}, policy)
			if err != nil {
				if utils.IsNoMatchError(err) {
					_, err = client.DeleteDynamicConfig(context.Background(), &languagev1.DeleteDynamicConfigRequest{
						PolicyName: policyName,
					})
					if err != nil {
						return err
					}
				} else {
					return fmt.Errorf("failed to get Dynamic Config '%s': %w", policyName, err)
				}
			}
			if policy.DynamicConfig.Raw != nil {
				policy.DynamicConfig = runtime.RawExtension{}
				err = kubeClient.Update(context.Background(), policy)
				if err != nil {
					return fmt.Errorf("failed to delete DynamicConfig for policy '%s': %w", policyName, err)
				}
			} else {
				fmt.Println("No DynamicConfig found for policy", policyName)
				return nil
			}
		} else {
			_, err := client.DeleteDynamicConfig(context.Background(), &languagev1.DeleteDynamicConfigRequest{
				PolicyName: policyName,
			})
			if err != nil {
				return err
			}
		}
		fmt.Println("Deleted DynamicConfig successfully")
		return nil
	},
}

DelCmd is a command to delete a policy's dynamic config.

View Source
var DynamicConfigCmd = &cobra.Command{
	Use:   "dynamic-config",
	Short: "DynamicConfig of Aperture Policy related commands for the Controller",
	Long: `
Use this command to manage the DynamicConfig of the Aperture Policies to the Controller.`,
	SilenceErrors: true,
	PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
		var err error
		err = controller.PreRunE(cmd, args)
		if err != nil {
			return fmt.Errorf("failed to run controller pre-run: %w", err)
		}

		controllerNs = utils.GetControllerNs()

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

		return nil
	},
	PersistentPostRun: controller.PostRun,
}

DynamicConfigCmd is the command to manage DynamicCOnfig of Policies in the Controller.

View Source
var GetCmd = &cobra.Command{
	Use:           "get POLICY_NAME",
	Short:         "Get Aperture DynamicConfig for a Policy.",
	Long:          "Use this command to get the Aperture DynamicConfig of a Policy.",
	Example:       "aperture dynamic-config get rate-limiting",
	SilenceErrors: true,
	Args:          cobra.ExactArgs(1),
	RunE: func(_ *cobra.Command, args []string) error {
		policyName := args[0]
		if controller.IsKube() {
			err := api.AddToScheme(scheme.Scheme)
			if err != nil {
				return fmt.Errorf("failed to connect to Kubernetes: %w", err)
			}

			var kubeClient k8sclient.Client
			kubeClient, err = k8sclient.New(controller.GetKubeRestConfig(), k8sclient.Options{
				Scheme: scheme.Scheme,
			})
			if err != nil {
				return fmt.Errorf("failed to create Kubernetes client: %w", err)
			}

			var deployment *appsv1.Deployment
			deployment, err = utils.GetControllerDeployment(controller.GetKubeRestConfig(), controllerNs)
			if err != nil {
				return fmt.Errorf("failed to get controller deployment: %w", err)
			}

			policy := &policyv1alpha1.Policy{}
			err = kubeClient.Get(context.Background(), k8sclient.ObjectKey{
				Namespace: deployment.Namespace,
				Name:      policyName,
			}, policy)
			if err != nil {
				if utils.IsNoMatchError(err) {
					err = utils.GetDynamicConfigUsingAPI(client, policyName)
					if err != nil {
						return err
					}
				} else {
					return fmt.Errorf("failed to get Dynamic Config '%s': %w", policyName, err)
				}
			}
			if len(policy.DynamicConfig.Raw) == 0 {
				log.Info().Str("policy-name", policyName).Msg("DynamicConfig is not set for the given Policy")
				return nil
			}
			j, err := policy.DynamicConfig.MarshalJSON()
			if err != nil {
				return fmt.Errorf("failed to marshal response: %w", err)
			}

			yamlString, err := utils.GetYAMLString(j)
			if err != nil {
				return fmt.Errorf("failed to convert JSON to YAML: %w", err)
			}
			fmt.Print(yamlString)
		} else {
			err := utils.GetDynamicConfigUsingAPI(client, policyName)
			if err != nil {
				return fmt.Errorf("failed to get dynamic config using API: %w", err)
			}
		}
		return nil
	},
}

GetCmd is a command to get a policy's dynamic config.

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