diff --git a/services/dis-apim-operator/PROJECT b/services/dis-apim-operator/PROJECT index 99840efb..72e837e8 100644 --- a/services/dis-apim-operator/PROJECT +++ b/services/dis-apim-operator/PROJECT @@ -20,4 +20,22 @@ resources: webhooks: defaulting: true webhookVersion: v1 +- api: + crdVersion: v1 + namespaced: true + controller: true + domain: dis.altinn.cloud + group: apim + kind: Api + path: github.com/Altinn/altinn-platform/services/dis-apim-operator/api/v1alpha1 + version: v1alpha1 +- api: + crdVersion: v1 + namespaced: true + controller: true + domain: dis.altinn.cloud + group: apim + kind: ApiVersion + path: github.com/Altinn/altinn-platform/services/dis-apim-operator/api/v1alpha1 + version: v1alpha1 version: "3" diff --git a/services/dis-apim-operator/api/v1alpha1/api_types.go b/services/dis-apim-operator/api/v1alpha1/api_types.go new file mode 100644 index 00000000..8e469515 --- /dev/null +++ b/services/dis-apim-operator/api/v1alpha1/api_types.go @@ -0,0 +1,64 @@ +/* +Copyright 2024 altinn. + +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. + +// ApiSpec defines the desired state of Api. +type ApiSpec 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 Api. Edit api_types.go to remove/update + Foo string `json:"foo,omitempty"` +} + +// ApiStatus defines the observed state of Api. +type ApiStatus 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 + +// Api is the Schema for the apis API. +type Api struct { + metav1.TypeMeta `json:",inline"` + metav1.ObjectMeta `json:"metadata,omitempty"` + + Spec ApiSpec `json:"spec,omitempty"` + Status ApiStatus `json:"status,omitempty"` +} + +// +kubebuilder:object:root=true + +// ApiList contains a list of Api. +type ApiList struct { + metav1.TypeMeta `json:",inline"` + metav1.ListMeta `json:"metadata,omitempty"` + Items []Api `json:"items"` +} + +func init() { + SchemeBuilder.Register(&Api{}, &ApiList{}) +} diff --git a/services/dis-apim-operator/api/v1alpha1/apiversion_types.go b/services/dis-apim-operator/api/v1alpha1/apiversion_types.go new file mode 100644 index 00000000..d4eaa7d0 --- /dev/null +++ b/services/dis-apim-operator/api/v1alpha1/apiversion_types.go @@ -0,0 +1,64 @@ +/* +Copyright 2024 altinn. + +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. + +// ApiVersionSpec defines the desired state of ApiVersion. +type ApiVersionSpec 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 ApiVersion. Edit apiversion_types.go to remove/update + Foo string `json:"foo,omitempty"` +} + +// ApiVersionStatus defines the observed state of ApiVersion. +type ApiVersionStatus 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 + +// ApiVersion is the Schema for the apiversions API. +type ApiVersion struct { + metav1.TypeMeta `json:",inline"` + metav1.ObjectMeta `json:"metadata,omitempty"` + + Spec ApiVersionSpec `json:"spec,omitempty"` + Status ApiVersionStatus `json:"status,omitempty"` +} + +// +kubebuilder:object:root=true + +// ApiVersionList contains a list of ApiVersion. +type ApiVersionList struct { + metav1.TypeMeta `json:",inline"` + metav1.ListMeta `json:"metadata,omitempty"` + Items []ApiVersion `json:"items"` +} + +func init() { + SchemeBuilder.Register(&ApiVersion{}, &ApiVersionList{}) +} diff --git a/services/dis-apim-operator/api/v1alpha1/zz_generated.deepcopy.go b/services/dis-apim-operator/api/v1alpha1/zz_generated.deepcopy.go index 5f7ad017..b61dd50e 100644 --- a/services/dis-apim-operator/api/v1alpha1/zz_generated.deepcopy.go +++ b/services/dis-apim-operator/api/v1alpha1/zz_generated.deepcopy.go @@ -24,6 +24,184 @@ import ( runtime "k8s.io/apimachinery/pkg/runtime" ) +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *Api) DeepCopyInto(out *Api) { + *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 Api. +func (in *Api) DeepCopy() *Api { + if in == nil { + return nil + } + out := new(Api) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *Api) 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 *ApiList) DeepCopyInto(out *ApiList) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ListMeta.DeepCopyInto(&out.ListMeta) + if in.Items != nil { + in, out := &in.Items, &out.Items + *out = make([]Api, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ApiList. +func (in *ApiList) DeepCopy() *ApiList { + if in == nil { + return nil + } + out := new(ApiList) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *ApiList) 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 *ApiSpec) DeepCopyInto(out *ApiSpec) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ApiSpec. +func (in *ApiSpec) DeepCopy() *ApiSpec { + if in == nil { + return nil + } + out := new(ApiSpec) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *ApiStatus) DeepCopyInto(out *ApiStatus) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ApiStatus. +func (in *ApiStatus) DeepCopy() *ApiStatus { + if in == nil { + return nil + } + out := new(ApiStatus) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *ApiVersion) DeepCopyInto(out *ApiVersion) { + *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 ApiVersion. +func (in *ApiVersion) DeepCopy() *ApiVersion { + if in == nil { + return nil + } + out := new(ApiVersion) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *ApiVersion) 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 *ApiVersionList) DeepCopyInto(out *ApiVersionList) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ListMeta.DeepCopyInto(&out.ListMeta) + if in.Items != nil { + in, out := &in.Items, &out.Items + *out = make([]ApiVersion, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ApiVersionList. +func (in *ApiVersionList) DeepCopy() *ApiVersionList { + if in == nil { + return nil + } + out := new(ApiVersionList) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *ApiVersionList) 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 *ApiVersionSpec) DeepCopyInto(out *ApiVersionSpec) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ApiVersionSpec. +func (in *ApiVersionSpec) DeepCopy() *ApiVersionSpec { + if in == nil { + return nil + } + out := new(ApiVersionSpec) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *ApiVersionStatus) DeepCopyInto(out *ApiVersionStatus) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ApiVersionStatus. +func (in *ApiVersionStatus) DeepCopy() *ApiVersionStatus { + if in == nil { + return nil + } + out := new(ApiVersionStatus) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *Backend) DeepCopyInto(out *Backend) { *out = *in diff --git a/services/dis-apim-operator/cmd/main.go b/services/dis-apim-operator/cmd/main.go index ea37eff9..ffacc422 100644 --- a/services/dis-apim-operator/cmd/main.go +++ b/services/dis-apim-operator/cmd/main.go @@ -181,6 +181,20 @@ func main() { os.Exit(1) } } + if err = (&controller.ApiReconciler{ + Client: mgr.GetClient(), + Scheme: mgr.GetScheme(), + }).SetupWithManager(mgr); err != nil { + setupLog.Error(err, "unable to create controller", "controller", "Api") + os.Exit(1) + } + if err = (&controller.ApiVersionReconciler{ + Client: mgr.GetClient(), + Scheme: mgr.GetScheme(), + }).SetupWithManager(mgr); err != nil { + setupLog.Error(err, "unable to create controller", "controller", "ApiVersion") + os.Exit(1) + } // +kubebuilder:scaffold:builder if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil { diff --git a/services/dis-apim-operator/config/crd/kustomization.yaml b/services/dis-apim-operator/config/crd/kustomization.yaml index ffed2166..b57c87c4 100644 --- a/services/dis-apim-operator/config/crd/kustomization.yaml +++ b/services/dis-apim-operator/config/crd/kustomization.yaml @@ -3,6 +3,8 @@ # It should be run by config/default resources: - bases/apim.dis.altinn.cloud_backends.yaml +- bases/apim.dis.altinn.cloud_apis.yaml +- bases/apim.dis.altinn.cloud_apiversions.yaml # +kubebuilder:scaffold:crdkustomizeresource patches: @@ -14,6 +16,8 @@ patches: # [CERTMANAGER] To enable cert-manager, uncomment all the sections with [CERTMANAGER] prefix. # patches here are for enabling the CA injection for each CRD #- path: patches/cainjection_in_backends.yaml +#- path: patches/cainjection_in_apis.yaml +#- path: patches/cainjection_in_apiversions.yaml # +kubebuilder:scaffold:crdkustomizecainjectionpatch # [WEBHOOK] To enable webhook, uncomment the following section diff --git a/services/dis-apim-operator/config/rbac/api_editor_role.yaml b/services/dis-apim-operator/config/rbac/api_editor_role.yaml new file mode 100644 index 00000000..8393c90a --- /dev/null +++ b/services/dis-apim-operator/config/rbac/api_editor_role.yaml @@ -0,0 +1,27 @@ +# permissions for end users to edit apis. +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + labels: + app.kubernetes.io/name: dis-apim-operator + app.kubernetes.io/managed-by: kustomize + name: api-editor-role +rules: +- apiGroups: + - apim.dis.altinn.cloud + resources: + - apis + verbs: + - create + - delete + - get + - list + - patch + - update + - watch +- apiGroups: + - apim.dis.altinn.cloud + resources: + - apis/status + verbs: + - get diff --git a/services/dis-apim-operator/config/rbac/api_viewer_role.yaml b/services/dis-apim-operator/config/rbac/api_viewer_role.yaml new file mode 100644 index 00000000..3f687af3 --- /dev/null +++ b/services/dis-apim-operator/config/rbac/api_viewer_role.yaml @@ -0,0 +1,23 @@ +# permissions for end users to view apis. +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + labels: + app.kubernetes.io/name: dis-apim-operator + app.kubernetes.io/managed-by: kustomize + name: api-viewer-role +rules: +- apiGroups: + - apim.dis.altinn.cloud + resources: + - apis + verbs: + - get + - list + - watch +- apiGroups: + - apim.dis.altinn.cloud + resources: + - apis/status + verbs: + - get diff --git a/services/dis-apim-operator/config/rbac/apiversion_editor_role.yaml b/services/dis-apim-operator/config/rbac/apiversion_editor_role.yaml new file mode 100644 index 00000000..9e12d716 --- /dev/null +++ b/services/dis-apim-operator/config/rbac/apiversion_editor_role.yaml @@ -0,0 +1,27 @@ +# permissions for end users to edit apiversions. +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + labels: + app.kubernetes.io/name: dis-apim-operator + app.kubernetes.io/managed-by: kustomize + name: apiversion-editor-role +rules: +- apiGroups: + - apim.dis.altinn.cloud + resources: + - apiversions + verbs: + - create + - delete + - get + - list + - patch + - update + - watch +- apiGroups: + - apim.dis.altinn.cloud + resources: + - apiversions/status + verbs: + - get diff --git a/services/dis-apim-operator/config/rbac/apiversion_viewer_role.yaml b/services/dis-apim-operator/config/rbac/apiversion_viewer_role.yaml new file mode 100644 index 00000000..f7e3c33c --- /dev/null +++ b/services/dis-apim-operator/config/rbac/apiversion_viewer_role.yaml @@ -0,0 +1,23 @@ +# permissions for end users to view apiversions. +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + labels: + app.kubernetes.io/name: dis-apim-operator + app.kubernetes.io/managed-by: kustomize + name: apiversion-viewer-role +rules: +- apiGroups: + - apim.dis.altinn.cloud + resources: + - apiversions + verbs: + - get + - list + - watch +- apiGroups: + - apim.dis.altinn.cloud + resources: + - apiversions/status + verbs: + - get diff --git a/services/dis-apim-operator/config/rbac/kustomization.yaml b/services/dis-apim-operator/config/rbac/kustomization.yaml index 653bd92e..2ab554d7 100644 --- a/services/dis-apim-operator/config/rbac/kustomization.yaml +++ b/services/dis-apim-operator/config/rbac/kustomization.yaml @@ -22,6 +22,10 @@ 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. +- apiversion_editor_role.yaml +- apiversion_viewer_role.yaml +- api_editor_role.yaml +- api_viewer_role.yaml - backend_editor_role.yaml - backend_viewer_role.yaml diff --git a/services/dis-apim-operator/config/samples/apim_v1alpha1_api.yaml b/services/dis-apim-operator/config/samples/apim_v1alpha1_api.yaml new file mode 100644 index 00000000..91d92c80 --- /dev/null +++ b/services/dis-apim-operator/config/samples/apim_v1alpha1_api.yaml @@ -0,0 +1,9 @@ +apiVersion: apim.dis.altinn.cloud/v1alpha1 +kind: Api +metadata: + labels: + app.kubernetes.io/name: dis-apim-operator + app.kubernetes.io/managed-by: kustomize + name: api-sample +spec: + # TODO(user): Add fields here diff --git a/services/dis-apim-operator/config/samples/apim_v1alpha1_apiversion.yaml b/services/dis-apim-operator/config/samples/apim_v1alpha1_apiversion.yaml new file mode 100644 index 00000000..c9b687c7 --- /dev/null +++ b/services/dis-apim-operator/config/samples/apim_v1alpha1_apiversion.yaml @@ -0,0 +1,9 @@ +apiVersion: apim.dis.altinn.cloud/v1alpha1 +kind: ApiVersion +metadata: + labels: + app.kubernetes.io/name: dis-apim-operator + app.kubernetes.io/managed-by: kustomize + name: apiversion-sample +spec: + # TODO(user): Add fields here diff --git a/services/dis-apim-operator/config/samples/kustomization.yaml b/services/dis-apim-operator/config/samples/kustomization.yaml index 364efc2f..5ca984c9 100644 --- a/services/dis-apim-operator/config/samples/kustomization.yaml +++ b/services/dis-apim-operator/config/samples/kustomization.yaml @@ -1,4 +1,6 @@ ## Append samples of your project ## resources: - apim_v1alpha1_backend.yaml +- apim_v1alpha1_api.yaml +- apim_v1alpha1_apiversion.yaml # +kubebuilder:scaffold:manifestskustomizesamples diff --git a/services/dis-apim-operator/internal/controller/api_controller.go b/services/dis-apim-operator/internal/controller/api_controller.go new file mode 100644 index 00000000..003b39fd --- /dev/null +++ b/services/dis-apim-operator/internal/controller/api_controller.go @@ -0,0 +1,63 @@ +/* +Copyright 2024 altinn. + +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" + + apimv1alpha1 "github.com/Altinn/altinn-platform/services/dis-apim-operator/api/v1alpha1" +) + +// ApiReconciler reconciles a Api object +type ApiReconciler struct { + client.Client + Scheme *runtime.Scheme +} + +// +kubebuilder:rbac:groups=apim.dis.altinn.cloud,resources=apis,verbs=get;list;watch;create;update;patch;delete +// +kubebuilder:rbac:groups=apim.dis.altinn.cloud,resources=apis/status,verbs=get;update;patch +// +kubebuilder:rbac:groups=apim.dis.altinn.cloud,resources=apis/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 Api 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.19.0/pkg/reconcile +func (r *ApiReconciler) 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 *ApiReconciler) SetupWithManager(mgr ctrl.Manager) error { + return ctrl.NewControllerManagedBy(mgr). + For(&apimv1alpha1.Api{}). + Named("api"). + Complete(r) +} diff --git a/services/dis-apim-operator/internal/controller/api_controller_test.go b/services/dis-apim-operator/internal/controller/api_controller_test.go new file mode 100644 index 00000000..e296d288 --- /dev/null +++ b/services/dis-apim-operator/internal/controller/api_controller_test.go @@ -0,0 +1,84 @@ +/* +Copyright 2024 altinn. + +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" + + apimv1alpha1 "github.com/Altinn/altinn-platform/services/dis-apim-operator/api/v1alpha1" +) + +var _ = Describe("Api 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 + } + api := &apimv1alpha1.Api{} + + BeforeEach(func() { + By("creating the custom resource for the Kind Api") + err := k8sClient.Get(ctx, typeNamespacedName, api) + if err != nil && errors.IsNotFound(err) { + resource := &apimv1alpha1.Api{ + 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 := &apimv1alpha1.Api{} + err := k8sClient.Get(ctx, typeNamespacedName, resource) + Expect(err).NotTo(HaveOccurred()) + + By("Cleanup the specific resource instance Api") + Expect(k8sClient.Delete(ctx, resource)).To(Succeed()) + }) + It("should successfully reconcile the resource", func() { + By("Reconciling the created resource") + controllerReconciler := &ApiReconciler{ + 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. + }) + }) +}) diff --git a/services/dis-apim-operator/internal/controller/apiversion_controller.go b/services/dis-apim-operator/internal/controller/apiversion_controller.go new file mode 100644 index 00000000..664c4c6e --- /dev/null +++ b/services/dis-apim-operator/internal/controller/apiversion_controller.go @@ -0,0 +1,63 @@ +/* +Copyright 2024 altinn. + +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" + + apimv1alpha1 "github.com/Altinn/altinn-platform/services/dis-apim-operator/api/v1alpha1" +) + +// ApiVersionReconciler reconciles a ApiVersion object +type ApiVersionReconciler struct { + client.Client + Scheme *runtime.Scheme +} + +// +kubebuilder:rbac:groups=apim.dis.altinn.cloud,resources=apiversions,verbs=get;list;watch;create;update;patch;delete +// +kubebuilder:rbac:groups=apim.dis.altinn.cloud,resources=apiversions/status,verbs=get;update;patch +// +kubebuilder:rbac:groups=apim.dis.altinn.cloud,resources=apiversions/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 ApiVersion 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.19.0/pkg/reconcile +func (r *ApiVersionReconciler) 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 *ApiVersionReconciler) SetupWithManager(mgr ctrl.Manager) error { + return ctrl.NewControllerManagedBy(mgr). + For(&apimv1alpha1.ApiVersion{}). + Named("apiversion"). + Complete(r) +} diff --git a/services/dis-apim-operator/internal/controller/apiversion_controller_test.go b/services/dis-apim-operator/internal/controller/apiversion_controller_test.go new file mode 100644 index 00000000..787e417a --- /dev/null +++ b/services/dis-apim-operator/internal/controller/apiversion_controller_test.go @@ -0,0 +1,84 @@ +/* +Copyright 2024 altinn. + +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" + + apimv1alpha1 "github.com/Altinn/altinn-platform/services/dis-apim-operator/api/v1alpha1" +) + +var _ = Describe("ApiVersion 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 + } + apiversion := &apimv1alpha1.ApiVersion{} + + BeforeEach(func() { + By("creating the custom resource for the Kind ApiVersion") + err := k8sClient.Get(ctx, typeNamespacedName, apiversion) + if err != nil && errors.IsNotFound(err) { + resource := &apimv1alpha1.ApiVersion{ + 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 := &apimv1alpha1.ApiVersion{} + err := k8sClient.Get(ctx, typeNamespacedName, resource) + Expect(err).NotTo(HaveOccurred()) + + By("Cleanup the specific resource instance ApiVersion") + Expect(k8sClient.Delete(ctx, resource)).To(Succeed()) + }) + It("should successfully reconcile the resource", func() { + By("Reconciling the created resource") + controllerReconciler := &ApiVersionReconciler{ + 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. + }) + }) +})