【修改】k8s 配置

This commit is contained in:
PandaGoAdmin
2022-01-22 17:07:04 +08:00
parent c6ebe89865
commit 33cc74711d
439 changed files with 9936 additions and 21687 deletions

View File

@@ -0,0 +1,19 @@
package common
import v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
// Condition represents a single condition of a pod or node.
type Condition struct {
// Type of a condition.
Type string `json:"type"`
// Status of a condition.
Status v1.ConditionStatus `json:"status"`
// Last probe time of a condition.
LastProbeTime v1.Time `json:"lastProbeTime"`
// Last transition time of a condition.
LastTransitionTime v1.Time `json:"lastTransitionTime"`
// Reason of a condition.
Reason string `json:"reason"`
// Message of a condition.
Message string `json:"message"`
}

View File

@@ -0,0 +1,66 @@
package common
import (
"bytes"
api "k8s.io/api/core/v1"
)
// Endpoint describes an endpoint that is host and a list of available ports for that host.
type Endpoint struct {
// Hostname, either as a domain name or IP address.
Host string `json:"host"`
// List of ports opened for this endpoint on the hostname.
Ports []ServicePort `json:"ports"`
}
// GetExternalEndpoints returns endpoints that are externally reachable for a service.
func GetExternalEndpoints(service *api.Service) []Endpoint {
externalEndpoints := make([]Endpoint, 0)
if service.Spec.Type == api.ServiceTypeLoadBalancer {
for _, ingress := range service.Status.LoadBalancer.Ingress {
externalEndpoints = append(externalEndpoints, getExternalEndpoint(ingress, service.Spec.Ports))
}
}
for _, ip := range service.Spec.ExternalIPs {
externalEndpoints = append(externalEndpoints, Endpoint{
Host: ip,
Ports: GetServicePorts(service.Spec.Ports),
})
}
return externalEndpoints
}
// GetInternalEndpoint returns internal endpoint name for the given service properties, e.g.,
// "my-service.namespace 80/TCP" or "my-service 53/TCP,53/UDP".
func GetInternalEndpoint(serviceName, namespace string, ports []api.ServicePort) Endpoint {
name := serviceName
if namespace != api.NamespaceDefault && len(namespace) > 0 && len(serviceName) > 0 {
bufferName := bytes.NewBufferString(name)
bufferName.WriteString(".")
bufferName.WriteString(namespace)
name = bufferName.String()
}
return Endpoint{
Host: name,
Ports: GetServicePorts(ports),
}
}
// Returns external endpoint name for the given service properties.
func getExternalEndpoint(ingress api.LoadBalancerIngress, ports []api.ServicePort) Endpoint {
var host string
if ingress.Hostname != "" {
host = ingress.Hostname
} else {
host = ingress.IP
}
return Endpoint{
Host: host,
Ports: GetServicePorts(ports),
}
}

View File

@@ -0,0 +1,63 @@
package common
import (
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"pandax/apps/devops/entity/k8s"
)
// EventList is an events response structure.
type EventList struct {
ListMeta k8s.ListMeta `json:"listMeta"`
// List of events from given namespace.
Events []Event `json:"events"`
}
// Event is a single event representation.
type Event struct {
ObjectMeta k8s.ObjectMeta `json:"objectMeta"`
TypeMeta k8s.TypeMeta `json:"typeMeta"`
// A human-readable description of the status of related object.
Message string `json:"message"`
// Component from which the event is generated.
SourceComponent string `json:"sourceComponent"`
// Host name on which the event is generated.
SourceHost string `json:"sourceHost"`
// Reference to a piece of an object, which triggered an event. For example
// "spec.containers{name}" refers to container within pod with given name, if no container
// name is specified, for example "spec.containers[2]", then it refers to container with
// index 2 in this pod.
SubObject string `json:"object"`
// Kind of the referent.
// +optional
SubObjectKind string `json:"objectKind,omitempty"`
// Name of the referent.
// +optional
SubObjectName string `json:"objectName,omitempty"`
// Namespace of the referent.
// +optional
SubObjectNamespace string `json:"objectNamespace,omitempty"`
// The number of times this event has occurred.
Count int32 `json:"count"`
// The time at which the event was first recorded.
FirstSeen v1.Time `json:"firstSeen"`
// The time at which the most recent occurrence of this event was recorded.
LastSeen v1.Time `json:"lastSeen"`
// Short, machine understandable string that gives the reason
// for this event being generated.
Reason string `json:"reason"`
// Event type (at the moment only normal and warning are supported).
Type string `json:"type"`
}

