---
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
+metadata:
+ annotations:
+ controller-gen.kubebuilder.io/version: v0.2.5
+ creationTimestamp: null
+ name: cnfroutes.batch.sdewan.akraino.org
+spec:
+ group: batch.sdewan.akraino.org
+ names:
+ kind: CNFRoute
+ listKind: CNFRouteList
+ plural: cnfroutes
+ singular: cnfroute
+ scope: Namespaced
+ subresources:
+ status: {}
+ validation:
+ openAPIV3Schema:
+ description: CNFRoute is the Schema for the cnfroutes API
+ properties:
+ apiVersion:
+ description: 'APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
+ type: string
+ kind:
+ description: 'Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
+ type: string
+ metadata:
+ type: object
+ spec:
+ description: CNFRouteSpec defines the desired state of CNFRoute
+ properties:
+ dev:
+ type: string
+ dst:
+ type: string
+ gw:
+ type: string
+ table:
+ enum:
+ - default
+ - cnf
+ type: string
+ type: object
+ status:
+ description: status subsource used for Sdewan rule CRDs
+ properties:
+ appliedGeneration:
+ format: int64
+ type: integer
+ appliedTime:
+ format: date-time
+ type: string
+ message:
+ type: string
+ state:
+ type: string
+ required:
+ - state
+ type: object
+ type: object
+ version: v1alpha1
+ versions:
+ - name: v1alpha1
+ served: true
+ storage: true
+status:
+ acceptedNames:
+ kind: ""
+ plural: ""
+ conditions: []
+ storedVersions: []
+---
+apiVersion: apiextensions.k8s.io/v1beta1
+kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.2.5
- get
- list
- watch
+- apiGroups:
+ - batch.sdewan.akraino.org
+ resources:
+ - cnfroutes
+ verbs:
+ - create
+ - delete
+ - get
+ - list
+ - patch
+ - update
+ - watch
+- apiGroups:
+ - batch.sdewan.akraino.org
+ resources:
+ - cnfroutes/status
+ verbs:
+ - get
+ - patch
+ - update
- apiGroups:
- batch.sdewan.akraino.org
resources:
- group: batch
kind: CNFStatus
version: v1alpha1
+- group: batch
+ kind: CNFRoute
+ version: v1alpha1
version: "2"
--- /dev/null
+/*
+
+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.
+
+// CNFRouteSpec defines the desired state of CNFRoute
+type CNFRouteSpec struct {
+ Dst string `json:"dst,omitempty"`
+ Gw string `json:"gw,omitempty"`
+ Dev string `json:"dev,omitempty"`
+ // +kubebuilder:validation:Enum=default;cnf
+ Table string `json:"table,omitempty"`
+}
+
+// +kubebuilder:object:root=true
+// +kubebuilder:subresource:status
+
+// CNFRoute is the Schema for the cnfroutes API
+type CNFRoute struct {
+ metav1.TypeMeta `json:",inline"`
+ metav1.ObjectMeta `json:"metadata,omitempty"`
+
+ Spec CNFRouteSpec `json:"spec,omitempty"`
+ Status SdewanStatus `json:"status,omitempty"`
+}
+
+// +kubebuilder:object:root=true
+
+// CNFRouteList contains a list of CNFRoute
+type CNFRouteList struct {
+ metav1.TypeMeta `json:",inline"`
+ metav1.ListMeta `json:"metadata,omitempty"`
+ Items []CNFRoute `json:"items"`
+}
+
+func init() {
+ SchemeBuilder.Register(&CNFRoute{}, &CNFRouteList{})
+}
return *out
}
+// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
+func (in *CNFRoute) DeepCopyInto(out *CNFRoute) {
+ *out = *in
+ out.TypeMeta = in.TypeMeta
+ in.ObjectMeta.DeepCopyInto(&out.ObjectMeta)
+ out.Spec = in.Spec
+ in.Status.DeepCopyInto(&out.Status)
+}
+
+// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CNFRoute.
+func (in *CNFRoute) DeepCopy() *CNFRoute {
+ if in == nil {
+ return nil
+ }
+ out := new(CNFRoute)
+ in.DeepCopyInto(out)
+ return out
+}
+
+// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object.
+func (in *CNFRoute) 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 *CNFRouteList) DeepCopyInto(out *CNFRouteList) {
+ *out = *in
+ out.TypeMeta = in.TypeMeta
+ in.ListMeta.DeepCopyInto(&out.ListMeta)
+ if in.Items != nil {
+ in, out := &in.Items, &out.Items
+ *out = make([]CNFRoute, len(*in))
+ for i := range *in {
+ (*in)[i].DeepCopyInto(&(*out)[i])
+ }
+ }
+}
+
+// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CNFRouteList.
+func (in *CNFRouteList) DeepCopy() *CNFRouteList {
+ if in == nil {
+ return nil
+ }
+ out := new(CNFRouteList)
+ in.DeepCopyInto(out)
+ return out
+}
+
+// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object.
+func (in *CNFRouteList) 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 *CNFRouteSpec) DeepCopyInto(out *CNFRouteSpec) {
+ *out = *in
+}
+
+// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CNFRouteSpec.
+func (in *CNFRouteSpec) DeepCopy() *CNFRouteSpec {
+ if in == nil {
+ return nil
+ }
+ out := new(CNFRouteSpec)
+ in.DeepCopyInto(out)
+ return out
+}
+
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *CNFService) DeepCopyInto(out *CNFService) {
*out = *in
--- /dev/null
+
+---
+apiVersion: apiextensions.k8s.io/v1beta1
+kind: CustomResourceDefinition
+metadata:
+ annotations:
+ controller-gen.kubebuilder.io/version: v0.2.5
+ creationTimestamp: null
+ name: cnfroutes.batch.sdewan.akraino.org
+spec:
+ group: batch.sdewan.akraino.org
+ names:
+ kind: CNFRoute
+ listKind: CNFRouteList
+ plural: cnfroutes
+ singular: cnfroute
+ scope: Namespaced
+ subresources:
+ status: {}
+ validation:
+ openAPIV3Schema:
+ description: CNFRoute is the Schema for the cnfroutes API
+ properties:
+ apiVersion:
+ description: 'APIVersion defines the versioned schema of this representation
+ of an object. Servers should convert recognized schemas to the latest
+ internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
+ type: string
+ kind:
+ description: 'Kind is a string value representing the REST resource this
+ object represents. Servers may infer this from the endpoint the client
+ submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
+ type: string
+ metadata:
+ type: object
+ spec:
+ description: CNFRouteSpec defines the desired state of CNFRoute
+ properties:
+ dev:
+ type: string
+ dst:
+ type: string
+ gw:
+ type: string
+ table:
+ enum:
+ - default
+ - cnf
+ type: string
+ type: object
+ status:
+ description: status subsource used for Sdewan rule CRDs
+ properties:
+ appliedGeneration:
+ format: int64
+ type: integer
+ appliedTime:
+ format: date-time
+ type: string
+ message:
+ type: string
+ state:
+ type: string
+ required:
+ - state
+ type: object
+ type: object
+ version: v1alpha1
+ versions:
+ - name: v1alpha1
+ served: true
+ storage: true
+status:
+ acceptedNames:
+ kind: ""
+ plural: ""
+ conditions: []
+ storedVersions: []
- bases/batch.sdewan.akraino.org_cnfservices.yaml
- bases/batch.sdewan.akraino.org_sdewanapplications.yaml
- bases/batch.sdewan.akraino.org_cnfstatuses.yaml
+- bases/batch.sdewan.akraino.org_cnfroutes.yaml
# +kubebuilder:scaffold:crdkustomizeresource
patchesStrategicMerge:
#- patches/webhook_in_cnfservices.yaml
#- patches/webhook_in_sdewanapplications.yaml
#- patches/webhook_in_cnfstatuses.yaml
+#- patches/webhook_in_cnfroutes.yaml
# +kubebuilder:scaffold:crdkustomizewebhookpatch
# [CERTMANAGER] To enable webhook, uncomment all the sections with [CERTMANAGER] prefix.
#- patches/cainjection_in_cnfservices.yaml
#- patches/cainjection_in_sdewanapplications.yaml
#- patches/cainjection_in_cnfstatuses.yaml
+#- patches/cainjection_in_cnfroutes.yaml
# +kubebuilder:scaffold:crdkustomizecainjectionpatch
# the following config is for teaching kustomize how to do kustomization for CRDs.
--- /dev/null
+# The following patch adds a directive for certmanager to inject CA into the CRD
+# CRD conversion requires k8s 1.13 or later.
+apiVersion: apiextensions.k8s.io/v1beta1
+kind: CustomResourceDefinition
+metadata:
+ annotations:
+ cert-manager.io/inject-ca-from: $(CERTIFICATE_NAMESPACE)/$(CERTIFICATE_NAME)
+ name: cnfroutes.batch.sdewan.akraino.org
--- /dev/null
+# The following patch enables conversion webhook for CRD
+# CRD conversion requires k8s 1.13 or later.
+apiVersion: apiextensions.k8s.io/v1beta1
+kind: CustomResourceDefinition
+metadata:
+ name: cnfroutes.batch.sdewan.akraino.org
+spec:
+ conversion:
+ strategy: Webhook
+ webhookClientConfig:
+ # this is "\n" used as a placeholder, otherwise it will be rejected by the apiserver for being blank,
+ # but we're going to set it later using the cert-manager (or potentially a patch if not using cert-manager)
+ caBundle: Cg==
+ service:
+ namespace: system
+ name: webhook-service
+ path: /convert
--- /dev/null
+# permissions for end users to edit cnfroutes.
+apiVersion: rbac.authorization.k8s.io/v1
+kind: ClusterRole
+metadata:
+ name: cnfroute-editor-role
+rules:
+- apiGroups:
+ - batch.sdewan.akraino.org
+ resources:
+ - cnfroutes
+ verbs:
+ - create
+ - delete
+ - get
+ - list
+ - patch
+ - update
+ - watch
+- apiGroups:
+ - batch.sdewan.akraino.org
+ resources:
+ - cnfroutes/status
+ verbs:
+ - get
--- /dev/null
+# permissions for end users to view cnfroutes.
+apiVersion: rbac.authorization.k8s.io/v1
+kind: ClusterRole
+metadata:
+ name: cnfroute-viewer-role
+rules:
+- apiGroups:
+ - batch.sdewan.akraino.org
+ resources:
+ - cnfroutes
+ verbs:
+ - get
+ - list
+ - watch
+- apiGroups:
+ - batch.sdewan.akraino.org
+ resources:
+ - cnfroutes/status
+ verbs:
+ - get
- get
- list
- watch
+- apiGroups:
+ - batch.sdewan.akraino.org
+ resources:
+ - cnfroutes
+ verbs:
+ - create
+ - delete
+ - get
+ - list
+ - patch
+ - update
+ - watch
+- apiGroups:
+ - batch.sdewan.akraino.org
+ resources:
+ - cnfroutes/status
+ verbs:
+ - get
+ - patch
+ - update
- apiGroups:
- batch.sdewan.akraino.org
resources:
--- /dev/null
+apiVersion: batch.sdewan.akraino.org/v1alpha1
+kind: CNFRoute
+metadata:
+ name: cnfroute-sample
+ labels:
+ sdewanPurpose: cnf1
+spec:
+ dst: "192.167.23.0/24"
+ gw: "172.16.44.11"
+ dev: "net1"
+ table: "cnf"
--- /dev/null
+/*
+
+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"
+ "reflect"
+
+ "github.com/go-logr/logr"
+ appsv1 "k8s.io/api/apps/v1"
+ "k8s.io/apimachinery/pkg/runtime"
+ ctrl "sigs.k8s.io/controller-runtime"
+ "sigs.k8s.io/controller-runtime/pkg/client"
+ "sigs.k8s.io/controller-runtime/pkg/handler"
+ "sigs.k8s.io/controller-runtime/pkg/source"
+
+ batchv1alpha1 "sdewan.akraino.org/sdewan/api/v1alpha1"
+ "sdewan.akraino.org/sdewan/openwrt"
+)
+
+var cnfRouteHandler = new(CNFRouteHandler)
+
+type CNFRouteHandler struct {
+}
+
+func (m *CNFRouteHandler) GetType() string {
+ return "cnfRoute"
+}
+
+func (m *CNFRouteHandler) GetName(instance runtime.Object) string {
+ route := instance.(*batchv1alpha1.CNFRoute)
+ return route.Name
+}
+
+func (m *CNFRouteHandler) GetFinalizer() string {
+ return "rule.finalizers.sdewan.akraino.org"
+}
+
+func (m *CNFRouteHandler) GetInstance(r client.Client, ctx context.Context, req ctrl.Request) (runtime.Object, error) {
+ instance := &batchv1alpha1.CNFRoute{}
+ err := r.Get(ctx, req.NamespacedName, instance)
+ return instance, err
+}
+
+func (m *CNFRouteHandler) Convert(instance runtime.Object, deployment appsv1.Deployment) (openwrt.IOpenWrtObject, error) {
+ route := instance.(*batchv1alpha1.CNFRoute)
+ openwrtroute := openwrt.SdewanRoute{
+ Name: route.Name,
+ Dst: route.Spec.Dst,
+ Gw: route.Spec.Gw,
+ Dev: route.Spec.Dev,
+ Table: route.Spec.Table,
+ }
+ return &openwrtroute, nil
+}
+
+func (m *CNFRouteHandler) IsEqual(instance1 openwrt.IOpenWrtObject, instance2 openwrt.IOpenWrtObject) bool {
+ route1 := instance1.(*openwrt.SdewanRoute)
+ route2 := instance2.(*openwrt.SdewanRoute)
+ return reflect.DeepEqual(*route1, *route2)
+}
+
+func (m *CNFRouteHandler) GetObject(clientInfo *openwrt.OpenwrtClientInfo, name string) (openwrt.IOpenWrtObject, error) {
+ openwrtClient := openwrt.GetOpenwrtClient(*clientInfo)
+ route := openwrt.RouteClient{OpenwrtClient: openwrtClient}
+ ret, err := route.GetRoute(name)
+ return ret, err
+}
+
+func (m *CNFRouteHandler) CreateObject(clientInfo *openwrt.OpenwrtClientInfo, instance openwrt.IOpenWrtObject) (openwrt.IOpenWrtObject, error) {
+ openwrtClient := openwrt.GetOpenwrtClient(*clientInfo)
+ route := openwrt.RouteClient{OpenwrtClient: openwrtClient}
+ obj := instance.(*openwrt.SdewanRoute)
+ return route.CreateRoute(*obj)
+}
+
+func (m *CNFRouteHandler) UpdateObject(clientInfo *openwrt.OpenwrtClientInfo, instance openwrt.IOpenWrtObject) (openwrt.IOpenWrtObject, error) {
+ openwrtClient := openwrt.GetOpenwrtClient(*clientInfo)
+ route := openwrt.RouteClient{OpenwrtClient: openwrtClient}
+ obj := instance.(*openwrt.SdewanRoute)
+ return route.UpdateRoute(*obj)
+}
+
+func (m *CNFRouteHandler) DeleteObject(clientInfo *openwrt.OpenwrtClientInfo, name string) error {
+ openwrtClient := openwrt.GetOpenwrtClient(*clientInfo)
+ route := openwrt.RouteClient{OpenwrtClient: openwrtClient}
+ return route.DeleteRoute(name)
+}
+
+func (m *CNFRouteHandler) Restart(clientInfo *openwrt.OpenwrtClientInfo) (bool, error) {
+ return true, nil
+}
+
+// CNFRouteReconciler reconciles a CNFRoute object
+type CNFRouteReconciler struct {
+ client.Client
+ Log logr.Logger
+ Scheme *runtime.Scheme
+}
+
+// +kubebuilder:rbac:groups=batch.sdewan.akraino.org,resources=cnfroutes,verbs=get;list;watch;create;update;patch;delete
+// +kubebuilder:rbac:groups=batch.sdewan.akraino.org,resources=cnfroutes/status,verbs=get;update;patch
+
+func (r *CNFRouteReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
+ return ProcessReconcile(r, r.Log, req, cnfRouteHandler)
+}
+
+func (r *CNFRouteReconciler) SetupWithManager(mgr ctrl.Manager) error {
+ return ctrl.NewControllerManagedBy(mgr).
+ For(&batchv1alpha1.CNFRoute{}).
+ Watches(
+ &source.Kind{Type: &appsv1.Deployment{}},
+ &handler.EnqueueRequestsFromMapFunc{
+ ToRequests: handler.ToRequestsFunc(GetToRequestsFunc(r, &batchv1alpha1.CNFRouteList{})),
+ },
+ Filter).
+ Complete(r)
+}
err = batchv1alpha1.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())
+ err = batchv1alpha1.AddToScheme(scheme.Scheme)
+ Expect(err).NotTo(HaveOccurred())
+
// +kubebuilder:scaffold:scheme
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
setupLog.Error(err, "unable to create controller", "controller", "SdewanApplication")
os.Exit(1)
}
+ if err = (&controllers.CNFRouteReconciler{
+ Client: mgr.GetClient(),
+ Log: ctrl.Log.WithName("controllers").WithName("CNFRoute"),
+ Scheme: mgr.GetScheme(),
+ }).SetupWithManager(mgr); err != nil {
+ setupLog.Error(err, "unable to create controller", "controller", "CNFRoute")
+ os.Exit(1)
+ }
// +kubebuilder:scaffold:builder
setupLog.Info("start CNFStatusController to query CNF status periodicly")
--- /dev/null
+package openwrt
+
+import (
+ "encoding/json"
+)
+
+const (
+ routeBaseURL = "sdewan/route/v1/"
+)
+
+type RouteClient struct {
+ OpenwrtClient *openwrtClient
+}
+
+// Route Info
+type SdewanRoute struct {
+ Name string `json:"name"`
+ Dst string `json:"dst"`
+ Gw string `json:"gw"`
+ Dev string `json:"dev"`
+ Table string `json:"table"`
+}
+
+type SdewanRoutes struct {
+ Routes []SdewanRoute `json:"routes"`
+}
+
+func (o *SdewanRoute) GetName() string {
+ return o.Name
+}
+
+// Route APIs
+// get routes
+func (m *RouteClient) GetRoutes() (*SdewanRoutes, error) {
+ response, err := m.OpenwrtClient.Get(routeBaseURL + "routes")
+ if err != nil {
+ return nil, err
+ }
+
+ var sdewanRoutes SdewanRoutes
+ err2 := json.Unmarshal([]byte(response), &sdewanRoutes)
+ if err2 != nil {
+ return nil, err2
+ }
+
+ return &sdewanRoutes, nil
+}
+
+// get route
+func (m *RouteClient) GetRoute(route_name string) (*SdewanRoute, error) {
+ response, err := m.OpenwrtClient.Get(routeBaseURL + "routes/" + route_name)
+ if err != nil {
+ return nil, err
+ }
+
+ var sdewanRoute SdewanRoute
+ err2 := json.Unmarshal([]byte(response), &sdewanRoute)
+ if err2 != nil {
+ return nil, err2
+ }
+
+ return &sdewanRoute, nil
+}
+
+// create route
+func (m *RouteClient) CreateRoute(route SdewanRoute) (*SdewanRoute, error) {
+ route_obj, _ := json.Marshal(route)
+ response, err := m.OpenwrtClient.Post(routeBaseURL+"routes/", string(route_obj))
+ if err != nil {
+ return nil, err
+ }
+
+ var sdewanRoute SdewanRoute
+ err2 := json.Unmarshal([]byte(response), &sdewanRoute)
+ if err2 != nil {
+ return nil, err2
+ }
+
+ return &sdewanRoute, nil
+}
+
+// delete route
+func (m *RouteClient) DeleteRoute(route_name string) error {
+ _, err := m.OpenwrtClient.Delete(routeBaseURL + "routes/" + route_name)
+ if err != nil {
+ return err
+ }
+
+ return nil
+}
+
+// update route
+func (m *RouteClient) UpdateRoute(route SdewanRoute) (*SdewanRoute, error) {
+ route_obj, _ := json.Marshal(route)
+ route_name := route.Name
+ response, err := m.OpenwrtClient.Put(routeBaseURL+"routes/"+route_name, string(route_obj))
+ if err != nil {
+ return nil, err
+ }
+
+ var sdewanRoute SdewanRoute
+ err2 := json.Unmarshal([]byte(response), &sdewanRoute)
+ if err2 != nil {
+ return nil, err2
+ }
+
+ return &sdewanRoute, nil
+}