github.com/GoogleContainerTools/skaffold/v2@v2.13.2/pkg/diag/diag.go (about) 1 /* 2 Copyright 2020 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 diag 18 19 import ( 20 "context" 21 "fmt" 22 23 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 24 "k8s.io/apimachinery/pkg/labels" 25 26 "github.com/GoogleContainerTools/skaffold/v2/pkg/diag/validator" 27 ) 28 29 type Diagnose interface { 30 Run(ctx context.Context) ([]validator.Resource, error) 31 WithLabel(key, value string) Diagnose 32 WithValidators(v []validator.Validator) Diagnose 33 } 34 35 type diag struct { 36 namespaces []string 37 labels map[string]string 38 validators []validator.Validator 39 } 40 41 func New(namespaces []string) Diagnose { 42 return &diag{ 43 namespaces: namespaces, 44 labels: map[string]string{}, 45 } 46 } 47 48 func (d *diag) WithLabel(key, value string) Diagnose { 49 d.labels[key] = value 50 return d 51 } 52 53 func (d *diag) WithValidators(v []validator.Validator) Diagnose { 54 d.validators = v 55 return d 56 } 57 58 func (d *diag) Run(ctx context.Context) ([]validator.Resource, error) { 59 var ( 60 res []validator.Resource 61 errs []error 62 ) 63 // get selector from labels 64 selector := labels.SelectorFromSet(d.labels) 65 listOptions := metav1.ListOptions{ 66 LabelSelector: selector.String(), 67 } 68 69 for _, v := range d.validators { 70 for _, ns := range d.namespaces { 71 r, err := v.Validate(ctx, ns, listOptions) 72 res = append(res, r...) 73 if err != nil { 74 errs = append(errs, err) 75 } 76 } 77 } 78 if len(errs) == 0 { 79 return res, nil 80 } 81 82 errBuilder := "" 83 for _, err := range errs { 84 errBuilder = errBuilder + err.Error() + "\n" 85 } 86 87 return res, fmt.Errorf("following errors occurred %s", errBuilder) 88 }