utils

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: May 26, 2023 License: Apache-2.0 Imports: 30 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// #nosec G101
	ACMClusterSecretLabel = "apps.open-cluster-management.io/secret-type"
	// #nosec G101
	ArgocdClusterSecretLabel = "apps.open-cluster-management.io/acm-cluster"
	// #nosec G101
	ACMClusterNameLabel = "apps.open-cluster-management.io/cluster-name"
)
View Source
const NoiseLogLel = 5

NoiseLogLel - information inside "important functions"

View Source
const QuiteLogLel = 4

QuiteLogLel - "important" information

View Source
const VeryNoisy = 10

VeryNoisy = show call stack, routine and everything

Variables

View Source
var (
	AdminUsers = map[string]bool{
		"admin":                               true,
		"multicluster-observability-operator": true,
		"openshift-gitops-argocd-application-controller": true,
	}
	AdminGroups = map[string]bool{
		"masters":        true,
		"cluster-admins": true,
	}
)
View Source
var AcmClusterSecretPredicateFunc = predicate.Funcs{
	UpdateFunc: func(e event.UpdateEvent) bool {
		oldSecret, ok := e.ObjectOld.(*v1.Secret)
		if !ok {
			return false
		}

		newSecret, nok := e.ObjectNew.(*v1.Secret)
		if !nok {
			return false
		}

		oldSecretType, ok := e.ObjectOld.GetLabels()[ACMClusterSecretLabel]
		newSecretType, nok := e.ObjectNew.GetLabels()[ACMClusterSecretLabel]

		if ok && oldSecretType == "acm-cluster" {
			klog.Infof("Update a old ACM cluster secret, old: %v/%v, new: %v/%v", oldSecret.Namespace, oldSecret.Name, newSecret.Namespace, newSecret.Name)
			return true
		}

		if nok && newSecretType == "acm-cluster" {
			klog.Infof("Update a new ACM cluster secret, old: %v/%v, new: %v/%v", oldSecret.Namespace, oldSecret.Name, newSecret.Namespace, newSecret.Name)
			return true
		}

		klog.Infof("Not a ACM cluster secret update, old: %v/%v, new: %v/%v", oldSecret.Namespace, oldSecret.Name, newSecret.Namespace, newSecret.Name)
		return false
	},
	CreateFunc: func(e event.CreateEvent) bool {
		SecretType, ok := e.Object.GetLabels()[ACMClusterSecretLabel]

		if !ok {
			return false
		} else if SecretType != "acm-cluster" {
			return false
		}

		klog.Infof("Create a ACM cluster secret: %v/%v", e.Object.GetNamespace(), e.Object.GetName())
		return true
	},
	DeleteFunc: func(e event.DeleteEvent) bool {
		SecretType, ok := e.Object.GetLabels()[ACMClusterSecretLabel]

		if !ok {
			return false
		} else if SecretType != "acm-cluster" {
			return false
		}

		klog.Infof("Delete a ACM cluster secret: %v/%v", e.Object.GetNamespace(), e.Object.GetName())
		return true
	},
}

AcmClusterSecretPredicateFunc defines predicate function for ACM cluster secrets watch

View Source
var ArgocdClusterSecretPredicateFunc = predicate.Funcs{
	UpdateFunc: func(e event.UpdateEvent) bool {
		oldSecret, ok := e.ObjectOld.(*v1.Secret)
		if !ok {
			return false
		}

		newSecret, nok := e.ObjectNew.(*v1.Secret)
		if !nok {
			return false
		}

		oldSecretType, ok := e.ObjectOld.GetLabels()[ArgocdClusterSecretLabel]
		newSecretType, nok := e.ObjectNew.GetLabels()[ArgocdClusterSecretLabel]

		if ok && oldSecretType == "true" {
			klog.Infof("Update a old ArgoCD cluster secret, old: %v/%v, new: %v/%v", oldSecret.Namespace, oldSecret.Name, newSecret.Namespace, newSecret.Name)
			return true
		}

		if nok && newSecretType == "true" {
			klog.Infof("Update a new Argocd cluster secret, old: %v/%v, new: %v/%v", oldSecret.Namespace, oldSecret.Name, newSecret.Namespace, newSecret.Name)
			return true
		}

		klog.Infof("Not a ArgoCD cluster secret update, old: %v/%v, new: %v/%v", oldSecret.Namespace, oldSecret.Name, newSecret.Namespace, newSecret.Name)
		return false
	},
	CreateFunc: func(e event.CreateEvent) bool {
		SecretType, ok := e.Object.GetLabels()[ArgocdClusterSecretLabel]

		if !ok {
			return false
		} else if SecretType != "true" {
			return false
		}

		klog.Infof("Create a ArgoCD cluster secret: %v/%v", e.Object.GetNamespace(), e.Object.GetName())
		return true
	},
	DeleteFunc: func(e event.DeleteEvent) bool {
		SecretType, ok := e.Object.GetLabels()[ArgocdClusterSecretLabel]

		if !ok {
			return false
		} else if SecretType != "true" {
			return false
		}

		klog.Infof("Delete a ArgoCD cluster secret: %v/%v", e.Object.GetNamespace(), e.Object.GetName())
		return true
	},
}

