openapi

package
v0.17.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 6, 2024 License: Apache-2.0 Imports: 15 Imported by: 27

README

Sampling New OpenAPI Data

This document describes how to fetch OpenAPI data from a live kubernetes API server. The scripts used will create a clean kind instance for this purpose.

Replacing the default openapi schema version

Delete all currently built-in schema

This will remove both the Kustomization and Kubernetes schemas:

make nuke
Choose the new version to use

The compiled-in schema version should maximize API availability with respect to all actively supported Kubernetes versions. For example, while 1.20, 1.21 and 1.22 are the actively supported versions, 1.21 is the best choice. This is because 1.21 introduces at least one new API and does not remove any, while 1.22 removes a large set of long-deprecated APIs that are still supported in 1.20/1.21.

Generating additional schema

If you'd like to change the default schema version, then in the Makefile in this directory, update the API_VERSION to your desired version.

You may need to update the version of Kind these scripts use by changing KIND_VERSION in the Makefile in this directory. You can find compatibility information in the kind release notes.

In this directory, fetch the openapi schema, generate the corresponding swagger.go for the kubernetes api, and update kubernetesapi/openapiinfo.go:

make all

If you want to run the steps individually instead of using make all, you can run the following commands:

make kustomizationapi/swagger.go
make kubernetesapi/swagger.go
make kubernetesapi/openapiinfo.go

You can optionally delete the old swagger.pb and swagger.go files if we no longer need to support that kubernetes version of openapi data. Make sure you rerun make kubernetesapi/openapiinfo.go after deleting any old schemas.

Precomputations

To avoid expensive schema lookups, some functions have precomputed results based on the schema. Unit tests ensure these are kept in sync with the schema; if these tests fail you will need to follow the suggested diff to update the precomputed results.

Run all tests

At the top of the repository, run the tests.

make prow-presubmit-check >& /tmp/k.txt; echo $?

The exit code should be zero; if not, examine /tmp/k.txt.

Partial regeneration

You can also regenerate the kubernetes api schemas specifically with:

rm kubernetesapi/swagger.go
make kubernetesapi/swagger.go

To fetch the schema without generating the swagger.go, you can run:

rm kubernetesapi/swagger.pb
make kubernetesapi/swagger.pb

Note that generating the swagger.go will re-fetch the schema.

Documentation

Overview

Example
package main

import (
	"fmt"

	"sigs.k8s.io/kustomize/kyaml/openapi"
	"sigs.k8s.io/kustomize/kyaml/yaml"
)

