Skip to content

Commit

Permalink
Merge pull request #8 from mbohlool/bugfix
Browse files Browse the repository at this point in the history
Bugfix in MergeSpecsIgnorePathConflict

kubectl OpenAPI support

kubectl OpenAPI bazel updates

Print a newline after ginkgo tests so the test infra doesn't think that they fail
Fixes #45279

Make OpenAPI GVK and Action extensions all lower-case

Get cmd uses print-column extn from Openapi schema

Get command now uses metadata x-kubernetes-print-columns, if present, in Openapi schema
to format output for a resource. This functionality is guarded by a boolean
flag 'use-openapi-print-columns'.

manually fix kubectl openapi unit test

openapi: Fetch protobuf rather than Json

This is much faster.

openapi: refactor into more generic structure

Refactor the openapi schema to be a more generic structure that can be
"visited" to get more specific types.

openapi: Remove cache mechanism

The cache will be removed and replaced with HTTP Etag caching instead.
This patch is simply removing the existing mechanism.

Revert "Merge pull request #47353 from apelisse/http-cache"

This reverts commit fc89743dca6b563063b74728c3b28100cf674d9d, reversing
changes made to 29ab38e898988c36e2de34f77fa33be556eb21bd.

Autogenerate BUILD files

Use buildozer to remove deprecated automanaged tags

Use buildozer to delete licenses() rules except under third_party/

openapi: Move Fakes to testing package

openapi: Add validation logic

This allows validation of a yaml/json object against an openapi schema.
A lot more testing would be needed to validate the logic, and also this
is not plumbed in, so it can't be used by kubectl yet.

Validate against OpenAPI schema (if available)

openapi: Use "group" to look for resources

openapi: Handle properly empty/null fileds

openapi-validation: Handle List special case

openapi validation: Ignore unknown types

This follows the exact same logic as swagger.

openapi: Change reference to be first-class

References in the openapi are currently completely hidden from the
model, and just passed through as we walk the tree. The problem is that
they can have a different description and more importantly, different
extensions.

Change them to be first-class citizen, and fully part of the model. It
means that visitors have to implement one more function and decide if
something specific should be done with references. Validation is updated
to just completely ignore them and passthrough (like it was done
before).

update pkg to not depend on other k8s repo
  • Loading branch information
mbohlool authored and ymqytw committed Aug 31, 2017
2 parents 4ff0210 + 12ec5ba commit dfe80dd
Show file tree
Hide file tree
Showing 11 changed files with 7,730 additions and 19 deletions.
234 changes: 234 additions & 0 deletions Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 35 additions & 19 deletions pkg/aggregator/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,28 @@ func (s *referenceWalker) Start() {
}
}

// FilterSpecByPaths remove unnecessary paths and unused definitions.
// usedDefinitionForSpec returns a map with all used definition in the provided spec as keys and true as values.
func usedDefinitionForSpec(sp *spec.Swagger) map[string]bool {
usedDefinitions := map[string]bool{}
walkOnAllReferences(func(ref spec.Ref) spec.Ref {
if refStr := ref.String(); refStr != "" && strings.HasPrefix(refStr, definitionPrefix) {
usedDefinitions[refStr[len(definitionPrefix):]] = true
}
return ref
}, sp)
return usedDefinitions
}

// FilterSpecByPaths removes unnecessary paths and definitions used by those paths.
// i.e. if a Path removed by this function, all definition used by it and not used
// anywhere else will also be removed.
func FilterSpecByPaths(sp *spec.Swagger, keepPathPrefixes []string) {
// Walk all references to find all used definitions. This function
// want to only deal with unused definitions resulted from filtering paths.
// Thus a definition will be removed only if it has been used before but
// it is unused because of a path prune.
initialUsedDefinitions := usedDefinitionForSpec(sp)

// First remove unwanted paths
prefixes := util.NewTrie(keepPathPrefixes)
orgPaths := sp.Paths
Expand All @@ -174,34 +194,24 @@ func FilterSpecByPaths(sp *spec.Swagger, keepPathPrefixes []string) {
}

// Walk all references to find all definition references.
usedDefinitions := map[string]bool{}

walkOnAllReferences(func(ref spec.Ref) spec.Ref {
if ref.String() != "" {
refStr := ref.String()
if strings.HasPrefix(refStr, definitionPrefix) {
usedDefinitions[refStr[len(definitionPrefix):]] = true
}
}
return ref
}, sp)
usedDefinitions := usedDefinitionForSpec(sp)

// Remove unused definitions
orgDefinitions := sp.Definitions
sp.Definitions = spec.Definitions{}
for k, v := range orgDefinitions {
if usedDefinitions[k] {
if usedDefinitions[k] || !initialUsedDefinitions[k] {
sp.Definitions[k] = v
}
}
}

func renameDefinition(s *spec.Swagger, old, new string) {
old_ref := definitionPrefix + old
new_ref := definitionPrefix + new
oldRef := definitionPrefix + old
newRef := definitionPrefix + new
walkOnAllReferences(func(ref spec.Ref) spec.Ref {
if ref.String() == old_ref {
return spec.MustCreateRef(new_ref)
if ref.String() == oldRef {
return spec.MustCreateRef(newRef)
}
return ref
}, s)
Expand All @@ -228,16 +238,22 @@ func MergeSpecs(dest, source *spec.Swagger) error {
}

func mergeSpecs(dest, source *spec.Swagger, renameModelConflicts, ignorePathConflicts bool) (err error) {

specCloned := false
if ignorePathConflicts {
keepPaths := []string{}
hasConflictingPath := false
for k := range source.Paths.Paths {
if _, found := dest.Paths.Paths[k]; !found {
keepPaths = append(keepPaths, k)
} else {
hasConflictingPath = true
}
}
if len(keepPaths) > 0 {
if len(keepPaths) == 0 {
// There is nothing to merge. All paths are conflicting.
return nil
}
if hasConflictingPath {
source, err = CloneSpec(source)
if err != nil {
return err
Expand Down
Loading

0 comments on commit dfe80dd

Please sign in to comment.