...

Source file src/github.com/kvaps/dnsmasq-controller/controllers/dhcphosts_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  // DhcpHostsReconciler reconciles a DhcpHosts object
    35  type DhcpHostsReconciler struct {
    36  	client.Client
    37  	Log    logr.Logger
    38  	Scheme *runtime.Scheme
    39  }
    40  
    41  // +kubebuilder:rbac:groups=dnsmasq.kvaps.cf,resources=dhcphosts,verbs=get;list;watch
    42  
    43  func (r *DhcpHostsReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
    44  	_ = context.Background()
    45  	_ = r.Log.WithValues("dhcphost", req.NamespacedName)
    46  	config := conf.GetConfig()
    47  
    48  	configFile := config.DnsmasqConfDir + "/dhcp-hosts/" + req.Namespace + "-" + req.Name
    49  
    50  	res := &dnsmasqv1beta1.DhcpHosts{}
    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 _, h := range res.Spec.Hosts {
    80  		configLine = ""
    81  		for _, v := range h.Macs {
    82  			configLine += "," + v
    83  		}
    84  		if h.ClientID != "" {
    85  			configLine += ",id:" + h.ClientID
    86  		}
    87  		for _, v := range h.SetTags {
    88  			configLine += ",set:" + v
    89  		}
    90  		for _, v := range h.Tags {
    91  			configLine += ",tag:" + v
    92  		}
    93  		if h.IP != "" {
    94  			configLine += "," + h.IP
    95  		}
    96  		if h.Hostname != "" {
    97  			configLine += "," + h.Hostname
    98  		}
    99  		if h.LeaseTime != "" {
   100  			configLine += "," + h.LeaseTime
   101  		}
   102  		if h.Ignore {
   103  			configLine += ",ignore"
   104  		}
   105  		configLine += "\n"
   106  		configData += configLine[1:]
   107  	}
   108  	configBytes := []byte(configData)
   109  
   110  	configWritten, err := util.WriteConfig(configFile, configFile, configBytes)
   111  	if err != nil {
   112  		r.Log.Error(err, "Failed to update "+configFile)
   113  		return ctrl.Result{}, nil
   114  	}
   115  
   116  	if configWritten {
   117  		r.Log.Info("Written " + configFile)
   118  		config.Generation++
   119  	}
   120  
   121  	return ctrl.Result{}, nil
   122  }
   123  
   124  func (r *DhcpHostsReconciler) SetupWithManager(mgr ctrl.Manager) error {
   125  	return ctrl.NewControllerManagedBy(mgr).
   126  		For(&dnsmasqv1beta1.DhcpHosts{}).
   127  		Complete(r)
   128  }
   129  

View as plain text