Skip to content

Commit

Permalink
Scaffolding for sample controller
Browse files Browse the repository at this point in the history
- kubebuilder init --domain k8s.io
- kubebuilder create resoure --group samplecontroller --version v1alpha1 --kind Foo
  • Loading branch information
pwittrock committed Mar 21, 2018
1 parent f596bcb commit b972355
Show file tree
Hide file tree
Showing 40 changed files with 2,343 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
Copyright 2017 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 main

import (
"flag"
"log"

// Import auth/gcp to connect to GKE clusters remotely
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"

configlib "github.com/kubernetes-sigs/kubebuilder/pkg/config"
"github.com/kubernetes-sigs/kubebuilder/pkg/inject/run"
"github.com/kubernetes-sigs/kubebuilder/pkg/install"
"github.com/kubernetes-sigs/kubebuilder/pkg/signals"
extensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"

"samplecontroller/pkg/inject"
"samplecontroller/pkg/inject/args"
)

var installCRDs = flag.Bool("install-crds", true, "install the CRDs used by the controller as part of startup")

// Controller-manager main.
func main() {
flag.Parse()

stopCh := signals.SetupSignalHandler()

config := configlib.GetConfigOrDie()

if *installCRDs {
if err := install.NewInstaller(config).Install(&InstallStrategy{crds: inject.Injector.CRDs}); err != nil {
log.Fatalf("Could not create CRDs: %v", err)
}
}

// Start the controllers
if err := inject.RunAll(run.RunArguments{Stop: stopCh}, args.CreateInjectArgs(config)); err != nil {
log.Fatalf("%v", err)
}
}

type InstallStrategy struct {
install.EmptyInstallStrategy
crds []*extensionsv1beta1.CustomResourceDefinition
}

func (s *InstallStrategy) GetCRDs() []*extensionsv1beta1.CustomResourceDefinition {
return s.crds
}
23 changes: 23 additions & 0 deletions samples/full/controller/src/samplecontroller/pkg/apis/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

/*
Copyright 2017 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.
*/


//
// +domain=k8s.io

package apis

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

/*
Copyright 2017 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.
*/



// +k8s:deepcopy-gen=package,register
// +groupName=samplecontroller.k8s.io

// Package api is the internal version of the API.
package samplecontroller
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

/*
Copyright 2017 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.
*/


// Api versions allow the api contract for a resource to be changed while keeping
// backward compatibility by support multiple concurrent versions
// of the same resource

// +k8s:openapi-gen=true
// +k8s:deepcopy-gen=package,register
// +k8s:conversion-gen=samplecontroller/pkg/apis/samplecontroller
// +k8s:defaulter-gen=TypeMeta
// +groupName=samplecontroller.k8s.io
package v1alpha1 // import "samplecontroller/pkg/apis/samplecontroller/v1alpha1"
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

/*
Copyright 2017 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 v1alpha1

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

// EDIT THIS FILE!
// Created by "kubebuilder create resource" for you to implement the Foo resource schema definition
// as a go struct.
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

// FooSpec defines the desired state of Foo
type FooSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
}

// FooStatus defines the observed state of Foo
type FooStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
}

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

// Foo
// +k8s:openapi-gen=true
// +resource:path=foos
type Foo struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec FooSpec `json:"spec,omitempty"`
Status FooStatus `json:"status,omitempty"`
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@

/*
Copyright 2017 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 v1alpha1_test

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

. "samplecontroller/pkg/apis/samplecontroller/v1alpha1"
. "samplecontroller/pkg/client/clientset/versioned/typed/samplecontroller/v1alpha1"
)

// EDIT THIS FILE!
// Created by "kubebuilder create resource" for you to implement the Foo resource tests

var _ = Describe("Foo", func() {
var instance Foo
var expected Foo
var client FooInterface

BeforeEach(func() {
instance = Foo{}
instance.Name = "instance-1"

expected = instance
})

AfterEach(func() {
client.Delete(instance.Name, &metav1.DeleteOptions{})
})

// INSERT YOUR CODE HERE - add more "Describe" tests

// Automatically created storage tests
Describe("when sending a storage request", func() {
Context("for a valid config", func() {
It("should provide CRUD access to the object", func() {
client = cs.SamplecontrollerV1alpha1().Foos("default")

By("returning success from the create request")
actual, err := client.Create(&instance)
Expect(err).ShouldNot(HaveOccurred())

By("defaulting the expected fields")
Expect(actual.Spec).To(Equal(expected.Spec))

By("returning the item for list requests")
result, err := client.List(metav1.ListOptions{})
Expect(err).ShouldNot(HaveOccurred())
Expect(result.Items).To(HaveLen(1))
Expect(result.Items[0].Spec).To(Equal(expected.Spec))

By("returning the item for get requests")
actual, err = client.Get(instance.Name, metav1.GetOptions{})
Expect(err).ShouldNot(HaveOccurred())
Expect(actual.Spec).To(Equal(expected.Spec))

By("deleting the item for delete requests")
err = client.Delete(instance.Name, &metav1.DeleteOptions{})
Expect(err).ShouldNot(HaveOccurred())
result, err = client.List(metav1.ListOptions{})
Expect(err).ShouldNot(HaveOccurred())
Expect(result.Items).To(HaveLen(0))
})
})
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

/*
Copyright 2017 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 v1alpha1_test

import (
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/kubernetes-sigs/kubebuilder/pkg/test"
"k8s.io/client-go/rest"

"samplecontroller/pkg/inject"
"samplecontroller/pkg/client/clientset/versioned"
)

var testenv *test.TestEnvironment
var config *rest.Config
var cs *versioned.Clientset

func TestV1alpha1(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecsWithDefaultAndCustomReporters(t, "v1 Suite", []Reporter{test.NewlineReporter{}})
}

var _ = BeforeSuite(func() {
testenv = &test.TestEnvironment{CRDs: inject.Injector.CRDs}

var err error
config, err = testenv.Start()
Expect(err).NotTo(HaveOccurred())

cs = versioned.NewForConfigOrDie(config)
})

var _ = AfterSuite(func() {
testenv.Stop()
})
Loading

0 comments on commit b972355

Please sign in to comment.