ArgocdClusterSecretPredicateFunc defines predicate function for ArgoCD cluster secrets watch

View Source
var ArgocdServerPredicateFunc = predicate.Funcs{
	UpdateFunc: func(e event.UpdateEvent) bool {
		oldService, ok := e.ObjectOld.(*v1.Service)
		if !ok {
			return false
		}

		newService, nok := e.ObjectNew.(*v1.Service)
		if !nok {
			return false
		}

		oldArgocdServerLabel := e.ObjectOld.GetLabels()
		newArgocdServerLabel := e.ObjectNew.GetLabels()

		if oldArgocdServerLabel != nil && oldArgocdServerLabel["app.kubernetes.io/part-of"] == "argocd" &&
			oldArgocdServerLabel["app.kubernetes.io/component"] == "server" {
			klog.Infof("Update a old ArgoCD Server Service, old: %v/%v, new: %v/%v", oldService.Namespace, oldService.Name, newService.Namespace, newService.Name)
			return true
		}

		if newArgocdServerLabel != nil && newArgocdServerLabel["app.kubernetes.io/part-of"] == "argocd" &&
			newArgocdServerLabel["app.kubernetes.io/component"] == "server" {
			klog.Infof("Update a new ArgoCD Server Service, old: %v/%v, new: %v/%v", oldService.Namespace, oldService.Name, newService.Namespace, newService.Name)
			return true
		}

		klog.Infof("Not a ArgoCD Server service, old: %v/%v, new: %v/%v", oldService.Namespace, oldService.Name, newService.Namespace, newService.Name)
		return false
	},
	CreateFunc: func(e event.CreateEvent) bool {
		ArgocdServerLabel := e.Object.GetLabels()

		if ArgocdServerLabel == nil {
			return false
		} else if ArgocdServerLabel["app.kubernetes.io/part-of"] != "argocd" ||
			ArgocdServerLabel["app.kubernetes.io/component"] != "server" {
			return false
		}

		klog.Infof("Create a ArgoCD Server Service: %v/%v", e.Object.GetNamespace(), e.Object.GetName())
		return true
	},
	DeleteFunc: func(e event.DeleteEvent) bool {
		ArgocdServerLabel := e.Object.GetLabels()

		if ArgocdServerLabel == nil {
			return false
		} else if ArgocdServerLabel["app.kubernetes.io/part-of"] != "argocd" ||
			ArgocdServerLabel["app.kubernetes.io/component"] != "server" {
			return false
		}

		klog.Infof("Delete a ArgoCD Server Service: %v/%v", e.Object.GetNamespace(), e.Object.GetName())
		return true
	},
}

ArgocdServerPredicateFunc defines predicate function for cluster related watch

View Source
var ClusterPredicateFunc = predicate.Funcs{
	CreateFunc: func(e event.CreateEvent) bool {
		newManagedCluster, ok := e.Object.(*spokeClusterV1.ManagedCluster)

		if !ok {
			return false
		}

		klog.Infof("new managed cluster created, %v/%v", newManagedCluster.Namespace, newManagedCluster.Name)

		return true
	},

	UpdateFunc: func(e event.UpdateEvent) bool {
		oldcl, oldOK := e.ObjectOld.(*spokeClusterV1.ManagedCluster)
		newcl, newOK := e.ObjectNew.(*spokeClusterV1.ManagedCluster)

		if !oldOK || !newOK {
			return false
		}

		if !reflect.DeepEqual(oldcl.DeletionTimestamp, newcl.DeletionTimestamp) {
			return true
		}

		if !reflect.DeepEqual(oldcl.Labels, newcl.Labels) {
			return true
		}

		oldcondMap := make(map[string]metav1.ConditionStatus)
		for _, cond := range oldcl.Status.Conditions {
			oldcondMap[cond.Type] = cond.Status
		}
		for _, cond := range newcl.Status.Conditions {
			oldcondst, ok := oldcondMap[cond.Type]
			if !ok || oldcondst != cond.Status {
				return true
			}
			delete(oldcondMap, cond.Type)
		}

		if len(oldcondMap) > 0 {
			return true
		}

		klog.V(1).Info("Out Cluster Predicate Func ", oldcl.Name, " with false possitive")
		return false
	},
}

ClusterPredicateFunc defines predicate function for cluster related watch, main purpose is to ignore heartbeat without change

