Skip to content

Commit

Permalink
add comment marker into api; generate addon clientset/lister/informers (
Browse files Browse the repository at this point in the history
#116)

* Changes: debug kubebuilder and code-generator comment

Signed-off-by: jhu02 <[email protected]>

* Add comment marker generate clientset; Verify clientset using existing api_type_tests

Signed-off-by: jhu02 <[email protected]>

* Changes: add make generate for code-generator

Signed-off-by: jhu02 <[email protected]>

* Fix Makefile types

Signed-off-by: jhu02 <[email protected]>

* Fix typo

Signed-off-by: jhu02 <[email protected]>

* Change: change code-generator download directory; revert changes

Signed-off-by: jhu02 <[email protected]>

* Changes: move api_types_test.go back to api directory

Signed-off-by: jhu02 <[email protected]>

Co-authored-by: jhu02 <[email protected]>
  • Loading branch information
ccfish2 and jhu02 authored Mar 1, 2022
1 parent 7ad77da commit 05e7852
Show file tree
Hide file tree
Showing 45 changed files with 1,274 additions and 118 deletions.
46 changes: 44 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,20 @@ all: test manager addonctl

# Run tests
test: generate fmt vet manifests
go test ./api/... ./controllers/... ./pkg/... ./cmd/... -coverprofile cover.out
go test ./api/... ./controllers/... ./pkg/... ./cmd/... -v -coverprofile cover.out

# tests both controllers and api
.PHONY: test.controllers
test.controllers:
go test -v -race ./controllers/... -coverprofile cover.out

.PHONY: test.pkg
test.pkg:
go test -v -race ./pkg/... -coverprofile cover.out

.PHONY: test.cmd
test.cmd:
go test -v -race ./cmd/... -coverprofile cover.out

# Run E2E tests
bdd: fmt vet deploy
Expand Down Expand Up @@ -95,9 +108,22 @@ vet:
go vet ./...

# Generate code
generate: controller-gen
generate: controller-gen types
$(CONTROLLER_GEN) object:headerFile=./hack/boilerplate.go.txt paths=./api/...

# generates many other files (listers, informers, client etc).
api/addon/v1alpha1/zz_generated.deepcopy.go: $(TYPES)
ln -s . v1
$(CODE_GENERATOR_GEN)/generate-groups.sh \
"deepcopy,client,informer,lister" \
github.com/keikoproj/addon-manager/pkg/client github.com/keikoproj/addon-manager/api\
addon:v1alpha1 \
--go-header-file ./hack/custom-boilerplate.go.txt
rm -rf v1

.PHONY: types
types: api/addon/v1alpha1/zz_generated.deepcopy.go

# Build the docker image
docker-build: manager
docker build --build-arg COMMIT=${GIT_COMMIT} --build-arg DATE=${BUILD_DATE} -t ${IMG} .
Expand Down Expand Up @@ -130,3 +156,19 @@ CONTROLLER_GEN=$(GOBIN)/controller-gen
else
CONTROLLER_GEN=$(shell which controller-gen)
endif

code-generator:
ifeq (, $(shell which code-generator))
@{ \
set -e ;\
CONTROLLER_GEN_TMP_DIR=$$(mktemp -d) ;\
cd $$CONTROLLER_GEN_TMP_DIR ;\
curl -L -o code-generator.zip https://github.com/kubernetes/code-generator/archive/refs/tags/v0.21.5.zip ;\
unzip code-generator.zip ;\
mv code-generator-0.21.5 $(GOPATH)/bin/ ;\
rm -rf code-generator.zip ;\
}
CODE_GENERATOR_GEN=$(GOBIN)/code-generator-0.21.5
else
CODE_GENERATOR_GEN=$(shell which code-generator)
endif
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,8 @@ type AddonStatus struct {
// +kubebuilder:printcolumn:name="STATUS",type="string",JSONPath=".status.lifecycle.installed"
// +kubebuilder:printcolumn:name="REASON",type="string",JSONPath=".status.reason"
// +kubebuilder:printcolumn:name="AGE",type="date",JSONPath=".metadata.creationTimestamp"
// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
type Addon struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Expand All @@ -301,18 +303,14 @@ type Addon struct {
}

// +kubebuilder:object:root=true

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// AddonList contains a list of Addon
type AddonList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []Addon `json:"items"`
}

func init() {
SchemeBuilder.Register(&Addon{}, &AddonList{})
}

// GetPackageSpec returns the addon package details from addon spec
func (a *Addon) GetPackageSpec() PackageSpec {
return PackageSpec{
Expand Down
3 changes: 3 additions & 0 deletions api/addon/v1alpha1/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// +k8s:deepcopy-gen=package
// +groupName=addonmgr.keikoproj.io
package v1alpha1
56 changes: 56 additions & 0 deletions api/addon/v1alpha1/register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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"
runtime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)

// SchemeGroupVersion is group version used to register these objects
var (
SchemeGroupVersion = schema.GroupVersion{Group: "addonmgr.keikoproj.io", Version: "v1alpha1"}
AddonSchemaGroupVersionKind = schema.GroupVersionKind{Group: "addonmgr.keikoproj.io", Version: "v1alpha1", Kind: "Addon"}
)

// Kind takes an unqualified kind and returns back a Group qualified GroupKind
func Kind(kind string) schema.GroupKind {
return SchemeGroupVersion.WithKind(kind).GroupKind()
}

// Resource takes an unqualified resource and returns a Group-qualified GroupResource.
func Resource(resource string) schema.GroupResource {
return SchemeGroupVersion.WithResource(resource).GroupResource()
}

var (

// SchemeBuilder is used to add go types to the GroupVersionKind scheme
SchemeBuilder = runtime.NewSchemeBuilder(addKnownTypes)

// AddToScheme adds the types in this group-version to the given scheme.
AddToScheme = SchemeBuilder.AddToScheme
)

// addKnownTypes adds the set of types defined in this package to the supplied scheme.
func addKnownTypes(scheme *runtime.Scheme) error {
scheme.AddKnownTypes(SchemeGroupVersion,
&Addon{},
&AddonList{},
)
metav1.AddToGroupVersion(scheme, SchemeGroupVersion)
return nil
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,19 @@
* limitations under the License.
*/

package v1alpha1
package apitests

import (
"fmt"

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

addonmgrv1alpha1 "github.com/keikoproj/addon-manager/api/addon/v1alpha1"
fakeAddonCli "github.com/keikoproj/addon-manager/pkg/client/clientset/versioned/fake"
"golang.org/x/net/context"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/runtime"
)

var wfSpecTemplate = `
Expand Down Expand Up @@ -66,10 +68,6 @@ spec:
// http://onsi.github.io/ginkgo to learn more.

var _ = Describe("Addon", func() {
var (
key types.NamespacedName
created, fetched *Addon
)

BeforeEach(func() {
// Add any setup steps that needs to be executed before each test
Expand All @@ -86,21 +84,18 @@ var _ = Describe("Addon", func() {
Context("Create API", func() {

It("should create an object successfully", func() {

key = types.NamespacedName{
Name: "foo",
Namespace: "default",
}
created = &Addon{
namespace := "default"
adddonName := "foo"
created := &addonmgrv1alpha1.Addon{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: "default",
Name: adddonName,
Namespace: namespace,
},
Spec: AddonSpec{
PackageSpec: PackageSpec{
Spec: addonmgrv1alpha1.AddonSpec{
PackageSpec: addonmgrv1alpha1.PackageSpec{
PkgName: "my-addon",
PkgVersion: "1.0.0",
PkgType: HelmPkg,
PkgType: addonmgrv1alpha1.HelmPkg,
PkgDescription: "",
PkgDeps: map[string]string{"core/A": "*", "core/B": "v1.0.0"},
},
Expand All @@ -109,39 +104,43 @@ var _ = Describe("Addon", func() {
"app": "my-app",
},
},
Params: AddonParams{
Params: addonmgrv1alpha1.AddonParams{
Namespace: "foo-ns",
Context: ClusterContext{
Context: addonmgrv1alpha1.ClusterContext{
ClusterName: "foo-cluster",
ClusterRegion: "foo-region",
AdditionalConfigs: map[string]FlexString{
AdditionalConfigs: map[string]addonmgrv1alpha1.FlexString{
"additional": "config",
},
},
Data: map[string]FlexString{
Data: map[string]addonmgrv1alpha1.FlexString{
"foo-param": "val",
},
},
Lifecycle: LifecycleWorkflowSpec{
Prereqs: WorkflowType{
Lifecycle: addonmgrv1alpha1.LifecycleWorkflowSpec{
Prereqs: addonmgrv1alpha1.WorkflowType{
NamePrefix: "my-prereqs",
Template: wfSpecTemplate,
},
Install: WorkflowType{
Install: addonmgrv1alpha1.WorkflowType{
Template: wfSpecTemplate,
},
Delete: WorkflowType{
Delete: addonmgrv1alpha1.WorkflowType{
Template: wfSpecTemplate,
},
},
},
}

apiCli := fakeAddonCli.NewSimpleClientset([]runtime.Object{}...)
ctx := context.TODO()
By("creating an API obj")
Expect(k8sClient.Create(context.TODO(), created)).To(Succeed())
created, err := apiCli.AddonmgrV1alpha1().Addons(namespace).Create(ctx, created, metav1.CreateOptions{})
Expect(err).To(BeNil())
Expect(created).NotTo(BeNil())

fetched = &Addon{}
Expect(k8sClient.Get(context.TODO(), key, fetched)).To(Succeed())
fetched, err := apiCli.AddonmgrV1alpha1().Addons(namespace).Get(ctx, adddonName, metav1.GetOptions{})
Expect(err).To(BeNil())
Expect(fetched).To(Equal(created))

By("Checking expected fetched values")
Expand All @@ -168,20 +167,23 @@ var _ = Describe("Addon", func() {
// Update status checksum
fetched.Status.Checksum = checksum

wfName := fetched.GetFormattedWorkflowName(Install)
wfName := fetched.GetFormattedWorkflowName(addonmgrv1alpha1.Install)
Expect(wfName).To(Equal(fmt.Sprintf("foo-install-%s-wf", checksum)))

By("updating labels")
updated := fetched.DeepCopy()
updated.Labels = map[string]string{"hello": "world"}
Expect(k8sClient.Update(context.TODO(), updated)).To(Succeed())
updated, err = apiCli.AddonmgrV1alpha1().Addons(namespace).Update(ctx, updated, metav1.UpdateOptions{})
Expect(err).To(BeNil())
Expect(updated).NotTo(BeNil())

Expect(k8sClient.Get(context.TODO(), key, fetched)).To(Succeed())
fetched, err = apiCli.AddonmgrV1alpha1().Addons(namespace).Get(ctx, adddonName, metav1.GetOptions{})
Expect(err).To(BeNil())
Expect(fetched).To(Equal(updated))

By("deleting the created object")
Expect(k8sClient.Delete(context.TODO(), fetched)).To(Succeed())
Expect(k8sClient.Get(context.TODO(), key, fetched)).ToNot(Succeed())
err = apiCli.AddonmgrV1alpha1().Addons(namespace).Delete(ctx, adddonName, metav1.DeleteOptions{})
Expect(err).To(BeNil())
})

})
Expand Down
29 changes: 1 addition & 28 deletions api/v1alpha1/suite_test.go → api/api-tests/suit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,22 @@
* limitations under the License.
*/

package v1alpha1
package apitests

import (
"path/filepath"
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
"sigs.k8s.io/controller-runtime/pkg/envtest/printer"
logf "sigs.k8s.io/controller-runtime/pkg/log"
)

// These tests use Ginkgo (BDD-style Go testing framework). Refer to
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.

var cfg *rest.Config
var k8sClient client.Client
var testEnv *envtest.Environment

func TestAPIs(t *testing.T) {
RegisterFailHandler(Fail)

Expand All @@ -48,27 +39,9 @@ func TestAPIs(t *testing.T) {
var _ = BeforeSuite(func(done Done) {
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))

By("bootstrapping test environment")
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")},
}

err := SchemeBuilder.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())

cfg, err = testEnv.Start()
Expect(err).ToNot(HaveOccurred())
Expect(cfg).ToNot(BeNil())

k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
Expect(err).ToNot(HaveOccurred())
Expect(k8sClient).ToNot(BeNil())

close(done)
}, 60)

var _ = AfterSuite(func() {
By("tearing down the test environment")
err := testEnv.Stop()
Expect(err).ToNot(HaveOccurred())
})
Loading

0 comments on commit 05e7852

Please sign in to comment.