View File

@@ -0,0 +1,47 @@
package common
import api "k8s.io/api/core/v1"
// NamespaceQuery is a query for namespaces of a list of objects.
// There's three cases:
// 1. No namespace selected: this means "user namespaces" query, i.e., all except kube-system
// 2. Single namespace selected: this allows for optimizations when querying backends
// 3. More than one namespace selected: resources from all namespaces are queried and then
// filtered here.
type NamespaceQuery struct {
Namespaces []string
}
// ToRequestParam returns K8s API namespace query for list of objects from this namespaces.
// This is an optimization to query for single namespace if one was selected and for all
// namespaces otherwise.
func (n *NamespaceQuery) ToRequestParam() string {
if len(n.Namespaces) == 1 {
return n.Namespaces[0]
}
return api.NamespaceAll
}
// Matches returns true when the given namespace matches this query.
func (n *NamespaceQuery) Matches(namespace string) bool {
if len(n.Namespaces) == 0 {
return true
}
for _, queryNamespace := range n.Namespaces {
if namespace == queryNamespace {
return true
}
}
return false
}
// NewSameNamespaceQuery creates new namespace query that queries single namespace.
func NewSameNamespaceQuery(namespace string) *NamespaceQuery {
return &NamespaceQuery{[]string{namespace}}
}
// NewNamespaceQuery creates new query for given namespaces.
func NewNamespaceQuery(namespaces []string) *NamespaceQuery {
return &NamespaceQuery{namespaces}
}

View File

