labelize

package
v0.1.0-rc3 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CheckParameters = func(rule *rules.Rule) error {
	parameters := rule.GetParameters()
	if err := utils.CheckParameters(parameters, "labels", utils.MapInterfaceStr, nil, true); err != nil {
		return err
	}
	if len(parameters["labels"].(map[string]interface{})) == 0 {
		return errors.New("missing parameter 'labels'")
	}
	return nil
}
View Source
var Labelize = func(rule *rules.Rule, event *events.Event) (utils.LogLine, error) {
	pod := event.GetPodName()
	namespace := event.GetNamespaceName()

	objects := map[string]string{
		"Pod":       pod,
		"Namespace": namespace,
	}

	payload := make([]patch, 0)
	parameters := rule.GetParameters()
	for i, j := range parameters["labels"].(map[string]interface{}) {
		if j.(string) == "" {
			continue
		}
		payload = append(payload, patch{
			Op:    "replace",
			Path:  metadataLabels + i,
			Value: fmt.Sprintf("%v", j),
		})
	}

	client := kubernetes.GetClient()

	payloadBytes, _ := json.Marshal(payload)
	_, err := client.Clientset.CoreV1().Pods(namespace).Patch(context.Background(), pod, types.JSONPatchType, payloadBytes, metav1.PatchOptions{})
	if err != nil {
		return utils.LogLine{
				Objects: objects,
				Error:   err.Error(),
				Status:  "failure",
			},
			err
	}

	payload = make([]patch, 0)
	rule.GetParameters()
	for i, j := range parameters["labels"].(map[string]interface{}) {
		if j.(string) != "" {
			continue
		}
		payload = append(payload, patch{
			Op:   "remove",
			Path: metadataLabels + i,
		})
	}

	payloadBytes, _ = json.Marshal(payload)
	_, err = client.Clientset.CoreV1().Pods(namespace).Patch(context.Background(), pod, types.JSONPatchType, payloadBytes, metav1.PatchOptions{})
	if err != nil {
		if err.Error() != "the server rejected our request due to an error in our request" {
			return utils.LogLine{
					Objects: objects,
					Error:   err.Error(),
					Status:  "failure",
				},
				err
		}
	}
	return utils.LogLine{
			Objects: objects,
			Status:  "success",
		},
		nil
}

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