-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
164 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -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
67
controllers/infrastructure/bootstrapkubeconfig_controller_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("")) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters