Library for getting Resource and field Schema from OpenAPI

This commit is contained in:
Phillip Wittrock
2020-01-06 10:33:19 -08:00
parent ed83b2d8fa
commit abeab51cae
7 changed files with 21438 additions and 1 deletions

View File

@@ -0,0 +1,87 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package openapi_test
import (
"fmt"
"sigs.k8s.io/kustomize/kyaml/openapi"
"sigs.k8s.io/kustomize/kyaml/yaml"
)
func Example() {
s := openapi.SchemaForResourceType(yaml.TypeMeta{APIVersion: "apps/v1", Kind: "Deployment"})
f := s.SchemaForField("spec").
SchemaForField("replicas")
fmt.Println(f.Schema.Description[:70] + "...")
fmt.Println(f.Schema.Type)
// Output:
// Number of desired pods. This is a pointer to distinguish between expli...
// [integer]
}
func Example_arrayMerge() {
s := openapi.SchemaForResourceType(yaml.TypeMeta{APIVersion: "apps/v1", Kind: "Deployment"})
f := s.SchemaForField("spec").
SchemaForField("template").
SchemaForField("spec").
SchemaForField("containers")
fmt.Println(f.Schema.Description[:70] + "...")
fmt.Println(f.Schema.Type)
fmt.Println(f.PatchStrategyAndKey()) // merge patch strategy on name
// Output:
// List of containers belonging to the pod. Containers cannot currently b...
// [array]
// merge name
}
func Example_arrayReplace() {
s := openapi.SchemaForResourceType(yaml.TypeMeta{APIVersion: "apps/v1", Kind: "Deployment"})
f := s.SchemaForField("spec").
SchemaForField("template").
SchemaForField("spec").
SchemaForField("containers").SchemaForElements().
SchemaForField("args")
fmt.Println(f.Schema.Description[:70] + "...")
fmt.Println(f.Schema.Type)
fmt.Println(f.PatchStrategyAndKey()) // no patch strategy or merge key
// Output:
// Arguments to the entrypoint. The docker image's CMD is used if this is...
// [array]
}
func Example_arrayElement() {
s := openapi.SchemaForResourceType(yaml.TypeMeta{APIVersion: "apps/v1", Kind: "Deployment"})
f := s.SchemaForField("spec").
SchemaForField("template").
SchemaForField("spec").
SchemaForField("containers").SchemaForElements().
SchemaForField("ports").SchemaForElements().
SchemaForField("containerPort")
fmt.Println(f.Schema.Description[:70] + "...")
fmt.Println(f.Schema.Type)
// Output:
// Number of port to expose on the pod's IP address. This must be a valid...
// [integer]
}
func Example_map() {
s := openapi.SchemaForResourceType(yaml.TypeMeta{APIVersion: "apps/v1", Kind: "Deployment"})
f := s.SchemaForField("metadata").SchemaForField("labels")
fmt.Println(f.Schema.Description[:70] + "...")
fmt.Println(f.Schema.Type)
// Output:
// Map of string keys and values that can be used to organize and categor...
// [object]
}