context

package
v1.0.0-beta.7 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2020 License: Apache-2.0 Imports: 17 Imported by: 1

README

Binding Context Generator

Binding Context Generator allows to generate binding context values for hook testing purpose.

Usage example:

  1. Declare the hook config
config := `
{
  "configVersion":"v1",
  "kubernetes":[
	{
	  "apiVersion":"v1",
	  "kind":"Pod",
	  "watchEvent":["Added", "Modified", "Deleted"],
	  "namespace": {
		"nameSelector": {
		  "matchNames": ["default"]
		}
	  }
	}
  ]
}`
  1. Declare initial state of kubernetes objects
initialState := `
---
apiVersion: v1
kind: Pod
metadata:
  name: pod-0
---
apiVersion: v1
kind: Pod
metadata:
  name: pod-1
`
  1. Declare new state (one of pods are deleted)
newState := `
---
apiVersion: v1
kind: Pod
metadata:
  name: pod-0
`
  1. Create new binding context controller
c, err := context.NewBindingContextController(config, initialState)
if err != nil {
  return err
}
  1. Register CRD (if you use some in tests) in format [Group, Version, Kind, isNamespaced]:
c.RegisterCRD("example.com", "v1", "Example", true)
c.RegisterCRD("example.com", "v1", "ClusterExample", false)
  1. Run controller to get initial binding context
contexts, err := c.Run()
if err != nil {
  return err
}
testContexts(contexts)
  1. Change state to new get binding contexts
