Skip to content

Commit

Permalink
add option to turn on/off the new controller
Browse files Browse the repository at this point in the history
A new and already deprecated boolean command line option —v07-controller turn v0.7 controller implementation on if true, and the new v0.8 implementation will be used if false. v0.8 will be released with this option (default false) and will be removed on v0.9 with all legacy controller code.
  • Loading branch information
jcmoraisjr committed Jan 20, 2019
1 parent 4b1d58c commit a8170ee
Show file tree
Hide file tree
Showing 8 changed files with 223 additions and 13 deletions.
12 changes: 12 additions & 0 deletions pkg/common/ingress/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ type Configuration struct {
UpdateStatusOnShutdown bool

SortBackends bool

V07 bool
}

// newIngressController creates an Ingress controller
Expand Down Expand Up @@ -202,6 +204,11 @@ func newIngressController(config *Configuration) *GenericController {
return &ic
}

// GetConfig expose the controller configuration
func (ic *GenericController) GetConfig() *Configuration {
return ic.cfg
}

// Info returns information about the backend
func (ic GenericController) Info() *ingress.BackendInfo {
return ic.cfg.Backend.Info()
Expand Down Expand Up @@ -260,6 +267,11 @@ func (ic *GenericController) syncIngress(item interface{}) error {
return nil
}

if !ic.cfg.V07 {
ic.cfg.Backend.SyncIngress(item)
return nil
}

// force reload of default backend data
// see GetDefaultBackend()
ic.defaultBackend = nil
Expand Down
4 changes: 4 additions & 0 deletions pkg/common/ingress/controller/launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ func NewIngressController(backend ingress.Controller) *GenericController {
useNodeInternalIP = flags.Bool("report-node-internal-ip-address", false,
`Defines if the nodes IP address to be returned in the ingress status should be the internal instead of the external IP address`)

v07 = flags.Bool("v07-controller", true,
`Defines if legacy v07 controller code should be used`)

showVersion = flags.Bool("version", false,
`Shows release information about the NGINX Ingress controller`)
)
Expand Down Expand Up @@ -252,6 +255,7 @@ func NewIngressController(backend ingress.Controller) *GenericController {
UpdateStatusOnShutdown: *updateStatusOnShutdown,
SortBackends: *sortBackends,
UseNodeInternalIP: *useNodeInternalIP,
V07: *v07,
}

ic := newIngressController(config)
Expand Down
2 changes: 2 additions & 0 deletions pkg/common/ingress/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ type Controller interface {
// The backend returns an error if was not possible to update the configuration.
//
OnUpdate(Configuration) error
// SyncIngress sync load balancer config from a very early stage
SyncIngress(item interface{}) error
// ConfigMap content of --configmap
SetConfig(*apiv1.ConfigMap)
// SetListers allows the access of store listers present in the generic controller
Expand Down
59 changes: 59 additions & 0 deletions pkg/controller/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
Copyright 2019 The HAProxy Ingress Controller Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package controller

import (
"fmt"
"strings"

api "k8s.io/api/core/v1"

"github.com/jcmoraisjr/haproxy-ingress/pkg/common/ingress"
)

type cache struct {
listers *ingress.StoreLister
}

func (c *cache) GetService(serviceName string) (*api.Service, error) {
return c.listers.Service.GetByName(serviceName)
}

func (c *cache) GetEndpoints(service *api.Service) (*api.Endpoints, error) {
ep, err := c.listers.Endpoint.GetServiceEndpoints(service)
return &ep, err
}

func (c *cache) GetPod(podName string) (*api.Pod, error) {
sname := strings.Split(podName, "/")
if len(sname) != 2 {
return nil, fmt.Errorf("invalid pod name: '%s'", podName)
}
return c.listers.Pod.GetPod(sname[0], sname[1])
}

func (c *cache) GetTLSSecretPath(secretName string) (string, error) {
return "", fmt.Errorf("implement")
}

func (c *cache) GetCASecretPath(secretName string) (string, error) {
return "", fmt.Errorf("implement")
}

func (c *cache) GetSecretContent(secretName, keyName string) ([]byte, error) {
return []byte{}, fmt.Errorf("implement")
}
103 changes: 90 additions & 13 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,39 @@ limitations under the License.
package controller

import (
"github.com/golang/glog"
"github.com/jcmoraisjr/haproxy-ingress/pkg/common/ingress"
"github.com/jcmoraisjr/haproxy-ingress/pkg/common/ingress/controller"
"github.com/jcmoraisjr/haproxy-ingress/pkg/common/ingress/defaults"
"github.com/jcmoraisjr/haproxy-ingress/pkg/controller/dynconfig"
"github.com/jcmoraisjr/haproxy-ingress/pkg/types"
"github.com/jcmoraisjr/haproxy-ingress/pkg/version"
"github.com/spf13/pflag"
"io/ioutil"
api "k8s.io/api/core/v1"
extensions "k8s.io/api/extensions/v1beta1"
"net/http"
"os"
"os/exec"
"sort"
"strings"
"time"

"github.com/golang/glog"
"github.com/spf13/pflag"
api "k8s.io/api/core/v1"
extensions "k8s.io/api/extensions/v1beta1"

"github.com/jcmoraisjr/haproxy-ingress/pkg/common/ingress"
"github.com/jcmoraisjr/haproxy-ingress/pkg/common/ingress/annotations/class"
"github.com/jcmoraisjr/haproxy-ingress/pkg/common/ingress/controller"
"github.com/jcmoraisjr/haproxy-ingress/pkg/common/ingress/defaults"
"github.com/jcmoraisjr/haproxy-ingress/pkg/controller/dynconfig"
ingressconverter "github.com/jcmoraisjr/haproxy-ingress/pkg/converters/ingress"
ingtypes "github.com/jcmoraisjr/haproxy-ingress/pkg/converters/ingress/types"
"github.com/jcmoraisjr/haproxy-ingress/pkg/haproxy"
"github.com/jcmoraisjr/haproxy-ingress/pkg/types"
"github.com/jcmoraisjr/haproxy-ingress/pkg/version"
)

// HAProxyController has internal data of a HAProxyController instance
type HAProxyController struct {
instance haproxy.Instance
controller *controller.GenericController
cfg *controller.Configuration
configMap *api.ConfigMap
storeLister *ingress.StoreLister
converterOptions *ingtypes.ConverterOptions
command string
reloadStrategy *string
configDir string
Expand Down Expand Up @@ -71,10 +80,50 @@ func (hc *HAProxyController) Info() *ingress.BackendInfo {
// Start starts the controller
func (hc *HAProxyController) Start() {
hc.controller = controller.NewIngressController(hc)
hc.configController()
hc.controller.Start()
}

func (hc *HAProxyController) configController() {
if *hc.reloadStrategy == "multibinder" {
glog.Warningf("multibinder is deprecated, using reusesocket strategy instead. update your deployment configuration")
}
hc.controller.Start()
hc.cfg = hc.controller.GetConfig()

if hc.cfg.V07 {
return
}

// starting v0.8 only config
logger := &logger{depth: 1}
hc.converterOptions = &ingtypes.ConverterOptions{
Logger: logger,
Cache: &cache{listers: hc.storeLister},
AnnotationPrefix: "ingress.kubernetes.io",
DefaultBackend: hc.cfg.DefaultService,
DefaultSSLSecret: hc.cfg.DefaultSSLCertificate,
}
instanceOptions := haproxy.InstanceOptions{
HAProxyCmd: "haproxy",
ReloadCmd: "/haproxy-reload.sh",
HAProxyConfigFile: "/etc/haproxy/haproxy.cfg",
ReloadStrategy: *hc.reloadStrategy,
}
hc.instance = haproxy.CreateInstance(logger, instanceOptions)
hc.instance.Templates().NewTemplate(
"spoe-modsecurity.tmpl",
"/etc/haproxy/modsecurity/spoe-modsecurity.tmpl",
"/etc/haproxy/spoe-modsecurity.conf",
0,
1024,
)
hc.instance.Templates().NewTemplate(
"haproxy.tmpl",
"/etc/haproxy/template/haproxy.tmpl",
"/etc/haproxy/haproxy.cfg",
*hc.maxOldConfigFiles,
16384,
)
}

// Stop shutdown the controller process
Expand Down Expand Up @@ -128,9 +177,9 @@ func (hc *HAProxyController) OverrideFlags(flags *pflag.FlagSet) {
hc.configDir = "/etc/haproxy"
hc.configFilePrefix = "haproxy"
hc.configFileSuffix = ".cfg"
hc.haproxyTemplate = newTemplate("haproxy.tmpl", "/etc/haproxy/template/haproxy.tmpl", 16384)
hc.haproxyTemplate = newTemplate("haproxy-v07.tmpl", "/etc/haproxy/template/haproxy-v07.tmpl", 16384)
hc.modsecConfigFile = "/etc/haproxy/spoe-modsecurity.conf"
hc.modsecTemplate = newTemplate("spoe-modsecurity.tmpl", "/etc/haproxy/modsecurity/spoe-modsecurity.tmpl", 1024)
hc.modsecTemplate = newTemplate("spoe-modsecurity-v07.tmpl", "/etc/haproxy/modsecurity/spoe-modsecurity-v07.tmpl", 1024)
hc.command = "/haproxy-reload.sh"

if !(*hc.reloadStrategy == "native" || *hc.reloadStrategy == "reusesocket" || *hc.reloadStrategy == "multibinder") {
Expand Down Expand Up @@ -169,6 +218,34 @@ func (hc *HAProxyController) DrainSupport() (drainSupport bool) {
return
}

// SyncIngress sync HAProxy config from a very early stage
func (hc *HAProxyController) SyncIngress(item interface{}) error {
var ingress []*extensions.Ingress
for _, iing := range hc.storeLister.Ingress.List() {
ing := iing.(*extensions.Ingress)
if class.IsValid(ing, hc.cfg.IngressClass, hc.cfg.DefaultIngressClass) {
ingress = append(ingress, ing)
}
}
sort.SliceStable(ingress, func(i, j int) bool {
return ingress[i].ResourceVersion < ingress[j].ResourceVersion
})

var globalConfig map[string]string
if hc.configMap != nil {
globalConfig = hc.configMap.Data
}
converter := ingressconverter.NewIngressConverter(
hc.converterOptions,
hc.instance.CreateConfig(),
globalConfig,
)
converter.Sync(ingress)
hc.instance.Update()

return nil
}

// OnUpdate regenerate the configuration file of the backend
func (hc *HAProxyController) OnUpdate(cfg ingress.Configuration) error {
updatedConfig, err := newControllerConfig(&cfg, hc)
Expand Down
56 changes: 56 additions & 0 deletions pkg/controller/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
Copyright 2019 The HAProxy Ingress Controller Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package controller

import (
"fmt"

"github.com/golang/glog"
)

type logger struct {
depth int
}

func (l *logger) build(msg string, args ...interface{}) string {
if len(args) == 0 {
return msg
}
return fmt.Sprintf(msg, args)
}

func (l *logger) InfoV(v int, msg string, args ...interface{}) {
if glog.V(glog.Level(v)) {
glog.InfoDepth(l.depth, l.build(msg, args))
}
}

func (l *logger) Info(msg string, args ...interface{}) {
glog.InfoDepth(l.depth, l.build(msg, args))
}

func (l *logger) Warn(msg string, args ...interface{}) {
glog.WarningDepth(l.depth, l.build(msg, args))
}

func (l *logger) Error(msg string, args ...interface{}) {
glog.ErrorDepth(l.depth, l.build(msg, args))
}

func (l *logger) Fatal(msg string, args ...interface{}) {
glog.FatalDepth(l.depth, l.build(msg, args))
}
File renamed without changes.

0 comments on commit a8170ee

Please sign in to comment.