Skip to content
This repository has been archived by the owner on Jun 8, 2022. It is now read-only.

Commit

Permalink
healthscope support PodSpecWorkload
Browse files Browse the repository at this point in the history
Signed-off-by: roy wang <seiwy2010@gmail.com>
  • Loading branch information
captainroy-hy committed Oct 9, 2020
1 parent 763a9ef commit e0bf40c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ import (

const (
reconcileTimeout = 1 * time.Minute
// shortWait = 30 * time.Second
longWait = 1 * time.Minute
longWait = 1 * time.Minute
)

// Reconcile error strings.
Expand Down Expand Up @@ -123,7 +122,7 @@ func NewReconciler(m ctrl.Manager, o ...ReconcilerOption) *Reconciler {
record: event.NewNopRecorder(),
traitChecker: WorkloadHealthCheckFn(CheckByHealthCheckTrait),
checkers: []WorloadHealthChecker{
WorkloadHealthCheckFn(CheckStandardContainerziedHealth),
WorkloadHealthCheckFn(CheckPodSpecWorkloadHealth),
WorkloadHealthCheckFn(CheckContainerziedWorkloadHealth),
WorkloadHealthCheckFn(CheckDeploymentHealth),
WorkloadHealthCheckFn(CheckStatefulsetHealth),
Expand Down
24 changes: 13 additions & 11 deletions pkg/controller/v1alpha2/core/scopes/healthscope/standard.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,35 @@ import (
)

var (
standardContainerziedGVK = schema.GroupVersionKind{
// PodSpecWorkload is a generic PaaS workload which adopts full K8s pod spec.
// More details refer to oam-dev/kubevela
podSpecWorkloadGVK = schema.GroupVersionKind{
Group: "standard.oam.dev",
Version: "v1alpha1",
Kind: "Containerized",
Kind: "PodSpecWorkload",
}
)

// CheckStandardContainerziedHealth check health condition of containerizeds.standard.oam.dev
func CheckStandardContainerziedHealth(ctx context.Context, c client.Client, ref runtimev1alpha1.TypedReference, namespace string) *WorkloadHealthCondition {
if ref.GroupVersionKind() != standardContainerziedGVK {
// CheckPodSpecWorkloadHealth check health condition of podspecworkloads.standard.oam.dev
func CheckPodSpecWorkloadHealth(ctx context.Context, c client.Client, ref runtimev1alpha1.TypedReference, namespace string) *WorkloadHealthCondition {
if ref.GroupVersionKind() != podSpecWorkloadGVK {
return nil
}
r := &WorkloadHealthCondition{
HealthStatus: StatusHealthy,
TargetWorkload: ref,
}
containerizedObj := unstructured.Unstructured{}
containerizedObj.SetGroupVersionKind(ref.GroupVersionKind())
if err := c.Get(ctx, types.NamespacedName{Namespace: namespace, Name: ref.Name}, &containerizedObj); err != nil {
workloadObj := unstructured.Unstructured{}
workloadObj.SetGroupVersionKind(ref.GroupVersionKind())
if err := c.Get(ctx, types.NamespacedName{Namespace: namespace, Name: ref.Name}, &workloadObj); err != nil {
r.HealthStatus = StatusUnhealthy
r.Diagnosis = errors.Wrap(err, errHealthCheck).Error()
return r
}
r.ComponentName = getComponentNameFromLabel(&containerizedObj)
r.TargetWorkload.UID = containerizedObj.GetUID()
r.ComponentName = getComponentNameFromLabel(&workloadObj)
r.TargetWorkload.UID = workloadObj.GetUID()

childRefsData, _, _ := unstructured.NestedSlice(containerizedObj.Object, "status", "resources")
childRefsData, _, _ := unstructured.NestedSlice(workloadObj.Object, "status", "resources")
childRefs := []runtimev1alpha1.TypedReference{}
for _, v := range childRefsData {
v := v.(map[string]interface{})
Expand Down
10 changes: 5 additions & 5 deletions pkg/controller/v1alpha2/core/scopes/healthscope/standard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import (
"github.com/crossplane/oam-kubernetes-runtime/pkg/oam/util"
)

func TestCheckStandardContainerziedHealth(t *testing.T) {
func TestCheckPodSpecWorkloadHealth(t *testing.T) {
mockClient := test.NewMockClient()
scRef := runtimev1alpha1.TypedReference{}
scRef.SetGroupVersionKind(standardContainerziedGVK)
scRef.SetGroupVersionKind(podSpecWorkloadGVK)

deployRef := runtimev1alpha1.TypedReference{}
deployRef.SetGroupVersionKind(apps.SchemeGroupVersion.WithKind(kindDeployment))
Expand All @@ -29,7 +29,7 @@ func TestCheckStandardContainerziedHealth(t *testing.T) {
svcRefData, _ := util.Object2Map(svcRef)

scUnstructured := unstructured.Unstructured{}
scUnstructured.SetGroupVersionKind(standardContainerziedGVK)
scUnstructured.SetGroupVersionKind(podSpecWorkloadGVK)
unstructured.SetNestedSlice(scUnstructured.Object, []interface{}{deployRefData, svcRefData}, "status", "resources")

tests := []struct {
Expand Down Expand Up @@ -92,7 +92,7 @@ func TestCheckStandardContainerziedHealth(t *testing.T) {
},
},
{
caseName: "unhealthy for ContainerizedWorkload not found",
caseName: "unhealthy for PodSpecWorkload not found",
wlRef: scRef,
mockGetFn: func(ctx context.Context, key types.NamespacedName, obj runtime.Object) error {
return errMockErr
Expand Down Expand Up @@ -123,7 +123,7 @@ func TestCheckStandardContainerziedHealth(t *testing.T) {
for _, tc := range tests {
func(t *testing.T) {
mockClient.MockGet = tc.mockGetFn
result := CheckStandardContainerziedHealth(ctx, mockClient, tc.wlRef, namespace)
result := CheckPodSpecWorkloadHealth(ctx, mockClient, tc.wlRef, namespace)
if tc.expect == nil {
assert.Nil(t, result, tc.caseName)
} else {
Expand Down

0 comments on commit e0bf40c

Please sign in to comment.