Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add OpenAPI generation and swagger to update-codegen.sh #1297

Merged
merged 1 commit into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ require (
k8s.io/apimachinery v0.22.5
k8s.io/client-go v0.22.5
k8s.io/code-generator v0.22.5
k8s.io/klog v1.0.0
k8s.io/kube-openapi v0.0.0-20211109043538-20434351676c
knative.dev/eventing v0.25.0
knative.dev/pkg v0.0.0-20220104185830-52e42b760b54
Expand Down
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,github.com/tektoncd/triggers/pkg/apis/triggers/v1beta1,EventListenerConfig,GeneratedResourceName
API rule violation: names_match,github.com/tektoncd/triggers/pkg/apis/triggers/v1beta1,InterceptorRequest,InterceptorParams
API rule violation: names_match,github.com/tektoncd/triggers/pkg/apis/triggers/v1beta1,KubernetesResource,WithPodSpec
API rule violation: names_match,github.com/tektoncd/triggers/pkg/apis/triggers/v1beta1,StatusError,s
API rule violation: names_match,github.com/tektoncd/triggers/pkg/apis/triggers/v1beta1,TriggerContext,EventID
API rule violation: names_match,github.com/tektoncd/triggers/pkg/apis/triggers/v1beta1,TriggerContext,EventURL
API rule violation: names_match,github.com/tektoncd/triggers/pkg/apis/triggers/v1beta1,TriggerContext,TriggerID
API rule violation: names_match,github.com/tektoncd/triggers/pkg/apis/triggers/v1beta1,TriggerSpecBinding,APIVersion
API rule violation: names_match,github.com/tektoncd/triggers/pkg/apis/triggers/v1beta1,TriggerSpecTemplate,APIVersion
API rule violation: names_match,github.com/tektoncd/triggers/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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: Why do we need to this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I literally just copied this over from Pipelines - I assume it does something relating to swagger.json.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah ok. was curious as to why we need to have this custom function.

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 github.com/tektoncd/triggers/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