mirror of
https://github.com/yannh/kubeconform.git
synced 2026-02-22 03:07:01 +00:00
Add comments to most exported functions/types
This commit is contained in:
parent
f78b65b025
commit
4b16128b75
6 changed files with 20 additions and 4 deletions
6
pkg/cache/schemacache.go
vendored
6
pkg/cache/schemacache.go
vendored
|
|
@ -7,21 +7,26 @@ import (
|
||||||
"github.com/xeipuuv/gojsonschema"
|
"github.com/xeipuuv/gojsonschema"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// SchemaCache is a cache for downloaded schemas, so each file is only retrieved once
|
||||||
type SchemaCache struct {
|
type SchemaCache struct {
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
schemas map[string]*gojsonschema.Schema
|
schemas map[string]*gojsonschema.Schema
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// New creates a new cache for downloaded schemas
|
||||||
func New() *SchemaCache {
|
func New() *SchemaCache {
|
||||||
return &SchemaCache{
|
return &SchemaCache{
|
||||||
schemas: map[string]*gojsonschema.Schema{},
|
schemas: map[string]*gojsonschema.Schema{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Key computes a key for a specific JSON schema from its Kind, the resource API Version, and the
|
||||||
|
// Kubernetes version
|
||||||
func Key(resourceKind, resourceAPIVersion, k8sVersion string) string {
|
func Key(resourceKind, resourceAPIVersion, k8sVersion string) string {
|
||||||
return fmt.Sprintf("%s-%s-%s", resourceKind, resourceAPIVersion, k8sVersion)
|
return fmt.Sprintf("%s-%s-%s", resourceKind, resourceAPIVersion, k8sVersion)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get retrieves the JSON schema given a resource signature
|
||||||
func (c *SchemaCache) Get(key string) (*gojsonschema.Schema, bool) {
|
func (c *SchemaCache) Get(key string) (*gojsonschema.Schema, bool) {
|
||||||
c.RLock()
|
c.RLock()
|
||||||
defer c.RUnlock()
|
defer c.RUnlock()
|
||||||
|
|
@ -29,6 +34,7 @@ func (c *SchemaCache) Get(key string) (*gojsonschema.Schema, bool) {
|
||||||
return schema, ok
|
return schema, ok
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set adds a JSON schema to the schema cache
|
||||||
func (c *SchemaCache) Set(key string, schema *gojsonschema.Schema) {
|
func (c *SchemaCache) Set(key string, schema *gojsonschema.Schema) {
|
||||||
c.Lock()
|
c.Lock()
|
||||||
defer c.Unlock()
|
defer c.Unlock()
|
||||||
|
|
|
||||||
|
|
@ -50,6 +50,7 @@ func splitCSV(csvStr string) map[string]bool {
|
||||||
return valuesMap
|
return valuesMap
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FromFlags retrieves kubeconform's runtime configuration from the command-line parameters
|
||||||
func FromFlags(progName string, args []string) (Config, string, error) {
|
func FromFlags(progName string, args []string) (Config, string, error) {
|
||||||
var schemaLocationsParam, ignoreFilenamePatterns arrayParam
|
var schemaLocationsParam, ignoreFilenamePatterns arrayParam
|
||||||
var skipKindsCSV, rejectKindsCSV string
|
var skipKindsCSV, rejectKindsCSV string
|
||||||
|
|
|
||||||
|
|
@ -8,12 +8,14 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type KubernetesRegistry struct {
|
// SchemaRegistry is a file repository (local or remote) that contains JSON schemas for Kubernetes resources
|
||||||
|
type SchemaRegistry struct {
|
||||||
c *http.Client
|
c *http.Client
|
||||||
schemaPathTemplate string
|
schemaPathTemplate string
|
||||||
strict bool
|
strict bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NotFoundError is returned when the registry does not contain a schema for the resource
|
||||||
type NotFoundError struct {
|
type NotFoundError struct {
|
||||||
err error
|
err error
|
||||||
}
|
}
|
||||||
|
|
@ -23,7 +25,7 @@ func newNetFoundError(err error) *NotFoundError {
|
||||||
}
|
}
|
||||||
func (e *NotFoundError) Error() string { return e.err.Error() }
|
func (e *NotFoundError) Error() string { return e.err.Error() }
|
||||||
|
|
||||||
func newHTTPRegistry(schemaPathTemplate string, strict bool, skipTLS bool) *KubernetesRegistry {
|
func newHTTPRegistry(schemaPathTemplate string, strict bool, skipTLS bool) *SchemaRegistry {
|
||||||
reghttp := &http.Transport{
|
reghttp := &http.Transport{
|
||||||
MaxIdleConns: 100,
|
MaxIdleConns: 100,
|
||||||
IdleConnTimeout: 3 * time.Second,
|
IdleConnTimeout: 3 * time.Second,
|
||||||
|
|
@ -34,14 +36,15 @@ func newHTTPRegistry(schemaPathTemplate string, strict bool, skipTLS bool) *Kube
|
||||||
reghttp.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
|
reghttp.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
|
||||||
}
|
}
|
||||||
|
|
||||||
return &KubernetesRegistry{
|
return &SchemaRegistry{
|
||||||
c: &http.Client{Transport: reghttp},
|
c: &http.Client{Transport: reghttp},
|
||||||
schemaPathTemplate: schemaPathTemplate,
|
schemaPathTemplate: schemaPathTemplate,
|
||||||
strict: strict,
|
strict: strict,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r KubernetesRegistry) DownloadSchema(resourceKind, resourceAPIVersion, k8sVersion string) ([]byte, error) {
|
// DownloadSchema downloads the schema for a particular resource from an HTTP server
|
||||||
|
func (r SchemaRegistry) DownloadSchema(resourceKind, resourceAPIVersion, k8sVersion string) ([]byte, error) {
|
||||||
url, err := schemaPath(r.schemaPathTemplate, resourceKind, resourceAPIVersion, k8sVersion, r.strict)
|
url, err := schemaPath(r.schemaPathTemplate, resourceKind, resourceAPIVersion, k8sVersion, r.strict)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
||||||
|
|
@ -4,16 +4,19 @@ import (
|
||||||
"sigs.k8s.io/yaml"
|
"sigs.k8s.io/yaml"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Resource represents a Kubernetes resource within a file
|
||||||
type Resource struct {
|
type Resource struct {
|
||||||
Path string
|
Path string
|
||||||
Bytes []byte
|
Bytes []byte
|
||||||
sig *Signature
|
sig *Signature
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Signature is a key representing a Kubernetes resource
|
||||||
type Signature struct {
|
type Signature struct {
|
||||||
Kind, Version, Namespace, Name string
|
Kind, Version, Namespace, Name string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Signature computes a signature for a resource, based on its Kind, Version, Namespace & Name
|
||||||
func (res *Resource) Signature() (*Signature, error) {
|
func (res *Resource) Signature() (*Signature, error) {
|
||||||
if res.sig != nil {
|
if res.sig != nil {
|
||||||
return res.sig, nil
|
return res.sig, nil
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// FromStream reads resources from a byte stream, usually here stdin
|
||||||
func FromStream(ctx context.Context, path string, r io.Reader) (<-chan Resource, <-chan error) {
|
func FromStream(ctx context.Context, path string, r io.Reader) (<-chan Resource, <-chan error) {
|
||||||
resources := make(chan Resource)
|
resources := make(chan Resource)
|
||||||
errors := make(chan error)
|
errors := make(chan error)
|
||||||
|
|
|
||||||
|
|
@ -37,12 +37,14 @@ func (f ValidFormat) IsFormat(input interface{}) bool {
|
||||||
// gojsonschema.FormatCheckers.Add("int-or-string", ValidFormat{})
|
// gojsonschema.FormatCheckers.Add("int-or-string", ValidFormat{})
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
// Result contains the details of the result of a resource validation
|
||||||
type Result struct {
|
type Result struct {
|
||||||
Resource resource.Resource
|
Resource resource.Resource
|
||||||
Err error
|
Err error
|
||||||
Status Status
|
Status Status
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewError is a utility function to generate a validation error
|
||||||
func NewError(filename string, err error) Result {
|
func NewError(filename string, err error) Result {
|
||||||
return Result{
|
return Result{
|
||||||
Resource: resource.Resource{Path: filename},
|
Resource: resource.Resource{Path: filename},
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue