Skip to content

Commit

Permalink
chore: create a method to get the status of custom resource
Browse files Browse the repository at this point in the history
  • Loading branch information
ericzzzzzzz committed Aug 8, 2023
1 parent f39c406 commit 19c000f
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
28 changes: 27 additions & 1 deletion pkg/diag/validator/config_connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ package validator

import (
"context"
"github.com/GoogleContainerTools/skaffold/v2/proto/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
kstatus "sigs.k8s.io/cli-utils/pkg/kstatus/status"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand Down Expand Up @@ -45,10 +48,33 @@ func (ccv *ConfigConnectorValidator) Validate(ctx context.Context, ns string, op
}
var rs []Resource
for _, r := range resources.Items {
status, ae := getResourceStatus(r)
status, ae := getConfigConnectorStatus(r)
// TODO: add recommendations from error codes
// TODO: add resource logs
rs = append(rs, NewResourceFromObject(&r, Status(status), ae, nil))
}
return rs, nil
}

func getConfigConnectorStatus(res unstructured.Unstructured) (kstatus.Status, *proto.ActionableErr) {
result, err := kstatus.Compute(&res)
if err != nil || result == nil {
return kstatus.UnknownStatus, &proto.ActionableErr{ErrCode: proto.StatusCode_STATUSCHECK_UNKNOWN, Message: "unable to check resource status"}
}
var ae proto.ActionableErr
switch result.Status {
case kstatus.CurrentStatus:
ae = proto.ActionableErr{ErrCode: proto.StatusCode_STATUSCHECK_SUCCESS}
case kstatus.InProgressStatus:
ae = proto.ActionableErr{ErrCode: proto.StatusCode_STATUSCHECK_CONFIG_CONNECTOR_IN_PROGRESS, Message: result.Message}
case kstatus.FailedStatus:
ae = proto.ActionableErr{ErrCode: proto.StatusCode_STATUSCHECK_CONFIG_CONNECTOR_FAILED, Message: result.Message}
case kstatus.TerminatingStatus:
ae = proto.ActionableErr{ErrCode: proto.StatusCode_STATUSCHECK_CONFIG_CONNECTOR_TERMINATING, Message: result.Message}
case kstatus.NotFoundStatus:
ae = proto.ActionableErr{ErrCode: proto.StatusCode_STATUSCHECK_CONFIG_CONNECTOR_NOT_FOUND, Message: result.Message}
case kstatus.UnknownStatus:
ae = proto.ActionableErr{ErrCode: proto.StatusCode_STATUSCHECK_UNKNOWN, Message: result.Message}
}
return result.Status, &ae
}
8 changes: 4 additions & 4 deletions pkg/diag/validator/custom_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ func getResourceStatus(res unstructured.Unstructured) (kstatus.Status, *proto.Ac
case kstatus.CurrentStatus:
ae = proto.ActionableErr{ErrCode: proto.StatusCode_STATUSCHECK_SUCCESS}
case kstatus.InProgressStatus:
ae = proto.ActionableErr{ErrCode: proto.StatusCode_STATUSCHECK_CONFIG_CONNECTOR_IN_PROGRESS, Message: result.Message}
ae = proto.ActionableErr{ErrCode: proto.StatusCode_STATUSCHECK_CUSTOM_RESOURCE_IN_PROGRESS, Message: result.Message}
case kstatus.FailedStatus:
ae = proto.ActionableErr{ErrCode: proto.StatusCode_STATUSCHECK_CONFIG_CONNECTOR_FAILED, Message: result.Message}
ae = proto.ActionableErr{ErrCode: proto.StatusCode_STATUSCHECK_CUSTOM_RESOURCE_FAILED, Message: result.Message}
case kstatus.TerminatingStatus:
ae = proto.ActionableErr{ErrCode: proto.StatusCode_STATUSCHECK_CONFIG_CONNECTOR_TERMINATING, Message: result.Message}
ae = proto.ActionableErr{ErrCode: proto.StatusCode_STATUSCHECK_CUSTOM_RESOURCE_TERMINATING, Message: result.Message}
case kstatus.NotFoundStatus:
ae = proto.ActionableErr{ErrCode: proto.StatusCode_STATUSCHECK_CONFIG_CONNECTOR_NOT_FOUND, Message: result.Message}
ae = proto.ActionableErr{ErrCode: proto.StatusCode_STATUSCHECK_CUSTOM_RESOURCE_NOT_FOUND, Message: result.Message}
case kstatus.UnknownStatus:
ae = proto.ActionableErr{ErrCode: proto.StatusCode_STATUSCHECK_UNKNOWN, Message: result.Message}
}
Expand Down
30 changes: 30 additions & 0 deletions pkg/skaffold/kubernetes/status/resource/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ func (r *Resource) CheckStatus(ctx context.Context, cfg kubectl.Config) {
ae = r.checkStandalonePodsStatus(ctx, cfg)
case ResourceTypes.ConfigConnector:
ae = r.checkConfigConnectorStatus()
case ResourceTypes.CustomResource:
ae = r.checkCustomResourceStatus()
default:
ae = r.checkRolloutStatus(ctx, cfg)
}
Expand Down Expand Up @@ -522,3 +524,31 @@ func (r *Resource) WithPodStatuses(scs []proto.StatusCode) *Resource {
}
return r
}

func (r *Resource) checkCustomResourceStatus() *proto.ActionableErr {
if len(r.resources) == 0 {
return &proto.ActionableErr{ErrCode: proto.StatusCode_STATUSCHECK_CUSTOM_RESOURCE_IN_PROGRESS}
}
var pendingResources []string
for _, resource := range r.resources {
ae := resource.ActionableError()
if ae == nil {
continue
}
switch ae.ErrCode {
case proto.StatusCode_STATUSCHECK_CUSTOM_RESOURCE_FAILED, proto.StatusCode_STATUSCHECK_CUSTOM_RESOURCE_TERMINATING:
return ae
case proto.StatusCode_STATUSCHECK_SUCCESS:
continue
default:
pendingResources = append(pendingResources, resource.Name())
}
}
if len(pendingResources) > 0 {
return &proto.ActionableErr{
ErrCode: proto.StatusCode_STATUSCHECK_CONFIG_CONNECTOR_IN_PROGRESS,
Message: fmt.Sprintf("custom resources not ready: %v", pendingResources),
}
}
return &proto.ActionableErr{ErrCode: proto.StatusCode_STATUSCHECK_SUCCESS}
}

0 comments on commit 19c000f

Please sign in to comment.