Skip to content

Commit

Permalink
This commit updates the generation logic for *OperationsImpl to inclu…
Browse files Browse the repository at this point in the history
…de proper scoping (namespaced vs cluster-wide) for all parts using the rewritten go generator.

Changes:
- go configuration requires scoping definition for all CRDs
- marker interface 'Namespaced' added
- go generator adds the marker interface to all Namespaced CRDs
- during the generation of the *OperationsImpl, the marker interface is used to detect the proper scoping.

This fixes fabric8io#2193
  • Loading branch information
Fabian-K committed May 5, 2020
1 parent 55b576c commit d9376eb
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@
#if ($annotation.getClassRef().getName().equals("ApiGroup"))
#set ($apiGroupName = $annotation.getParameters().get("value"))
#end
#if ($annotation.getClassRef().getName().equals("Namespaced"))
#set ($isResourceNamespacedFlag = $annotation.getParameters().get("value"))
#end
#end

#set ($isResourceNamespacedFlag = false)
#foreach ($impl in $model.getImplementsList())
#if ($impl.getFullyQualifiedName().equals("io.fabric8.kubernetes.api.model.Namespaced"))
#set ($isResourceNamespacedFlag = true)
#end
#end

package io.fabric8.tekton.client.internal.$apiGroupVersion;

Expand Down
26 changes: 26 additions & 0 deletions extensions/tekton/generateModel.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#
# Copyright (C) 2015 Red Hat, Inc.
#
# 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.
#

declare -a modules=("generator-v1alpha1" "generator-v1beta1"
)

