Skip to content

Commit

Permalink
Write first unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
anusha94 committed Jun 10, 2022
1 parent 831ca68 commit 3c2221d
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 11 deletions.
7 changes: 5 additions & 2 deletions apis/infrastructure/v1beta1/bootstrapkubeconfig_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package v1beta1

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

Expand All @@ -21,8 +22,10 @@ type BootstrapKubeconfigSpec struct {

// BootstrapKubeconfigStatus defines the observed state of BootstrapKubeconfig
type BootstrapKubeconfigStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file
// BootstrapKubeconfigData is an optional reference to a bootstrap kubeconfig info
// for starting the host registration process
// +optional
BootstrapKubeconfigData *corev1.ObjectReference `json:"bootstrapKubeconfigData,omitempty"`
}

//+kubebuilder:object:root=true
Expand Down
7 changes: 6 additions & 1 deletion apis/infrastructure/v1beta1/zz_generated.deepcopy.go

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 @@ -44,6 +44,44 @@ spec:
type: object
status:
description: BootstrapKubeconfigStatus defines the observed state of BootstrapKubeconfig
properties:
bootstrapKubeconfigData:
description: BootstrapKubeconfigData is an optional reference to a
bootstrap kubeconfig info for starting the host registration process
properties:
apiVersion:
description: API version of the referent.
type: string
fieldPath:
description: 'If referring to a piece of an object instead of
an entire object, this string should contain a valid JSON/Go
field access statement, such as desiredState.manifest.containers[2].
For example, if the object reference is to a container within
a pod, this would take on a value like: "spec.containers{name}"
(where "name" refers to the name of the container that triggered
the event) or if no container name is specified "spec.containers[2]"
(container with index 2 in this pod). This syntax is chosen
only to have some well-defined way of referencing a part of
an object. TODO: this design is not final and this field is
subject to change in the future.'
type: string
kind:
description: 'Kind of the referent. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
type: string
name:
description: 'Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names'
type: string
namespace:
description: 'Namespace of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/'
type: string
resourceVersion:
description: 'Specific resourceVersion to which this reference
is made, if any. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#concurrency-control-and-consistency'
type: string
uid:
description: 'UID of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#uids'
type: string
type: object
type: object
type: object
served: true
Expand Down
26 changes: 26 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,32 @@ rules:
- patch
- update
- watch
- apiGroups:
- infrastructure.cluster.x-k8s.io
resources:
- bootstrapkubeconfigs
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- infrastructure.cluster.x-k8s.io
resources:
- bootstrapkubeconfigs/finalizers
verbs:
- update
- apiGroups:
- infrastructure.cluster.x-k8s.io
resources:
- bootstrapkubeconfigs/status
verbs:
- get
- patch
- update
- apiGroups:
- infrastructure.cluster.x-k8s.io
resources:
Expand Down
23 changes: 15 additions & 8 deletions controllers/infrastructure/bootstrapkubeconfig_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package controllers
import (
"context"

infrastructurev1beta1 "github.com/vmware-tanzu/cluster-api-provider-bringyourownhost/apis/infrastructure/v1beta1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -28,21 +30,26 @@ type BootstrapKubeconfigReconciler struct {
// the BootstrapKubeconfig object against the actual cluster state, and then
// perform operations to make the cluster state reflect the state specified by
// the user.
//
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
func (r *BootstrapKubeconfigReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
_ = log.FromContext(ctx)

// TODO(user): your logic here
logger := log.FromContext(ctx)
logger.Info("Reconcile request received")

// Fetch the BootstrapKubeconfig instance
bootstrapKubeconfig := &infrastructurev1beta1.BootstrapKubeconfig{}
err := r.Client.Get(ctx, req.NamespacedName, bootstrapKubeconfig)
if err != nil {
if apierrors.IsNotFound(err) {
return ctrl.Result{}, nil
}
return ctrl.Result{}, err
}

return ctrl.Result{}, nil
}

// SetupWithManager sets up the controller with the Manager.
func (r *BootstrapKubeconfigReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
// Uncomment the following line adding a pointer to an instance of the controlled resource as an argument
// For().
For(&infrastructurev1beta1.BootstrapKubeconfig{}).
Complete(r)
}
67 changes: 67 additions & 0 deletions controllers/infrastructure/bootstrapkubeconfig_controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2022 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package controllers_test

import (
"context"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
infrav1 "github.com/vmware-tanzu/cluster-api-provider-bringyourownhost/apis/infrastructure/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

var _ = Describe("Controllers/BoottrapKubeconfigController", func() {
var (
k8sClientUncached client.Client
bootstrapKubeconfigLookupKey types.NamespacedName
bootstrapKubeConfig *infrav1.BootstrapKubeconfig
)

BeforeEach(func() {
ctx = context.Background()
var clientErr error
k8sClientUncached, clientErr = client.New(cfg, client.Options{Scheme: scheme.Scheme})
Expect(clientErr).NotTo(HaveOccurred())
bootstrapKubeConfig = &infrav1.BootstrapKubeconfig{
TypeMeta: metav1.TypeMeta{
Kind: "BootstrapKubeconfig",
APIVersion: infrav1.GroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
GenerateName: "bootstrap-kubeconfig",
Namespace: "default",
},
Spec: infrav1.BootstrapKubeconfigSpec{},
}
Expect(k8sClientUncached.Create(ctx, bootstrapKubeConfig)).Should(Succeed())

bootstrapKubeconfigLookupKey = types.NamespacedName{Name: bootstrapKubeConfig.Name, Namespace: bootstrapKubeConfig.Namespace}
})

It("should ignore bootstrapkubeconfig if it is not found", func() {
_, err := bootstrapKubeconfigReconciler.Reconcile(ctx, reconcile.Request{
NamespacedName: types.NamespacedName{
Name: "non-existent-bootstrap-kubeconfig",
Namespace: "non-existent-namespace"}})
Expect(err).NotTo(HaveOccurred())
})

Context("When BootstrapKubeconf CRD is created", func() {
It("should generate the bootstrap kubeconfig data", func() {
_, err := bootstrapKubeconfigReconciler.Reconcile(ctx, reconcile.Request{
NamespacedName: bootstrapKubeconfigLookupKey})
Expect(err).NotTo(HaveOccurred())

createdBootstrapKubeconfig := &infrav1.BootstrapKubeconfig{}
err = k8sClientUncached.Get(ctx, bootstrapKubeconfigLookupKey, createdBootstrapKubeconfig)
Expect(err).ToNot(HaveOccurred())
Expect(createdBootstrapKubeconfig.Status.BootstrapKubeconfigData).Should(Equal(""))
})
})
})
7 changes: 7 additions & 0 deletions controllers/infrastructure/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ var (
byoClusterReconciler *controllers.ByoClusterReconciler
byoAdmissionReconciler *controllers.ByoAdmissionReconciler
k8sInstallerConfigReconciler *controllers.K8sInstallerConfigReconciler
bootstrapKubeconfigReconciler *controllers.BootstrapKubeconfigReconciler
recorder *record.FakeRecorder
byoCluster *infrastructurev1beta1.ByoCluster
capiCluster *clusterv1.Cluster
Expand Down Expand Up @@ -150,6 +151,12 @@ var _ = BeforeSuite(func() {
err = k8sInstallerConfigReconciler.SetupWithManager(k8sManager)
Expect(err).NotTo(HaveOccurred())

bootstrapKubeconfigReconciler = &controllers.BootstrapKubeconfigReconciler{
Client: k8sManager.GetClient(),
}
err = bootstrapKubeconfigReconciler.SetupWithManager(k8sManager)
Expect(err).NotTo(HaveOccurred())

go func() {
err = k8sManager.GetCache().Start(ctx)
Expect(err).NotTo(HaveOccurred())
Expand Down

0 comments on commit 3c2221d

Please sign in to comment.