View Source
var ManagedClusterSecretPredicateFunc = predicate.Funcs{
	UpdateFunc: func(e event.UpdateEvent) bool {
		_, isSecretInArgo := e.ObjectNew.GetLabels()[ArgocdClusterSecretLabel]

		if isSecretInArgo {
			klog.Infof("Managed cluster secret in ArgoCD namespace updated: %v/%v", e.ObjectNew.GetNamespace(), e.ObjectNew.GetName())

			return false
		}

		return true
	},
	CreateFunc: func(e event.CreateEvent) bool {
		_, isSecretInArgo := e.Object.GetLabels()[ArgocdClusterSecretLabel]

		if isSecretInArgo {
			klog.Infof("Managed cluster secret in ArgoCD namespace created: %v/%v", e.Object.GetNamespace(), e.Object.GetName())

			return false
		}

		return true
	},
	DeleteFunc: func(e event.DeleteEvent) bool {
		_, isSecretInArgo := e.Object.GetLabels()[ArgocdClusterSecretLabel]

		if isSecretInArgo {
			klog.Infof("Managed cluster secret in ArgoCD namespace deleted: %v/%v", e.Object.GetNamespace(), e.Object.GetName())

			return true
		}

		return false
	},
}

ManagedClusterSecretPredicateFunc defines predicate function for managed cluster secrets watch

View Source
var PlacementDecisionPredicateFunc = predicate.Funcs{
	CreateFunc: func(e event.CreateEvent) bool {
		decision, ok := e.Object.(*clusterv1beta1.PlacementDecision)

		if !ok {
			return false
		}

		klog.Infof("placement decision created, %v/%v", decision.Namespace, decision.Name)
		return true
	},
	DeleteFunc: func(e event.DeleteEvent) bool {
		decision, ok := e.Object.(*clusterv1beta1.PlacementDecision)

		if !ok {
			return false
		}

		klog.Infof("placement decision deleted, %v/%v", decision.Namespace, decision.Name)
		return true
	},
	UpdateFunc: func(e event.UpdateEvent) bool {
		oldDecision := e.ObjectOld.(*clusterv1beta1.PlacementDecision)
		newDecision := e.ObjectNew.(*clusterv1beta1.PlacementDecision)

		klog.Infof("placement decision updated, %v/%v", newDecision.Namespace, newDecision.Name)

		return !reflect.DeepEqual(oldDecision.Status, newDecision.Status)
	},
}

Functions

func ConvertLabels

func ConvertLabels(labelSelector *metav1.LabelSelector) (labels.Selector, error)

ConvertLabels coverts label selector to lables.Selector

func DetectClusterRegistry

func DetectClusterRegistry(ctx context.Context, clReader client.Reader)

DetectClusterRegistry - Detect the ACM cluster API service every 10 seconds. the controller will be exited when it is ready The controller will be auto restarted by the multicluster-operators-application deployment CR later.

func EnterFnString

func EnterFnString() string

EnterFnString - called when enter a function

func ExitFuString

func ExitFuString(s string)

ExitFuString - called when exiting a function

func ExtractUserAndGroup

func ExtractUserAndGroup(annotations map[string]string) (string, []string)

func FilteClustersByIdentity

func FilteClustersByIdentity(authClient kubernetes.Interface, object runtime.Object, clmap map[string]*spokeClusterV1.ManagedCluster) error

func GetFnName

func GetFnName() string

GetFnName - get name of function

func IfClusterAdmin

func IfClusterAdmin(user string, groups []string) bool

func InstanceDeepCopy

func InstanceDeepCopy(a, b interface{}) error

func IsReadyACMClusterRegistry

func IsReadyACMClusterRegistry(clReader client.Reader) bool

IsReadyACMClusterRegistry check if ACM Cluster API service is ready or not.

func PlaceByGenericPlacmentFields

func PlaceByGenericPlacmentFields(kubeclient client.Client, placement appv1alpha1.GenericPlacementFields,
	object runtime.Object) (map[string]*spokeClusterV1.ManagedCluster, error)

PlaceByGenericPlacmentFields search with basic placement criteria Top priority: clusterNames, ignore selector Bottomline: Use label selector

func ToPlaceLocal

func ToPlaceLocal(placement *appv1alpha1.Placement) bool

func ValidateK8sLabel

func ValidateK8sLabel(s string) string

ValidateK8sLabel returns a valid k8s label string by enforcing k8s label values rules as below

  1. Must consist of alphanumeric characters, '-', '_' or '.' No need to check this as the input string is the host name of the k8s api url
  2. Must be no more than 63 characters
  3. Must start and end with an alphanumeric character

Types

type EventRecorder

type EventRecorder struct {
	record.EventRecorder
}

EventRecorder - record kubernetes event

func NewEventRecorder

func NewEventRecorder(cfg *rest.Config, scheme *apiruntime.Scheme) (*EventRecorder, error)

NewEventRecorder - create new event recorder from rect config

func (*EventRecorder) RecordEvent

func (rec *EventRecorder) RecordEvent(obj apiruntime.Object, reason, msg string, err error)

RecordEvent - record kuberentes event

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL