Skip to content

Commit cc248dc

Browse files
kahirokunnhligit
authored andcommitted
Add --envoy-ingress-name, and --envoy-ingress-namespace flags. (#4952)
Signed-off-by: kahirokunn <[email protected]>
1 parent 9c4952d commit cc248dc

File tree

8 files changed

+134
-0
lines changed

8 files changed

+134
-0
lines changed

apis/projectcontour/v1alpha1/contourconfig.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,12 @@ type EnvoyConfig struct {
239239
// +optional
240240
Service *NamespacedName `json:"service,omitempty"`
241241

242+
// Ingress holds Envoy service parameters for setting Ingress status.
243+
//
244+
// Contour's default is { namespace: "projectcontour", name: "envoy" }.
245+
// +optional
246+
Ingress *NamespacedName `json:"ingress,omitempty"`
247+
242248
// Defines the HTTP Listener for Envoy.
243249
//
244250
// Contour's default is { address: "0.0.0.0", port: 8080, accessLog: "/dev/stdout" }.

cmd/contour/serve.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ func registerServe(app *kingpin.Application) (*kingpin.CmdClause, *serveContext)
146146
serve.Flag("envoy-service-https-port", "Kubernetes Service port for HTTPS requests.").PlaceHolder("<port>").IntVar(&ctx.httpsPort)
147147
serve.Flag("envoy-service-name", "Name of the Envoy service to inspect for Ingress status details.").PlaceHolder("<name>").StringVar(&ctx.Config.EnvoyServiceName)
148148
serve.Flag("envoy-service-namespace", "Envoy Service Namespace.").PlaceHolder("<namespace>").StringVar(&ctx.Config.EnvoyServiceNamespace)
149+
serve.Flag("envoy-ingress-name", "Name of the Envoy ingress to inspect for Ingress status details.").PlaceHolder("<name>").StringVar(&ctx.Config.EnvoyIngressName)
150+
serve.Flag("envoy-ingress-namespace", "Envoy Ingress Namespace.").PlaceHolder("<namespace>").StringVar(&ctx.Config.EnvoyIngressNamespace)
149151

150152
serve.Flag("health-address", "Address the health HTTP endpoint will bind to.").PlaceHolder("<ipaddr>").StringVar(&ctx.healthAddr)
151153
serve.Flag("health-port", "Port the health HTTP endpoint will bind to.").PlaceHolder("<port>").IntVar(&ctx.healthPort)
@@ -688,6 +690,50 @@ func (s *Server) doServe() error {
688690
return err
689691
}
690692

693+
// Register an informer to watch envoy's service if we haven't been given static details.
694+
if lbAddress := contourConfiguration.Ingress.StatusAddress; len(lbAddress) > 0 {
695+
s.log.WithField("loadbalancer-address", lbAddress).Info("Using supplied information for Ingress status")
696+
lbsw.lbStatus <- parseStatusFlag(lbAddress)
697+
} else {
698+
serviceHandler := &k8s.ServiceStatusLoadBalancerWatcher{
699+
ServiceName: contourConfiguration.Envoy.Service.Name,
700+
LBStatus: lbsw.lbStatus,
701+
Log: s.log.WithField("context", "serviceStatusLoadBalancerWatcher"),
702+
}
703+
704+
var handler cache.ResourceEventHandler = serviceHandler
705+
if contourConfiguration.Envoy.Service.Namespace != "" {
706+
handler = k8s.NewNamespaceFilter([]string{contourConfiguration.Envoy.Service.Namespace}, handler)
707+
}
708+
709+
if err := s.informOnResource(&corev1.Service{}, handler); err != nil {
710+
s.log.WithError(err).WithField("resource", "services").Fatal("failed to create informer")
711+
}
712+
713+
ingressHandler := &k8s.IngressStatusLoadBalancerWatcher{
714+
ServiceName: contourConfiguration.Envoy.Service.Name,
715+
LBStatus: lbsw.lbStatus,
716+
Log: s.log.WithField("context", "ingressStatusLoadBalancerWatcher"),
717+
}
718+
719+
var ingressEventHandler cache.ResourceEventHandler = ingressHandler
720+
if contourConfiguration.Envoy.Ingress.Namespace != "" {
721+
handler = k8s.NewNamespaceFilter([]string{contourConfiguration.Envoy.Ingress.Namespace}, handler)
722+
}
723+
724+
if err := informOnResource(&networking_v1.Ingress{}, ingressEventHandler, s.mgr.GetCache()); err != nil {
725+
s.log.WithError(err).WithField("resource", "ingresses").Fatal("failed to create ingresses informer")
726+
}
727+
728+
s.log.WithField("envoy-service-name", contourConfiguration.Envoy.Service.Name).
729+
WithField("envoy-service-namespace", contourConfiguration.Envoy.Service.Namespace).
730+
Info("Watching Service for Ingress status")
731+
732+
s.log.WithField("envoy-ingress-name", contourConfiguration.Envoy.Ingress.Name).
733+
WithField("envoy-ingress-namespace", contourConfiguration.Envoy.Ingress.Namespace).
734+
Info("Watching Ingress for Ingress status")
735+
}
736+
691737
xdsServer := &xdsServer{
692738
log: s.log,
693739
registry: s.registry,

cmd/contour/servecontext.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,10 @@ func (ctx *serveContext) convertToContourConfigurationSpec() contour_v1alpha1.Co
571571
Name: ctx.Config.EnvoyServiceName,
572572
Namespace: ctx.Config.EnvoyServiceNamespace,
573573
},
574+
Ingress: &contour_v1alpha1.NamespacedName{
575+
Name: ctx.Config.EnvoyIngressName,
576+
Namespace: ctx.Config.EnvoyIngressNamespace,
577+
},
574578
HTTPListener: &contour_v1alpha1.EnvoyListener{
575579
Address: ctx.httpAddr,
576580
Port: ctx.httpPort,

internal/k8s/statusaddress.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,3 +280,63 @@ func lbStatusToGatewayAddresses(lbs core_v1.LoadBalancerStatus) []gatewayapi_v1.
280280

281281
return addrs
282282
}
283+
284+
// IngressStatusLoadBalancerWatcher implements ResourceEventHandler and
285+
// watches for changes to the status.loadbalancer field
286+
// Note that we specifically *don't* inspect inside the struct, as sending empty values
287+
// is desirable to clear the status.
288+
type IngressStatusLoadBalancerWatcher struct {
289+
IngressName string
290+
LBStatus chan networking_v1.IngressLoadBalancerStatus
291+
Log logrus.FieldLogger
292+
}
293+
294+
func (s *IngressStatusLoadBalancerWatcher) OnAdd(obj interface{}) {
295+
ingress, ok := obj.(*networking_v1.Ingress)
296+
if !ok {
297+
// not a service
298+
return
299+
}
300+
if ingress.Name != s.IngressName {
301+
return
302+
}
303+
s.Log.WithField("name", ingress.Name).
304+
WithField("namespace", ingress.Namespace).
305+
Debug("received new service address")
306+
307+
s.notify(ingress.Status.LoadBalancer)
308+
}
309+
310+
func (s *IngressStatusLoadBalancerWatcher) OnUpdate(oldObj, newObj interface{}) {
311+
ingress, ok := newObj.(*networking_v1.Ingress)
312+
if !ok {
313+
// not a service
314+
return
315+
}
316+
if ingress.Name != s.IngressName {
317+
return
318+
}
319+
s.Log.WithField("name", ingress.Name).
320+
WithField("namespace", ingress.Namespace).
321+
Debug("received new service address")
322+
323+
s.notify(ingress.Status.LoadBalancer)
324+
}
325+
326+
func (s *IngressStatusLoadBalancerWatcher) OnDelete(obj interface{}) {
327+
ingress, ok := obj.(*networking_v1.Ingress)
328+
if !ok {
329+
// not a service
330+
return
331+
}
332+
if ingress.Name != s.IngressName {
333+
return
334+
}
335+
s.notify(networking_v1.IngressLoadBalancerStatus{
336+
Ingress: nil,
337+
})
338+
}
339+
340+
func (s *IngressStatusLoadBalancerWatcher) notify(lbstatus networking_v1.IngressLoadBalancerStatus) {
341+
s.LBStatus <- lbstatus
342+
}

internal/provisioner/model/names.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ func (c *Contour) EnvoyServiceName() string {
3232
return "envoy-" + c.Name
3333
}
3434

35+
// EnvoyIngressName returns the name of the Envoy Ingress resource.
36+
func (c *Contour) EnvoyIngressName() string {
37+
return "envoy-" + c.Name
38+
}
39+
3540
// ContourDeploymentName returns the name of the Contour Deployment resource.
3641
func (c *Contour) ContourDeploymentName() string {
3742
return "contour-" + c.Name

internal/provisioner/objects/contourconfig/contourconfig.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ func setGatewayConfig(config *contour_v1alpha1.ContourConfiguration, contour *mo
7373
Namespace: contour.Namespace,
7474
Name: contour.EnvoyServiceName(),
7575
}
76+
config.Spec.Envoy.Ingress = &contour_api_v1alpha1.NamespacedName{
77+
Namespace: contour.Namespace,
78+
Name: contour.EnvoyIngressName(),
79+
}
7680
}
7781

7882
// EnsureContourConfigDeleted deletes a ContourConfig for the provided contour, if the configured owner labels exist.

internal/provisioner/objects/deployment/deployment.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ func DesiredDeployment(contour *model.Contour, image string) *apps_v1.Deployment
9595
fmt.Sprintf("--contour-config-name=%s", contour.ContourConfigurationName()),
9696
fmt.Sprintf("--leader-election-resource-name=%s", contour.LeaderElectionLeaseName()),
9797
fmt.Sprintf("--envoy-service-name=%s", contour.EnvoyServiceName()),
98+
fmt.Sprintf("--envoy-ingress-name=%s", contour.EnvoyIngressName()),
9899
fmt.Sprintf("--kubernetes-debug=%d", contour.Spec.KubernetesLogLevel),
99100
}
100101

pkg/config/parameters.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,12 @@ type Parameters struct {
667667
// Name of the envoy service to inspect for Ingress status details.
668668
EnvoyServiceName string `yaml:"envoy-service-name,omitempty"`
669669

670+
// Namespace of the envoy ingress to inspect for Ingress status details.
671+
EnvoyIngressNamespace string `yaml:"envoy-ingress-namespace,omitempty"`
672+
673+
// Name of the envoy ingress to inspect for Ingress status details.
674+
EnvoyIngressName string `yaml:"envoy-ingress-name,omitempty"`
675+
670676
// DefaultHTTPVersions defines the default set of HTTPS
671677
// versions the proxy should accept. HTTP versions are
672678
// strings of the form "HTTP/xx". Supported versions are
@@ -1093,6 +1099,8 @@ func Defaults() Parameters {
10931099
},
10941100
EnvoyServiceName: "envoy",
10951101
EnvoyServiceNamespace: contourNamespace,
1102+
EnvoyIngressName: "envoy",
1103+
EnvoyIngressNamespace: contourNamespace,
10961104
DefaultHTTPVersions: []HTTPVersionType{},
10971105
Cluster: ClusterParameters{
10981106
DNSLookupFamily: AutoClusterDNSFamily,

0 commit comments

Comments
 (0)