...

Source file src/github.com/kvaps/dnsmasq-controller/controllers/dnsmasqoptions_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  // DnsmasqOptionsReconciler reconciles a DnsmasqOptions object
    35  type DnsmasqOptionsReconciler struct {
    36  	client.Client
    37  	Log    logr.Logger
    38  	Scheme *runtime.Scheme
    39  }
    40  
    41  // +kubebuilder:rbac:groups=dnsmasq.kvaps.cf,resources=dnsmasqoptions,verbs=get;list;watch
    42  
    43  func (r *DnsmasqOptionsReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
    44  	_ = context.Background()
    45  	_ = r.Log.WithValues("dnsmasqconfiguration", req.NamespacedName)
    46  	config := conf.GetConfig()
    47  
    48  	configFile := config.DnsmasqConfDir + "/" + req.Namespace + "-" + req.Name + ".conf"
    49  	tmpConfigFile := config.DnsmasqConfDir + "/." + req.Namespace + "-" + req.Name + ".conf.tmp"
    50  
    51  	res := &dnsmasqv1beta1.DnsmasqOptions{}
    52  	err := r.Client.Get(context.TODO(), req.NamespacedName, res)
    53  	if err != nil {
    54  		if errors.IsNotFound(err) {
    55  			// Request object not found
    56  			if _, err := os.Stat(configFile); !os.IsNotExist(err) {
    57  				os.Remove(configFile)
    58  				r.Log.Info("Removed " + configFile)
    59  				config.Generation++
    60  			}
    61  			return ctrl.Result{}, nil
    62  		}
    63  		// Error reading the object - requeue the request.
    64  		return ctrl.Result{}, err
    65  	}
    66  
    67  	if res.Spec.Controller != config.ControllerName {
    68  		if _, err := os.Stat(configFile); !os.IsNotExist(err) {
    69  			// Controller name has been changed
    70  			os.Remove(configFile)
    71  			r.Log.Info("Removed " + configFile)
    72  			config.Generation++
    73  		}
    74  		return ctrl.Result{}, nil
    75  	}
    76  
    77  	// Write options
    78  	var configData string
    79  	for _, o := range res.Spec.Options {
    80  		if o.Key == "dhcp-range" && !config.EnableDHCP {
    81  			continue
    82  		}
    83  		configData += o.Key + "="
    84  		configValues := ""
    85  		for _, v := range o.Values {
    86  			configValues += "," + v
    87  		}
    88  		configData += configValues[1:]
    89  		configData += "\n"
    90  	}
    91  	configBytes := []byte(configData)
    92  
    93  	configWritten, err := util.WriteConfig(configFile, tmpConfigFile, configBytes)
    94  	if err != nil {
    95  		r.Log.Error(err, "Failed to update "+configFile)
    96  		return ctrl.Result{}, nil
    97  	}
    98  
    99  	if configWritten {
   100  		if err = util.TestConfig(tmpConfigFile); err != nil {
   101  			//os.Remove(tmpConfigFile)
   102  			r.Log.Error(err, "Config "+tmpConfigFile+" is invalid!")
   103  			return ctrl.Result{}, nil
   104  		}
   105  
   106  		if err = os.Rename(tmpConfigFile, configFile); err != nil {
   107  			os.Remove(tmpConfigFile)
   108  			r.Log.Error(err, "Failed to move "+tmpConfigFile+" to "+configFile)
   109  			return ctrl.Result{}, nil
   110  		}
   111  		r.Log.Info("Written " + configFile)
   112  		config.Generation++
   113  	}
   114  
   115  	return ctrl.Result{}, nil
   116  
   117  }
   118  
   119  func (r *DnsmasqOptionsReconciler) SetupWithManager(mgr ctrl.Manager) error {
   120  	return ctrl.NewControllerManagedBy(mgr).
   121  		For(&dnsmasqv1beta1.DnsmasqOptions{}).
   122  		Complete(r)
   123  }
   124  

View as plain text