add default reflectstatus implemetation
Signed-off-by: changzhen <changzhen5@huawei.com>
This commit is contained in:
parent
0b985f4463
commit
0454944b63
|
@ -121,5 +121,5 @@ func (e *DefaultInterpreter) ReflectStatus(object *unstructured.Unstructured) (s
|
||||||
}
|
}
|
||||||
|
|
||||||
// for resource types that don't have a build-in handler, try to collect the whole status from '.status' filed.
|
// for resource types that don't have a build-in handler, try to collect the whole status from '.status' filed.
|
||||||
return getWholeStatus(object)
|
return reflectWholeStatus(object)
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,9 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
appsv1 "k8s.io/api/apps/v1"
|
appsv1 "k8s.io/api/apps/v1"
|
||||||
|
batchv1 "k8s.io/api/batch/v1"
|
||||||
|
corev1 "k8s.io/api/core/v1"
|
||||||
|
extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
|
||||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||||
|
@ -18,6 +21,11 @@ type reflectStatusInterpreter func(object *unstructured.Unstructured) (*runtime.
|
||||||
func getAllDefaultReflectStatusInterpreter() map[schema.GroupVersionKind]reflectStatusInterpreter {
|
func getAllDefaultReflectStatusInterpreter() map[schema.GroupVersionKind]reflectStatusInterpreter {
|
||||||
s := make(map[schema.GroupVersionKind]reflectStatusInterpreter)
|
s := make(map[schema.GroupVersionKind]reflectStatusInterpreter)
|
||||||
s[appsv1.SchemeGroupVersion.WithKind(util.DeploymentKind)] = reflectDeploymentStatus
|
s[appsv1.SchemeGroupVersion.WithKind(util.DeploymentKind)] = reflectDeploymentStatus
|
||||||
|
s[corev1.SchemeGroupVersion.WithKind(util.ServiceKind)] = reflectServiceStatus
|
||||||
|
s[extensionsv1beta1.SchemeGroupVersion.WithKind(util.IngressKind)] = reflectIngressStatus
|
||||||
|
s[batchv1.SchemeGroupVersion.WithKind(util.JobKind)] = reflectJobStatus
|
||||||
|
s[appsv1.SchemeGroupVersion.WithKind(util.DaemonSetKind)] = reflectDaemonSetStatus
|
||||||
|
s[appsv1.SchemeGroupVersion.WithKind(util.StatefulSetKind)] = reflectStatefulSetStatus
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,7 +57,135 @@ func reflectDeploymentStatus(object *unstructured.Unstructured) (*runtime.RawExt
|
||||||
return helper.BuildStatusRawExtension(grabStatus)
|
return helper.BuildStatusRawExtension(grabStatus)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getWholeStatus(object *unstructured.Unstructured) (*runtime.RawExtension, error) {
|
func reflectServiceStatus(object *unstructured.Unstructured) (*runtime.RawExtension, error) {
|
||||||
|
serviceType, exist, err := unstructured.NestedString(object.Object, "spec", "type")
|
||||||
|
if err != nil {
|
||||||
|
klog.Errorf("Failed to get spec.type field from %s(%s/%s)")
|
||||||
|
}
|
||||||
|
if !exist {
|
||||||
|
klog.Errorf("Failed to get spec.type from %s(%s/%s) which should have spec.type field.",
|
||||||
|
object.GetKind(), object.GetNamespace(), object.GetName())
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
if serviceType != string(corev1.ServiceTypeLoadBalancer) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
statusMap, exist, err := unstructured.NestedMap(object.Object, "status")
|
||||||
|
if err != nil {
|
||||||
|
klog.Errorf("Failed to get status field from %s(%s/%s), error: %v",
|
||||||
|
object.GetKind(), object.GetNamespace(), object.GetName(), err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if !exist {
|
||||||
|
klog.Errorf("Failed to grab status from %s(%s/%s) which should have status field.",
|
||||||
|
object.GetKind(), object.GetNamespace(), object.GetName())
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
serviceStatus, err := helper.ConvertToServiceStatus(statusMap)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to convert ServiceStatus from map[string]interface{}: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
grabStatus := corev1.ServiceStatus{
|
||||||
|
LoadBalancer: serviceStatus.LoadBalancer,
|
||||||
|
}
|
||||||
|
return helper.BuildStatusRawExtension(grabStatus)
|
||||||
|
}
|
||||||
|
|
||||||
|
func reflectIngressStatus(object *unstructured.Unstructured) (*runtime.RawExtension, error) {
|
||||||
|
return reflectWholeStatus(object)
|
||||||
|
}
|
||||||
|
|
||||||
|
func reflectJobStatus(object *unstructured.Unstructured) (*runtime.RawExtension, error) {
|
||||||
|
statusMap, exist, err := unstructured.NestedMap(object.Object, "status")
|
||||||
|
if err != nil {
|
||||||
|
klog.Errorf("Failed to get status field from %s(%s/%s), error: %v",
|
||||||
|
object.GetKind(), object.GetNamespace(), object.GetName(), err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if !exist {
|
||||||
|
klog.Errorf("Failed to grab status from %s(%s/%s) which should have status field.",
|
||||||
|
object.GetKind(), object.GetNamespace(), object.GetName())
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
jobStatus, err := helper.ConvertToJobStatus(statusMap)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to convert JobStatus from map[string]interface{}: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
grabStatus := batchv1.JobStatus{
|
||||||
|
Active: jobStatus.Active,
|
||||||
|
Succeeded: jobStatus.Succeeded,
|
||||||
|
Failed: jobStatus.Failed,
|
||||||
|
Conditions: jobStatus.Conditions,
|
||||||
|
StartTime: jobStatus.StartTime,
|
||||||
|
CompletionTime: jobStatus.CompletionTime,
|
||||||
|
}
|
||||||
|
return helper.BuildStatusRawExtension(grabStatus)
|
||||||
|
}
|
||||||
|
|
||||||
|
func reflectDaemonSetStatus(object *unstructured.Unstructured) (*runtime.RawExtension, error) {
|
||||||
|
statusMap, exist, err := unstructured.NestedMap(object.Object, "status")
|
||||||
|
if err != nil {
|
||||||
|
klog.Errorf("Failed to get status field from %s(%s/%s), error: %v",
|
||||||
|
object.GetKind(), object.GetNamespace(), object.GetName(), err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if !exist {
|
||||||
|
klog.Errorf("Failed to grab status from %s(%s/%s) which should have status field.",
|
||||||
|
object.GetKind(), object.GetNamespace(), object.GetName())
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
daemonSetStatus, err := helper.ConvertToDaemonSetStatus(statusMap)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to convert DaemonSetStatus from map[string]interface{}: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
grabStatus := appsv1.DaemonSetStatus{
|
||||||
|
CurrentNumberScheduled: daemonSetStatus.CurrentNumberScheduled,
|
||||||
|
DesiredNumberScheduled: daemonSetStatus.DesiredNumberScheduled,
|
||||||
|
NumberAvailable: daemonSetStatus.NumberAvailable,
|
||||||
|
NumberMisscheduled: daemonSetStatus.NumberMisscheduled,
|
||||||
|
NumberReady: daemonSetStatus.NumberReady,
|
||||||
|
UpdatedNumberScheduled: daemonSetStatus.UpdatedNumberScheduled,
|
||||||
|
NumberUnavailable: daemonSetStatus.NumberUnavailable,
|
||||||
|
}
|
||||||
|
return helper.BuildStatusRawExtension(grabStatus)
|
||||||
|
}
|
||||||
|
|
||||||
|
func reflectStatefulSetStatus(object *unstructured.Unstructured) (*runtime.RawExtension, error) {
|
||||||
|
statusMap, exist, err := unstructured.NestedMap(object.Object, "status")
|
||||||
|
if err != nil {
|
||||||
|
klog.Errorf("Failed to get status field from %s(%s/%s), error: %v",
|
||||||
|
object.GetKind(), object.GetNamespace(), object.GetName(), err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if !exist {
|
||||||
|
klog.Errorf("Failed to grab status from %s(%s/%s) which should have status field.",
|
||||||
|
object.GetKind(), object.GetNamespace(), object.GetName())
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
statefulSetStatus, err := helper.ConvertToStatefulSetStatus(statusMap)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to convert StatefulSetStatus from map[string]interface{}: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
grabStatus := appsv1.StatefulSetStatus{
|
||||||
|
AvailableReplicas: statefulSetStatus.AvailableReplicas,
|
||||||
|
CurrentReplicas: statefulSetStatus.CurrentReplicas,
|
||||||
|
ReadyReplicas: statefulSetStatus.ReadyReplicas,
|
||||||
|
Replicas: statefulSetStatus.Replicas,
|
||||||
|
UpdatedReplicas: statefulSetStatus.UpdatedReplicas,
|
||||||
|
}
|
||||||
|
return helper.BuildStatusRawExtension(grabStatus)
|
||||||
|
}
|
||||||
|
|
||||||
|
func reflectWholeStatus(object *unstructured.Unstructured) (*runtime.RawExtension, error) {
|
||||||
statusMap, exist, err := unstructured.NestedMap(object.Object, "status")
|
statusMap, exist, err := unstructured.NestedMap(object.Object, "status")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
klog.Errorf("Failed to get status field from %s(%s/%s), error: %v",
|
klog.Errorf("Failed to get status field from %s(%s/%s), error: %v",
|
||||||
|
|
|
@ -59,13 +59,13 @@ func Test_getEntireStatus(t *testing.T) {
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
got, err := getWholeStatus(tt.args.object)
|
got, err := reflectWholeStatus(tt.args.object)
|
||||||
if (err != nil) != tt.wantErr {
|
if (err != nil) != tt.wantErr {
|
||||||
t.Errorf("getWholeStatus() error = %v, wantErr %v", err, tt.wantErr)
|
t.Errorf("reflectWholeStatus() error = %v, wantErr %v", err, tt.wantErr)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if !reflect.DeepEqual(got, tt.want) {
|
if !reflect.DeepEqual(got, tt.want) {
|
||||||
t.Errorf("getWholeStatus() got = %v, want %v", got, tt.want)
|
t.Errorf("reflectWholeStatus() got = %v, want %v", got, tt.want)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -105,6 +105,16 @@ func ConvertToDaemonSet(obj *unstructured.Unstructured) (*appsv1.DaemonSet, erro
|
||||||
return typedObj, nil
|
return typedObj, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ConvertToDaemonSetStatus converts a DaemonSetStatus object from unstructured to typed.
|
||||||
|
func ConvertToDaemonSetStatus(obj map[string]interface{}) (*appsv1.DaemonSetStatus, error) {
|
||||||
|
typedObj := &appsv1.DaemonSetStatus{}
|
||||||
|
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj, typedObj); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return typedObj, nil
|
||||||
|
}
|
||||||
|
|
||||||
// ConvertToStatefulSet converts a StatefulSet object from unstructured to typed.
|
// ConvertToStatefulSet converts a StatefulSet object from unstructured to typed.
|
||||||
func ConvertToStatefulSet(obj *unstructured.Unstructured) (*appsv1.StatefulSet, error) {
|
func ConvertToStatefulSet(obj *unstructured.Unstructured) (*appsv1.StatefulSet, error) {
|
||||||
typedObj := &appsv1.StatefulSet{}
|
typedObj := &appsv1.StatefulSet{}
|
||||||
|
@ -115,6 +125,16 @@ func ConvertToStatefulSet(obj *unstructured.Unstructured) (*appsv1.StatefulSet,
|
||||||
return typedObj, nil
|
return typedObj, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ConvertToStatefulSetStatus converts a StatefulSetStatus object from unstructured to typed.
|
||||||
|
func ConvertToStatefulSetStatus(obj map[string]interface{}) (*appsv1.StatefulSetStatus, error) {
|
||||||
|
typedObj := &appsv1.StatefulSetStatus{}
|
||||||
|
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj, typedObj); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return typedObj, nil
|
||||||
|
}
|
||||||
|
|
||||||
// ConvertToJob converts a Job object from unstructured to typed.
|
// ConvertToJob converts a Job object from unstructured to typed.
|
||||||
func ConvertToJob(obj *unstructured.Unstructured) (*batchv1.Job, error) {
|
func ConvertToJob(obj *unstructured.Unstructured) (*batchv1.Job, error) {
|
||||||
typedObj := &batchv1.Job{}
|
typedObj := &batchv1.Job{}
|
||||||
|
@ -125,6 +145,16 @@ func ConvertToJob(obj *unstructured.Unstructured) (*batchv1.Job, error) {
|
||||||
return typedObj, nil
|
return typedObj, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ConvertToJobStatus converts a JobStatus from unstructured to typed.
|
||||||
|
func ConvertToJobStatus(obj map[string]interface{}) (*batchv1.JobStatus, error) {
|
||||||
|
typedObj := &batchv1.JobStatus{}
|
||||||
|
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj, typedObj); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return typedObj, nil
|
||||||
|
}
|
||||||
|
|
||||||
// ConvertToEndpointSlice converts a EndpointSlice object from unstructured to typed.
|
// ConvertToEndpointSlice converts a EndpointSlice object from unstructured to typed.
|
||||||
func ConvertToEndpointSlice(obj *unstructured.Unstructured) (*discoveryv1.EndpointSlice, error) {
|
func ConvertToEndpointSlice(obj *unstructured.Unstructured) (*discoveryv1.EndpointSlice, error) {
|
||||||
typedObj := &discoveryv1.EndpointSlice{}
|
typedObj := &discoveryv1.EndpointSlice{}
|
||||||
|
@ -155,7 +185,17 @@ func ConvertToService(obj *unstructured.Unstructured) (*corev1.Service, error) {
|
||||||
return typedObj, nil
|
return typedObj, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ConvertToIngress converts a Service object from unstructured to typed.
|
// ConvertToServiceStatus converts a ServiceStatus object from unstructured to typed.
|
||||||
|
func ConvertToServiceStatus(obj map[string]interface{}) (*corev1.ServiceStatus, error) {
|
||||||
|
typedObj := &corev1.ServiceStatus{}
|
||||||
|
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj, typedObj); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return typedObj, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ConvertToIngress converts an Ingress object from unstructured to typed.
|
||||||
func ConvertToIngress(obj *unstructured.Unstructured) (*extensionsv1beta1.Ingress, error) {
|
func ConvertToIngress(obj *unstructured.Unstructured) (*extensionsv1beta1.Ingress, error) {
|
||||||
typedObj := &extensionsv1beta1.Ingress{}
|
typedObj := &extensionsv1beta1.Ingress{}
|
||||||
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.UnstructuredContent(), typedObj); err != nil {
|
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.UnstructuredContent(), typedObj); err != nil {
|
||||||
|
|
Loading…
Reference in New Issue