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

Generate OpenAPI specification and Swagger file #3081

Merged
merged 1 commit into from
Oct 30, 2020
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
79 changes: 79 additions & 0 deletions cmd/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"

"github.com/go-openapi/spec"
"k8s.io/klog"
"k8s.io/kube-openapi/pkg/common"
)

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.Replace(name, "./pkg/apis/pipeline/", "", -1)
name = strings.Replace(name, "./pkg/apis/resource/", "", -1)
name = strings.Replace(name, "github.com/tektoncd/pipeline/pkg/apis/pipeline/", "", -1)
name = strings.Replace(name, "github.com/tektoncd/pipeline/pkg/apis/resource/", "", -1)
name = strings.Replace(name, "k8s.io/api/core/", "", -1)
name = strings.Replace(name, "k8s.io/apimachinery/pkg/apis/meta/", "", -1)
name = strings.Replace(name, "knative.dev/pkg/apis.", "knative/", -1)
name = strings.Replace(name, "knative.dev/pkg/apis/duck/v1beta1.", "knative/", -1)
name = strings.Replace(name, "/", ".", -1)
return name
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/GoogleCloudPlatform/cloud-builders/gcs-fetcher v0.0.0-20191203181535-308b93ad1f39
github.com/cloudevents/sdk-go/v2 v2.1.0
github.com/ghodss/yaml v1.0.0
github.com/go-openapi/spec v0.19.6
github.com/google/go-cmp v0.5.2
github.com/google/go-containerregistry v0.1.2
github.com/google/uuid v1.1.1
Expand Down
1 change: 1 addition & 0 deletions hack/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Tekton Pipelines.
- [`update-codegen.sh`](./update-codegen.sh): Updates auto-generated client
libraries.
- [`update-deps.sh`](./update-deps.sh): Updates Go dependencies.
- [`update-openapigen.sh`](./update-openapigen.sh): Updates OpenAPI specification and Swagger file.
- [`verify-codegen.sh`](./verify-codegen.sh): Verifies that auto-generated
client libraries are up-to-date.
- Release docs have been moved to [the top-level `tekton` dir](../tekton)
3 changes: 3 additions & 0 deletions hack/update-codegen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,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
28 changes: 28 additions & 0 deletions hack/update-openapigen.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/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

echo "Generating OpenAPI specification ..."
go run k8s.io/kube-openapi/cmd/openapi-gen \
--input-dirs ./pkg/apis/pipeline/v1beta1,./pkg/apis/pipeline/pod,./pkg/apis/resource/v1alpha1,knative.dev/pkg/apis,knative.dev/pkg/apis/duck/v1beta1 \
--output-package ./pkg/apis/pipeline/v1beta1 -o ./ \
--go-header-file hack/boilerplate/boilerplate.go.txt >> /dev/null

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

1 change: 1 addition & 0 deletions pkg/apis/pipeline/pod/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

// +k8s:openapi-gen=true
package pod
3 changes: 3 additions & 0 deletions pkg/apis/pipeline/pod/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

// PodTemplate holds pod specific configuration
// +k8s:deepcopy-gen=true
// +k8s:openapi-gen=true
type Template struct {
// NodeSelector is a selector which must be true for the pod to fit on a node.
// Selector which must match a node's labels for the pod to be scheduled on that node.
Expand All @@ -47,6 +48,8 @@ type Template struct {
// List of volumes that can be mounted by containers belonging to the pod.
// More info: https://kubernetes.io/docs/concepts/storage/volumes
// +optional
// +patchMergeKey=name
// +patchStrategy=merge,retainKeys
Volumes []corev1.Volume `json:"volumes,omitempty" patchStrategy:"merge,retainKeys" patchMergeKey:"name" protobuf:"bytes,1,rep,name=volumes"`

// RuntimeClassName refers to a RuntimeClass object in the node.k8s.io
Expand Down
Loading