diff --git a/PROJECT b/PROJECT index d45a60a..0ed4cbb 100644 --- a/PROJECT +++ b/PROJECT @@ -30,4 +30,13 @@ resources: kind: ClusterTunnel path: github.com/adyanth/cloudflare-operator/api/v1alpha1 version: v1alpha1 +- api: + crdVersion: v1 + namespaced: true + controller: true + domain: cfargotunnel.com + group: networking + kind: AccessService + path: github.com/adyanth/cloudflare-operator/api/v1alpha1 + version: v1alpha1 version: "3" diff --git a/api/v1alpha1/accessservice_types.go b/api/v1alpha1/accessservice_types.go new file mode 100644 index 0000000..c509b73 --- /dev/null +++ b/api/v1alpha1/accessservice_types.go @@ -0,0 +1,64 @@ +/* +Copyright 2022. + +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. + +// AccessServiceSpec defines the desired state of AccessService +type AccessServiceSpec 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 AccessService. Edit accessservice_types.go to remove/update + Foo string `json:"foo,omitempty"` +} + +// AccessServiceStatus defines the observed state of AccessService +type AccessServiceStatus 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 + +// AccessService is the Schema for the accessservices API +type AccessService struct { + metav1.TypeMeta `json:",inline"` + metav1.ObjectMeta `json:"metadata,omitempty"` + + Spec AccessServiceSpec `json:"spec,omitempty"` + Status AccessServiceStatus `json:"status,omitempty"` +} + +//+kubebuilder:object:root=true + +// AccessServiceList contains a list of AccessService +type AccessServiceList struct { + metav1.TypeMeta `json:",inline"` + metav1.ListMeta `json:"metadata,omitempty"` + Items []AccessService `json:"items"` +} + +func init() { + SchemeBuilder.Register(&AccessService{}, &AccessServiceList{}) +} diff --git a/api/v1alpha1/zz_generated.deepcopy.go b/api/v1alpha1/zz_generated.deepcopy.go index 153967f..4cf7a81 100644 --- a/api/v1alpha1/zz_generated.deepcopy.go +++ b/api/v1alpha1/zz_generated.deepcopy.go @@ -25,6 +25,95 @@ 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 *AccessService) DeepCopyInto(out *AccessService) { + *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 AccessService. +func (in *AccessService) DeepCopy() *AccessService { + if in == nil { + return nil + } + out := new(AccessService) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *AccessService) 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 *AccessServiceList) DeepCopyInto(out *AccessServiceList) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ListMeta.DeepCopyInto(&out.ListMeta) + if in.Items != nil { + in, out := &in.Items, &out.Items + *out = make([]AccessService, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new AccessServiceList. +func (in *AccessServiceList) DeepCopy() *AccessServiceList { + if in == nil { + return nil + } + out := new(AccessServiceList) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *AccessServiceList) 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 *AccessServiceSpec) DeepCopyInto(out *AccessServiceSpec) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new AccessServiceSpec. +func (in *AccessServiceSpec) DeepCopy() *AccessServiceSpec { + if in == nil { + return nil + } + out := new(AccessServiceSpec) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *AccessServiceStatus) DeepCopyInto(out *AccessServiceStatus) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new AccessServiceStatus. +func (in *AccessServiceStatus) DeepCopy() *AccessServiceStatus { + if in == nil { + return nil + } + out := new(AccessServiceStatus) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *CloudflareDetails) DeepCopyInto(out *CloudflareDetails) { *out = *in diff --git a/config/crd/kustomization.yaml b/config/crd/kustomization.yaml index c5325ef..e62cc58 100644 --- a/config/crd/kustomization.yaml +++ b/config/crd/kustomization.yaml @@ -5,6 +5,7 @@ resources: - bases/networking.cfargotunnel.com_tunnels.yaml - bases/networking.cfargotunnel.com_clustertunnels.yaml - bases/networking.cfargotunnel.com_tunnelbindings.yaml +- bases/networking.cfargotunnel.com_accessservices.yaml #+kubebuilder:scaffold:crdkustomizeresource patchesStrategicMerge: @@ -13,6 +14,7 @@ patchesStrategicMerge: #- patches/webhook_in_tunnels.yaml #- patches/webhook_in_clustertunnels.yaml #- patches/webhook_in_tunnelbindings.yaml +#- patches/webhook_in_accessservices.yaml #+kubebuilder:scaffold:crdkustomizewebhookpatch # [CERTMANAGER] To enable cert-manager, uncomment all the sections with [CERTMANAGER] prefix. @@ -20,6 +22,7 @@ patchesStrategicMerge: #- patches/cainjection_in_tunnels.yaml #- patches/cainjection_in_clustertunnels.yaml #- patches/cainjection_in_tunnelbindings.yaml +#- patches/cainjection_in_accessservices.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_accessservices.yaml b/config/crd/patches/cainjection_in_accessservices.yaml new file mode 100644 index 0000000..3248b0a --- /dev/null +++ b/config/crd/patches/cainjection_in_accessservices.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: accessservices.networking.cfargotunnel.com diff --git a/config/crd/patches/webhook_in_accessservices.yaml b/config/crd/patches/webhook_in_accessservices.yaml new file mode 100644 index 0000000..e098118 --- /dev/null +++ b/config/crd/patches/webhook_in_accessservices.yaml @@ -0,0 +1,16 @@ +# The following patch enables a conversion webhook for the CRD +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + name: accessservices.networking.cfargotunnel.com +spec: + conversion: + strategy: Webhook + webhook: + clientConfig: + service: + namespace: system + name: webhook-service + path: /convert + conversionReviewVersions: + - v1 diff --git a/config/rbac/accessservice_editor_role.yaml b/config/rbac/accessservice_editor_role.yaml new file mode 100644 index 0000000..42150ae --- /dev/null +++ b/config/rbac/accessservice_editor_role.yaml @@ -0,0 +1,31 @@ +# permissions for end users to edit accessservices. +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + labels: + app.kubernetes.io/name: clusterrole + app.kubernetes.io/instance: accessservice-editor-role + app.kubernetes.io/component: rbac + app.kubernetes.io/created-by: cloudflare-operator + app.kubernetes.io/part-of: cloudflare-operator + app.kubernetes.io/managed-by: kustomize + name: accessservice-editor-role +rules: +- apiGroups: + - networking.cfargotunnel.com + resources: + - accessservices + verbs: + - create + - delete + - get + - list + - patch + - update + - watch +- apiGroups: + - networking.cfargotunnel.com + resources: + - accessservices/status + verbs: + - get diff --git a/config/rbac/accessservice_viewer_role.yaml b/config/rbac/accessservice_viewer_role.yaml new file mode 100644 index 0000000..1f4f9e1 --- /dev/null +++ b/config/rbac/accessservice_viewer_role.yaml @@ -0,0 +1,27 @@ +# permissions for end users to view accessservices. +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + labels: + app.kubernetes.io/name: clusterrole + app.kubernetes.io/instance: accessservice-viewer-role + app.kubernetes.io/component: rbac + app.kubernetes.io/created-by: cloudflare-operator + app.kubernetes.io/part-of: cloudflare-operator + app.kubernetes.io/managed-by: kustomize + name: accessservice-viewer-role +rules: +- apiGroups: + - networking.cfargotunnel.com + resources: + - accessservices + verbs: + - get + - list + - watch +- apiGroups: + - networking.cfargotunnel.com + resources: + - accessservices/status + verbs: + - get diff --git a/config/samples/kustomization.yaml b/config/samples/kustomization.yaml index e7477b7..1813e32 100644 --- a/config/samples/kustomization.yaml +++ b/config/samples/kustomization.yaml @@ -7,4 +7,5 @@ resources: - v1_service.yaml - networking_v1alpha1_clustertunnel.yaml - networking_v1alpha1_tunnelbinding.yaml +- networking_v1alpha1_accessservice.yaml #+kubebuilder:scaffold:manifestskustomizesamples diff --git a/config/samples/networking_v1alpha1_accessservice.yaml b/config/samples/networking_v1alpha1_accessservice.yaml new file mode 100644 index 0000000..cd785cd --- /dev/null +++ b/config/samples/networking_v1alpha1_accessservice.yaml @@ -0,0 +1,12 @@ +apiVersion: networking.cfargotunnel.com/v1alpha1 +kind: AccessService +metadata: + labels: + app.kubernetes.io/name: accessservice + app.kubernetes.io/instance: accessservice-sample + app.kubernetes.io/part-of: cloudflare-operator + app.kubernetes.io/managed-by: kustomize + app.kubernetes.io/created-by: cloudflare-operator + name: accessservice-sample +spec: + # TODO(user): Add fields here diff --git a/controllers/accessservice_controller.go b/controllers/accessservice_controller.go new file mode 100644 index 0000000..7779818 --- /dev/null +++ b/controllers/accessservice_controller.go @@ -0,0 +1,62 @@ +/* +Copyright 2022. + +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" + + "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" + + networkingv1alpha1 "github.com/adyanth/cloudflare-operator/api/v1alpha1" +) + +// AccessServiceReconciler reconciles a AccessService object +type AccessServiceReconciler struct { + client.Client + Scheme *runtime.Scheme +} + +//+kubebuilder:rbac:groups=networking.cfargotunnel.com,resources=accessservices,verbs=get;list;watch;create;update;patch;delete +//+kubebuilder:rbac:groups=networking.cfargotunnel.com,resources=accessservices/status,verbs=get;update;patch +//+kubebuilder:rbac:groups=networking.cfargotunnel.com,resources=accessservices/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 AccessService 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.13.0/pkg/reconcile +func (r *AccessServiceReconciler) 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 *AccessServiceReconciler) SetupWithManager(mgr ctrl.Manager) error { + return ctrl.NewControllerManagedBy(mgr). + For(&networkingv1alpha1.AccessService{}). + Complete(r) +} diff --git a/go.sum b/go.sum index 87abf9a..86ed184 100644 --- a/go.sum +++ b/go.sum @@ -80,7 +80,6 @@ github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+Ce github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= -github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE= diff --git a/main.go b/main.go index 16a8bdc..a03e1e2 100644 --- a/main.go +++ b/main.go @@ -106,6 +106,13 @@ func main() { setupLog.Error(err, "unable to create controller", "controller", "ClusterTunnel") os.Exit(1) } + if err = (&controllers.AccessServiceReconciler{ + Client: mgr.GetClient(), + Scheme: mgr.GetScheme(), + }).SetupWithManager(mgr); err != nil { + setupLog.Error(err, "unable to create controller", "controller", "AccessService") + os.Exit(1) + } //+kubebuilder:scaffold:builder if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {