From e0eded58e5d85d5b6b59cddd2946a172a032ba48 Mon Sep 17 00:00:00 2001 From: majian <46617237@qq.com> Date: Thu, 9 Sep 2021 09:28:31 +0800 Subject: [PATCH] Feat: support code-generator and sync to kubevela-core-api (#2174) --- .github/workflows/sync-api.yml | 7 +- .../v1beta1/appdeployment_types.go | 3 + .../core.oam.dev/v1beta1/application_types.go | 3 + .../v1beta1/applicationrevision_types.go | 3 + apis/core.oam.dev/v1beta1/approllout_types.go | 3 + apis/core.oam.dev/v1beta1/cluster_types.go | 3 + .../v1beta1/componentdefinition_types.go | 3 + apis/core.oam.dev/v1beta1/core_types.go | 12 ++++ .../v1beta1/definitionrevision_types.go | 3 + apis/core.oam.dev/v1beta1/initializer_type.go | 3 + .../core.oam.dev/v1beta1/policy_definition.go | 3 + apis/core.oam.dev/v1beta1/register.go | 8 +++ .../v1beta1/workflow_step_definition.go | 3 + hack/apis/clientgen.sh | 71 +++++++++++++++++++ hack/apis/sync.sh | 11 +++ 15 files changed, 136 insertions(+), 3 deletions(-) create mode 100755 hack/apis/clientgen.sh diff --git a/.github/workflows/sync-api.yml b/.github/workflows/sync-api.yml index 425db987c..efb181d51 100644 --- a/.github/workflows/sync-api.yml +++ b/.github/workflows/sync-api.yml @@ -11,10 +11,10 @@ jobs: sync-core-api: runs-on: ubuntu-20.04 steps: - - name: Set up Go 1.14 + - name: Set up Go 1.16 uses: actions/setup-go@v1 env: - GO_VERSION: '1.14' + GO_VERSION: '1.16' GOLANGCI_VERSION: 'v1.38' with: go-version: ${{ env.GO_VERSION }} @@ -33,4 +33,5 @@ jobs: VERSION: ${{ steps.get_version.outputs.VERSION }} COMMIT_ID: ${{ github.sha }} run: | - bash ./hack/apis/sync.sh \ No newline at end of file + bash ./hack/apis/clientgen.sh + bash ./hack/apis/sync.sh diff --git a/apis/core.oam.dev/v1beta1/appdeployment_types.go b/apis/core.oam.dev/v1beta1/appdeployment_types.go index b56619d13..808692df9 100644 --- a/apis/core.oam.dev/v1beta1/appdeployment_types.go +++ b/apis/core.oam.dev/v1beta1/appdeployment_types.go @@ -156,6 +156,8 @@ type AppDeploymentStatus struct { // +kubebuilder:object:root=true // +kubebuilder:resource:categories={oam},shortName=appdeploy // +kubebuilder:subresource:status +// +genclient +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object type AppDeployment struct { metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata,omitempty"` @@ -166,6 +168,7 @@ type AppDeployment struct { // AppDeploymentList contains a list of AppDeployment // +kubebuilder:object:root=true +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object type AppDeploymentList struct { metav1.TypeMeta `json:",inline"` metav1.ListMeta `json:"metadata,omitempty"` diff --git a/apis/core.oam.dev/v1beta1/application_types.go b/apis/core.oam.dev/v1beta1/application_types.go index a6e00ea31..2d0bc65e9 100644 --- a/apis/core.oam.dev/v1beta1/application_types.go +++ b/apis/core.oam.dev/v1beta1/application_types.go @@ -119,6 +119,8 @@ type ApplicationSpec struct { // +kubebuilder:printcolumn:name="HEALTHY",type=boolean,JSONPath=`.status.services[*].healthy` // +kubebuilder:printcolumn:name="STATUS",type=string,JSONPath=`.status.services[*].message` // +kubebuilder:printcolumn:name="AGE",type=date,JSONPath=".metadata.creationTimestamp" +// +genclient +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object type Application struct { metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata,omitempty"` @@ -130,6 +132,7 @@ type Application struct { // +kubebuilder:object:root=true // ApplicationList contains a list of Application +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object type ApplicationList struct { metav1.TypeMeta `json:",inline"` metav1.ListMeta `json:"metadata,omitempty"` diff --git a/apis/core.oam.dev/v1beta1/applicationrevision_types.go b/apis/core.oam.dev/v1beta1/applicationrevision_types.go index 21a4b9759..e9230fa7a 100644 --- a/apis/core.oam.dev/v1beta1/applicationrevision_types.go +++ b/apis/core.oam.dev/v1beta1/applicationrevision_types.go @@ -62,6 +62,8 @@ type ApplicationRevisionSpec struct { // +kubebuilder:storageversion // +kubebuilder:resource:categories={oam},shortName=apprev // +kubebuilder:printcolumn:name="AGE",type=date,JSONPath=".metadata.creationTimestamp" +// +genclient +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object type ApplicationRevision struct { metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata,omitempty"` @@ -72,6 +74,7 @@ type ApplicationRevision struct { // +kubebuilder:object:root=true // ApplicationRevisionList contains a list of ApplicationRevision +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object type ApplicationRevisionList struct { metav1.TypeMeta `json:",inline"` metav1.ListMeta `json:"metadata,omitempty"` diff --git a/apis/core.oam.dev/v1beta1/approllout_types.go b/apis/core.oam.dev/v1beta1/approllout_types.go index 6476f9eb4..da0b69092 100644 --- a/apis/core.oam.dev/v1beta1/approllout_types.go +++ b/apis/core.oam.dev/v1beta1/approllout_types.go @@ -61,6 +61,8 @@ type AppRolloutSpec struct { // +kubebuilder:printcolumn:name="BATCH-STATE",type=string,JSONPath=`.status.batchRollingState` // +kubebuilder:printcolumn:name="ROLLING-STATE",type=string,JSONPath=`.status.rollingState` // +kubebuilder:printcolumn:name="AGE",type=date,JSONPath=".metadata.creationTimestamp" +// +genclient +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object type AppRollout struct { metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata,omitempty"` @@ -71,6 +73,7 @@ type AppRollout struct { // AppRolloutList contains a list of AppRollout // +kubebuilder:object:root=true +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object type AppRolloutList struct { metav1.TypeMeta `json:",inline"` metav1.ListMeta `json:"metadata,omitempty"` diff --git a/apis/core.oam.dev/v1beta1/cluster_types.go b/apis/core.oam.dev/v1beta1/cluster_types.go index 2a14b110e..8532823da 100644 --- a/apis/core.oam.dev/v1beta1/cluster_types.go +++ b/apis/core.oam.dev/v1beta1/cluster_types.go @@ -41,6 +41,8 @@ type ClusterStatus struct { } // +kubebuilder:object:root=true +// +genclient +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object // Cluster is the Schema for the clusters API type Cluster struct { @@ -52,6 +54,7 @@ type Cluster struct { } // +kubebuilder:object:root=true +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object // ClusterList contains a list of Cluster type ClusterList struct { diff --git a/apis/core.oam.dev/v1beta1/componentdefinition_types.go b/apis/core.oam.dev/v1beta1/componentdefinition_types.go index cf853b5e7..f12476f04 100644 --- a/apis/core.oam.dev/v1beta1/componentdefinition_types.go +++ b/apis/core.oam.dev/v1beta1/componentdefinition_types.go @@ -76,6 +76,8 @@ type ComponentDefinitionStatus struct { // +kubebuilder:subresource:status // +kubebuilder:printcolumn:name="WORKLOAD-KIND",type=string,JSONPath=".spec.workload.definition.kind" // +kubebuilder:printcolumn:name="DESCRIPTION",type=string,JSONPath=".metadata.annotations.definition\\.oam\\.dev/description" +// +genclient +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object type ComponentDefinition struct { metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata,omitempty"` @@ -95,6 +97,7 @@ func (cd *ComponentDefinition) GetCondition(conditionType condition.ConditionTyp } // +kubebuilder:object:root=true +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object // ComponentDefinitionList contains a list of ComponentDefinition type ComponentDefinitionList struct { diff --git a/apis/core.oam.dev/v1beta1/core_types.go b/apis/core.oam.dev/v1beta1/core_types.go index 76a99f679..3a7f819f5 100644 --- a/apis/core.oam.dev/v1beta1/core_types.go +++ b/apis/core.oam.dev/v1beta1/core_types.go @@ -73,6 +73,8 @@ type WorkloadDefinitionStatus struct { // +kubebuilder:storageversion // +kubebuilder:printcolumn:name="DEFINITION-NAME",type=string,JSONPath=".spec.definitionRef.name" // +kubebuilder:printcolumn:name="DESCRIPTION",type=string,JSONPath=".metadata.annotations.definition\\.oam\\.dev/description" +// +genclient +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object type WorkloadDefinition struct { metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata,omitempty"` @@ -94,6 +96,7 @@ func (wd *WorkloadDefinition) GetCondition(conditionType condition.ConditionType // +kubebuilder:object:root=true // WorkloadDefinitionList contains a list of WorkloadDefinition. +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object type WorkloadDefinitionList struct { metav1.TypeMeta `json:",inline"` metav1.ListMeta `json:"metadata,omitempty"` @@ -179,6 +182,8 @@ type TraitDefinitionStatus struct { // +kubebuilder:storageversion // +kubebuilder:printcolumn:name="APPLIES-TO",type=string,JSONPath=".spec.appliesToWorkloads" // +kubebuilder:printcolumn:name="DESCRIPTION",type=string,JSONPath=".metadata.annotations.definition\\.oam\\.dev/description" +// +genclient +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object type TraitDefinition struct { metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata,omitempty"` @@ -198,6 +203,7 @@ func (td *TraitDefinition) GetCondition(conditionType condition.ConditionType) c } // +kubebuilder:object:root=true +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object // TraitDefinitionList contains a list of TraitDefinition. type TraitDefinitionList struct { @@ -233,6 +239,8 @@ type ScopeDefinitionSpec struct { // +kubebuilder:printcolumn:JSONPath=".spec.definitionRef.name",name=DEFINITION-NAME,type=string // +kubebuilder:resource:scope=Namespaced,categories={oam},shortName=scope // +kubebuilder:storageversion +// +genclient +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object type ScopeDefinition struct { metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata,omitempty"` @@ -241,6 +249,7 @@ type ScopeDefinition struct { } // +kubebuilder:object:root=true +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object // ScopeDefinitionList contains a list of ScopeDefinition. type ScopeDefinitionList struct { @@ -251,6 +260,8 @@ type ScopeDefinitionList struct { // +kubebuilder:object:root=true // +kubebuilder:subresource:status +// +genclient +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object // An ResourceTracker represents a tracker for track cross namespace resources // +kubebuilder:resource:scope=Cluster,categories={oam},shortName=tracker @@ -267,6 +278,7 @@ type ResourceTrackerStatus struct { } // +kubebuilder:object:root=true +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object // ResourceTrackerList contains a list of ResourceTracker type ResourceTrackerList struct { diff --git a/apis/core.oam.dev/v1beta1/definitionrevision_types.go b/apis/core.oam.dev/v1beta1/definitionrevision_types.go index 4439fde7e..33984445b 100644 --- a/apis/core.oam.dev/v1beta1/definitionrevision_types.go +++ b/apis/core.oam.dev/v1beta1/definitionrevision_types.go @@ -53,6 +53,8 @@ type DefinitionRevisionSpec struct { // +kubebuilder:printcolumn:name="REVISION",type=integer,JSONPath=".spec.revision" // +kubebuilder:printcolumn:name="HASH",type=string,JSONPath=".spec.revisionHash" // +kubebuilder:printcolumn:name="TYPE",type=string,JSONPath=".spec.definitionType" +// +genclient +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object type DefinitionRevision struct { metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata,omitempty"` @@ -61,6 +63,7 @@ type DefinitionRevision struct { } // +kubebuilder:object:root=true +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object // DefinitionRevisionList contains a list of DefinitionRevision type DefinitionRevisionList struct { diff --git a/apis/core.oam.dev/v1beta1/initializer_type.go b/apis/core.oam.dev/v1beta1/initializer_type.go index d4417f778..1d641ff6b 100644 --- a/apis/core.oam.dev/v1beta1/initializer_type.go +++ b/apis/core.oam.dev/v1beta1/initializer_type.go @@ -69,6 +69,8 @@ type InitializerStatus struct { // +kubebuilder:resource:scope=Namespaced,categories={oam},shortName=init // +kubebuilder:printcolumn:name="PHASE",type=string,JSONPath=`.status.status` // +kubebuilder:printcolumn:name="AGE",type=date,JSONPath=".metadata.creationTimestamp" +// +genclient +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object type Initializer struct { metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata,omitempty"` @@ -78,6 +80,7 @@ type Initializer struct { } // +kubebuilder:object:root=true +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object // InitializerList contains a list of Initializer. type InitializerList struct { diff --git a/apis/core.oam.dev/v1beta1/policy_definition.go b/apis/core.oam.dev/v1beta1/policy_definition.go index c4b437b61..c4c4e2fff 100644 --- a/apis/core.oam.dev/v1beta1/policy_definition.go +++ b/apis/core.oam.dev/v1beta1/policy_definition.go @@ -64,6 +64,8 @@ func (d *PolicyDefinition) GetCondition(conditionType condition.ConditionType) c // +kubebuilder:resource:scope=Namespaced,categories={oam},shortName=policy // +kubebuilder:storageversion // +kubebuilder:subresource:status +// +genclient +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object type PolicyDefinition struct { metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata,omitempty"` @@ -73,6 +75,7 @@ type PolicyDefinition struct { } // +kubebuilder:object:root=true +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object // PolicyDefinitionList contains a list of PolicyDefinition type PolicyDefinitionList struct { diff --git a/apis/core.oam.dev/v1beta1/register.go b/apis/core.oam.dev/v1beta1/register.go index 2b07d5916..a13640f16 100644 --- a/apis/core.oam.dev/v1beta1/register.go +++ b/apis/core.oam.dev/v1beta1/register.go @@ -35,6 +35,9 @@ var ( // SchemeBuilder is used to add go types to the GroupVersionKind scheme SchemeBuilder = &scheme.Builder{GroupVersion: SchemeGroupVersion} + + // AddToScheme is a global function that registers this API group & version to a scheme + AddToScheme = SchemeBuilder.AddToScheme ) // ComponentDefinition type metadata. @@ -165,3 +168,8 @@ func init() { SchemeBuilder.Register(&ResourceTracker{}, &ResourceTrackerList{}) SchemeBuilder.Register(&Initializer{}, &InitializerList{}) } + +// Resource takes an unqualified resource and returns a Group qualified GroupResource +func Resource(resource string) schema.GroupResource { + return SchemeGroupVersion.WithResource(resource).GroupResource() +} diff --git a/apis/core.oam.dev/v1beta1/workflow_step_definition.go b/apis/core.oam.dev/v1beta1/workflow_step_definition.go index 0664e9585..a2518fd72 100644 --- a/apis/core.oam.dev/v1beta1/workflow_step_definition.go +++ b/apis/core.oam.dev/v1beta1/workflow_step_definition.go @@ -60,6 +60,8 @@ func (d *WorkflowStepDefinition) GetCondition(conditionType condition.ConditionT // +kubebuilder:resource:scope=Namespaced,categories={oam},shortName=workflowstep // +kubebuilder:storageversion // +kubebuilder:subresource:status +// +genclient +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object type WorkflowStepDefinition struct { metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata,omitempty"` @@ -69,6 +71,7 @@ type WorkflowStepDefinition struct { } // +kubebuilder:object:root=true +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object // WorkflowStepDefinitionList contains a list of WorkflowStepDefinition type WorkflowStepDefinitionList struct { diff --git a/hack/apis/clientgen.sh b/hack/apis/clientgen.sh new file mode 100755 index 000000000..eb4c30614 --- /dev/null +++ b/hack/apis/clientgen.sh @@ -0,0 +1,71 @@ +#!/bin/bash -l +# +# Copyright 2021. The KubeVela 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 -e + +cd "$(dirname "${BASH_SOURCE[0]}")/../.." + +WORK_TEMP_DIR="./clientgen_work_temp" + +# client generator parameters +CODEGEN_GENERATORS="all" # deepcopy,defaulter,client,lister,informer or all +OUTPUT_PACKAGE="github.com/oam-dev/kubevela/pkg/generated/client" +APIS_PACKAGE="github.com/oam-dev/kubevela/apis" +CODEGEN_GROUP_VERSIONS="core.oam.dev:v1beta1" +OUTPUT_DIR="${WORK_TEMP_DIR}" +BOILERPLATE_FILE="./hack/boilerplate.go.txt" + +installDep() { + cp go.mod go.sum "${WORK_TEMP_DIR}/backup/" + + cat <"${WORK_TEMP_DIR}/tools.go" +// +build tools + +package tools + +import _ "k8s.io/code-generator" +EOF + go mod vendor +} + +clientGen() { + bash ./vendor/k8s.io/code-generator/generate-groups.sh "${CODEGEN_GENERATORS}" \ + ${OUTPUT_PACKAGE} \ + ${APIS_PACKAGE} \ + "${CODEGEN_GROUP_VERSIONS}" \ + --output-base "${OUTPUT_DIR}" \ + --go-header-file "${BOILERPLATE_FILE}" + + mkdir -p ./pkg/generated/ + mv "${WORK_TEMP_DIR}/github.com/oam-dev/kubevela/pkg/generated/client" ./pkg/generated/ +} + +cleanup() { + mv "${WORK_TEMP_DIR}/backup/"* ./ + rm -drf "${WORK_TEMP_DIR}/" + rm -drf vendor +} + +main() { + mkdir -p "${WORK_TEMP_DIR}/backup/" + + installDep + clientGen + cleanup +} + +main diff --git a/hack/apis/sync.sh b/hack/apis/sync.sh index 5a903d5f8..8d8cd3436 100644 --- a/hack/apis/sync.sh +++ b/hack/apis/sync.sh @@ -36,12 +36,23 @@ rm -r kubevela-core-api/apis/* echo "clear kubevela-core-api pkg/oam" rm -r kubevela-core-api/pkg/oam/* +echo "clear kubevela-core-api pkg/generated/client" +if [[ -d "kubevela-core-api/pkg/generated/client/" ]] +then + rm -r kubevela-core-api/pkg/generated/client/* +else + mkdir -p kubevela-core-api/pkg/generated/client/ +fi + echo "update kubevela-core-api api/" cp -R kubevela/apis/* kubevela-core-api/apis/ echo "update kubevela-core-api pkg/oam" cp -R kubevela/pkg/oam/* kubevela-core-api/pkg/oam/ +echo "update kubevela-core-api pkg/generated/client" +cp -R kubevela/pkg/generated/client/* kubevela-core-api/pkg/generated/client/ + echo "change import path" find ./kubevela-core-api -type f -name "*.go" -print0 | xargs -0 sed -i 's|github.com/oam-dev/kubevela/|github.com/oam-dev/kubevela-core-api/|g'