From e69fd4023552a456209b557c748aa4bf1a1d42cd Mon Sep 17 00:00:00 2001 From: Dan Prince Date: Wed, 18 Aug 2021 15:52:34 -0400 Subject: [PATCH] Generate OpenStackConfigVersion API/controller. --- PROJECT | 9 ++ api/v1beta1/openstackconfigversion_types.go | 64 +++++++++++++ api/v1beta1/zz_generated.deepcopy.go | 89 +++++++++++++++++++ config/crd/kustomization.yaml | 3 + ...ainjection_in_openstackconfigversions.yaml | 7 ++ .../webhook_in_openstackconfigversions.yaml | 14 +++ .../openstackconfigversion_editor_role.yaml | 24 +++++ .../openstackconfigversion_viewer_role.yaml | 20 +++++ ...rector_v1beta1_openstackconfigversion.yaml | 7 ++ .../openstackconfigversion_controller.go | 63 +++++++++++++ main.go | 8 ++ 11 files changed, 308 insertions(+) create mode 100644 api/v1beta1/openstackconfigversion_types.go create mode 100644 config/crd/patches/cainjection_in_openstackconfigversions.yaml create mode 100644 config/crd/patches/webhook_in_openstackconfigversions.yaml create mode 100644 config/rbac/openstackconfigversion_editor_role.yaml create mode 100644 config/rbac/openstackconfigversion_viewer_role.yaml create mode 100644 config/samples/osp-director_v1beta1_openstackconfigversion.yaml create mode 100644 controllers/openstackconfigversion_controller.go diff --git a/PROJECT b/PROJECT index bb97e201..118f32f8 100644 --- a/PROJECT +++ b/PROJECT @@ -77,4 +77,13 @@ resources: kind: OpenStackMACAddress path: github.com/openstack-k8s-operators/osp-director-operator/api/v1beta1 version: v1beta1 +- api: + crdVersion: v1 + namespaced: true + controller: true + domain: openstack.org + group: osp-director + kind: OpenStackConfigVersion + path: github.com/openstack-k8s-operators/osp-director-operator/api/v1beta1 + version: v1beta1 version: "3" diff --git a/api/v1beta1/openstackconfigversion_types.go b/api/v1beta1/openstackconfigversion_types.go new file mode 100644 index 00000000..39479424 --- /dev/null +++ b/api/v1beta1/openstackconfigversion_types.go @@ -0,0 +1,64 @@ +/* + + +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 v1beta1 + +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. + +// OpenStackConfigVersionSpec defines the desired state of OpenStackConfigVersion +type OpenStackConfigVersionSpec 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 OpenStackConfigVersion. Edit openstackconfigversion_types.go to remove/update + Foo string `json:"foo,omitempty"` +} + +// OpenStackConfigVersionStatus defines the observed state of OpenStackConfigVersion +type OpenStackConfigVersionStatus 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 + +// OpenStackConfigVersion is the Schema for the openstackconfigversions API +type OpenStackConfigVersion struct { + metav1.TypeMeta `json:",inline"` + metav1.ObjectMeta `json:"metadata,omitempty"` + + Spec OpenStackConfigVersionSpec `json:"spec,omitempty"` + Status OpenStackConfigVersionStatus `json:"status,omitempty"` +} + +//+kubebuilder:object:root=true + +// OpenStackConfigVersionList contains a list of OpenStackConfigVersion +type OpenStackConfigVersionList struct { + metav1.TypeMeta `json:",inline"` + metav1.ListMeta `json:"metadata,omitempty"` + Items []OpenStackConfigVersion `json:"items"` +} + +func init() { + SchemeBuilder.Register(&OpenStackConfigVersion{}, &OpenStackConfigVersionList{}) +} diff --git a/api/v1beta1/zz_generated.deepcopy.go b/api/v1beta1/zz_generated.deepcopy.go index 3cc065bb..de89e4b4 100644 --- a/api/v1beta1/zz_generated.deepcopy.go +++ b/api/v1beta1/zz_generated.deepcopy.go @@ -621,6 +621,95 @@ func (in *OpenStackClientStatus) DeepCopy() *OpenStackClientStatus { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *OpenStackConfigVersion) DeepCopyInto(out *OpenStackConfigVersion) { + *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 OpenStackConfigVersion. +func (in *OpenStackConfigVersion) DeepCopy() *OpenStackConfigVersion { + if in == nil { + return nil + } + out := new(OpenStackConfigVersion) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *OpenStackConfigVersion) 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 *OpenStackConfigVersionList) DeepCopyInto(out *OpenStackConfigVersionList) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ListMeta.DeepCopyInto(&out.ListMeta) + if in.Items != nil { + in, out := &in.Items, &out.Items + *out = make([]OpenStackConfigVersion, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new OpenStackConfigVersionList. +func (in *OpenStackConfigVersionList) DeepCopy() *OpenStackConfigVersionList { + if in == nil { + return nil + } + out := new(OpenStackConfigVersionList) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *OpenStackConfigVersionList) 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 *OpenStackConfigVersionSpec) DeepCopyInto(out *OpenStackConfigVersionSpec) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new OpenStackConfigVersionSpec. +func (in *OpenStackConfigVersionSpec) DeepCopy() *OpenStackConfigVersionSpec { + if in == nil { + return nil + } + out := new(OpenStackConfigVersionSpec) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *OpenStackConfigVersionStatus) DeepCopyInto(out *OpenStackConfigVersionStatus) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new OpenStackConfigVersionStatus. +func (in *OpenStackConfigVersionStatus) DeepCopy() *OpenStackConfigVersionStatus { + if in == nil { + return nil + } + out := new(OpenStackConfigVersionStatus) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *OpenStackControlPlane) DeepCopyInto(out *OpenStackControlPlane) { *out = *in diff --git a/config/crd/kustomization.yaml b/config/crd/kustomization.yaml index 4be5b0cd..97c880cc 100644 --- a/config/crd/kustomization.yaml +++ b/config/crd/kustomization.yaml @@ -12,6 +12,7 @@ resources: - bases/osp-director.openstack.org_openstackplaybookgenerators.yaml - bases/osp-director.openstack.org_openstackephemeralheats.yaml - bases/osp-director.openstack.org_openstackmacaddresses.yaml +- bases/osp-director.openstack.org_openstackconfigversions.yaml # +kubebuilder:scaffold:crdkustomizeresource patchesStrategicMerge: @@ -28,6 +29,7 @@ patchesStrategicMerge: #- patches/webhook_in_openstackplaybookgenerators.yaml #- patches/webhook_in_openstackephemeralheats.yaml #- patches/webhook_in_openstackmacaddresses.yaml +#- patches/webhook_in_openstackconfigversions.yaml # +kubebuilder:scaffold:crdkustomizewebhookpatch # [CERTMANAGER] To enable webhook, uncomment all the sections with [CERTMANAGER] prefix. @@ -43,6 +45,7 @@ patchesStrategicMerge: #- patches/cainjection_in_openstackplaybookgenerators.yaml - patches/cainjection_in_openstackephemeralheats.yaml #- patches/cainjection_in_openstackmacaddresses.yaml +#- patches/cainjection_in_openstackconfigversions.yaml # +kubebuilder:scaffold:crdkustomizecainjectionpatch # the following config is for teaching kustomize how to do kustomization for CRDs. diff --git a/config/crd/patches/cainjection_in_openstackconfigversions.yaml b/config/crd/patches/cainjection_in_openstackconfigversions.yaml new file mode 100644 index 00000000..d3ce1972 --- /dev/null +++ b/config/crd/patches/cainjection_in_openstackconfigversions.yaml @@ -0,0 +1,7 @@ +# The following patch adds a directive for certmanager to inject CA into the CRD +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + annotations: + cert-manager.io/inject-ca-from: $(CERTIFICATE_NAMESPACE)/$(CERTIFICATE_NAME) + name: openstackconfigversions.osp-director.openstack.org diff --git a/config/crd/patches/webhook_in_openstackconfigversions.yaml b/config/crd/patches/webhook_in_openstackconfigversions.yaml new file mode 100644 index 00000000..e2aff883 --- /dev/null +++ b/config/crd/patches/webhook_in_openstackconfigversions.yaml @@ -0,0 +1,14 @@ +# The following patch enables a conversion webhook for the CRD +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + name: openstackconfigversions.osp-director.openstack.org +spec: + conversion: + strategy: Webhook + webhook: + clientConfig: + service: + namespace: system + name: webhook-service + path: /convert diff --git a/config/rbac/openstackconfigversion_editor_role.yaml b/config/rbac/openstackconfigversion_editor_role.yaml new file mode 100644 index 00000000..c39d91c5 --- /dev/null +++ b/config/rbac/openstackconfigversion_editor_role.yaml @@ -0,0 +1,24 @@ +# permissions for end users to edit openstackconfigversions. +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + name: openstackconfigversion-editor-role +rules: +- apiGroups: + - osp-director.openstack.org + resources: + - openstackconfigversions + verbs: + - create + - delete + - get + - list + - patch + - update + - watch +- apiGroups: + - osp-director.openstack.org + resources: + - openstackconfigversions/status + verbs: + - get diff --git a/config/rbac/openstackconfigversion_viewer_role.yaml b/config/rbac/openstackconfigversion_viewer_role.yaml new file mode 100644 index 00000000..c8a9c43d --- /dev/null +++ b/config/rbac/openstackconfigversion_viewer_role.yaml @@ -0,0 +1,20 @@ +# permissions for end users to view openstackconfigversions. +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + name: openstackconfigversion-viewer-role +rules: +- apiGroups: + - osp-director.openstack.org + resources: + - openstackconfigversions + verbs: + - get + - list + - watch +- apiGroups: + - osp-director.openstack.org + resources: + - openstackconfigversions/status + verbs: + - get diff --git a/config/samples/osp-director_v1beta1_openstackconfigversion.yaml b/config/samples/osp-director_v1beta1_openstackconfigversion.yaml new file mode 100644 index 00000000..72c1ea8b --- /dev/null +++ b/config/samples/osp-director_v1beta1_openstackconfigversion.yaml @@ -0,0 +1,7 @@ +apiVersion: osp-director.openstack.org/v1beta1 +kind: OpenStackConfigVersion +metadata: + name: openstackconfigversion-sample +spec: + # Add fields here + foo: bar diff --git a/controllers/openstackconfigversion_controller.go b/controllers/openstackconfigversion_controller.go new file mode 100644 index 00000000..fab3c524 --- /dev/null +++ b/controllers/openstackconfigversion_controller.go @@ -0,0 +1,63 @@ +/* + + +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 controllers + +import ( + "context" + + "github.com/go-logr/logr" + "k8s.io/apimachinery/pkg/runtime" + ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/client" + + ospdirectorv1beta1 "github.com/openstack-k8s-operators/osp-director-operator/api/v1beta1" +) + +// OpenStackConfigVersionReconciler reconciles a OpenStackConfigVersion object +type OpenStackConfigVersionReconciler struct { + client.Client + Log logr.Logger + Scheme *runtime.Scheme +} + +//+kubebuilder:rbac:groups=osp-director.openstack.org,resources=openstackconfigversions,verbs=get;list;watch;create;update;patch;delete +//+kubebuilder:rbac:groups=osp-director.openstack.org,resources=openstackconfigversions/status,verbs=get;update;patch +//+kubebuilder:rbac:groups=osp-director.openstack.org,resources=openstackconfigversions/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 OpenStackConfigVersion 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.7.2/pkg/reconcile +func (r *OpenStackConfigVersionReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { + _ = r.Log.WithValues("openstackconfigversion", req.NamespacedName) + + // your logic here + + return ctrl.Result{}, nil +} + +// SetupWithManager sets up the controller with the Manager. +func (r *OpenStackConfigVersionReconciler) SetupWithManager(mgr ctrl.Manager) error { + return ctrl.NewControllerManagedBy(mgr). + For(&ospdirectorv1beta1.OpenStackConfigVersion{}). + Complete(r) +} diff --git a/main.go b/main.go index f7187050..ae1b049f 100644 --- a/main.go +++ b/main.go @@ -263,6 +263,14 @@ func main() { } } + if err = (&controllers.OpenStackConfigVersionReconciler{ + Client: mgr.GetClient(), + Log: ctrl.Log.WithName("controllers").WithName("OpenStackConfigVersion"), + Scheme: mgr.GetScheme(), + }).SetupWithManager(mgr); err != nil { + setupLog.Error(err, "unable to create controller", "controller", "OpenStackConfigVersion") + os.Exit(1) + } // +kubebuilder:scaffold:builder setupLog.Info("starting manager")