Created
November 11, 2019 11:31
-
-
Save jasoet/0a8528f8c1ffe770282c037489426e29 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package status | |
import ( | |
"experiment/pkg/k8s" | |
"experiment/pkg/util" | |
"fmt" | |
v1 "k8s.io/api/core/v1" | |
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1" | |
"k8s.io/client-go/kubernetes" | |
"time" | |
) | |
type DeploymentCondition struct { | |
Type string | |
Status string | |
LastUpdateTime time.Time | |
LastTransitionTime time.Time | |
Reason string | |
Message string | |
} | |
type DeploymentDetail struct { | |
Name string | |
Namespace string | |
Pods []v1.Pod | |
Replicas int32 | |
UpdatedReplicas int32 | |
ReadyReplicas int32 | |
AvailableReplicas int32 | |
UnavailableReplicas int32 | |
Conditions []DeploymentCondition | |
} | |
type DaemonSetCondition struct { | |
Type string | |
Status string | |
LastUpdateTime time.Time | |
LastTransitionTime time.Time | |
Reason string | |
Message string | |
} | |
type DaemonSetDetail struct { | |
Name string | |
Namespace string | |
Pods []v1.Pod | |
Replicas int32 | |
UpdatedReplicas int32 | |
ReadyReplicas int32 | |
AvailableReplicas int32 | |
UnavailableReplicas int32 | |
Conditions []DaemonSetCondition | |
} | |
type AppsInterface interface { | |
DeploymentDetail() (detail *DeploymentDetail, err error) | |
DaemonSetDetail() (detail *DaemonSetDetail, err error) | |
getPods(namespace string, labels map[string]string) (pods []v1.Pod, err error) | |
} | |
type apps struct { | |
resource k8s.SupportedResources | |
clientSet kubernetes.Clientset | |
} | |
func (core *apps) getPods(namespace string, labels map[string]string) (pods []v1.Pod, err error) { | |
podList, err := core.clientSet.CoreV1().Pods(namespace).List(metaV1.ListOptions{ | |
LabelSelector: util.LabelToSelector(labels), | |
}) | |
if err != nil { | |
return | |
} | |
pods = podList.Items | |
return | |
} | |
func (core *apps) DeploymentDetail() (detail *DeploymentDetail, err error) { | |
resource := core.resource | |
clientSet := core.clientSet | |
objectMeta := resource.ObjectMeta | |
if resource.Deployment != nil { | |
deployment, err := clientSet.AppsV1().Deployments(objectMeta.Namespace).Get(objectMeta.Name, metaV1.GetOptions{}) | |
if err != nil { | |
return | |
} | |
detail = &DeploymentDetail{ | |
Name: objectMeta.Name, | |
Namespace: objectMeta.Namespace, | |
Replicas: deployment.Status.Replicas, | |
UpdatedReplicas: deployment.Status.UpdatedReplicas, | |
ReadyReplicas: deployment.Status.ReadyReplicas, | |
AvailableReplicas: deployment.Status.AvailableReplicas, | |
UnavailableReplicas: deployment.Status.UnavailableReplicas, | |
} | |
var conditions []DeploymentCondition | |
for _, condition := range deployment.Status.Conditions { | |
conditions = append(conditions, DeploymentCondition{ | |
Type: string(condition.Type), | |
Status: string(condition.Status), | |
LastUpdateTime: condition.LastUpdateTime.Time, | |
LastTransitionTime: condition.LastTransitionTime.Time, | |
Reason: condition.Reason, | |
Message: condition.Message, | |
}) | |
} | |
detail.Conditions = conditions | |
pods, err := core.getPods(deployment.Namespace, deployment.Spec.Template.Labels) | |
if err != nil { | |
return | |
} | |
detail.Pods = pods | |
} else if resource.DeploymentExtensionV1Beta1 != nil { | |
deployment, err := clientSet.ExtensionsV1beta1().Deployments(objectMeta.Namespace).Get(objectMeta.Name, metaV1.GetOptions{}) | |
if err != nil { | |
return | |
} | |
detail = &DeploymentDetail{ | |
Name: objectMeta.Name, | |
Namespace: objectMeta.Namespace, | |
Replicas: deployment.Status.Replicas, | |
UpdatedReplicas: deployment.Status.UpdatedReplicas, | |
ReadyReplicas: deployment.Status.ReadyReplicas, | |
AvailableReplicas: deployment.Status.AvailableReplicas, | |
UnavailableReplicas: deployment.Status.UnavailableReplicas, | |
} | |
var conditions []DeploymentCondition | |
for _, condition := range deployment.Status.Conditions { | |
conditions = append(conditions, DeploymentCondition{ | |
Type: string(condition.Type), | |
Status: string(condition.Status), | |
LastUpdateTime: condition.LastUpdateTime.Time, | |
LastTransitionTime: condition.LastTransitionTime.Time, | |
Reason: condition.Reason, | |
Message: condition.Message, | |
}) | |
} | |
detail.Conditions = conditions | |
pods, err := core.getPods(deployment.Namespace, deployment.Spec.Template.Labels) | |
if err != nil { | |
return | |
} | |
detail.Pods = pods | |
} else if resource.DeploymentV1Beta1 != nil { | |
deployment, err := clientSet.AppsV1beta1().Deployments(objectMeta.Namespace).Get(objectMeta.Name, metaV1.GetOptions{}) | |
if err != nil { | |
return | |
} | |
detail = &DeploymentDetail{ | |
Name: objectMeta.Name, | |
Namespace: objectMeta.Namespace, | |
Replicas: deployment.Status.Replicas, | |
UpdatedReplicas: deployment.Status.UpdatedReplicas, | |
ReadyReplicas: deployment.Status.ReadyReplicas, | |
AvailableReplicas: deployment.Status.AvailableReplicas, | |
UnavailableReplicas: deployment.Status.UnavailableReplicas, | |
} | |
var conditions []DeploymentCondition | |
for _, condition := range deployment.Status.Conditions { | |
conditions = append(conditions, DeploymentCondition{ | |
Type: string(condition.Type), | |
Status: string(condition.Status), | |
LastUpdateTime: condition.LastUpdateTime.Time, | |
LastTransitionTime: condition.LastTransitionTime.Time, | |
Reason: condition.Reason, | |
Message: condition.Message, | |
}) | |
} | |
detail.Conditions = conditions | |
pods, err := core.getPods(deployment.Namespace, deployment.Spec.Template.Labels) | |
if err != nil { | |
return | |
} | |
detail.Pods = pods | |
} else if resource.DeploymentV1Beta2 != nil { | |
deployment, err := clientSet.AppsV1beta2().Deployments(objectMeta.Namespace).Get(objectMeta.Name, metaV1.GetOptions{}) | |
if err != nil { | |
return | |
} | |
detail = &DeploymentDetail{ | |
Name: objectMeta.Name, | |
Namespace: objectMeta.Namespace, | |
Replicas: deployment.Status.Replicas, | |
UpdatedReplicas: deployment.Status.UpdatedReplicas, | |
ReadyReplicas: deployment.Status.ReadyReplicas, | |
AvailableReplicas: deployment.Status.AvailableReplicas, | |
UnavailableReplicas: deployment.Status.UnavailableReplicas, | |
} | |
var conditions []DeploymentCondition | |
for _, condition := range deployment.Status.Conditions { | |
conditions = append(conditions, DeploymentCondition{ | |
Type: string(condition.Type), | |
Status: string(condition.Status), | |
LastUpdateTime: condition.LastUpdateTime.Time, | |
LastTransitionTime: condition.LastTransitionTime.Time, | |
Reason: condition.Reason, | |
Message: condition.Message, | |
}) | |
} | |
detail.Conditions = conditions | |
pods, err := core.getPods(deployment.Namespace, deployment.Spec.Template.Labels) | |
if err != nil { | |
return | |
} | |
detail.Pods = pods | |
} else { | |
err = fmt.Errorf("resource is not deployment") | |
} | |
return | |
} | |
func NewApps(resource k8s.SupportedResources, clientSet kubernetes.Clientset) AppsInterface { | |
return &apps{ | |
resource: resource, | |
clientSet: clientSet, | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment