github.com/terraform-modules-krish/terratest@v0.29.0/modules/k8s/errors.go (about) 1 package k8s 2 3 import ( 4 "fmt" 5 6 corev1 "k8s.io/api/core/v1" 7 extensionsv1beta1 "k8s.io/api/extensions/v1beta1" 8 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 9 ) 10 11 // IngressNotAvailable is returned when a Kubernetes service is not yet available to accept traffic. 12 type IngressNotAvailable struct { 13 ingress *extensionsv1beta1.Ingress 14 } 15 16 // Error is a simple function to return a formatted error message as a string 17 func (err IngressNotAvailable) Error() string { 18 return fmt.Sprintf("Ingress %s is not available", err.ingress.Name) 19 } 20 21 // UnknownKubeResourceType is returned if the given resource type does not match the list of known resource types. 22 type UnknownKubeResourceType struct { 23 ResourceType KubeResourceType 24 } 25 26 func (err UnknownKubeResourceType) Error() string { 27 return fmt.Sprintf("ResourceType ID %d is unknown", err.ResourceType) 28 } 29 30 // DesiredNumberOfPodsNotCreated is returned when the number of pods matching a filter condition does not match the 31 // desired number of Pods. 32 type DesiredNumberOfPodsNotCreated struct { 33 Filter metav1.ListOptions 34 DesiredCount int 35 } 36 37 // Error is a simple function to return a formatted error message as a string 38 func (err DesiredNumberOfPodsNotCreated) Error() string { 39 return fmt.Sprintf("Desired number of pods (%d) matching filter %v not yet created", err.DesiredCount, err.Filter) 40 } 41 42 // ServiceAccountTokenNotAvailable is returned when a Kubernetes ServiceAccount does not have a token provisioned yet. 43 type ServiceAccountTokenNotAvailable struct { 44 Name string 45 } 46 47 // Error is a simple function to return a formatted error message as a string 48 func (err ServiceAccountTokenNotAvailable) Error() string { 49 return fmt.Sprintf("ServiceAccount %s does not have a token yet.", err.Name) 50 } 51 52 // PodNotAvailable is returned when a Kubernetes service is not yet available to accept traffic. 53 type PodNotAvailable struct { 54 pod *corev1.Pod 55 } 56 57 // Error is a simple function to return a formatted error message as a string 58 func (err PodNotAvailable) Error() string { 59 return fmt.Sprintf("Pod %s is not available", err.pod.Name) 60 } 61 62 // NewPodNotAvailableError returnes a PodNotAvailable struct when Kubernetes deems a pod is not available 63 func NewPodNotAvailableError(pod *corev1.Pod) PodNotAvailable { 64 return PodNotAvailable{pod} 65 } 66 67 // ServiceNotAvailable is returned when a Kubernetes service is not yet available to accept traffic. 68 type ServiceNotAvailable struct { 69 service *corev1.Service 70 } 71 72 // Error is a simple function to return a formatted error message as a string 73 func (err ServiceNotAvailable) Error() string { 74 return fmt.Sprintf("Service %s is not available", err.service.Name) 75 } 76 77 // NewServiceNotAvailableError returnes a ServiceNotAvailable struct when Kubernetes deems a service is not available 78 func NewServiceNotAvailableError(service *corev1.Service) ServiceNotAvailable { 79 return ServiceNotAvailable{service} 80 } 81 82 // UnknownServiceType is returned when a Kubernetes service has a type that is not yet handled by the test functions. 83 type UnknownServiceType struct { 84 service *corev1.Service 85 } 86 87 // Error is a simple function to return a formatted error message as a string 88 func (err UnknownServiceType) Error() string { 89 return fmt.Sprintf("Service %s has an unknown service type", err.service.Name) 90 } 91 92 // NewUnknownServiceTypeError returns an UnknownServiceType struct when is it deemed that Kubernetes does not know the service type provided 93 func NewUnknownServiceTypeError(service *corev1.Service) UnknownServiceType { 94 return UnknownServiceType{service} 95 } 96 97 // UnknownServicePort is returned when the given service port is not an exported port of the service. 98 type UnknownServicePort struct { 99 service *corev1.Service 100 port int32 101 } 102 103 // Error is a simple function to return a formatted error message as a string 104 func (err UnknownServicePort) Error() string { 105 return fmt.Sprintf("Port %d is not a part of the service %s", err.port, err.service.Name) 106 } 107 108 // NewUnknownServicePortError returns an UnknownServicePort struct when it is deemed that Kuberenetes does not know of the provided Service Port 109 func NewUnknownServicePortError(service *corev1.Service, port int32) UnknownServicePort { 110 return UnknownServicePort{service, port} 111 } 112 113 // NoNodesInKubernetes is returned when the Kubernetes cluster has no nodes registered. 114 type NoNodesInKubernetes struct{} 115 116 // Error is a simple function to return a formatted error message as a string 117 func (err NoNodesInKubernetes) Error() string { 118 return "There are no nodes in the Kubernetes cluster" 119 } 120 121 // NewNoNodesInKubernetesError returns a NoNodesInKubernetes struct when it is deemed that there are no Kubernetes nodes registered 122 func NewNoNodesInKubernetesError() NoNodesInKubernetes { 123 return NoNodesInKubernetes{} 124 } 125 126 // NodeHasNoHostname is returned when a Kubernetes node has no discernible hostname 127 type NodeHasNoHostname struct { 128 node *corev1.Node 129 } 130 131 // Error is a simple function to return a formatted error message as a string 132 func (err NodeHasNoHostname) Error() string { 133 return fmt.Sprintf("Node %s has no hostname", err.node.Name) 134 } 135 136 // NewNodeHasNoHostnameError returns a NodeHasNoHostname struct when it is deemed that the provided node has no hostname 137 func NewNodeHasNoHostnameError(node *corev1.Node) NodeHasNoHostname { 138 return NodeHasNoHostname{node} 139 } 140 141 // MalformedNodeID is returned when a Kubernetes node has a malformed node id scheme 142 type MalformedNodeID struct { 143 node *corev1.Node 144 } 145 146 // Error is a simple function to return a formatted error message as a string 147 func (err MalformedNodeID) Error() string { 148 return fmt.Sprintf("Node %s has malformed ID %s", err.node.Name, err.node.Spec.ProviderID) 149 } 150 151 // NewMalformedNodeIDError returns a MalformedNodeID struct when Kubernetes deems that a NodeID is malformed 152 func NewMalformedNodeIDError(node *corev1.Node) MalformedNodeID { 153 return MalformedNodeID{node} 154 }