diff --git a/PROJECT b/PROJECT index e02e47849..4d009fafb 100644 --- a/PROJECT +++ b/PROJECT @@ -26,4 +26,13 @@ resources: kind: HMCDeployment path: github.com/Mirantis/hmc/api/v1alpha1 version: v1alpha1 +- api: + crdVersion: v1 + namespaced: true + controller: true + domain: hmc.mirantis.com + group: hmc.mirantis.com + kind: HMCManagement + path: github.com/Mirantis/hmc/api/v1alpha1 + version: v1alpha1 version: "3" diff --git a/api/v1alpha1/hmcmanagement_types.go b/api/v1alpha1/hmcmanagement_types.go new file mode 100644 index 000000000..e48d3b333 --- /dev/null +++ b/api/v1alpha1/hmcmanagement_types.go @@ -0,0 +1,64 @@ +/* +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 v1alpha1 + +import ( + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! +// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized. + +// HMCManagementSpec defines the desired state of HMCManagement +type HMCManagementSpec struct { + // INSERT ADDITIONAL SPEC FIELDS - desired state of cluster + // Important: Run "make" to regenerate code after modifying this file + + // Foo is an example field of HMCManagement. Edit hmcmanagement_types.go to remove/update + Foo string `json:"foo,omitempty"` +} + +// HMCManagementStatus defines the observed state of HMCManagement +type HMCManagementStatus struct { + // INSERT ADDITIONAL STATUS FIELD - define observed state of cluster + // Important: Run "make" to regenerate code after modifying this file +} + +//+kubebuilder:object:root=true +//+kubebuilder:subresource:status + +// HMCManagement is the Schema for the hmcmanagements API +type HMCManagement struct { + metav1.TypeMeta `json:",inline"` + metav1.ObjectMeta `json:"metadata,omitempty"` + + Spec HMCManagementSpec `json:"spec,omitempty"` + Status HMCManagementStatus `json:"status,omitempty"` +} + +//+kubebuilder:object:root=true + +// HMCManagementList contains a list of HMCManagement +type HMCManagementList struct { + metav1.TypeMeta `json:",inline"` + metav1.ListMeta `json:"metadata,omitempty"` + Items []HMCManagement `json:"items"` +} + +func init() { + SchemeBuilder.Register(&HMCManagement{}, &HMCManagementList{}) +} diff --git a/api/v1alpha1/zz_generated.deepcopy.go b/api/v1alpha1/zz_generated.deepcopy.go index c5a1b7fa8..874e26cd3 100644 --- a/api/v1alpha1/zz_generated.deepcopy.go +++ b/api/v1alpha1/zz_generated.deepcopy.go @@ -113,6 +113,95 @@ func (in *HMCDeploymentStatus) DeepCopy() *HMCDeploymentStatus { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *HMCManagement) DeepCopyInto(out *HMCManagement) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) + out.Spec = in.Spec + out.Status = in.Status +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new HMCManagement. +func (in *HMCManagement) DeepCopy() *HMCManagement { + if in == nil { + return nil + } + out := new(HMCManagement) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *HMCManagement) DeepCopyObject() runtime.Object { + if c := in.DeepCopy(); c != nil { + return c + } + return nil +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *HMCManagementList) DeepCopyInto(out *HMCManagementList) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ListMeta.DeepCopyInto(&out.ListMeta) + if in.Items != nil { + in, out := &in.Items, &out.Items + *out = make([]HMCManagement, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new HMCManagementList. +func (in *HMCManagementList) DeepCopy() *HMCManagementList { + if in == nil { + return nil + } + out := new(HMCManagementList) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *HMCManagementList) DeepCopyObject() runtime.Object { + if c := in.DeepCopy(); c != nil { + return c + } + return nil +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *HMCManagementSpec) DeepCopyInto(out *HMCManagementSpec) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new HMCManagementSpec. +func (in *HMCManagementSpec) DeepCopy() *HMCManagementSpec { + if in == nil { + return nil + } + out := new(HMCManagementSpec) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *HMCManagementStatus) DeepCopyInto(out *HMCManagementStatus) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new HMCManagementStatus. +func (in *HMCManagementStatus) DeepCopy() *HMCManagementStatus { + if in == nil { + return nil + } + out := new(HMCManagementStatus) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *HMCTemplate) DeepCopyInto(out *HMCTemplate) { *out = *in diff --git a/cmd/main.go b/cmd/main.go index 1bb32e42d..876b70f2c 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -136,6 +136,13 @@ func main() { setupLog.Error(err, "unable to create controller", "controller", "HMCDeployment") os.Exit(1) } + if err = (&controller.HMCManagementReconciler{ + Client: mgr.GetClient(), + Scheme: mgr.GetScheme(), + }).SetupWithManager(mgr); err != nil { + setupLog.Error(err, "unable to create controller", "controller", "HMCManagement") + os.Exit(1) + } //+kubebuilder:scaffold:builder if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil { diff --git a/config/crd/kustomization.yaml b/config/crd/kustomization.yaml index 5996e6cb0..a3d96ad45 100644 --- a/config/crd/kustomization.yaml +++ b/config/crd/kustomization.yaml @@ -4,6 +4,7 @@ resources: - bases/hmc.mirantis.com.hmc.mirantis.com_hmctemplates.yaml - bases/hmc.mirantis.com.hmc.mirantis.com_hmcdeployments.yaml +- bases/hmc.mirantis.com.hmc.mirantis.com_hmcmanagements.yaml #+kubebuilder:scaffold:crdkustomizeresource patches: @@ -15,6 +16,7 @@ patches: # patches here are for enabling the CA injection for each CRD #- path: patches/cainjection_in_hmctemplates.yaml #- path: patches/cainjection_in_hmcdeployments.yaml +#- path: patches/cainjection_in_hmcmanagements.yaml #+kubebuilder:scaffold:crdkustomizecainjectionpatch # [WEBHOOK] To enable webhook, uncomment the following section diff --git a/config/rbac/hmcmanagement_editor_role.yaml b/config/rbac/hmcmanagement_editor_role.yaml new file mode 100644 index 000000000..2b6420d0a --- /dev/null +++ b/config/rbac/hmcmanagement_editor_role.yaml @@ -0,0 +1,27 @@ +# permissions for end users to edit hmcmanagements. +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + labels: + app.kubernetes.io/name: hmc + app.kubernetes.io/managed-by: kustomize + name: hmcmanagement-editor-role +rules: +- apiGroups: + - hmc.mirantis.com.hmc.mirantis.com + resources: + - hmcmanagements + verbs: + - create + - delete + - get + - list + - patch + - update + - watch +- apiGroups: + - hmc.mirantis.com.hmc.mirantis.com + resources: + - hmcmanagements/status + verbs: + - get diff --git a/config/rbac/hmcmanagement_viewer_role.yaml b/config/rbac/hmcmanagement_viewer_role.yaml new file mode 100644 index 000000000..22737e944 --- /dev/null +++ b/config/rbac/hmcmanagement_viewer_role.yaml @@ -0,0 +1,23 @@ +# permissions for end users to view hmcmanagements. +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + labels: + app.kubernetes.io/name: hmc + app.kubernetes.io/managed-by: kustomize + name: hmcmanagement-viewer-role +rules: +- apiGroups: + - hmc.mirantis.com.hmc.mirantis.com + resources: + - hmcmanagements + verbs: + - get + - list + - watch +- apiGroups: + - hmc.mirantis.com.hmc.mirantis.com + resources: + - hmcmanagements/status + verbs: + - get diff --git a/config/rbac/kustomization.yaml b/config/rbac/kustomization.yaml index 469dab259..85659d633 100644 --- a/config/rbac/kustomization.yaml +++ b/config/rbac/kustomization.yaml @@ -14,6 +14,8 @@ resources: # default, aiding admins in cluster management. Those roles are # not used by the Project itself. You can comment the following lines # if you do not want those helpers be installed with your Project. +- hmcmanagement_editor_role.yaml +- hmcmanagement_viewer_role.yaml - hmcdeployment_editor_role.yaml - hmcdeployment_viewer_role.yaml - hmctemplate_editor_role.yaml diff --git a/config/samples/hmc.mirantis.com_v1alpha1_hmcmanagement.yaml b/config/samples/hmc.mirantis.com_v1alpha1_hmcmanagement.yaml new file mode 100644 index 000000000..3b964d542 --- /dev/null +++ b/config/samples/hmc.mirantis.com_v1alpha1_hmcmanagement.yaml @@ -0,0 +1,9 @@ +apiVersion: hmc.mirantis.com.hmc.mirantis.com/v1alpha1 +kind: HMCManagement +metadata: + labels: + app.kubernetes.io/name: hmc + app.kubernetes.io/managed-by: kustomize + name: hmcmanagement-sample +spec: + # TODO(user): Add fields here diff --git a/config/samples/kustomization.yaml b/config/samples/kustomization.yaml index 69c335557..cb1e96b3b 100644 --- a/config/samples/kustomization.yaml +++ b/config/samples/kustomization.yaml @@ -2,4 +2,5 @@ resources: - hmc.mirantis.com_v1alpha1_hmctemplate.yaml - hmc.mirantis.com_v1alpha1_hmcdeployment.yaml +- hmc.mirantis.com_v1alpha1_hmcmanagement.yaml #+kubebuilder:scaffold:manifestskustomizesamples diff --git a/internal/controller/hmcmanagement_controller.go b/internal/controller/hmcmanagement_controller.go new file mode 100644 index 000000000..8fb4ac944 --- /dev/null +++ b/internal/controller/hmcmanagement_controller.go @@ -0,0 +1,62 @@ +/* +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" + + "k8s.io/apimachinery/pkg/runtime" + ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/log" + + hmcmirantiscomv1alpha1 "github.com/Mirantis/hmc/api/v1alpha1" +) + +// HMCManagementReconciler reconciles a HMCManagement object +type HMCManagementReconciler struct { + client.Client + Scheme *runtime.Scheme +} + +//+kubebuilder:rbac:groups=hmc.mirantis.com.hmc.mirantis.com,resources=hmcmanagements,verbs=get;list;watch;create;update;patch;delete +//+kubebuilder:rbac:groups=hmc.mirantis.com.hmc.mirantis.com,resources=hmcmanagements/status,verbs=get;update;patch +//+kubebuilder:rbac:groups=hmc.mirantis.com.hmc.mirantis.com,resources=hmcmanagements/finalizers,verbs=update + +// Reconcile is part of the main kubernetes reconciliation loop which aims to +// move the current state of the cluster closer to the desired state. +// TODO(user): Modify the Reconcile function to compare the state specified by +// the HMCManagement 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/controller-runtime@v0.17.3/pkg/reconcile +func (r *HMCManagementReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { + _ = log.FromContext(ctx) + + // TODO(user): your logic here + + return ctrl.Result{}, nil +} + +// SetupWithManager sets up the controller with the Manager. +func (r *HMCManagementReconciler) SetupWithManager(mgr ctrl.Manager) error { + return ctrl.NewControllerManagedBy(mgr). + For(&hmcmirantiscomv1alpha1.HMCManagement{}). + Complete(r) +} diff --git a/internal/controller/hmcmanagement_controller_test.go b/internal/controller/hmcmanagement_controller_test.go new file mode 100644 index 000000000..e698e4fa0 --- /dev/null +++ b/internal/controller/hmcmanagement_controller_test.go @@ -0,0 +1,84 @@ +/* +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("HMCManagement Controller", func() { + Context("When reconciling a resource", func() { + const resourceName = "test-resource" + + ctx := context.Background() + + typeNamespacedName := types.NamespacedName{ + Name: resourceName, + Namespace: "default", // TODO(user):Modify as needed + } + hmcmanagement := &hmcmirantiscomv1alpha1.HMCManagement{} + + BeforeEach(func() { + By("creating the custom resource for the Kind HMCManagement") + err := k8sClient.Get(ctx, typeNamespacedName, hmcmanagement) + if err != nil && errors.IsNotFound(err) { + resource := &hmcmirantiscomv1alpha1.HMCManagement{ + ObjectMeta: metav1.ObjectMeta{ + Name: resourceName, + Namespace: "default", + }, + // 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.HMCManagement{} + err := k8sClient.Get(ctx, typeNamespacedName, resource) + Expect(err).NotTo(HaveOccurred()) + + By("Cleanup the specific resource instance HMCManagement") + Expect(k8sClient.Delete(ctx, resource)).To(Succeed()) + }) + It("should successfully reconcile the resource", func() { + By("Reconciling the created resource") + controllerReconciler := &HMCManagementReconciler{ + 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. + }) + }) +})