contexts, err = c.ChangeState(newState)
if err != nil {
  return err
}
testNewContexts(contexts)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ClusterResources = []*metav1.APIResourceList{
	{
		GroupVersion: "v1",
		APIResources: []metav1.APIResource{
			{
				Kind:       "Binding",
				Name:       "bindings",
				Verbs:      metav1.Verbs{"create"},
				Group:      "",
				Version:    "v1",
				Namespaced: true,
			},
			{
				Kind:       "ComponentStatus",
				Name:       "componentstatuses",
				Verbs:      metav1.Verbs{"get", "list"},
				Group:      "",
				Version:    "v1",
				Namespaced: false,
			},
			{
				Kind:       "ConfigMap",
				Name:       "configmaps",
				Verbs:      metav1.Verbs{"create", "delete", "deletecollection", "get", "list", "patch", "update", "watch"},
				Group:      "",
				Version:    "v1",
				Namespaced: true,
			},
			{
				Kind:       "Endpoints",
				Name:       "endpoints",
				Verbs:      metav1.Verbs{"create", "delete", "deletecollection", "get", "list", "patch", "update", "watch"},
				Group:      "",
				Version:    "v1",
				Namespaced: true,
			},
			{
				Kind:       "Event",
				Name:       "events",
				Verbs:      metav1.Verbs{"create", "delete", "deletecollection", "get", "list", "patch", "update", "watch"},
				Group:      "",
				Version:    "v1",
				Namespaced: true,
			},
			{
				Kind:       "LimitRange",
				Name:       "limitranges",
				Verbs:      metav1.Verbs{"create", "delete", "deletecollection", "get", "list", "patch", "update", "watch"},
				Group:      "",
				Version:    "v1",
				Namespaced: true,
			},
			{
				Kind:       "Namespace",
				Name:       "namespaces",
				Verbs:      metav1.Verbs{"create", "delete", "get", "list", "patch", "update", "watch"},
				Group:      "",
				Version:    "v1",
				Namespaced: false,
			},
			{
				Kind:       "Node",
				Name:       "nodes",
				Verbs:      metav1.Verbs{"create", "delete", "deletecollection", "get", "list", "patch", "update", "watch"},
				Group:      "",
				Version:    "v1",
				Namespaced: false,
			},
			{
				Kind:       "PersistentVolumeClaim",
				Name:       "persistentvolumeclaims",
				Verbs:      metav1.Verbs{"create", "delete", "deletecollection", "get", "list", "patch", "update", "watch"},
				Group:      "",
				Version:    "v1",
				Namespaced: true,
			},
			{
				Kind:       "PersistentVolume",
				Name:       "persistentvolumes",
				Verbs:      metav1.Verbs{"create", "delete", "deletecollection", "get", "list", "patch", "update", "watch"},
				Group:      "",
				Version:    "v1",
				Namespaced: false,
			},
			{
				Kind:       "Pod",
				Name:       "pods",
				Verbs:      metav1.Verbs{"create", "delete", "deletecollection", "get", "list", "patch", "update", "watch"},
				Group:      "",
				Version:    "v1",
				Namespaced: true,
			},
			{
				Kind:       "PodTemplate",
				Name:       "podtemplates",
				Verbs:      metav1.Verbs{"create", "delete", "deletecollection", "get", "list", "patch", "update", "watch"},
				Group:      "",
				Version:    "v1",
				Namespaced: true,
			},
			{
				Kind:       "ReplicationController",
				Name:       "replicationcontrollers",
				Verbs:      metav1.Verbs{"create", "delete", "deletecollection", "get", "list", "patch", "update", "watch"},
				Group:      "",
				Version:    "v1",
				Namespaced: true,
			},
			{
				Kind:       "ResourceQuota",
				Name:       "resourcequotas",
				Verbs:      metav1.Verbs{"create", "delete", "deletecollection", "get", "list", "patch", "update", "watch"},
				Group:      "",
				Version:    "v1",
				Namespaced: true,
			},
			{
				Kind:       "Secret",
				Name:       "secrets",
				Verbs:      metav1.Verbs{"create", "delete", "deletecollection", "get", "list", "patch", "update", "watch"},
				Group:      "",
				Version:    "v1",
				Namespaced: true,
			},
			{
				Kind:       "ServiceAccount",
				Name:       "serviceaccounts",
				Verbs:      metav1.Verbs{"create", "delete", "deletecollection", "get", "list", "patch", "update", "watch"},
				Group:      "",
				Version:    "v1",
				Namespaced: true,
			},
			{
				Kind:       "Service",
				Name:       "services",
				Verbs:      metav1.Verbs{"create", "delete", "get", "list", "patch", "update", "watch"},
				Group:      "",
				Version:    "v1",
				Namespaced: true,
			},
		},
	},
	{
		GroupVersion: "apiregistration.k8s.io/v1",
		APIResources: []metav1.APIResource{
			{
				Kind:       "APIService",
				Name:       "apiservices",
				Verbs:      metav1.Verbs{"create", "delete", "deletecollection", "get", "list", "patch", "update", "watch"},
				Group:      "apiregistration.k8s.io",
				Version:    "v1",
				Namespaced: false,
			},
		},
	},
	{
		GroupVersion: "extensions/v1beta1",
		APIResources: []metav1.APIResource{
			{
				Kind:       "Ingress",
				Name:       "ingresses",
				Verbs:      metav1.Verbs{"create", "delete", "deletecollection", "get", "list", "patch", "update", "watch"},
				Group:      "extensions",
				Version:    "v1beta1",
				Namespaced: true,
			},
			{
				Kind:       "NetworkPolicy",
				Name:       "networkpolicies",
				Verbs:      metav1.Verbs{"create", "delete", "deletecollection", "get", "list", "patch", "update", "watch"},
				Group:      "extensions",
				Version:    "v1beta1",
				Namespaced: true,
			},
			{
				Kind:       "PodSecurityPolicy",
				Name:       "podsecuritypolicies",
				Verbs:      metav1.Verbs{"create", "delete", "deletecollection", "get", "list", "patch", "update", "watch"},
				Group:      "extensions",
				Version:    "v1beta1",
				Namespaced: false,
			},
			{
				Kind:       "ReplicaSet",
				Name:       "replicasets",
				Verbs:      metav1.Verbs{"create", "delete", "deletecollection", "get", "list", "patch", "update", "watch"},
				Group:      "extensions",
				Version:    "v1beta1",
				Namespaced: true,
			},
			{
				Kind:       "ReplicationControllerDummy",
				Name:       "replicationcontrollers",
				Verbs:      metav1.Verbs{},
				Group:      "extensions",
				Version:    "v1beta1",
				Namespaced: true,
			},
		},
	},
	{
		GroupVersion: "apps/v1",
		APIResources: []metav1.APIResource{
			{
				Kind:       "ControllerRevision",
				Name:       "controllerrevisions",
				Verbs:      metav1.Verbs{"create", "delete", "deletecollection", "get", "list", "patch", "update", "watch"},
				Group:      "apps",
				Version:    "v1",
				Namespaced: true,
			},
			{
				Kind:       "DaemonSet",
				Name:       "daemonsets",
				Verbs:      metav1.Verbs{"create", "delete", "deletecollection", "get", "list", "patch", "update", "watch"},
				Group:      "apps",
				Version:    "v1",
				Namespaced: true,
			},
			{
				Kind:       "Deployment",
				Name:       "deployments",
				Verbs:      metav1.Verbs{"create", "delete", "deletecollection", "get", "list", "patch", "update", "watch"},
				Group:      "apps",
				Version:    "v1",
				Namespaced: true,
			},
			{
				Kind:       "ReplicaSet",
				Name:       "replicasets",
				Verbs:      metav1.Verbs{"create", "delete", "deletecollection", "get", "list", "patch", "update", "watch"},
				Group:      "apps",
				Version:    "v1",
				Namespaced: true,
			},
			{
				Kind:       "StatefulSet",
				Name:       "statefulsets",
				Verbs:      metav1.Verbs{"create", "delete", "deletecollection", "get", "list", "patch", "update", "watch"},
				Group:      "apps",
				Version:    "v1",
				Namespaced: true,
			},
		},
	},
	{
		GroupVersion: "events.k8s.io/v1beta1",
		APIResources: []metav1.APIResource{
			{
				Kind:       "Event",
				Name:       "events",
				Verbs:      metav1.Verbs{"create", "delete", "deletecollection", "get", "list", "patch", "update", "watch"},
				Group:      "events.k8s.io",
				Version:    "v1beta1",
				Namespaced: true,
			},
		},
	},
	{
		GroupVersion: "authentication.k8s.io/v1",
		APIResources: []metav1.APIResource{
			{
				Kind:       "TokenReview",
				Name:       "tokenreviews",
				Verbs:      metav1.Verbs{"create"},
				Group:      "authentication.k8s.io",
				Version:    "v1",
				Namespaced: false,
			},
		},
	},
	{
		GroupVersion: "authorization.k8s.io/v1",
		APIResources: []metav1.APIResource{
			{
				Kind:       "LocalSubjectAccessReview",
				Name:       "localsubjectaccessreviews",
				Verbs:      metav1.Verbs{"create"},
				Group:      "authorization.k8s.io",
				Version:    "v1",
				Namespaced: true,
			},
			{
				Kind:       "SelfSubjectAccessReview",
				Name:       "selfsubjectaccessreviews",
				Verbs:      metav1.Verbs{"create"},
				Group:      "authorization.k8s.io",
				Version:    "v1",
				Namespaced: false,
			},
			{
				Kind:       "SelfSubjectRulesReview",
				Name:       "selfsubjectrulesreviews",
				Verbs:      metav1.Verbs{"create"},
				Group:      "authorization.k8s.io",
				Version:    "v1",
				Namespaced: false,
			},
			{
				Kind:       "SubjectAccessReview",
				Name:       "subjectaccessreviews",
				Verbs:      metav1.Verbs{"create"},
				Group:      "authorization.k8s.io",
				Version:    "v1",
				Namespaced: false,
			},
		},
	},
	{
		GroupVersion: "autoscaling/v1",
		APIResources: []metav1.APIResource{
			{
				Kind:       "HorizontalPodAutoscaler",
				Name:       "horizontalpodautoscalers",
				Verbs:      metav1.Verbs{"create", "delete", "deletecollection", "get", "list", "patch", "update", "watch"},
				Group:      "autoscaling",
				Version:    "v1",
				Namespaced: true,
			},
		},
	},
	{
		GroupVersion: "batch/v1",
		APIResources: []metav1.APIResource{
			{
				Kind:       "Job",
				Name:       "jobs",
				Verbs:      metav1.Verbs{"create", "delete", "deletecollection", "get", "list", "patch", "update", "watch"},
				Group:      "batch",
				Version:    "v1",
				Namespaced: true,
			},
		},
	},
	{
		GroupVersion: "networking.k8s.io/v1",
		APIResources: []metav1.APIResource{
			{
				Kind:       "NetworkPolicy",
				Name:       "networkpolicies",
				Verbs:      metav1.Verbs{"create", "delete", "deletecollection", "get", "list", "patch", "update", "watch"},
				Group:      "networking.k8s.io",
				Version:    "v1",
				Namespaced: true,
			},
		},
	},
	{
		GroupVersion: "policy/v1beta1",
		APIResources: []metav1.APIResource{
			{
				Kind:       "PodDisruptionBudget",
				Name:       "poddisruptionbudgets",
				Verbs:      metav1.Verbs{"create", "delete", "deletecollection", "get", "list", "patch", "update", "watch"},
				Group:      "policy",
				Version:    "v1beta1",
				Namespaced: true,
			},
			{
				Kind:       "PodSecurityPolicy",
				Name:       "podsecuritypolicies",
				Verbs:      metav1.Verbs{"create", "delete", "deletecollection", "get", "list", "patch", "update", "watch"},
				Group:      "policy",
				Version:    "v1beta1",
				Namespaced: false,
			},
		},
	},
	{
		GroupVersion: "rbac.authorization.k8s.io/v1",
		APIResources: []metav1.APIResource{
			{
				Kind:       "ClusterRoleBinding",
				Name:       "clusterrolebindings",
				Verbs:      metav1.Verbs{"create", "delete", "deletecollection", "get", "list", "patch", "update", "watch"},
				Group:      "rbac.authorization.k8s.io",
				Version:    "v1",
				Namespaced: false,
			},
			{
				Kind:       "ClusterRole",
				Name:       "clusterroles",
				Verbs:      metav1.Verbs{"create", "delete", "deletecollection", "get", "list", "patch", "update", "watch"},
				Group:      "rbac.authorization.k8s.io",
				Version:    "v1",
				Namespaced: false,
			},
			{
				Kind:       "RoleBinding",
				Name:       "rolebindings",
				Verbs:      metav1.Verbs{"create", "delete", "deletecollection", "get", "list", "patch", "update", "watch"},
				Group:      "rbac.authorization.k8s.io",
				Version:    "v1",
				Namespaced: true,
			},
			{
				Kind:       "Role",
				Name:       "roles",
				Verbs:      metav1.Verbs{"create", "delete", "deletecollection", "get", "list", "patch", "update", "watch"},
				Group:      "rbac.authorization.k8s.io",
				Version:    "v1",
				Namespaced: true,
			},
		},
	},
	{
		GroupVersion: "storage.k8s.io/v1",
		APIResources: []metav1.APIResource{
			{
				Kind:       "StorageClass",
				Name:       "storageclasses",
				Verbs:      metav1.Verbs{"create", "delete", "deletecollection", "get", "list", "patch", "update", "watch"},
				Group:      "storage.k8s.io",
				Version:    "v1",
				Namespaced: false,
			},
			{
				Kind:       "VolumeAttachment",
				Name:       "volumeattachments",
				Verbs:      metav1.Verbs{"create", "delete", "deletecollection", "get", "list", "patch", "update", "watch"},
				Group:      "storage.k8s.io",
				Version:    "v1",
				Namespaced: false,
			},
		},
	},
	{
		GroupVersion: "admissionregistration.k8s.io/v1beta1",
		APIResources: []metav1.APIResource{
			{
				Kind:       "MutatingWebhookConfiguration",
				Name:       "mutatingwebhookconfigurations",
				Verbs:      metav1.Verbs{"create", "delete", "deletecollection", "get", "list", "patch", "update", "watch"},
				Group:      "admissionregistration.k8s.io",
				Version:    "v1beta1",
				Namespaced: false,
			},
			{
				Kind:       "ValidatingWebhookConfiguration",
				Name:       "validatingwebhookconfigurations",
				Verbs:      metav1.Verbs{"create", "delete", "deletecollection", "get", "list", "patch", "update", "watch"},
				Group:      "admissionregistration.k8s.io",
				Version:    "v1beta1",
				Namespaced: false,
			},
		},
	},
	{
		GroupVersion: "apiextensions.k8s.io/v1beta1",
		APIResources: []metav1.APIResource{
			{
				Kind:       "CustomResourceDefinition",
				Name:       "customresourcedefinitions",
				Verbs:      metav1.Verbs{"create", "delete", "deletecollection", "get", "list", "patch", "update", "watch"},
				Group:      "apiextensions.k8s.io",
				Version:    "v1beta1",
				Namespaced: false,
			},
		},
	},
	{
		GroupVersion: "scheduling.k8s.io/v1",
		APIResources: []metav1.APIResource{
			{
				Kind:       "PriorityClass",
				Name:       "priorityclasses",
				Verbs:      metav1.Verbs{"create", "delete", "deletecollection", "get", "list", "patch", "update", "watch"},
				Group:      "scheduling.k8s.io",
				Version:    "v1",
				Namespaced: false,
			},
		},
	},
	{
		GroupVersion: "coordination.k8s.io/v1",
		APIResources: []metav1.APIResource{
			{
				Kind:       "Lease",
				Name:       "leases",
				Verbs:      metav1.Verbs{"create", "delete", "deletecollection", "get", "list", "patch", "update", "watch"},
				Group:      "coordination.k8s.io",
				Version:    "v1",
				Namespaced: true,
			},
		},
	},
	{
		GroupVersion: "node.k8s.io/v1beta1",
		APIResources: []metav1.APIResource{
			{
				Kind:       "RuntimeClass",
				Name:       "runtimeclasses",
				Verbs:      metav1.Verbs{"create", "delete", "deletecollection", "get", "list", "patch", "update", "watch"},
				Group:      "node.k8s.io",
				Version:    "v1beta1",
				Namespaced: false,
			},
		},
	},
	{
		GroupVersion: "snapshot.storage.k8s.io/v1alpha1",
		APIResources: []metav1.APIResource{
			{
				Kind:       "VolumeSnapshotContent",
				Name:       "volumesnapshotcontents",
				Verbs:      metav1.Verbs{"delete", "deletecollection", "get", "list", "patch", "create", "update", "watch"},
				Group:      "snapshot.storage.k8s.io",
				Version:    "v1alpha1",
				Namespaced: false,
			},
			{
				Kind:       "VolumeSnapshot",
				Name:       "volumesnapshots",
				Verbs:      metav1.Verbs{"delete", "deletecollection", "get", "list", "patch", "create", "update", "watch"},
				Group:      "snapshot.storage.k8s.io",
				Version:    "v1alpha1",
				Namespaced: true,
			},
			{
				Kind:       "VolumeSnapshotClass",
				Name:       "volumesnapshotclasses",
				Verbs:      metav1.Verbs{"delete", "deletecollection", "get", "list", "patch", "create", "update", "watch"},
				Group:      "snapshot.storage.k8s.io",
				Version:    "v1alpha1",
				Namespaced: false,
			},
		},
	},
	{
		GroupVersion: "autoscaling.k8s.io/v1beta2",
		APIResources: []metav1.APIResource{
			{
				Kind:       "VerticalPodAutoscalerCheckpoint",
				Name:       "verticalpodautoscalercheckpoints",
				Verbs:      metav1.Verbs{"delete", "deletecollection", "get", "list", "patch", "create", "update", "watch"},
				Group:      "autoscaling.k8s.io",
				Version:    "v1beta2",
				Namespaced: true,
			},
			{
				Kind:       "VerticalPodAutoscaler",
				Name:       "verticalpodautoscalers",
				Verbs:      metav1.Verbs{"delete", "deletecollection", "get", "list", "patch", "create", "update", "watch"},
				Group:      "autoscaling.k8s.io",
				Version:    "v1beta2",
				Namespaced: true,
			},
		},
	},
	{
		GroupVersion: "metrics.k8s.io/v1beta1",
		APIResources: []metav1.APIResource{
			{
				Kind:       "NodeMetrics",
				Name:       "nodes",
				Verbs:      metav1.Verbs{"get", "list"},
				Group:      "metrics.k8s.io",
				Version:    "v1beta1",
				Namespaced: false,
			},
			{
				Kind:       "PodMetrics",
				Name:       "pods",
				Verbs:      metav1.Verbs{"get", "list"},
				Group:      "metrics.k8s.io",
				Version:    "v1beta1",
				Namespaced: true,
			},
		},
	},
}

Functions

This section is empty.

Types

type BindingContextController

type BindingContextController struct {
	HookCtrl     controller.HookController
	HookMap      map[string]string
	HookConfig   string
	InitialState string
	Controller   StateController
	Manager      manager.KubeEventsManager
	Context      context.Context
	Cancel       context.CancelFunc
}

func NewBindingContextController

func NewBindingContextController(config, initialState string) (BindingContextController, error)

func (*BindingContextController) ChangeState

func (b *BindingContextController) ChangeState(newState ...string) (string, error)

func (*BindingContextController) RegisterCRD

func (b *BindingContextController) RegisterCRD(group, version, kind string, namespaced bool)

RegisterCRD registers custom resources for the cluster

func (*BindingContextController) Run

BindingContextsGenerator generates binding contexts for hook tests

type StateController

type StateController struct {
	CurrentState State
}

func NewStateController

func NewStateController(initialState string) (StateController, error)

NewStateController creates controller to apply state changes

func (*StateController) ChangeState

func (c *StateController) ChangeState(newRawState string) error

ChangeState apply changes to current objects state

type StateObject

type StateObject struct {
	Kind string
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

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