From 8713affb054448820f6d61ac50bac2b4579c6903 Mon Sep 17 00:00:00 2001 From: Andrei Pavlov Date: Mon, 27 May 2024 15:40:55 +0700 Subject: [PATCH] Init HMCProviderAWS api Signed-off-by: Andrei Pavlov --- PROJECT | 9 ++ api/v1alpha1/hmcprovideraws_types.go | 64 +++++++++++++ api/v1alpha1/zz_generated.deepcopy.go | 89 +++++++++++++++++++ cmd/main.go | 7 ++ config/crd/kustomization.yaml | 2 + config/rbac/hmcprovideraws_editor_role.yaml | 27 ++++++ config/rbac/hmcprovideraws_viewer_role.yaml | 23 +++++ config/rbac/kustomization.yaml | 2 + ....mirantis.com_v1alpha1_hmcprovideraws.yaml | 9 ++ config/samples/kustomization.yaml | 1 + .../controller/hmcprovideraws_controller.go | 62 +++++++++++++ .../hmcprovideraws_controller_test.go | 84 +++++++++++++++++ 12 files changed, 379 insertions(+) create mode 100644 api/v1alpha1/hmcprovideraws_types.go create mode 100644 config/rbac/hmcprovideraws_editor_role.yaml create mode 100644 config/rbac/hmcprovideraws_viewer_role.yaml create mode 100644 config/samples/hmc.mirantis.com_v1alpha1_hmcprovideraws.yaml create mode 100644 internal/controller/hmcprovideraws_controller.go create mode 100644 internal/controller/hmcprovideraws_controller_test.go diff --git a/PROJECT b/PROJECT index 4d009fafb..fec5387e4 100644 --- a/PROJECT +++ b/PROJECT @@ -35,4 +35,13 @@ resources: kind: HMCManagement 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: HMCProviderAWS + path: github.com/Mirantis/hmc/api/v1alpha1 + version: v1alpha1 version: "3" diff --git a/api/v1alpha1/hmcprovideraws_types.go b/api/v1alpha1/hmcprovideraws_types.go new file mode 100644 index 000000000..e5937e2c9 --- /dev/null +++ b/api/v1alpha1/hmcprovideraws_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. + +// HMCProviderAWSSpec defines the desired state of HMCProviderAWS +type HMCProviderAWSSpec 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 HMCProviderAWS. Edit hmcprovideraws_types.go to remove/update + Foo string `json:"foo,omitempty"` +} + +// HMCProviderAWSStatus defines the observed state of HMCProviderAWS +type HMCProviderAWSStatus 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 + +// HMCProviderAWS is the Schema for the hmcprovideraws API +type HMCProviderAWS struct { + metav1.TypeMeta `json:",inline"` + metav1.ObjectMeta `json:"metadata,omitempty"` + + Spec HMCProviderAWSSpec `json:"spec,omitempty"` + Status HMCProviderAWSStatus `json:"status,omitempty"` +} + +//+kubebuilder:object:root=true + +// HMCProviderAWSList contains a list of HMCProviderAWS +type HMCProviderAWSList struct { + metav1.TypeMeta `json:",inline"` + metav1.ListMeta `json:"metadata,omitempty"` + Items []HMCProviderAWS `json:"items"` +} + +func init() { + SchemeBuilder.Register(&HMCProviderAWS{}, &HMCProviderAWSList{}) +} diff --git a/api/v1alpha1/zz_generated.deepcopy.go b/api/v1alpha1/zz_generated.deepcopy.go index 874e26cd3..7a3b6384c 100644 --- a/api/v1alpha1/zz_generated.deepcopy.go +++ b/api/v1alpha1/zz_generated.deepcopy.go @@ -202,6 +202,95 @@ func (in *HMCManagementStatus) DeepCopy() *HMCManagementStatus { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *HMCProviderAWS) DeepCopyInto(out *HMCProviderAWS) { + *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 HMCProviderAWS. +func (in *HMCProviderAWS) DeepCopy() *HMCProviderAWS { + if in == nil { + return nil + } + out := new(HMCProviderAWS) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *HMCProviderAWS) 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 *HMCProviderAWSList) DeepCopyInto(out *HMCProviderAWSList) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ListMeta.DeepCopyInto(&out.ListMeta) + if in.Items != nil { + in, out := &in.Items, &out.Items + *out = make([]HMCProviderAWS, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new HMCProviderAWSList. +func (in *HMCProviderAWSList) DeepCopy() *HMCProviderAWSList { + if in == nil { + return nil + } + out := new(HMCProviderAWSList) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *HMCProviderAWSList) 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 *HMCProviderAWSSpec) DeepCopyInto(out *HMCProviderAWSSpec) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new HMCProviderAWSSpec. +func (in *HMCProviderAWSSpec) DeepCopy() *HMCProviderAWSSpec { + if in == nil { + return nil + } + out := new(HMCProviderAWSSpec) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *HMCProviderAWSStatus) DeepCopyInto(out *HMCProviderAWSStatus) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new HMCProviderAWSStatus. +func (in *HMCProviderAWSStatus) DeepCopy() *HMCProviderAWSStatus { + if in == nil { + return nil + } + out := new(HMCProviderAWSStatus) + 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 876b70f2c..d631fa3ae 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -143,6 +143,13 @@ func main() { setupLog.Error(err, "unable to create controller", "controller", "HMCManagement") os.Exit(1) } + if err = (&controller.HMCProviderAWSReconciler{ + Client: mgr.GetClient(), + Scheme: mgr.GetScheme(), + }).SetupWithManager(mgr); err != nil { + setupLog.Error(err, "unable to create controller", "controller", "HMCProviderAWS") + 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 a3d96ad45..5acf70a1f 100644 --- a/config/crd/kustomization.yaml +++ b/config/crd/kustomization.yaml @@ -5,6 +5,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 +- bases/hmc.mirantis.com.hmc.mirantis.com_hmcprovideraws.yaml #+kubebuilder:scaffold:crdkustomizeresource patches: @@ -17,6 +18,7 @@ patches: #- path: patches/cainjection_in_hmctemplates.yaml #- path: patches/cainjection_in_hmcdeployments.yaml #- path: patches/cainjection_in_hmcmanagements.yaml +#- path: patches/cainjection_in_hmcprovideraws.yaml #+kubebuilder:scaffold:crdkustomizecainjectionpatch # [WEBHOOK] To enable webhook, uncomment the following section diff --git a/config/rbac/hmcprovideraws_editor_role.yaml b/config/rbac/hmcprovideraws_editor_role.yaml new file mode 100644 index 000000000..443b00799 --- /dev/null +++ b/config/rbac/hmcprovideraws_editor_role.yaml @@ -0,0 +1,27 @@ +# permissions for end users to edit hmcprovideraws. +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + labels: + app.kubernetes.io/name: hmc + app.kubernetes.io/managed-by: kustomize + name: hmcprovideraws-editor-role +rules: +- apiGroups: + - hmc.mirantis.com.hmc.mirantis.com + resources: + - hmcprovideraws + verbs: + - create + - delete + - get + - list + - patch + - update + - watch +- apiGroups: + - hmc.mirantis.com.hmc.mirantis.com + resources: + - hmcprovideraws/status + verbs: + - get diff --git a/config/rbac/hmcprovideraws_viewer_role.yaml b/config/rbac/hmcprovideraws_viewer_role.yaml new file mode 100644 index 000000000..9a0be6958 --- /dev/null +++ b/config/rbac/hmcprovideraws_viewer_role.yaml @@ -0,0 +1,23 @@ +# permissions for end users to view hmcprovideraws. +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + labels: + app.kubernetes.io/name: hmc + app.kubernetes.io/managed-by: kustomize + name: hmcprovideraws-viewer-role +rules: +- apiGroups: + - hmc.mirantis.com.hmc.mirantis.com + resources: + - hmcprovideraws + verbs: + - get + - list + - watch +- apiGroups: + - hmc.mirantis.com.hmc.mirantis.com + resources: + - hmcprovideraws/status + verbs: + - get diff --git a/config/rbac/kustomization.yaml b/config/rbac/kustomization.yaml index 85659d633..978dfb38e 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. +- hmcprovideraws_editor_role.yaml +- hmcprovideraws_viewer_role.yaml - hmcmanagement_editor_role.yaml - hmcmanagement_viewer_role.yaml - hmcdeployment_editor_role.yaml diff --git a/config/samples/hmc.mirantis.com_v1alpha1_hmcprovideraws.yaml b/config/samples/hmc.mirantis.com_v1alpha1_hmcprovideraws.yaml new file mode 100644 index 000000000..97fc7d49c --- /dev/null +++ b/config/samples/hmc.mirantis.com_v1alpha1_hmcprovideraws.yaml @@ -0,0 +1,9 @@ +apiVersion: hmc.mirantis.com.hmc.mirantis.com/v1alpha1 +kind: HMCProviderAWS +metadata: + labels: + app.kubernetes.io/name: hmc + app.kubernetes.io/managed-by: kustomize + name: hmcprovideraws-sample +spec: + # TODO(user): Add fields here diff --git a/config/samples/kustomization.yaml b/config/samples/kustomization.yaml index cb1e96b3b..5213ca4ae 100644 --- a/config/samples/kustomization.yaml +++ b/config/samples/kustomization.yaml @@ -3,4 +3,5 @@ resources: - hmc.mirantis.com_v1alpha1_hmctemplate.yaml - hmc.mirantis.com_v1alpha1_hmcdeployment.yaml - hmc.mirantis.com_v1alpha1_hmcmanagement.yaml +- hmc.mirantis.com_v1alpha1_hmcprovideraws.yaml #+kubebuilder:scaffold:manifestskustomizesamples diff --git a/internal/controller/hmcprovideraws_controller.go b/internal/controller/hmcprovideraws_controller.go new file mode 100644 index 000000000..9b9613595 --- /dev/null +++ b/internal/controller/hmcprovideraws_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" +) + +// HMCProviderAWSReconciler reconciles a HMCProviderAWS object +type HMCProviderAWSReconciler struct { + client.Client + Scheme *runtime.Scheme +} + +//+kubebuilder:rbac:groups=hmc.mirantis.com.hmc.mirantis.com,resources=hmcprovideraws,verbs=get;list;watch;create;update;patch;delete +//+kubebuilder:rbac:groups=hmc.mirantis.com.hmc.mirantis.com,resources=hmcprovideraws/status,verbs=get;update;patch +//+kubebuilder:rbac:groups=hmc.mirantis.com.hmc.mirantis.com,resources=hmcprovideraws/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 HMCProviderAWS 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 *HMCProviderAWSReconciler) 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 *HMCProviderAWSReconciler) SetupWithManager(mgr ctrl.Manager) error { + return ctrl.NewControllerManagedBy(mgr). + For(&hmcmirantiscomv1alpha1.HMCProviderAWS{}). + Complete(r) +} diff --git a/internal/controller/hmcprovideraws_controller_test.go b/internal/controller/hmcprovideraws_controller_test.go new file mode 100644 index 000000000..a05db8d5a --- /dev/null +++ b/internal/controller/hmcprovideraws_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("HMCProviderAWS 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 + } + hmcprovideraws := &hmcmirantiscomv1alpha1.HMCProviderAWS{} + + BeforeEach(func() { + By("creating the custom resource for the Kind HMCProviderAWS") + err := k8sClient.Get(ctx, typeNamespacedName, hmcprovideraws) + if err != nil && errors.IsNotFound(err) { + resource := &hmcmirantiscomv1alpha1.HMCProviderAWS{ + 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.HMCProviderAWS{} + err := k8sClient.Get(ctx, typeNamespacedName, resource) + Expect(err).NotTo(HaveOccurred()) + + By("Cleanup the specific resource instance HMCProviderAWS") + Expect(k8sClient.Delete(ctx, resource)).To(Succeed()) + }) + It("should successfully reconcile the resource", func() { + By("Reconciling the created resource") + controllerReconciler := &HMCProviderAWSReconciler{ + 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. + }) + }) +})