...

Source file src/github.com/kvaps/dnsmasq-controller/controllers/dhcpoptions_controller.go

Documentation: github.com/kvaps/dnsmasq-controller/controllers

     1  /*
     2  
     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 controllers
    18  
    19  import (
    20  	"context"
    21  	"os"
    22  
    23  	"github.com/go-logr/logr"
    24  	"k8s.io/apimachinery/pkg/api/errors"
    25  	"k8s.io/apimachinery/pkg/runtime"
    26  	ctrl "sigs.k8s.io/controller-runtime"
    27  	"sigs.k8s.io/controller-runtime/pkg/client"
    28  
    29  	dnsmasqv1beta1 "github.com/kvaps/dnsmasq-controller/api/v1beta1"
    30  	"github.com/kvaps/dnsmasq-controller/pkg/conf"
    31  	"github.com/kvaps/dnsmasq-controller/pkg/util"
    32  )
    33  
    34  // DhcpOptionsReconciler reconciles a DhcpOptions object
    35  type DhcpOptionsReconciler struct {
    36  	client.Client
    37  	Log    logr.Logger
    38  	Scheme *runtime.Scheme
    39  }
    40  
    41  // +kubebuilder:rbac:groups=dnsmasq.kvaps.cf,resources=dhcpoptions,verbs=get;list;watch
    42  
    43  func (r *DhcpOptionsReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
    44  	_ = context.Background()
    45  	_ = r.Log.WithValues("dnsmasqdhcpoptionset", req.NamespacedName)
    46  	config := conf.GetConfig()
    47  
    48  	configFile := config.DnsmasqConfDir + "/dhcp-opts/" + req.Namespace + "-" + req.Name
    49  
    50  	res := &dnsmasqv1beta1.DhcpOptions{}
    51  	err := r.Client.Get(context.TODO(), req.NamespacedName, res)
    52  	if err != nil {
    53  		if errors.IsNotFound(err) {
    54  			// Request object not found
    55  			if _, err := os.Stat(configFile); !os.IsNotExist(err) {
    56  				os.Remove(configFile)
    57  				r.Log.Info("Removed " + configFile)
    58  				config.Generation++
    59  			}
    60  			return ctrl.Result{}, nil
    61  		}
    62  		// Error reading the object - requeue the request.
    63  		return ctrl.Result{}, err
    64  	}
    65  
    66  	if res.Spec.Controller != config.ControllerName {
    67  		if _, err := os.Stat(configFile); !os.IsNotExist(err) {
    68  			// Controller name has been changed
    69  			os.Remove(configFile)
    70  			r.Log.Info("Removed " + configFile)
    71  			config.Generation++
    72  		}
    73  		return ctrl.Result{}, nil
    74  	}
    75  
    76  	// Write dhcp-hosts
    77  	var configData string
    78  	var configLine string
    79  	for _, r := range res.Spec.Options {
    80  		configLine = ""
    81  		for _, v := range r.Tags {
    82  			configLine += ",tag:" + v
    83  		}
    84  		if r.Encap != "" {
    85  			configLine += ",encap:" + r.Encap
    86  		}
    87  		if r.ViEncap != "" {
    88  			configLine += ",vi-encap:" + r.ViEncap
    89  		}
    90  		if r.Vendor != "" {
    91  			configLine += ",vendor:" + r.Vendor
    92  		}
    93  		if r.Key != "" {
    94  			configLine += "," + r.Key
    95  		}
    96  		for _, v := range r.Values {
    97  			configLine += "," + v
    98  		}
    99  		configLine += "\n"
   100  		configData += configLine[1:]
   101  	}
   102  	configBytes := []byte(configData)
   103  
   104  	configWritten, err := util.WriteConfig(configFile, configFile, configBytes)
   105  	if err != nil {
   106  		r.Log.Error(err, "Failed to update "+configFile)
   107  		return ctrl.Result{}, nil
   108  	}
   109  
   110  	if configWritten {
   111  		r.Log.Info("Written " + configFile)
   112  		config.Generation++
   113  	}
   114  
   115  	return ctrl.Result{}, nil
   116  }
   117  
   118  func (r *DhcpOptionsReconciler) SetupWithManager(mgr ctrl.Manager) error {
   119  	return ctrl.NewControllerManagedBy(mgr).
   120  		For(&dnsmasqv1beta1.DhcpOptions{}).
   121  		Complete(r)
   122  }
   123  

View as plain text