|
| 1 | +/* |
| 2 | +Copyright 2018 Louis Taylor. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package onionservice |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "log" |
| 22 | + "reflect" |
| 23 | + |
| 24 | + torv1alpha1 "github.com/kragniz/tor-controller/pkg/apis/tor/v1alpha1" |
| 25 | + appsv1 "k8s.io/api/apps/v1" |
| 26 | + corev1 "k8s.io/api/core/v1" |
| 27 | + "k8s.io/apimachinery/pkg/api/errors" |
| 28 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 29 | + "k8s.io/apimachinery/pkg/runtime" |
| 30 | + "k8s.io/apimachinery/pkg/types" |
| 31 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 32 | + "sigs.k8s.io/controller-runtime/pkg/controller" |
| 33 | + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" |
| 34 | + "sigs.k8s.io/controller-runtime/pkg/handler" |
| 35 | + "sigs.k8s.io/controller-runtime/pkg/manager" |
| 36 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 37 | + "sigs.k8s.io/controller-runtime/pkg/source" |
| 38 | +) |
| 39 | + |
| 40 | +/** |
| 41 | +* USER ACTION REQUIRED: This is a scaffold file intended for the user to modify with their own Controller |
| 42 | +* business logic. Delete these comments after modifying this file.* |
| 43 | + */ |
| 44 | + |
| 45 | +// Add creates a new OnionService Controller and adds it to the Manager with default RBAC. The Manager will set fields on the Controller |
| 46 | +// and Start it when the Manager is Started. |
| 47 | +// USER ACTION REQUIRED: update cmd/manager/main.go to call this tor.Add(mgr) to install this Controller |
| 48 | +func Add(mgr manager.Manager) error { |
| 49 | + return add(mgr, newReconciler(mgr)) |
| 50 | +} |
| 51 | + |
| 52 | +// newReconciler returns a new reconcile.Reconciler |
| 53 | +func newReconciler(mgr manager.Manager) reconcile.Reconciler { |
| 54 | + return &ReconcileOnionService{Client: mgr.GetClient(), scheme: mgr.GetScheme()} |
| 55 | +} |
| 56 | + |
| 57 | +// add adds a new Controller to mgr with r as the reconcile.Reconciler |
| 58 | +func add(mgr manager.Manager, r reconcile.Reconciler) error { |
| 59 | + // Create a new controller |
| 60 | + c, err := controller.New("onionservice-controller", mgr, controller.Options{Reconciler: r}) |
| 61 | + if err != nil { |
| 62 | + return err |
| 63 | + } |
| 64 | + |
| 65 | + // Watch for changes to OnionService |
| 66 | + err = c.Watch(&source.Kind{Type: &torv1alpha1.OnionService{}}, &handler.EnqueueRequestForObject{}) |
| 67 | + if err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + |
| 71 | + // TODO(user): Modify this to be the types you create |
| 72 | + // Uncomment watch a Deployment created by OnionService - change this for objects you create |
| 73 | + err = c.Watch(&source.Kind{Type: &appsv1.Deployment{}}, &handler.EnqueueRequestForOwner{ |
| 74 | + IsController: true, |
| 75 | + OwnerType: &torv1alpha1.OnionService{}, |
| 76 | + }) |
| 77 | + if err != nil { |
| 78 | + return err |
| 79 | + } |
| 80 | + |
| 81 | + return nil |
| 82 | +} |
| 83 | + |
| 84 | +var _ reconcile.Reconciler = &ReconcileOnionService{} |
| 85 | + |
| 86 | +// ReconcileOnionService reconciles a OnionService object |
| 87 | +type ReconcileOnionService struct { |
| 88 | + client.Client |
| 89 | + scheme *runtime.Scheme |
| 90 | +} |
| 91 | + |
| 92 | +// Reconcile reads that state of the cluster for a OnionService object and makes changes based on the state read |
| 93 | +// and what is in the OnionService.Spec |
| 94 | +// TODO(user): Modify this Reconcile function to implement your Controller logic. The scaffolding writes |
| 95 | +// a Deployment as an example |
| 96 | +// Automatically generate RBAC rules to allow the Controller to read and write Deployments |
| 97 | +// +kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;list;watch;create;update;patch;delete |
| 98 | +// +kubebuilder:rbac:groups=tor.k8s.io,resources=onionservices,verbs=get;list;watch;create;update;patch;delete |
| 99 | +func (r *ReconcileOnionService) Reconcile(request reconcile.Request) (reconcile.Result, error) { |
| 100 | + // Fetch the OnionService instance |
| 101 | + instance := &torv1alpha1.OnionService{} |
| 102 | + err := r.Get(context.TODO(), request.NamespacedName, instance) |
| 103 | + if err != nil { |
| 104 | + if errors.IsNotFound(err) { |
| 105 | + // Object not found, return. Created objects are automatically garbage collected. |
| 106 | + // For additional cleanup logic use finalizers. |
| 107 | + return reconcile.Result{}, nil |
| 108 | + } |
| 109 | + // Error reading the object - requeue the request. |
| 110 | + return reconcile.Result{}, err |
| 111 | + } |
| 112 | + |
| 113 | + // TODO(user): Change this to be the object type created by your controller |
| 114 | + // Define the desired Deployment object |
| 115 | + deploy := &appsv1.Deployment{ |
| 116 | + ObjectMeta: metav1.ObjectMeta{ |
| 117 | + Name: instance.Name + "-deployment", |
| 118 | + Namespace: instance.Namespace, |
| 119 | + }, |
| 120 | + Spec: appsv1.DeploymentSpec{ |
| 121 | + Selector: &metav1.LabelSelector{ |
| 122 | + MatchLabels: map[string]string{"deployment": instance.Name + "-deployment"}, |
| 123 | + }, |
| 124 | + Template: corev1.PodTemplateSpec{ |
| 125 | + ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{"deployment": instance.Name + "-deployment"}}, |
| 126 | + Spec: corev1.PodSpec{ |
| 127 | + Containers: []corev1.Container{ |
| 128 | + { |
| 129 | + Name: "nginx", |
| 130 | + Image: "nginx", |
| 131 | + }, |
| 132 | + }, |
| 133 | + }, |
| 134 | + }, |
| 135 | + }, |
| 136 | + } |
| 137 | + if err := controllerutil.SetControllerReference(instance, deploy, r.scheme); err != nil { |
| 138 | + return reconcile.Result{}, err |
| 139 | + } |
| 140 | + |
| 141 | + // TODO(user): Change this for the object type created by your controller |
| 142 | + // Check if the Deployment already exists |
| 143 | + found := &appsv1.Deployment{} |
| 144 | + err = r.Get(context.TODO(), types.NamespacedName{Name: deploy.Name, Namespace: deploy.Namespace}, found) |
| 145 | + if err != nil && errors.IsNotFound(err) { |
| 146 | + log.Printf("Creating Deployment %s/%s\n", deploy.Namespace, deploy.Name) |
| 147 | + err = r.Create(context.TODO(), deploy) |
| 148 | + if err != nil { |
| 149 | + return reconcile.Result{}, err |
| 150 | + } |
| 151 | + } else if err != nil { |
| 152 | + return reconcile.Result{}, err |
| 153 | + } |
| 154 | + |
| 155 | + // TODO(user): Change this for the object type created by your controller |
| 156 | + // Update the found object and write the result back if there are any changes |
| 157 | + if !reflect.DeepEqual(deploy.Spec, found.Spec) { |
| 158 | + found.Spec = deploy.Spec |
| 159 | + log.Printf("Updating Deployment %s/%s\n", deploy.Namespace, deploy.Name) |
| 160 | + err = r.Update(context.TODO(), found) |
| 161 | + if err != nil { |
| 162 | + return reconcile.Result{}, err |
| 163 | + } |
| 164 | + } |
| 165 | + return reconcile.Result{}, nil |
| 166 | +} |
0 commit comments