Skip to content

Commit

Permalink
Add OpenAPI generation and swagger to update-codegen.sh
Browse files Browse the repository at this point in the history
And fix #1271 along the way by using the same scripting as in tektoncd/pipeline#4402 to ignore API rule violations for `name_match` which already exist. Any new mismatch will cause an error.

Signed-off-by: Andrew Bayer <andrew.bayer@gmail.com>
  • Loading branch information
abayer committed Jan 20, 2022
1 parent 91a821e commit 1b1921d
Show file tree
Hide file tree
Showing 6 changed files with 4,712 additions and 0 deletions.
10 changes: 10 additions & 0 deletions hack/ignored-openapi-violations.list
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
API rule violation: names_match,./pkg/apis/triggers/v1beta1,EventListenerConfig,GeneratedResourceName
API rule violation: names_match,./pkg/apis/triggers/v1beta1,InterceptorRequest,InterceptorParams
API rule violation: names_match,./pkg/apis/triggers/v1beta1,KubernetesResource,WithPodSpec
API rule violation: names_match,./pkg/apis/triggers/v1beta1,StatusError,s
API rule violation: names_match,./pkg/apis/triggers/v1beta1,TriggerContext,EventID
API rule violation: names_match,./pkg/apis/triggers/v1beta1,TriggerContext,EventURL
API rule violation: names_match,./pkg/apis/triggers/v1beta1,TriggerContext,TriggerID
API rule violation: names_match,./pkg/apis/triggers/v1beta1,TriggerSpecBinding,APIVersion
API rule violation: names_match,./pkg/apis/triggers/v1beta1,TriggerSpecTemplate,APIVersion
API rule violation: names_match,./pkg/apis/triggers/v1beta1,TriggerTemplateSpec,ResourceTemplates
79 changes: 79 additions & 0 deletions hack/spec-gen/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
Copyright 2020 The Tekton Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"encoding/json"
"fmt"
"os"
"strings"

tekton "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"

"k8s.io/klog"
"k8s.io/kube-openapi/pkg/common"
spec "k8s.io/kube-openapi/pkg/validation/spec"
)

func main() {
if len(os.Args) <= 1 {
klog.Fatal("Supply a version")
}
version := os.Args[1]
if !strings.HasPrefix(version, "v") {
version = "v" + version
}
oAPIDefs := tekton.GetOpenAPIDefinitions(func(name string) spec.Ref {
return spec.MustCreateRef("#/definitions/" + common.EscapeJsonPointer(swaggify(name)))
})
defs := spec.Definitions{}
for defName, val := range oAPIDefs {
defs[swaggify(defName)] = val.Schema
}
swagger := spec.Swagger{
SwaggerProps: spec.SwaggerProps{
Swagger: "2.0",
Definitions: defs,
Paths: &spec.Paths{Paths: map[string]spec.PathItem{}},
Info: &spec.Info{
InfoProps: spec.InfoProps{
Title: "Tekton",
Description: "Tekton Pipeline",
Version: version,
},
},
},
}
jsonBytes, err := json.MarshalIndent(swagger, "", " ")
if err != nil {
klog.Fatal(err.Error())
}
fmt.Println(string(jsonBytes))
}

func swaggify(name string) string {
name = strings.ReplaceAll(name, "./pkg/apis/pipeline/", "")
name = strings.ReplaceAll(name, "./pkg/apis/resource/", "")
name = strings.ReplaceAll(name, "github.com/tektoncd/pipeline/pkg/apis/pipeline/", "")
name = strings.ReplaceAll(name, "github.com/tektoncd/pipeline/pkg/apis/resource/", "")
name = strings.ReplaceAll(name, "k8s.io/api/core/", "")
name = strings.ReplaceAll(name, "k8s.io/apimachinery/pkg/apis/meta/", "")
name = strings.ReplaceAll(name, "knative.dev/pkg/apis.", "knative/")
name = strings.ReplaceAll(name, "knative.dev/pkg/apis/duck/v1beta1.", "knative/")
name = strings.ReplaceAll(name, "/", ".")
return name
}
3 changes: 3 additions & 0 deletions hack/update-codegen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,6 @@ GOFLAGS="${OLDGOFLAGS}"

# Make sure our dependencies are up-to-date
${REPO_ROOT_DIR}/hack/update-deps.sh

# Make sure the OpenAPI specification and Swagger file are up-to-date
${REPO_ROOT_DIR}/hack/update-openapigen.sh
54 changes: 54 additions & 0 deletions hack/update-openapigen.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env bash

# Copyright 2020 The Tekton Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -o errexit
set -o nounset

source $(git rev-parse --show-toplevel)/vendor/github.com/tektoncd/plumbing/scripts/library.sh

cd "${REPO_ROOT_DIR}"

readonly TMP_DIFFROOT="$(mktemp -d ${REPO_ROOT_DIR}/tmpdiffroot.XXXXXX)"

cleanup() {
rm -rf "${TMP_DIFFROOT}"
}

cleanup

mkdir -p "${TMP_DIFFROOT}"

trap cleanup EXIT

echo "Generating OpenAPI specification ..."
go run k8s.io/kube-openapi/cmd/openapi-gen \
--input-dirs ./pkg/apis/triggers/v1beta1,knative.dev/pkg/apis,knative.dev/pkg/apis/duck/v1beta1 \
--output-package ./pkg/apis/triggers/v1beta1 -o ./ \
--go-header-file hack/boilerplate/boilerplate.go.txt \
-r "${TMP_DIFFROOT}/api-report"

violations=$(diff --changed-group-format='%>' --unchanged-group-format='' "hack/ignored-openapi-violations.list" "${TMP_DIFFROOT}/api-report" || echo "")
if [ -n "${violations}" ]; then
echo ""
echo "New API rule violations found which are not present in hack/ignored-openapi-violations.list. Please fix these violations:"
echo ""
echo "${violations}"
echo ""
exit 1
fi

echo "Generating swagger file ..."
go run hack/spec-gen/main.go v0.17.2 > pkg/apis/triggers/v1beta1/swagger.json
Loading

0 comments on commit 1b1921d

Please sign in to comment.