Skip to content

Commit b972355

Browse files
committed
Scaffolding for sample controller
- kubebuilder init --domain k8s.io - kubebuilder create resoure --group samplecontroller --version v1alpha1 --kind Foo
1 parent f596bcb commit b972355

File tree

40 files changed

+2343
-0
lines changed

40 files changed

+2343
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
Copyright 2017 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
18+
package main
19+
20+
import (
21+
"flag"
22+
"log"
23+
24+
// Import auth/gcp to connect to GKE clusters remotely
25+
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
26+
27+
configlib "github.com/kubernetes-sigs/kubebuilder/pkg/config"
28+
"github.com/kubernetes-sigs/kubebuilder/pkg/inject/run"
29+
"github.com/kubernetes-sigs/kubebuilder/pkg/install"
30+
"github.com/kubernetes-sigs/kubebuilder/pkg/signals"
31+
extensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
32+
33+
"samplecontroller/pkg/inject"
34+
"samplecontroller/pkg/inject/args"
35+
)
36+
37+
var installCRDs = flag.Bool("install-crds", true, "install the CRDs used by the controller as part of startup")
38+
39+
// Controller-manager main.
40+
func main() {
41+
flag.Parse()
42+
43+
stopCh := signals.SetupSignalHandler()
44+
45+
config := configlib.GetConfigOrDie()
46+
47+
if *installCRDs {
48+
if err := install.NewInstaller(config).Install(&InstallStrategy{crds: inject.Injector.CRDs}); err != nil {
49+
log.Fatalf("Could not create CRDs: %v", err)
50+
}
51+
}
52+
53+
// Start the controllers
54+
if err := inject.RunAll(run.RunArguments{Stop: stopCh}, args.CreateInjectArgs(config)); err != nil {
55+
log.Fatalf("%v", err)
56+
}
57+
}
58+
59+
type InstallStrategy struct {
60+
install.EmptyInstallStrategy
61+
crds []*extensionsv1beta1.CustomResourceDefinition
62+
}
63+
64+
func (s *InstallStrategy) GetCRDs() []*extensionsv1beta1.CustomResourceDefinition {
65+
return s.crds
66+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
/*
3+
Copyright 2017 The Kubernetes Authors.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
18+
19+
//
20+
// +domain=k8s.io
21+
22+
package apis
23+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
/*
3+
Copyright 2017 The Kubernetes Authors.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
18+
19+
20+
// +k8s:deepcopy-gen=package,register
21+
// +groupName=samplecontroller.k8s.io
22+
23+
// Package api is the internal version of the API.
24+
package samplecontroller
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
/*
3+
Copyright 2017 The Kubernetes Authors.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
18+
19+
// Api versions allow the api contract for a resource to be changed while keeping
20+
// backward compatibility by support multiple concurrent versions
21+
// of the same resource
22+
23+
// +k8s:openapi-gen=true
24+
// +k8s:deepcopy-gen=package,register
25+
// +k8s:conversion-gen=samplecontroller/pkg/apis/samplecontroller
26+
// +k8s:defaulter-gen=TypeMeta
27+
// +groupName=samplecontroller.k8s.io
28+
package v1alpha1 // import "samplecontroller/pkg/apis/samplecontroller/v1alpha1"
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
/*
3+
Copyright 2017 The Kubernetes Authors.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
18+
19+
package v1alpha1
20+
21+
import (
22+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23+
)
24+
25+
// EDIT THIS FILE!
26+
// Created by "kubebuilder create resource" for you to implement the Foo resource schema definition
27+
// as a go struct.
28+
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.
29+
30+
// FooSpec defines the desired state of Foo
31+
type FooSpec struct {
32+
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
33+
}
34+
35+
// FooStatus defines the observed state of Foo
36+
type FooStatus struct {
37+
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
38+
}
39+
40+
// +genclient
41+
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
42+
43+
// Foo
44+
// +k8s:openapi-gen=true
45+
// +resource:path=foos
46+
type Foo struct {
47+
metav1.TypeMeta `json:",inline"`
48+
metav1.ObjectMeta `json:"metadata,omitempty"`
49+
50+
Spec FooSpec `json:"spec,omitempty"`
51+
Status FooStatus `json:"status,omitempty"`
52+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
2+
/*
3+
Copyright 2017 The Kubernetes Authors.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
18+
19+
package v1alpha1_test
20+
21+
import (
22+
. "github.com/onsi/ginkgo"
23+
. "github.com/onsi/gomega"
24+
25+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26+
27+
. "samplecontroller/pkg/apis/samplecontroller/v1alpha1"
28+
. "samplecontroller/pkg/client/clientset/versioned/typed/samplecontroller/v1alpha1"
29+
)
30+
31+
// EDIT THIS FILE!
32+
// Created by "kubebuilder create resource" for you to implement the Foo resource tests
33+
34+
var _ = Describe("Foo", func() {
35+
var instance Foo
36+
var expected Foo
37+
var client FooInterface
38+
39+
BeforeEach(func() {
40+
instance = Foo{}
41+
instance.Name = "instance-1"
42+
43+
expected = instance
44+
})
45+
46+
AfterEach(func() {
47+
client.Delete(instance.Name, &metav1.DeleteOptions{})
48+
})
49+
50+
// INSERT YOUR CODE HERE - add more "Describe" tests
51+
52+
// Automatically created storage tests
53+
Describe("when sending a storage request", func() {
54+
Context("for a valid config", func() {
55+
It("should provide CRUD access to the object", func() {
56+
client = cs.SamplecontrollerV1alpha1().Foos("default")
57+
58+
By("returning success from the create request")
59+
actual, err := client.Create(&instance)
60+
Expect(err).ShouldNot(HaveOccurred())
61+
62+
By("defaulting the expected fields")
63+
Expect(actual.Spec).To(Equal(expected.Spec))
64+
65+
By("returning the item for list requests")
66+
result, err := client.List(metav1.ListOptions{})
67+
Expect(err).ShouldNot(HaveOccurred())
68+
Expect(result.Items).To(HaveLen(1))
69+
Expect(result.Items[0].Spec).To(Equal(expected.Spec))
70+
71+
By("returning the item for get requests")
72+
actual, err = client.Get(instance.Name, metav1.GetOptions{})
73+
Expect(err).ShouldNot(HaveOccurred())
74+
Expect(actual.Spec).To(Equal(expected.Spec))
75+
76+
By("deleting the item for delete requests")
77+
err = client.Delete(instance.Name, &metav1.DeleteOptions{})
78+
Expect(err).ShouldNot(HaveOccurred())
79+
result, err = client.List(metav1.ListOptions{})
80+
Expect(err).ShouldNot(HaveOccurred())
81+
Expect(result.Items).To(HaveLen(0))
82+
})
83+
})
84+
})
85+
})
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
/*
3+
Copyright 2017 The Kubernetes Authors.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
18+
19+
package v1alpha1_test
20+
21+
import (
22+
"testing"
23+
24+
. "github.com/onsi/ginkgo"
25+
. "github.com/onsi/gomega"
26+
"github.com/kubernetes-sigs/kubebuilder/pkg/test"
27+
"k8s.io/client-go/rest"
28+
29+
"samplecontroller/pkg/inject"
30+
"samplecontroller/pkg/client/clientset/versioned"
31+
)
32+
33+
var testenv *test.TestEnvironment
34+
var config *rest.Config
35+
var cs *versioned.Clientset
36+
37+
func TestV1alpha1(t *testing.T) {
38+
RegisterFailHandler(Fail)
39+
RunSpecsWithDefaultAndCustomReporters(t, "v1 Suite", []Reporter{test.NewlineReporter{}})
40+
}
41+
42+
var _ = BeforeSuite(func() {
43+
testenv = &test.TestEnvironment{CRDs: inject.Injector.CRDs}
44+
45+
var err error
46+
config, err = testenv.Start()
47+
Expect(err).NotTo(HaveOccurred())
48+
49+
cs = versioned.NewForConfigOrDie(config)
50+
})
51+
52+
var _ = AfterSuite(func() {
53+
testenv.Stop()
54+
})

0 commit comments

Comments
 (0)