github.com/GoogleContainerTools/skaffold/v2@v2.13.2/pkg/diag/validator/custom_resource.go (about) 1 /* 2 Copyright 2021 The Skaffold Authors 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package validator 18 19 import ( 20 "context" 21 "fmt" 22 23 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 24 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" 25 "k8s.io/apimachinery/pkg/runtime/schema" 26 "k8s.io/client-go/dynamic" 27 "k8s.io/client-go/kubernetes" 28 kstatus "sigs.k8s.io/cli-utils/pkg/kstatus/status" 29 30 "github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/deploy/util" 31 "github.com/GoogleContainerTools/skaffold/v2/proto/v1" 32 ) 33 34 var _ Validator = (*CustomValidator)(nil) 35 36 type CustomResourceSelector struct { 37 client kubernetes.Interface 38 dynClient dynamic.Interface 39 kind schema.GroupVersionKind 40 } 41 42 type CustomValidator struct { 43 resourceSelector *CustomResourceSelector 44 } 45 46 func (c CustomValidator) Validate(ctx context.Context, ns string, opts metav1.ListOptions) ([]Resource, error) { 47 resources, err := c.resourceSelector.Select(ctx, ns, opts) 48 if err != nil { 49 return []Resource{}, err 50 } 51 var rs []Resource 52 for _, r := range resources.Items { 53 status, ae := getResourceStatus(r) 54 // TODO: add recommendations from error codes 55 // TODO: add resource logs 56 rs = append(rs, NewResourceFromObject(&r, Status(status), ae, nil)) 57 } 58 return rs, nil 59 } 60 61 // NewCustomValidator initializes a CustomValidator 62 func NewCustomValidator(k kubernetes.Interface, d dynamic.Interface, gvk schema.GroupVersionKind) *CustomValidator { 63 return &CustomValidator{resourceSelector: NewCustomResourceSelector(k, d, gvk)} 64 } 65 66 func NewCustomResourceSelector(client kubernetes.Interface, dynClient dynamic.Interface, gvk schema.GroupVersionKind) *CustomResourceSelector { 67 return &CustomResourceSelector{client: client, dynClient: dynClient, kind: gvk} 68 } 69 70 // Select returns the updated list of custom resources for the given GroupVersionKind deployed by skaffold 71 func (c *CustomResourceSelector) Select(ctx context.Context, namespace string, opts metav1.ListOptions) (*unstructured.UnstructuredList, error) { 72 _, r, err := util.GroupVersionResource(c.client.Discovery(), c.kind) 73 if err != nil { 74 return nil, fmt.Errorf("listing resources of kind %v: %w", c.kind, err) 75 } 76 resList, err := c.dynClient.Resource(r).Namespace(namespace).List(ctx, opts) 77 if err != nil { 78 return nil, fmt.Errorf("listing resources of kind %v: %w", c.kind, err) 79 } 80 return resList, nil 81 } 82 83 func getResourceStatus(res unstructured.Unstructured) (kstatus.Status, *proto.ActionableErr) { 84 result, err := kstatus.Compute(&res) 85 if err != nil || result == nil { 86 return kstatus.UnknownStatus, &proto.ActionableErr{ErrCode: proto.StatusCode_STATUSCHECK_UNKNOWN, Message: "unable to check resource status"} 87 } 88 var ae proto.ActionableErr 89 switch result.Status { 90 case kstatus.CurrentStatus: 91 ae = proto.ActionableErr{ErrCode: proto.StatusCode_STATUSCHECK_SUCCESS} 92 case kstatus.InProgressStatus: 93 ae = proto.ActionableErr{ErrCode: proto.StatusCode_STATUSCHECK_CUSTOM_RESOURCE_IN_PROGRESS, Message: result.Message} 94 case kstatus.FailedStatus: 95 ae = proto.ActionableErr{ErrCode: proto.StatusCode_STATUSCHECK_CUSTOM_RESOURCE_FAILED, Message: result.Message} 96 case kstatus.TerminatingStatus: 97 ae = proto.ActionableErr{ErrCode: proto.StatusCode_STATUSCHECK_CUSTOM_RESOURCE_TERMINATING, Message: result.Message} 98 case kstatus.NotFoundStatus: 99 ae = proto.ActionableErr{ErrCode: proto.StatusCode_STATUSCHECK_CUSTOM_RESOURCE_NOT_FOUND, Message: result.Message} 100 case kstatus.UnknownStatus: 101 ae = proto.ActionableErr{ErrCode: proto.StatusCode_STATUSCHECK_UNKNOWN, Message: result.Message} 102 } 103 return result.Status, &ae 104 }