func main() {
	s := openapi.SchemaForResourceType(yaml.TypeMeta{APIVersion: "apps/v1", Kind: "Deployment"})

	f := s.Lookup("spec", "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]
Example (ArrayElement)
package main

import (
	"fmt"

	"sigs.k8s.io/kustomize/kyaml/openapi"
	"sigs.k8s.io/kustomize/kyaml/yaml"
)

func main() {
	s := openapi.SchemaForResourceType(yaml.TypeMeta{APIVersion: "apps/v1", Kind: "Deployment"})

	f := s.Lookup("spec", "template", "spec", "containers",
		openapi.Elements, "ports", openapi.Elements, "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]
Example (ArrayMerge)
package main

import (
	"fmt"

	"sigs.k8s.io/kustomize/kyaml/openapi"
	"sigs.k8s.io/kustomize/kyaml/yaml"
)

func main() {
	s := openapi.SchemaForResourceType(yaml.TypeMeta{APIVersion: "apps/v1", Kind: "Deployment"})

	f := s.Lookup("spec", "template", "spec", "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
Example (ArrayReplace)
package main

import (
	"fmt"

	"sigs.k8s.io/kustomize/kyaml/openapi"
	"sigs.k8s.io/kustomize/kyaml/yaml"
)

func main() {
	s := openapi.SchemaForResourceType(yaml.TypeMeta{APIVersion: "apps/v1", Kind: "Deployment"})

	f := s.Lookup("spec", "template", "spec", "containers", openapi.Elements, "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]
Example (Map)
package main

import (
	"fmt"

	"sigs.k8s.io/kustomize/kyaml/openapi"
	"sigs.k8s.io/kustomize/kyaml/yaml"
)

func main() {
	s := openapi.SchemaForResourceType(yaml.TypeMeta{APIVersion: "apps/v1", Kind: "Deployment"})

	f := s.Lookup("metadata", "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]

Index

Examples

Constants

View Source
const (
	JsonOrYaml format = "jsonOrYaml"
	Proto      format = "proto"
)
View Source
const Definitions = "definitions"
View Source
const Elements = "[]"
View Source
const SupplementaryOpenAPIFieldName = "openAPI"

SupplementaryOpenAPIFieldName is the conventional field name (JSON/YAML) containing supplementary OpenAPI definitions.

Variables

This section is empty.

Functions

func AddDefinitions added in v0.0.11

func AddDefinitions(definitions spec.Definitions)

AddDefinitions adds the definitions to the global schema.

func AddSchema added in v0.0.11

func AddSchema(s []byte) error

AddSchema parses s, and adds definitions from s to the global schema.

func DefinitionRefs added in v0.9.0

func DefinitionRefs(openAPIPath string) ([]string, error)

DefinitionRefs returns the list of openAPI definition references present in the input openAPIPath

func GetSchemaVersion added in v0.10.7

func GetSchemaVersion() string

GetSchemaVersion returns what kubernetes OpenAPI version is being used

func IsCertainlyClusterScoped added in v0.10.20

func IsCertainlyClusterScoped(typeMeta yaml.TypeMeta) bool

IsCertainlyClusterScoped returns true for Node, Namespace, etc. and false for Pod, Deployment, etc. and kinds that aren't recognized in the openapi data. See: https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces

func IsNamespaceScoped added in v0.8.0

func IsNamespaceScoped(typeMeta yaml.TypeMeta) (bool, bool)

IsNamespaceScoped determines whether a resource is namespace or cluster-scoped by looking at the information in the openapi schema. The second return value tells whether the provided type could be found in the openapi schema. If the value is false here, the scope of the resource is not known. If the type is found, the first return value will be true if the resource is namespace-scoped, and false if the type is cluster-scoped.

func ResetOpenAPI added in v0.0.12

func ResetOpenAPI()

ResetOpenAPI resets the openapi data to empty

func Resolve added in v0.0.11

func Resolve(ref *spec.Ref, schema *spec.Schema) (*spec.Schema, error)

Resolve resolves the reference against the global schema

func Schema added in v0.0.11

func Schema() *spec.Schema

Schema returns the global schema

func SchemaFromFile added in v0.10.0

func SchemaFromFile(path string) (*spec.Schema, error)

AddSchemaFromFile reads the file at path and parses the OpenAPI definitions from the field "openAPI", also returns a function to clean the added definitions The returned clean function is a no-op on error, or else it's a function that the caller should use to remove the added openAPI definitions from global schema

func SetSchema added in v0.10.11

func SetSchema(openAPIField map[string]string, schema []byte, reset bool) error

SetSchema sets the kubernetes OpenAPI schema version to use

func SuppressBuiltInSchemaUse added in v0.0.11

func SuppressBuiltInSchemaUse()

SuppressBuiltInSchemaUse can be called to prevent using the built-in Kubernetes schema as part of the global schema. Must be called before the schema is used.

Types

type ResourceSchema

type ResourceSchema struct {
	// Schema is the OpenAPI schema for a Resource or field
	Schema *spec.Schema
}

ResourceSchema wraps the OpenAPI Schema.

func GetSchema added in v0.0.11

func GetSchema(s string, schema *spec.Schema) (*ResourceSchema, error)

GetSchema parses s into a ResourceSchema, resolving References within the global schema.

func SchemaForResourceType

func SchemaForResourceType(t yaml.TypeMeta) *ResourceSchema

SchemaForResourceType returns the Schema for the given Resource TODO(pwittrock): create a version of this function that will return a schema which can be used for duck-typed Resources -- e.g. contains common fields such as metadata, replicas and spec.template.spec

func (*ResourceSchema) Elements

func (rs *ResourceSchema) Elements() *ResourceSchema

Elements returns the Schema for the elements of an array.

func (*ResourceSchema) Field

func (rs *ResourceSchema) Field(field string) *ResourceSchema

Field returns the Schema for a field.

func (*ResourceSchema) IsMissingOrNull added in v0.6.0

func (rs *ResourceSchema) IsMissingOrNull() bool

IsEmpty returns true if the ResourceSchema is empty

func (*ResourceSchema) Lookup

func (rs *ResourceSchema) Lookup(path ...string) *ResourceSchema

Lookup calls either Field or Elements for each item in the path. If the path item is "[]", then Elements is called, otherwise Field is called. If any Field or Elements call returns nil, then Lookup returns nil immediately.

func (*ResourceSchema) PatchStrategyAndKey

func (rs *ResourceSchema) PatchStrategyAndKey() (string, string)

PatchStrategyAndKey returns the patch strategy and merge key extensions

func (*ResourceSchema) PatchStrategyAndKeyList added in v0.9.4

func (rs *ResourceSchema) PatchStrategyAndKeyList() (string, []string)

PatchStrategyAndKeyList returns the patch strategy and complete merge key list

Directories

Path Synopsis
v1_21_2
Package v1_21_2 generated by go-bindata.// sources: kubernetesapi/v1_21_2/swagger.pb
Package v1_21_2 generated by go-bindata.// sources: kubernetesapi/v1_21_2/swagger.pb
Code generated for package kustomizationapi by go-bindata DO NOT EDIT.
Code generated for package kustomizationapi by go-bindata DO NOT EDIT.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL