Skip to content

Commit

Permalink
Fix domain resource issues in controller
Browse files Browse the repository at this point in the history
bind resource for controller based on whether resource is created.
1) If resource is created in project, bind with created resource.
2) If resource not created, gkv matches core types, bind with core-types.
3) Else, using specified resourcePath by flag --resource-pkg-path (TODO)
  • Loading branch information
fanzhangio committed Aug 2, 2018
1 parent 4642040 commit 952ac6d
Show file tree
Hide file tree
Showing 17 changed files with 622 additions and 16 deletions.
1 change: 1 addition & 0 deletions generated_golden.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ ln -s ../vendor vendor
../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
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.k8s.io
spec:
group: policy.k8s.io
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
12 changes: 12 additions & 0 deletions test/config/rbac/rbac_role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ rules:
- update
- patch
- delete
- apiGroups:
- policy.k8s.io
resources:
- healthcheckpolicies
verbs:
- get
- list
- watch
- create
- update
- patch
- delete
- apiGroups:
- creatures.k8s.io
resources:
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.k8s.io/v1beta1
kind: HealthCheckPolicy
metadata:
labels:
controller-tools.k8s.io: "1.0"
name: healthcheckpolicy-sample
spec:
# Add fields here
foo: bar
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)
}
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.k8s.io
package v1beta1
64 changes: 64 additions & 0 deletions test/pkg/apis/policy/v1beta1/healthcheckpolicy_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
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

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

// HealthCheckPolicySpec defines the desired state of HealthCheckPolicy
type HealthCheckPolicySpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "make" to regenerate code after modifying this file
}

// HealthCheckPolicyStatus defines the observed state of HealthCheckPolicy
type HealthCheckPolicyStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file
}

// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +genclient:nonNamespaced

// HealthCheckPolicy is the Schema for the healthcheckpolicies API
// +k8s:openapi-gen=true
type HealthCheckPolicy struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec HealthCheckPolicySpec `json:"spec,omitempty"`
Status HealthCheckPolicyStatus `json:"status,omitempty"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +genclient:nonNamespaced

// HealthCheckPolicyList contains a list of HealthCheckPolicy
type HealthCheckPolicyList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []HealthCheckPolicy `json:"items"`
}

func init() {
SchemeBuilder.Register(&HealthCheckPolicy{}, &HealthCheckPolicyList{})
}
51 changes: 51 additions & 0 deletions test/pkg/apis/policy/v1beta1/healthcheckpolicy_types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
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

import (
"testing"

"github.com/onsi/gomega"
"golang.org/x/net/context"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)

func TestStorageHealthCheckPolicy(t *testing.T) {
key := types.NamespacedName{Name: "foo", Namespace: "default"}
created := &HealthCheckPolicy{ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "default"}}
g := gomega.NewGomegaWithT(t)

// Test Create
fetched := &HealthCheckPolicy{}
g.Expect(c.Create(context.TODO(), created)).NotTo(gomega.HaveOccurred())

g.Expect(c.Get(context.TODO(), key, fetched)).NotTo(gomega.HaveOccurred())
g.Expect(fetched).To(gomega.Equal(created))

// Test Updating the Labels
updated := fetched.DeepCopy()
updated.Labels = map[string]string{"hello": "world"}
g.Expect(c.Update(context.TODO(), updated)).NotTo(gomega.HaveOccurred())

g.Expect(c.Get(context.TODO(), key, fetched)).NotTo(gomega.HaveOccurred())
g.Expect(fetched).To(gomega.Equal(updated))

// Test Delete
g.Expect(c.Delete(context.TODO(), fetched)).NotTo(gomega.HaveOccurred())
g.Expect(c.Get(context.TODO(), key, fetched)).To(gomega.HaveOccurred())
}
38 changes: 38 additions & 0 deletions test/pkg/apis/policy/v1beta1/register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
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.
*/

// NOTE: Boilerplate only. Ignore this file.

// 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.k8s.io
package v1beta1

import (
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/runtime/scheme"
)

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

// SchemeBuilder is used to add go types to the GroupVersionKind scheme
SchemeBuilder = &scheme.Builder{GroupVersion: SchemeGroupVersion}
)
Loading

0 comments on commit 952ac6d

Please sign in to comment.