...

Source file src/github.com/kvaps/dnsmasq-controller/controllers/dnshosts_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  // DnsHostsReconciler reconciles a DnsHosts object
    35  type DnsHostsReconciler struct {
    36  	client.Client
    37  	Log    logr.Logger
    38  	Scheme *runtime.Scheme
    39  }
    40  
    41  // +kubebuilder:rbac:groups=dnsmasq.kvaps.cf,resources=dnshosts,verbs=get;list;watch
    42  
    43  func (r *DnsHostsReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
    44  	_ = context.Background()
    45  	_ = r.Log.WithValues("dnshost", req.NamespacedName)
    46  	config := conf.GetConfig()
    47  
    48  	configFile := config.DnsmasqConfDir + "/hosts/" + req.Namespace + "-" + req.Name
    49  
    50  	res := &dnsmasqv1beta1.DnsHosts{}
    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 hosts
    77  	var configData string
    78  	for _, h := range res.Spec.Hosts {
    79  		configData += h.IP
    80  		for _, hostname := range h.Hostnames {
    81  			configData += " " + hostname
    82  		}
    83  		configData += "\n"
    84  	}
    85  	configBytes := []byte(configData)
    86  
    87  	configWritten, err := util.WriteConfig(configFile, configFile, configBytes)
    88  	if err != nil {
    89  		r.Log.Error(err, "Failed to update "+configFile)
    90  		return ctrl.Result{}, nil
    91  	}
    92  
    93  	if configWritten {
    94  		r.Log.Info("Written " + configFile)
    95  		config.Generation++
    96  	}
    97  
    98  	return ctrl.Result{}, nil
    99  }
   100  
   101  func (r *DnsHostsReconciler) SetupWithManager(mgr ctrl.Manager) error {
   102  	return ctrl.NewControllerManagedBy(mgr).
   103  		For(&dnsmasqv1beta1.DnsHosts{}).
   104  		Complete(r)
   105  }
   106  

View as plain text