Skip to content

Commit

Permalink
rename validatorfactory -> validator
Browse files Browse the repository at this point in the history
  • Loading branch information
alexzielenski committed Sep 14, 2023
1 parent f3fde0a commit 8b66d4c
Show file tree
Hide file tree
Showing 10 changed files with 18 additions and 18 deletions.
4 changes: 2 additions & 2 deletions cmd/download-builtin-schemas/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/kube-openapi/pkg/spec3"
"sigs.k8s.io/kubectl-validate/pkg/openapiclient"
"sigs.k8s.io/kubectl-validate/pkg/validatorfactory"
"sigs.k8s.io/kubectl-validate/pkg/validator"
)

// Downloads builtin schemas from GitHub and saves them to disk for embedding
Expand Down Expand Up @@ -89,7 +89,7 @@ func main() {
}

for k, d := range parsed.Components.Schemas {
validatorfactory.ApplySchemaPatches(i, gv, k, d)
validator.ApplySchemaPatches(i, gv, k, d)
}

newJSON, err := json.Marshal(parsed)
Expand Down
8 changes: 4 additions & 4 deletions pkg/cmd/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"k8s.io/client-go/tools/clientcmd"
"sigs.k8s.io/kubectl-validate/pkg/openapiclient"
"sigs.k8s.io/kubectl-validate/pkg/utils"
"sigs.k8s.io/kubectl-validate/pkg/validatorfactory"
"sigs.k8s.io/kubectl-validate/pkg/validator"

yamlv2 "gopkg.in/yaml.v2"
)
Expand Down Expand Up @@ -165,7 +165,7 @@ func errorToStatus(err error) metav1.Status {

func (c *commandFlags) Run(cmd *cobra.Command, args []string) error {
// tool fetches openapi in the following priority order:
factory, err := validatorfactory.New(
factory, err := validator.New(
openapiclient.NewOverlay(
// apply user defined patches on top of the final schema
openapiclient.PatchLoaderFromDirectory(nil, c.schemaPatchesDir),
Expand Down Expand Up @@ -246,7 +246,7 @@ func (c *commandFlags) Run(cmd *cobra.Command, args []string) error {
return nil
}

func ValidateFile(filePath string, resolver *validatorfactory.ValidatorFactory) []error {
func ValidateFile(filePath string, resolver *validator.Validator) []error {
fileBytes, err := os.ReadFile(filePath)
if err != nil {
return []error{fmt.Errorf("error reading file: %w", err)}
Expand All @@ -272,7 +272,7 @@ func ValidateFile(filePath string, resolver *validatorfactory.ValidatorFactory)
}
}

func ValidateDocument(document []byte, resolver *validatorfactory.ValidatorFactory) error {
func ValidateDocument(document []byte, resolver *validator.Validator) error {
gvk, parsed, err := resolver.Parse(document)
if gvk.Group == "apiextensions.k8s.io" && gvk.Kind == "CustomResourceDefinition" {
// CRD spec contains an infinite loop which is not supported by K8s
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package validatorfactory
package validator

import (
"errors"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package validatorfactory
package validator

import (
"os"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package validatorfactory
package validator

import (
"path/filepath"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package validatorfactory
package validator

import (
"fmt"
Expand Down
File renamed without changes.
File renamed without changes.
14 changes: 7 additions & 7 deletions pkg/validatorfactory/factory.go → pkg/validator/validator.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package validatorfactory
package validator

import (
"context"
Expand Down Expand Up @@ -26,18 +26,18 @@ import (
"sigs.k8s.io/yaml"
)

type ValidatorFactory struct {
type Validator struct {
gvs map[string]openapi.GroupVersion
validatorCache map[schema.GroupVersionKind]*validatorEntry
}

func New(client openapi.Client) (*ValidatorFactory, error) {
func New(client openapi.Client) (*Validator, error) {
gvs, err := client.Paths()
if err != nil {
return nil, err
}

return &ValidatorFactory{
return &Validator{
gvs: gvs,
validatorCache: map[schema.GroupVersionKind]*validatorEntry{},
}, nil
Expand All @@ -48,7 +48,7 @@ func New(client openapi.Client) (*ValidatorFactory, error) {
//
// It will return errors when there is an issue parsing the object, or if
// it contains fields unknown to the schema, or if the schema was recursive.
func (s *ValidatorFactory) Parse(document []byte) (schema.GroupVersionKind, *unstructured.Unstructured, error) {
func (s *Validator) Parse(document []byte) (schema.GroupVersionKind, *unstructured.Unstructured, error) {
metadata := metav1.TypeMeta{}
if err := yaml.Unmarshal(document, &metadata); err != nil {
return schema.GroupVersionKind{}, nil, fmt.Errorf("failed to parse yaml: %w", err)
Expand Down Expand Up @@ -87,7 +87,7 @@ func (s *ValidatorFactory) Parse(document []byte) (schema.GroupVersionKind, *uns

// Validate takes a parsed resource as input and validates it against
// its schema.
func (s *ValidatorFactory) Validate(obj *unstructured.Unstructured) error {
func (s *Validator) Validate(obj *unstructured.Unstructured) error {
gvk := obj.GroupVersionKind()
validators, err := s.infoForGVK(gvk)
if err != nil {
Expand Down Expand Up @@ -119,7 +119,7 @@ func (s *ValidatorFactory) Validate(obj *unstructured.Unstructured) error {
return rest.BeforeCreate(strat, request.WithNamespace(context.TODO(), obj.GetNamespace()), obj)
}

func (s *ValidatorFactory) infoForGVK(gvk schema.GroupVersionKind) (*validatorEntry, error) {
func (s *Validator) infoForGVK(gvk schema.GroupVersionKind) (*validatorEntry, error) {
if existing, ok := s.validatorCache[gvk]; ok {
return existing, nil
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package validatorfactory
package validator

import (
"encoding/json"
Expand Down

0 comments on commit 8b66d4c

Please sign in to comment.