for module in ${modules[*]}
do
echo "Compiling ${module}"
cd $module
make
cd ..
done
16 changes: 8 additions & 8 deletions extensions/tekton/generator-v1alpha1/cmd/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ func main() {

// the CRD List types for which the model should be generated
// no other types need to be defined as they are auto discovered
crdLists := []reflect.Type{
crdLists := map[reflect.Type]schemagen.CrdScope{
// v1alpha1
reflect.TypeOf(v1alpha1.ConditionList{}),
reflect.TypeOf(v1alpha1.PipelineList{}),
reflect.TypeOf(v1alpha1.PipelineRunList{}),
reflect.TypeOf(v1alpha1.TaskList{}),
reflect.TypeOf(v1alpha1.TaskRunList{}),
reflect.TypeOf(v1alpha1.ClusterTaskList{}),
reflect.TypeOf(v1alpha1.ConditionList{}): schemagen.Namespaced,
reflect.TypeOf(v1alpha1.PipelineList{}): schemagen.Namespaced,
reflect.TypeOf(v1alpha1.PipelineRunList{}): schemagen.Namespaced,
reflect.TypeOf(v1alpha1.TaskList{}): schemagen.Namespaced,
reflect.TypeOf(v1alpha1.TaskRunList{}): schemagen.Namespaced,
reflect.TypeOf(v1alpha1.ClusterTaskList{}): schemagen.Cluster,

reflect.TypeOf(resource.PipelineResourceList{}),
reflect.TypeOf(resource.PipelineResourceList{}): schemagen.Namespaced,
}

// constraints and patterns for fields
Expand Down
12 changes: 6 additions & 6 deletions extensions/tekton/generator-v1beta1/cmd/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ func main() {

// the CRD List types for which the model should be generated
// no other types need to be defined as they are auto discovered
crdLists := []reflect.Type{
crdLists := map[reflect.Type]schemagen.CrdScope{
// v1beta1
reflect.TypeOf(v1beta1.PipelineList{}),
reflect.TypeOf(v1beta1.PipelineRunList{}),
reflect.TypeOf(v1beta1.TaskList{}),
reflect.TypeOf(v1beta1.TaskRunList{}),
reflect.TypeOf(v1beta1.ClusterTaskList{}),
reflect.TypeOf(v1beta1.PipelineList{}): schemagen.Namespaced,
reflect.TypeOf(v1beta1.PipelineRunList{}): schemagen.Namespaced,
reflect.TypeOf(v1beta1.TaskList{}): schemagen.Namespaced,
reflect.TypeOf(v1beta1.TaskRunList{}): schemagen.Namespaced,
reflect.TypeOf(v1beta1.ClusterTaskList{}): schemagen.Cluster,
}

// constraints and patterns for fields
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,8 @@
},
"javaType": "io.fabric8.tekton.pipeline.v1alpha1.Condition",
"javaInterfaces": [
"io.fabric8.kubernetes.api.model.HasMetadata"
"io.fabric8.kubernetes.api.model.HasMetadata",
"io.fabric8.kubernetes.api.model.Namespaced"
]
},
"github_com_tektoncd_pipeline_pkg_apis_pipeline_v1alpha1_ConditionCheckStatus": {
Expand Down Expand Up @@ -390,7 +391,8 @@
},
"javaType": "io.fabric8.tekton.pipeline.v1alpha1.Pipeline",
"javaInterfaces": [
"io.fabric8.kubernetes.api.model.HasMetadata"
"io.fabric8.kubernetes.api.model.HasMetadata",
"io.fabric8.kubernetes.api.model.Namespaced"
]
},
"github_com_tektoncd_pipeline_pkg_apis_pipeline_v1alpha1_PipelineList": {
Expand Down Expand Up @@ -549,7 +551,8 @@
},
"javaType": "io.fabric8.tekton.pipeline.v1alpha1.PipelineRun",
"javaInterfaces": [
"io.fabric8.kubernetes.api.model.HasMetadata"
"io.fabric8.kubernetes.api.model.HasMetadata",
"io.fabric8.kubernetes.api.model.Namespaced"
]
},
"github_com_tektoncd_pipeline_pkg_apis_pipeline_v1alpha1_PipelineRunConditionCheckStatus": {
Expand Down Expand Up @@ -944,7 +947,8 @@
},
"javaType": "io.fabric8.tekton.pipeline.v1alpha1.Task",
"javaInterfaces": [
"io.fabric8.kubernetes.api.model.HasMetadata"
"io.fabric8.kubernetes.api.model.HasMetadata",
"io.fabric8.kubernetes.api.model.Namespaced"
]
},
"github_com_tektoncd_pipeline_pkg_apis_pipeline_v1alpha1_TaskList": {
Expand Down Expand Up @@ -1090,7 +1094,8 @@
},
"javaType": "io.fabric8.tekton.pipeline.v1alpha1.TaskRun",
"javaInterfaces": [
"io.fabric8.kubernetes.api.model.HasMetadata"
"io.fabric8.kubernetes.api.model.HasMetadata",
"io.fabric8.kubernetes.api.model.Namespaced"
]
},
"github_com_tektoncd_pipeline_pkg_apis_pipeline_v1alpha1_TaskRunInputs": {
Expand Down Expand Up @@ -1821,7 +1826,8 @@
},
"javaType": "io.fabric8.tekton.resource.v1alpha1.PipelineResource",
"javaInterfaces": [
"io.fabric8.kubernetes.api.model.HasMetadata"
"io.fabric8.kubernetes.api.model.HasMetadata",
"io.fabric8.kubernetes.api.model.Namespaced"
]
},
"github_com_tektoncd_pipeline_pkg_apis_resource_v1alpha1_PipelineResourceList": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,8 @@
},
"javaType": "io.fabric8.tekton.pipeline.v1beta1.Pipeline",
"javaInterfaces": [
"io.fabric8.kubernetes.api.model.HasMetadata"
"io.fabric8.kubernetes.api.model.HasMetadata",
"io.fabric8.kubernetes.api.model.Namespaced"
]
},
"github_com_tektoncd_pipeline_pkg_apis_pipeline_v1beta1_PipelineDeclaredResource": {
Expand Down Expand Up @@ -454,7 +455,8 @@
},
"javaType": "io.fabric8.tekton.pipeline.v1beta1.PipelineRun",
"javaInterfaces": [
"io.fabric8.kubernetes.api.model.HasMetadata"
"io.fabric8.kubernetes.api.model.HasMetadata",
"io.fabric8.kubernetes.api.model.Namespaced"
]
},
"github_com_tektoncd_pipeline_pkg_apis_pipeline_v1beta1_PipelineRunConditionCheckStatus": {
Expand Down Expand Up @@ -1094,7 +1096,8 @@
},
"javaType": "io.fabric8.tekton.pipeline.v1beta1.Task",
"javaInterfaces": [
"io.fabric8.kubernetes.api.model.HasMetadata"
"io.fabric8.kubernetes.api.model.HasMetadata",
"io.fabric8.kubernetes.api.model.Namespaced"
]
},
"github_com_tektoncd_pipeline_pkg_apis_pipeline_v1beta1_TaskList": {
Expand Down Expand Up @@ -1290,7 +1293,8 @@
},
"javaType": "io.fabric8.tekton.pipeline.v1beta1.TaskRun",
"javaInterfaces": [
"io.fabric8.kubernetes.api.model.HasMetadata"
"io.fabric8.kubernetes.api.model.HasMetadata",
"io.fabric8.kubernetes.api.model.Namespaced"
]
},
"github_com_tektoncd_pipeline_pkg_apis_pipeline_v1beta1_TaskRunList": {
Expand Down
46 changes: 39 additions & 7 deletions generator/pkg/schemagen/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type PackageInformation struct {
}

type schemaGenerator struct {
crdLists []reflect.Type
crdLists map[reflect.Type]CrdScope
types map[reflect.Type]*JSONObjectDescriptor
providedPackages map[string]string
manualTypeMap map[reflect.Type]string
Expand All @@ -50,7 +50,14 @@ type Constraint struct {
Pattern string
}

func GenerateSchema(crdLists []reflect.Type, providedPackages map[string]string, manualTypeMap map[reflect.Type]string, packageMapping map[string]PackageInformation, mappingSchema map[string]string, providedTypes []ProvidedType, constraints map[reflect.Type]map[string]*Constraint) string {
type CrdScope int32

const (
Namespaced CrdScope = 0
Cluster CrdScope = 1
)

func GenerateSchema(crdLists map[reflect.Type]CrdScope, providedPackages map[string]string, manualTypeMap map[reflect.Type]string, packageMapping map[string]PackageInformation, mappingSchema map[string]string, providedTypes []ProvidedType, constraints map[reflect.Type]map[string]*Constraint) string {
g := newSchemaGenerator(crdLists, providedPackages, manualTypeMap, packageMapping, mappingSchema, providedTypes, constraints)
schema, err := g.generate(crdLists)

Expand All @@ -72,7 +79,7 @@ func GenerateSchema(crdLists []reflect.Type, providedPackages map[string]string,
return out.String()
}

func newSchemaGenerator(crdLists []reflect.Type, providedPackages map[string]string, manualTypeMap map[reflect.Type]string, packageMapping map[string]PackageInformation, mappingSchema map[string]string, providedTypes []ProvidedType, constraints map[reflect.Type]map[string]*Constraint) *schemaGenerator {
func newSchemaGenerator(crdLists map[reflect.Type]CrdScope, providedPackages map[string]string, manualTypeMap map[reflect.Type]string, packageMapping map[string]PackageInformation, mappingSchema map[string]string, providedTypes []ProvidedType, constraints map[reflect.Type]map[string]*Constraint) *schemaGenerator {
g := schemaGenerator{
crdLists: crdLists,
types: make(map[reflect.Type]*JSONObjectDescriptor),
Expand Down Expand Up @@ -212,6 +219,13 @@ func (g *schemaGenerator) javaType(t reflect.Type) string {
func (g *schemaGenerator) javaInterfaces(t reflect.Type) []string {

if g.isCRD(t) {

scope := g.crdScope(t)

if scope == Namespaced {
return []string{"io.fabric8.kubernetes.api.model.HasMetadata", "io.fabric8.kubernetes.api.model.Namespaced"}
}

return []string{"io.fabric8.kubernetes.api.model.HasMetadata"}
}

Expand All @@ -228,7 +242,7 @@ func (g *schemaGenerator) resourceListInterface(listType reflect.Type) string {
return "io.fabric8.kubernetes.api.model.KubernetesResourceList<" + g.javaType(itemType) + ">"
}

func (g *schemaGenerator) generate(crdLists []reflect.Type) (*JSONSchema, error) {
func (g *schemaGenerator) generate(crdLists map[reflect.Type]CrdScope) (*JSONSchema, error) {

s := JSONSchema{
ID: "http://fabric8.io/tekton/v1alpha1/TektonSchema#",
Expand All @@ -243,7 +257,7 @@ func (g *schemaGenerator) generate(crdLists []reflect.Type) (*JSONSchema, error)
},
}

for _, crd := range crdLists {
for crd, _ := range crdLists {
g.handleType(crd)
}

Expand Down Expand Up @@ -650,7 +664,7 @@ func (g *schemaGenerator) isCRD(t reflect.Type) bool {

typeName := t.PkgPath() + "." + t.Name() + "List"

for _, crd := range g.crdLists {
for crd, _ := range g.crdLists {
// provided are CRDList as an entry point
crdListName := crd.PkgPath() + "." + crd.Name()

Expand All @@ -663,11 +677,29 @@ func (g *schemaGenerator) isCRD(t reflect.Type) bool {
return false
}

func (g *schemaGenerator) crdScope(t reflect.Type) CrdScope {

typeName := t.PkgPath() + "." + t.Name() + "List"

for crd, scope := range g.crdLists {
// provided are CRDList as an entry point
crdListName := crd.PkgPath() + "." + crd.Name()

if typeName == crdListName {
return scope
}

}

panic("No CRD scope information for " + t.Name())

}

func (g *schemaGenerator) isCRDList(t reflect.Type) bool {

typeName := t.PkgPath() + "." + t.Name()

for _, crd := range g.crdLists {
for crd, _ := range g.crdLists {
// provided are CRDList as an entry point
crdListName := crd.PkgPath() + "." + crd.Name()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Copyright (C) 2015 Red Hat, Inc.
*
* 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 io.fabric8.kubernetes.api.model;

public interface Namespaced {
}

0 comments on commit d9376eb

Please sign in to comment.