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

Fix domain resource issues in controller #54

Merged
merged 1 commit into from
Aug 3, 2018
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
3 changes: 2 additions & 1 deletion generated_golden.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ go build -o ./bin/controller-scaffold sigs.k8s.io/controller-tools/cmd/controll
rm -rf ./test/*
cd test
ln -s ../vendor vendor
../bin/controller-scaffold project --domain k8s.io --controller-tools-path ".." --license apache2 --owner "The Kubernetes authors" --dep=false
../bin/controller-scaffold project --domain testproject.org --controller-tools-path ".." --license apache2 --owner "The Kubernetes authors" --dep=false
../bin/controller-scaffold api --group crew --version v1 --kind FirstMate --controller=true --resource=true --make=false
../bin/controller-scaffold api --group ship --version v1beta1 --kind Frigate --example=false --controller=true --resource=true --make=false
../bin/controller-scaffold api --group creatures --version v2alpha1 --kind Kraken --namespaced=false --example=false --controller=true --resource=true --make=false
../bin/controller-scaffold api --group core --version v1 --kind Namespace --example=false --controller=true --resource=false --namespaced=false
../bin/controller-scaffold api --group policy --version v1beta1 --kind HealthCheckPolicy --example=false --controller=true --resource=true --namespaced=false
make
rm -rf ./bin/
cd -
31 changes: 21 additions & 10 deletions pkg/scaffold/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package controller

import (
"fmt"
"os"
"path"
"path/filepath"
"strings"
Expand Down Expand Up @@ -61,16 +63,8 @@ func (a *Controller) GetInput() (input.Input, error) {
"rbac.authorization": "k8s.io",
"storage": "k8s.io",
}
if domain, found := coreGroups[a.Resource.Group]; found {
a.ResourcePackage = path.Join("k8s.io", "api")
a.GroupDomain = a.Resource.Group
if domain != "" {
a.GroupDomain = a.Resource.Group + "." + domain
}
} else {
a.ResourcePackage = path.Join(a.Repo, "pkg", "apis")
a.GroupDomain = a.Resource.Group + "." + a.Domain
}

a.ResourcePackage, a.GroupDomain = getResourceInfo(coreGroups, a.Resource, a.Input)

if a.Plural == "" {
rs := inflect.NewDefaultRuleset()
Expand All @@ -87,6 +81,23 @@ func (a *Controller) GetInput() (input.Input, error) {
return a.Input, nil
}

func getResourceInfo(coreGroups map[string]string, r *resource.Resource, in input.Input) (resourcePackage, groupDomain string) {
resourcePath := filepath.Join("pkg", "apis", r.Group, r.Version,
fmt.Sprintf("%s_types.go", strings.ToLower(r.Kind)))
if _, err := os.Stat(resourcePath); os.IsNotExist(err) {
if domain, found := coreGroups[r.Group]; found {
resourcePackage := path.Join("k8s.io", "api")
groupDomain = r.Group
if domain != "" {
groupDomain = r.Group + "." + domain
}
return resourcePackage, groupDomain
}
// TODO: need to support '--resource-pkg-path' flag for specifying resourcePath
}
return path.Join(in.Repo, "pkg", "apis"), r.Group + "." + in.Domain
}

var controllerTemplate = `{{ .Boilerplate }}

package {{ lower .Resource.Kind }}
Expand Down
8 changes: 2 additions & 6 deletions pkg/scaffold/controller/controllertest.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package controller

import (
"path"
"path/filepath"
"strings"

Expand Down Expand Up @@ -59,11 +58,8 @@ func (a *Test) GetInput() (input.Input, error) {
"rbac.authorization": "k8s.io",
"storage": "k8s.io",
}
if _, found := coreGroups[a.Resource.Group]; found {
a.ResourcePackage = path.Join("k8s.io", "api")
} else {
a.ResourcePackage = path.Join(a.Repo, "pkg", "apis")
}

a.ResourcePackage, _ = getResourceInfo(coreGroups, a.Resource, a.Input)

a.TemplateBody = controllerTestTemplate
a.Input.IfExistsAction = input.Error
Expand Down
2 changes: 1 addition & 1 deletion pkg/scaffold/project/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ Copyright 2019 Example Owners.
It("should match the golden file", func() {
instance := &Project{}
instance.Version = "2"
instance.Domain = "k8s.io"
instance.Domain = "testproject.org"
instance.Repo = "sigs.k8s.io/controller-tools/test"
Expect(s.Execute(input.Options{}, instance)).NotTo(HaveOccurred())

Expand Down
2 changes: 1 addition & 1 deletion test/PROJECT
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
version: "2"
domain: k8s.io
domain: testproject.org
repo: sigs.k8s.io/controller-tools/test
4 changes: 2 additions & 2 deletions test/config/crds/creatures_v2alpha1_kraken.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ metadata:
creationTimestamp: null
labels:
controller-tools.k8s.io: "1.0"
name: krakens.creatures.k8s.io
name: krakens.creatures.testproject.org
spec:
group: creatures.k8s.io
group: creatures.testproject.org
names:
kind: Kraken
plural: krakens
Expand Down
4 changes: 2 additions & 2 deletions test/config/crds/crew_v1_firstmate.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ metadata:
creationTimestamp: null
labels:
controller-tools.k8s.io: "1.0"
name: firstmates.crew.k8s.io
name: firstmates.crew.testproject.org
spec:
group: crew.k8s.io
group: crew.testproject.org
names:
kind: FirstMate
plural: firstmates
Expand Down
33 changes: 33 additions & 0 deletions test/config/crds/policy_v1beta1_healthcheckpolicy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
creationTimestamp: null
labels:
controller-tools.k8s.io: "1.0"
name: healthcheckpolicies.policy.testproject.org
spec:
group: policy.testproject.org
names:
kind: HealthCheckPolicy
plural: healthcheckpolicies
scope: Cluster
validation:
openAPIV3Schema:
properties:
apiVersion:
type: string
kind:
type: string
metadata:
type: object
spec:
type: object
status:
type: object
type: object
version: v1beta1
status:
acceptedNames:
kind: ""
plural: ""
conditions: null
4 changes: 2 additions & 2 deletions test/config/crds/ship_v1beta1_frigate.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ metadata:
creationTimestamp: null
labels:
controller-tools.k8s.io: "1.0"
name: frigates.ship.k8s.io
name: frigates.ship.testproject.org
spec:
group: ship.k8s.io
group: ship.testproject.org
names:
kind: Frigate
plural: frigates
Expand Down
18 changes: 15 additions & 3 deletions test/config/rbac/rbac_role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ rules:
- patch
- delete
- apiGroups:
- crew.k8s.io
- crew.testproject.org
resources:
- firstmates
verbs:
Expand All @@ -29,7 +29,7 @@ rules:
- patch
- delete
- apiGroups:
- ship.k8s.io
- ship.testproject.org
resources:
- frigates
verbs:
Expand All @@ -41,7 +41,19 @@ rules:
- patch
- delete
- apiGroups:
- creatures.k8s.io
- policy.testproject.org
resources:
- healthcheckpolicies
verbs:
- get
- list
- watch
- create
- update
- patch
- delete
- apiGroups:
- creatures.testproject.org
resources:
- krakens
verbs:
Expand Down
2 changes: 1 addition & 1 deletion test/config/samples/creatures_v2alpha1_kraken.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: creatures.k8s.io/v2alpha1
apiVersion: creatures.testproject.org/v2alpha1
kind: Kraken
metadata:
labels:
Expand Down
2 changes: 1 addition & 1 deletion test/config/samples/crew_v1_firstmate.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: crew.k8s.io/v1
apiVersion: crew.testproject.org/v1
kind: FirstMate
metadata:
labels:
Expand Down
9 changes: 9 additions & 0 deletions test/config/samples/policy_v1beta1_healthcheckpolicy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apiVersion: policy.testproject.org/v1beta1
kind: HealthCheckPolicy
metadata:
labels:
controller-tools.k8s.io: "1.0"
name: healthcheckpolicy-sample
spec:
# Add fields here
foo: bar
2 changes: 1 addition & 1 deletion test/config/samples/ship_v1beta1_frigate.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: ship.k8s.io/v1beta1
apiVersion: ship.testproject.org/v1beta1
kind: Frigate
metadata:
labels:
Expand Down
26 changes: 26 additions & 0 deletions test/pkg/apis/addtoscheme_policy_v1beta1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
Copyright 2018 The Kubernetes 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 apis

import (
"sigs.k8s.io/controller-tools/test/pkg/apis/policy/v1beta1"
)

func init() {
// Register the types with the Scheme so the components can map objects to GroupVersionKinds and back
AddToSchemes = append(AddToSchemes, v1beta1.SchemeBuilder.AddToScheme)
}
2 changes: 1 addition & 1 deletion test/pkg/apis/creatures/v2alpha1/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ limitations under the License.
// +k8s:deepcopy-gen=package,register
// +k8s:conversion-gen=sigs.k8s.io/controller-tools/test/pkg/apis/creatures
// +k8s:defaulter-gen=TypeMeta
// +groupName=creatures.k8s.io
// +groupName=creatures.testproject.org
package v2alpha1
4 changes: 2 additions & 2 deletions test/pkg/apis/creatures/v2alpha1/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ limitations under the License.
// +k8s:deepcopy-gen=package,register
// +k8s:conversion-gen=sigs.k8s.io/controller-tools/test/pkg/apis/creatures
// +k8s:defaulter-gen=TypeMeta
// +groupName=creatures.k8s.io
// +groupName=creatures.testproject.org
package v2alpha1

import (
Expand All @@ -31,7 +31,7 @@ import (

var (
// SchemeGroupVersion is group version used to register these objects
SchemeGroupVersion = schema.GroupVersion{Group: "creatures.k8s.io", Version: "v2alpha1"}
SchemeGroupVersion = schema.GroupVersion{Group: "creatures.testproject.org", Version: "v2alpha1"}

// SchemeBuilder is used to add go types to the GroupVersionKind scheme
SchemeBuilder = &scheme.Builder{GroupVersion: SchemeGroupVersion}
Expand Down
2 changes: 1 addition & 1 deletion test/pkg/apis/crew/v1/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ limitations under the License.
// +k8s:deepcopy-gen=package,register
// +k8s:conversion-gen=sigs.k8s.io/controller-tools/test/pkg/apis/crew
// +k8s:defaulter-gen=TypeMeta
// +groupName=crew.k8s.io
// +groupName=crew.testproject.org
package v1
4 changes: 2 additions & 2 deletions test/pkg/apis/crew/v1/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ limitations under the License.
// +k8s:deepcopy-gen=package,register
// +k8s:conversion-gen=sigs.k8s.io/controller-tools/test/pkg/apis/crew
// +k8s:defaulter-gen=TypeMeta
// +groupName=crew.k8s.io
// +groupName=crew.testproject.org
package v1

import (
Expand All @@ -31,7 +31,7 @@ import (

var (
// SchemeGroupVersion is group version used to register these objects
SchemeGroupVersion = schema.GroupVersion{Group: "crew.k8s.io", Version: "v1"}
SchemeGroupVersion = schema.GroupVersion{Group: "crew.testproject.org", Version: "v1"}

// SchemeBuilder is used to add go types to the GroupVersionKind scheme
SchemeBuilder = &scheme.Builder{GroupVersion: SchemeGroupVersion}
Expand Down
18 changes: 18 additions & 0 deletions test/pkg/apis/policy/group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
Copyright 2018 The Kubernetes 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 policy contains policy API versions
package policy
23 changes: 23 additions & 0 deletions test/pkg/apis/policy/v1beta1/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
Copyright 2018 The Kubernetes 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 v1beta1 contains API Schema definitions for the policy v1beta1 API group
// +k8s:openapi-gen=true
// +k8s:deepcopy-gen=package,register
// +k8s:conversion-gen=sigs.k8s.io/controller-tools/test/pkg/apis/policy
// +k8s:defaulter-gen=TypeMeta
// +groupName=policy.testproject.org
package v1beta1
Loading