-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add base code for the TemplateManagement reconciler
- Loading branch information
Showing
4 changed files
with
163 additions
and
4 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
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,59 @@ | ||
// Copyright 2024 | ||
// | ||
// 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 controller | ||
|
||
import ( | ||
"context" | ||
|
||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/client-go/rest" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/log" | ||
|
||
hmc "github.com/Mirantis/hmc/api/v1alpha1" | ||
) | ||
|
||
// TemplateManagementReconciler reconciles a TemplateManagement object | ||
type TemplateManagementReconciler struct { | ||
client.Client | ||
Scheme *runtime.Scheme | ||
Config *rest.Config | ||
SystemNamespace string | ||
} | ||
|
||
func (r *TemplateManagementReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | ||
l := log.FromContext(ctx).WithValues("TemplateManagementController", req.NamespacedName) | ||
log.IntoContext(ctx, l) | ||
l.Info("Reconciling TemplateManagement") | ||
templateMgmt := &hmc.TemplateManagement{} | ||
if err := r.Get(ctx, req.NamespacedName, templateMgmt); err != nil { | ||
if apierrors.IsNotFound(err) { | ||
l.Info("TemplateManagement not found, ignoring since object must be deleted") | ||
return ctrl.Result{}, nil | ||
} | ||
l.Error(err, "Failed to get TemplateManagement") | ||
return ctrl.Result{}, err | ||
} | ||
return ctrl.Result{}, nil | ||
} | ||
|
||
// SetupWithManager sets up the controller with the Manager. | ||
func (r *TemplateManagementReconciler) SetupWithManager(mgr ctrl.Manager) error { | ||
return ctrl.NewControllerManagedBy(mgr). | ||
For(&hmc.TemplateManagement{}). | ||
Complete(r) | ||
} |
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,80 @@ | ||
// Copyright 2024 | ||
// | ||
// 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 controller | ||
|
||
import ( | ||
"context" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/types" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
hmcmirantiscomv1alpha1 "github.com/Mirantis/hmc/api/v1alpha1" | ||
) | ||
|
||
var _ = Describe("Template Management Controller", func() { | ||
Context("When reconciling a resource", func() { | ||
const resourceName = "test-resource" | ||
|
||
ctx := context.Background() | ||
|
||
typeNamespacedName := types.NamespacedName{ | ||
Name: resourceName, | ||
} | ||
templateMgmt := &hmcmirantiscomv1alpha1.TemplateManagement{} | ||
|
||
BeforeEach(func() { | ||
By("creating the custom resource for the Kind TemplateManagement") | ||
err := k8sClient.Get(ctx, typeNamespacedName, templateMgmt) | ||
if err != nil && errors.IsNotFound(err) { | ||
resource := &hmcmirantiscomv1alpha1.TemplateManagement{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: resourceName, | ||
}, | ||
// TODO(user): Specify other spec details if needed. | ||
} | ||
Expect(k8sClient.Create(ctx, resource)).To(Succeed()) | ||
} | ||
}) | ||
|
||
AfterEach(func() { | ||
// TODO(user): Cleanup logic after each test, like removing the resource instance. | ||
resource := &hmcmirantiscomv1alpha1.TemplateManagement{} | ||
err := k8sClient.Get(ctx, typeNamespacedName, resource) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
By("Cleanup the specific resource instance TemplateManagement") | ||
Expect(k8sClient.Delete(ctx, resource)).To(Succeed()) | ||
}) | ||
It("should successfully reconcile the resource", func() { | ||
By("Reconciling the created resource") | ||
controllerReconciler := &TemplateManagementReconciler{ | ||
Client: k8sClient, | ||
Scheme: k8sClient.Scheme(), | ||
} | ||
|
||
_, err := controllerReconciler.Reconcile(ctx, reconcile.Request{ | ||
NamespacedName: typeNamespacedName, | ||
}) | ||
Expect(err).NotTo(HaveOccurred()) | ||
// TODO(user): Add more specific assertions depending on your controller's reconciliation logic. | ||
// Example: If you expect a certain status condition after reconciliation, verify it here. | ||
}) | ||
}) | ||
}) |
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