Serge Bazanski | be538db | 2020-11-12 00:22:42 +0100 | [diff] [blame] | 1 | // Copyright 2017 The kubecfg authors |
| 2 | // |
| 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 | package kubecfg |
| 17 | |
| 18 | import ( |
| 19 | "fmt" |
| 20 | "io" |
| 21 | "strings" |
| 22 | |
| 23 | log "github.com/sirupsen/logrus" |
| 24 | "k8s.io/apimachinery/pkg/api/errors" |
| 25 | "k8s.io/apimachinery/pkg/api/meta" |
| 26 | "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 27 | "k8s.io/apimachinery/pkg/runtime/schema" |
| 28 | "k8s.io/apimachinery/pkg/util/sets" |
| 29 | "k8s.io/client-go/discovery" |
| 30 | |
| 31 | "code.hackerspace.pl/hscloud/cluster/tools/kartongips/utils" |
| 32 | ) |
| 33 | |
| 34 | // ValidateCmd represents the validate subcommand |
| 35 | type ValidateCmd struct { |
| 36 | Mapper meta.RESTMapper |
| 37 | Discovery discovery.DiscoveryInterface |
| 38 | IgnoreUnknown bool |
| 39 | } |
| 40 | |
| 41 | func (c ValidateCmd) Run(apiObjects []*unstructured.Unstructured, out io.Writer) error { |
| 42 | knownGVKs := sets.NewString() |
| 43 | gvkExists := func(gvk schema.GroupVersionKind) bool { |
| 44 | if knownGVKs.Has(gvk.String()) { |
| 45 | return true |
| 46 | } |
| 47 | gv := gvk.GroupVersion() |
| 48 | rls, err := c.Discovery.ServerResourcesForGroupVersion(gv.String()) |
| 49 | if err != nil { |
| 50 | if !errors.IsNotFound(err) { |
| 51 | log.Debugf("ServerResourcesForGroupVersion(%q) returned unexpected error %v", gv, err) |
| 52 | } |
| 53 | return false |
| 54 | } |
| 55 | for _, rl := range rls.APIResources { |
| 56 | knownGVKs.Insert(gv.WithKind(rl.Kind).String()) |
| 57 | } |
| 58 | return knownGVKs.Has(gvk.String()) |
| 59 | } |
| 60 | |
| 61 | hasError := false |
| 62 | |
| 63 | for _, obj := range apiObjects { |
| 64 | desc := fmt.Sprintf("%s %s", utils.ResourceNameFor(c.Mapper, obj), utils.FqName(obj)) |
| 65 | log.Info("Validating ", desc) |
| 66 | |
| 67 | gvk := obj.GroupVersionKind() |
| 68 | |
| 69 | var allErrs []error |
| 70 | |
| 71 | schema, err := utils.NewOpenAPISchemaFor(c.Discovery, gvk) |
| 72 | if err != nil { |
| 73 | isNotFound := errors.IsNotFound(err) || |
| 74 | strings.Contains(err.Error(), "is not supported by the server") |
| 75 | if isNotFound && (c.IgnoreUnknown || gvkExists(gvk)) { |
| 76 | log.Infof(" No schema found for %s, skipping validation", gvk) |
| 77 | continue |
| 78 | } |
| 79 | allErrs = append(allErrs, fmt.Errorf("Unable to fetch schema: %v", err)) |
| 80 | } else { |
| 81 | // Validate obj |
| 82 | for _, err := range schema.Validate(obj) { |
| 83 | allErrs = append(allErrs, err) |
| 84 | } |
| 85 | if obj.GetName() == "" { |
| 86 | allErrs = append(allErrs, fmt.Errorf("An Object does not have a name set")) |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | for _, err := range allErrs { |
| 91 | log.Errorf("Error in %s: %v", desc, err) |
| 92 | hasError = true |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | if hasError { |
| 97 | return fmt.Errorf("Validation failed") |
| 98 | } |
| 99 | |
| 100 | return nil |
| 101 | } |