@@ -0,0 +1,151 @@
package common
import (
apps "k8s.io/api/apps/v1"
batch "k8s.io/api/batch/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// FilterDeploymentPodsByOwnerReference returns a subset of pods controlled by given deployment.
func FilterDeploymentPodsByOwnerReference(deployment apps.Deployment, allRS []apps.ReplicaSet,
allPods []v1.Pod) []v1.Pod {
var matchingPods []v1.Pod
for _, rs := range allRS {
if metav1.IsControlledBy(&rs, &deployment) {
matchingPods = append(matchingPods, FilterPodsByControllerRef(&rs, allPods)...)
}
}
return matchingPods
}
// FilterPodsByControllerRef returns a subset of pods controlled by given controller resource, excluding deployments.
func FilterPodsByControllerRef(owner metav1.Object, allPods []v1.Pod) []v1.Pod {
var matchingPods []v1.Pod
for _, pod := range allPods {
if metav1.IsControlledBy(&pod, owner) {
matchingPods = append(matchingPods, pod)
}
}
return matchingPods
}
// GetContainerImages returns container image strings from the given pod spec.
func GetContainerImages(podTemplate *v1.PodSpec) []string {
var containerImages []string
for _, container := range podTemplate.Containers {
containerImages = append(containerImages, container.Image)
}
return containerImages
}
// GetInitContainerImages returns init container image strings from the given pod spec.
func GetInitContainerImages(podTemplate *v1.PodSpec) []string {
var initContainerImages []string
for _, initContainer := range podTemplate.InitContainers {
initContainerImages = append(initContainerImages, initContainer.Image)
}
return initContainerImages
}
// FilterPodsForJob returns a list of pods that matches to a job controller's selector
func FilterPodsForJob(job batch.Job, pods []v1.Pod) []v1.Pod {
result := make([]v1.Pod, 0)
for _, pod := range pods {
if pod.Namespace == job.Namespace {
selectorMatch := true
for key, value := range job.Spec.Selector.MatchLabels {
if pod.Labels[key] != value {
selectorMatch = false
break
}
}
if selectorMatch {
result = append(result, pod)
}
}
}
return result
}
// GetContainerNames returns the container image name without the version number from the given pod spec.
func GetContainerNames(podTemplate *v1.PodSpec) []string {
var containerNames []string
for _, container := range podTemplate.Containers {
containerNames = append(containerNames, container.Name)
}
return containerNames
}
// GetInitContainerNames returns the init container image name without the version number from the given pod spec.
func GetInitContainerNames(podTemplate *v1.PodSpec) []string {
var initContainerNames []string
for _, initContainer := range podTemplate.InitContainers {
initContainerNames = append(initContainerNames, initContainer.Name)
}
return initContainerNames
}
// GetNonduplicateContainerImages returns list of container image strings without duplicates
func GetNonduplicateContainerImages(podList []v1.Pod) []string {
var containerImages []string
for _, pod := range podList {
for _, container := range pod.Spec.Containers {
if noStringInSlice(container.Image, containerImages) {
containerImages = append(containerImages, container.Image)
}
}
}
return containerImages
}
// GetNonduplicateInitContainerImages returns list of init container image strings without duplicates
func GetNonduplicateInitContainerImages(podList []v1.Pod) []string {
var initContainerImages []string
for _, pod := range podList {
for _, initContainer := range pod.Spec.InitContainers {
if noStringInSlice(initContainer.Image, initContainerImages) {
initContainerImages = append(initContainerImages, initContainer.Image)
}
}
}
return initContainerImages
}
// GetNonduplicateContainerNames returns list of container names strings without duplicates
func GetNonduplicateContainerNames(podList []v1.Pod) []string {
var containerNames []string
for _, pod := range podList {
for _, container := range pod.Spec.Containers {
if noStringInSlice(container.Name, containerNames) {
containerNames = append(containerNames, container.Name)
}
}
}
return containerNames
}
// GetNonduplicateInitContainerNames returns list of init container names strings without duplicates
func GetNonduplicateInitContainerNames(podList []v1.Pod) []string {
var initContainerNames []string
for _, pod := range podList {
for _, initContainer := range pod.Spec.InitContainers {
if noStringInSlice(initContainer.Name, initContainerNames) {
initContainerNames = append(initContainerNames, initContainer.Name)
}
}
}
return initContainerNames
}
//noStringInSlice checks if string in array
func noStringInSlice(str string, array []string) bool {
for _, alreadystr := range array {
if alreadystr == str {
return false
}
}
return true
}

View File

@@ -0,0 +1,53 @@
package common
import (
api "k8s.io/api/core/v1"
)
// PodInfo represents aggregate information about controller's pods.
type PodInfo struct {
// Number of pods that are created.
Current int32 `json:"current"`
// Number of pods that are desired.
Desired *int32 `json:"desired,omitempty"`
// Number of pods that are currently running.
Running int32 `json:"running"`
// Number of pods that are currently waiting.
Pending int32 `json:"pending"`
// Number of pods that are failed.
Failed int32 `json:"failed"`
// Number of pods that are succeeded.
Succeeded int32 `json:"succeeded"`
// Unique warning messages related to pods in this resource.
Warnings []Event `json:"warnings"`
}
// GetPodInfo returns aggregate information about a group of pods.
func GetPodInfo(current int32, desired *int32, pods []api.Pod) PodInfo {
result := PodInfo{
Current: current,
Desired: desired,
Warnings: make([]Event, 0),
}
for _, pod := range pods {
switch pod.Status.Phase {
case api.PodRunning:
result.Running++
case api.PodPending:
result.Pending++
case api.PodFailed:
result.Failed++
case api.PodSucceeded:
result.Succeeded++
}
}
return result
}

View File

@@ -0,0 +1,589 @@
package common
import (
"context"
batch "k8s.io/api/batch/v1"
batch2 "k8s.io/api/batch/v1beta1"
apps "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
extensions "k8s.io/api/extensions/v1beta1"
storage "k8s.io/api/storage/v1"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
client "k8s.io/client-go/kubernetes"
"pandax/apps/devops/entity/k8s"
)
// ResourceChannels struct holds channels to resource lists. Each list channel is paired with
// an error channel which *must* be read sequentially: first read the list channel and then
// the error channel.
//
// This struct can be used when there are multiple clients that want to process, e.g., a
// list of pods. With this helper, the list can be read only once from the backend and
// distributed asynchronously to clients that need it.
//
// When a channel is nil, it means that no resource list is available for getting.
//
// Each channel pair can be read up to N times. N is specified upon creation of the channels.
type ResourceChannels struct {
// List and error channels to Replica Sets.
ReplicaSetList ReplicaSetListChannel
// List and error channels to Deployments.
DeploymentList DeploymentListChannel
// List and error channels to Pods.
PodList PodListChannel
// List and error channels to Events.
EventList EventListChannel
// List and error channels to ConfigMaps.
ConfigMapList ConfigMapListChannel
// List and error channels to Secrets.
SecretList SecretListChannel
// List and error channels to PersistentVolumes
PersistentVolumeList PersistentVolumeListChannel
// List and error channels to PersistentVolumeClaims
PersistentVolumeClaimList PersistentVolumeClaimListChannel
// List and error channels to StatefulSets.
StatefulSetList StatefulSetListChannel
// List and error channels to Daemon Sets.
DaemonSetList DaemonSetListChannel
// List and error channels to Services.
ServiceList ServiceListChannel
// List and error channels to Jobs.
JobList JobListChannel
// List and error channels to Cron Jobs.
CronJobList CronJobListChannel
// List and error channels to StorageClasses
StorageClassList StorageClassListChannel
// List and error channels to Endpoints.
EndpointList EndpointListChannel
// List and error channels to Ingresses.
//IngressList IngressListChannel
}
// EventListChannel is a list and error channels to Events.
type EventListChannel struct {
List chan *v1.EventList
Error chan error
}
// GetEventListChannel returns a pair of channels to an Event list and errors that both must be read
// numReads times.
func GetEventListChannel(client *client.Clientset, nsQuery *NamespaceQuery, numReads int) EventListChannel {
return GetEventListChannelWithOptions(client, nsQuery, k8s.ListEverything, numReads)
}
// GetEventListChannelWithOptions is GetEventListChannel plus list options.
func GetEventListChannelWithOptions(client *client.Clientset, nsQuery *NamespaceQuery, options metaV1.ListOptions, numReads int) EventListChannel {
channel := EventListChannel{
List: make(chan *v1.EventList, numReads),
Error: make(chan error, numReads),
}
go func() {
// 原options是根据label过滤来过滤Event事件信息, 代码deployment_detail + L78
// TODO 改成field过滤
//options.FieldSelector = fmt.Sprintf("involvedObject.name=%v", deploymentName)
list, err := client.CoreV1().Events(nsQuery.ToRequestParam()).List(context.TODO(), options)
var filteredItems []v1.Event
for _, item := range list.Items {
if nsQuery.Matches(item.ObjectMeta.Namespace) {
filteredItems = append(filteredItems, item)
}
}
list.Items = filteredItems
for i := 0; i < numReads; i++ {
channel.List <- list
channel.Error <- err
}
}()
return channel
}
// PodListChannel is a list and error channels to Pods.
type PodListChannel struct {
List chan *v1.PodList
Error chan error
}
// GetPodListChannel returns a pair of channels to a Pod list and errors that both must be read
// numReads times.
func GetPodListChannel(client client.Interface, nsQuery *NamespaceQuery, numReads int) PodListChannel {
return GetPodListChannelWithOptions(client, nsQuery, k8s.ListEverything, numReads)
}
// GetPodListChannelWithOptions is GetPodListChannel plus listing options.
func GetPodListChannelWithOptions(client client.Interface, nsQuery *NamespaceQuery, options metaV1.ListOptions, numReads int) PodListChannel {
channel := PodListChannel{
List: make(chan *v1.PodList, numReads),
Error: make(chan error, numReads),
}
go func() {
list, err := client.CoreV1().Pods(nsQuery.ToRequestParam()).List(context.TODO(), options)
var filteredItems []v1.Pod
for _, item := range list.Items {
if nsQuery.Matches(item.ObjectMeta.Namespace) {
filteredItems = append(filteredItems, item)
}
}
list.Items = filteredItems
for i := 0; i < numReads; i++ {
channel.List <- list
channel.Error <- err
}
}()
return channel
}
// DeploymentListChannel is a list and error channels to Deployments.
type DeploymentListChannel struct {
List chan *apps.DeploymentList
Error chan error
}
// GetDeploymentListChannel returns a pair of channels to a Deployment list and errors
// that both must be read numReads times.
func GetDeploymentListChannel(client client.Interface, nsQuery *NamespaceQuery, numReads int) DeploymentListChannel {
channel := DeploymentListChannel{
List: make(chan *apps.DeploymentList, numReads),
Error: make(chan error, numReads),
}
go func() {
list, err := client.AppsV1().Deployments(nsQuery.ToRequestParam()).List(context.TODO(), k8s.ListEverything)
var filteredItems []apps.Deployment
for _, item := range list.Items {
if nsQuery.Matches(item.ObjectMeta.Namespace) {
filteredItems = append(filteredItems, item)
}
}
list.Items = filteredItems
for i := 0; i < numReads; i++ {
channel.List <- list
channel.Error <- err
}
}()
return channel
}
// ReplicaSetListChannel is a list and error channels to Replica Sets.
type ReplicaSetListChannel struct {
List chan *apps.ReplicaSetList
Error chan error
}
// GetReplicaSetListChannel returns a pair of channels to a ReplicaSet list and
// errors that both must be read numReads times.
func GetReplicaSetListChannel(client client.Interface, nsQuery *NamespaceQuery, numReads int) ReplicaSetListChannel {
return GetReplicaSetListChannelWithOptions(client, nsQuery, k8s.ListEverything, numReads)
}
// GetReplicaSetListChannelWithOptions returns a pair of channels to a ReplicaSet list filtered
// by provided options and errors that both must be read numReads times.
func GetReplicaSetListChannelWithOptions(client client.Interface, nsQuery *NamespaceQuery, options metaV1.ListOptions, numReads int) ReplicaSetListChannel {
channel := ReplicaSetListChannel{
List: make(chan *apps.ReplicaSetList, numReads),
Error: make(chan error, numReads),
}
go func() {
list, err := client.AppsV1().ReplicaSets(nsQuery.ToRequestParam()).
List(context.TODO(), options)
var filteredItems []apps.ReplicaSet
for _, item := range list.Items {
if nsQuery.Matches(item.ObjectMeta.Namespace) {
filteredItems = append(filteredItems, item)
}
}
list.Items = filteredItems
for i := 0; i < numReads; i++ {
channel.List <- list
channel.Error <- err
}
}()
return channel
}
// ConfigMapListChannel is a list and error channels to ConfigMaps.
type ConfigMapListChannel struct {
List chan *v1.ConfigMapList
Error chan error
}
// GetConfigMapListChannel returns a pair of channels to a ConfigMap list and errors that both must be read
// numReads times.
func GetConfigMapListChannel(client client.Interface, nsQuery *NamespaceQuery, numReads int) ConfigMapListChannel {
channel := ConfigMapListChannel{
List: make(chan *v1.ConfigMapList, numReads),
Error: make(chan error, numReads),
}
go func() {
list, err := client.CoreV1().ConfigMaps(nsQuery.ToRequestParam()).List(context.TODO(), k8s.ListEverything)
var filteredItems []v1.ConfigMap
for _, item := range list.Items {
if nsQuery.Matches(item.ObjectMeta.Namespace) {
filteredItems = append(filteredItems, item)
}
}
list.Items = filteredItems
for i := 0; i < numReads; i++ {
channel.List <- list
channel.Error <- err
}
}()
return channel
}
// SecretListChannel is a list and error channels to Secrets.
type SecretListChannel struct {
List chan *v1.SecretList
Error chan error
}
// GetSecretListChannel returns a pair of channels to a Secret list and errors that
// both must be read numReads times.
func GetSecretListChannel(client client.Interface, nsQuery *NamespaceQuery, numReads int) SecretListChannel {
channel := SecretListChannel{
List: make(chan *v1.SecretList, numReads),
Error: make(chan error, numReads),
}
go func() {
list, err := client.CoreV1().Secrets(nsQuery.ToRequestParam()).List(context.TODO(), k8s.ListEverything)
var filteredItems []v1.Secret
for _, item := range list.Items {
if nsQuery.Matches(item.ObjectMeta.Namespace) {
filteredItems = append(filteredItems, item)
}
}
list.Items = filteredItems
for i := 0; i < numReads; i++ {
channel.List <- list
channel.Error <- err
}
}()
return channel
}
// PersistentVolumeListChannel is a list and error channels to PersistentVolumes.
type PersistentVolumeListChannel struct {
List chan *v1.PersistentVolumeList
Error chan error
}
// GetPersistentVolumeListChannel returns a pair of channels to a PersistentVolume list and errors
// that both must be read numReads times.
func GetPersistentVolumeListChannel(client client.Interface, numReads int) PersistentVolumeListChannel {
channel := PersistentVolumeListChannel{
List: make(chan *v1.PersistentVolumeList, numReads),
Error: make(chan error, numReads),
}
go func() {
list, err := client.CoreV1().PersistentVolumes().List(context.TODO(), k8s.ListEverything)
for i := 0; i < numReads; i++ {
channel.List <- list
channel.Error <- err
}
}()
return channel
}
// PersistentVolumeClaimListChannel is a list and error channels to PersistentVolumeClaims.
type PersistentVolumeClaimListChannel struct {
List chan *v1.PersistentVolumeClaimList
Error chan error
}
// GetPersistentVolumeClaimListChannel returns a pair of channels to a PersistentVolumeClaim list
// and errors that both must be read numReads times.
func GetPersistentVolumeClaimListChannel(client client.Interface, nsQuery *NamespaceQuery, numReads int) PersistentVolumeClaimListChannel {
channel := PersistentVolumeClaimListChannel{
List: make(chan *v1.PersistentVolumeClaimList, numReads),
Error: make(chan error, numReads),
}
go func() {
list, err := client.CoreV1().PersistentVolumeClaims(nsQuery.ToRequestParam()).List(context.TODO(), k8s.ListEverything)
for i := 0; i < numReads; i++ {
channel.List <- list
channel.Error <- err
}
}()
return channel
}
// StatefulSetListChannel is a list and error channels to StatefulSets.
type StatefulSetListChannel struct {
List chan *apps.StatefulSetList
Error chan error
}
// GetStatefulSetListChannel returns a pair of channels to a StatefulSet list and errors that both must be read
// numReads times.
func GetStatefulSetListChannel(client client.Interface, nsQuery *NamespaceQuery, numReads int) StatefulSetListChannel {
channel := StatefulSetListChannel{
List: make(chan *apps.StatefulSetList, numReads),
Error: make(chan error, numReads),
}
go func() {
statefulSets, err := client.AppsV1().StatefulSets(nsQuery.ToRequestParam()).List(context.TODO(), k8s.ListEverything)
var filteredItems []apps.StatefulSet
for _, item := range statefulSets.Items {
if nsQuery.Matches(item.ObjectMeta.Namespace) {
filteredItems = append(filteredItems, item)
}
}
statefulSets.Items = filteredItems
for i := 0; i < numReads; i++ {
channel.List <- statefulSets
channel.Error <- err
}
}()
return channel
}
// DaemonSetListChannel is a list and error channels to Daemon Sets.
type DaemonSetListChannel struct {
List chan *apps.DaemonSetList
Error chan error
}
// GetDaemonSetListChannel returns a pair of channels to a DaemonSet list and errors that both must be read
// numReads times.
func GetDaemonSetListChannel(client client.Interface, nsQuery *NamespaceQuery, numReads int) DaemonSetListChannel {
channel := DaemonSetListChannel{
List: make(chan *apps.DaemonSetList, numReads),
Error: make(chan error, numReads),
}
go func() {
list, err := client.AppsV1().DaemonSets(nsQuery.ToRequestParam()).List(context.TODO(), k8s.ListEverything)
var filteredItems []apps.DaemonSet
for _, item := range list.Items {
if nsQuery.Matches(item.ObjectMeta.Namespace) {
filteredItems = append(filteredItems, item)
}
}
list.Items = filteredItems
for i := 0; i < numReads; i++ {
channel.List <- list
channel.Error <- err
}
}()
return channel
}
// ServiceListChannel is a list and error channels to Services.
type ServiceListChannel struct {
List chan *v1.ServiceList
Error chan error
}
// GetServiceListChannel returns a pair of channels to a Service list and errors that both
// must be read numReads times.
func GetServiceListChannel(client client.Interface, nsQuery *NamespaceQuery, numReads int) ServiceListChannel {
channel := ServiceListChannel{
List: make(chan *v1.ServiceList, numReads),
Error: make(chan error, numReads),
}
go func() {
list, err := client.CoreV1().Services(nsQuery.ToRequestParam()).List(context.TODO(), k8s.ListEverything)
var filteredItems []v1.Service
for _, item := range list.Items {
if nsQuery.Matches(item.ObjectMeta.Namespace) {
filteredItems = append(filteredItems, item)
}
}
list.Items = filteredItems
for i := 0; i < numReads; i++ {
channel.List <- list
channel.Error <- err
}
}()
return channel
}
// JobListChannel is a list and error channels to Jobs.
type JobListChannel struct {
List chan *batch.JobList
Error chan error
}
// GetJobListChannel returns a pair of channels to a Job list and errors that both must be read numReads times.
func GetJobListChannel(client client.Interface, nsQuery *NamespaceQuery, numReads int) JobListChannel {
channel := JobListChannel{
List: make(chan *batch.JobList, numReads),
Error: make(chan error, numReads),
}
go func() {
list, err := client.BatchV1().Jobs(nsQuery.ToRequestParam()).List(context.TODO(), k8s.ListEverything)
var filteredItems []batch.Job
for _, item := range list.Items {
if nsQuery.Matches(item.ObjectMeta.Namespace) {
filteredItems = append(filteredItems, item)
}
}
list.Items = filteredItems
for i := 0; i < numReads; i++ {
channel.List <- list
channel.Error <- err
}
}()
return channel
}
// CronJobListChannel is a list and error channels to Cron Jobs.
type CronJobListChannel struct {
List chan *batch2.CronJobList
Error chan error
}
// GetCronJobListChannel returns a pair of channels to a Cron Job list and errors that both must be read numReads times.
func GetCronJobListChannel(client client.Interface, nsQuery *NamespaceQuery, numReads int) CronJobListChannel {
channel := CronJobListChannel{
List: make(chan *batch2.CronJobList, numReads),
Error: make(chan error, numReads),
}
go func() {
list, err := client.BatchV1beta1().CronJobs(nsQuery.ToRequestParam()).List(context.TODO(), k8s.ListEverything)
var filteredItems []batch2.CronJob
for _, item := range list.Items {
if nsQuery.Matches(item.ObjectMeta.Namespace) {
filteredItems = append(filteredItems, item)
}
}
list.Items = filteredItems
for i := 0; i < numReads; i++ {
channel.List <- list
channel.Error <- err
}
}()
return channel
}
// StorageClassListChannel is a list and error channels to storage classes.
type StorageClassListChannel struct {
List chan *storage.StorageClassList
Error chan error
}
// GetStorageClassListChannel returns a pair of channels to a storage class list and
// errors that both must be read numReads times.
func GetStorageClassListChannel(client client.Interface, numReads int) StorageClassListChannel {
channel := StorageClassListChannel{
List: make(chan *storage.StorageClassList, numReads),
Error: make(chan error, numReads),
}
go func() {
list, err := client.StorageV1().StorageClasses().List(context.TODO(), k8s.ListEverything)
for i := 0; i < numReads; i++ {
channel.List <- list
channel.Error <- err
}
}()
return channel
}
// EndpointListChannel is a list and error channels to Endpoints.
type EndpointListChannel struct {
List chan *v1.EndpointsList
Error chan error
}
func GetEndpointListChannel(client client.Interface, nsQuery *NamespaceQuery, numReads int) EndpointListChannel {
return GetEndpointListChannelWithOptions(client, nsQuery, k8s.ListEverything, numReads)
}
// GetEndpointListChannelWithOptions is GetEndpointListChannel plus list options.
func GetEndpointListChannelWithOptions(client client.Interface,
nsQuery *NamespaceQuery, opt metaV1.ListOptions, numReads int) EndpointListChannel {
channel := EndpointListChannel{
List: make(chan *v1.EndpointsList, numReads),
Error: make(chan error, numReads),
}
go func() {
list, err := client.CoreV1().Endpoints(nsQuery.ToRequestParam()).List(context.TODO(), opt)
for i := 0; i < numReads; i++ {
channel.List <- list
channel.Error <- err
}
}()
return channel
}
// IngressListChannel is a list and error channels to Ingresss.
type IngressListChannel struct {
List chan *extensions.IngressList
Error chan error
}
// GetIngressListChannel returns a pair of channels to an Ingress list and errors that both
// must be read numReads times.
func GetIngressListChannel(client client.Interface, nsQuery *NamespaceQuery, numReads int) IngressListChannel {
channel := IngressListChannel{
List: make(chan *extensions.IngressList, numReads),
Error: make(chan error, numReads),
}
go func() {
list, err := client.ExtensionsV1beta1().Ingresses(nsQuery.ToRequestParam()).List(context.TODO(), k8s.ListEverything)
var filteredItems []extensions.Ingress
for _, item := range list.Items {
if nsQuery.Matches(item.ObjectMeta.Namespace) {
filteredItems = append(filteredItems, item)
}
}
list.Items = filteredItems
for i := 0; i < numReads; i++ {
channel.List <- list
channel.Error <- err
}
}()
return channel
}

View File

@@ -0,0 +1,22 @@
package common
// ResourceStatus provides basic information about resources status on the list.
type ResourceStatus struct {
// Number of resources that are currently in running state.
Running int `json:"running"`
// Number of resources that are currently in pending state.
Pending int `json:"pending"`
// Number of resources that are in failed state.
Failed int `json:"failed"`
// Number of resources that are in succeeded state.
Succeeded int `json:"succeeded"`
// Number of resources that are in unknown state.
Unknown int `json:"unknown"`
// Number of resources that are in terminating state.
Terminating int `json:"terminating"`
}

View File

@@ -0,0 +1,24 @@
package common
import api "k8s.io/api/core/v1"
// ServicePort is a pair of port and protocol, e.g. a service endpoint.
type ServicePort struct {
// Positive port number.
Port int32 `json:"port"`
// Protocol name, e.g., TCP or UDP.
Protocol api.Protocol `json:"protocol"`
// The port on each node on which service is exposed.
NodePort int32 `json:"nodePort"`
}
// GetServicePorts returns human readable name for the given service ports list.
func GetServicePorts(apiPorts []api.ServicePort) []ServicePort {
var ports []ServicePort
for _, port := range apiPorts {
ports = append(ports, ServicePort{port.Port, port.Protocol, port.NodePort})
}
return ports
}