Skip to content

Commit

Permalink
feat(ingress): aws alb (#294)
Browse files Browse the repository at this point in the history
* fix: only for v1beta1

* fix: make lb controller optional

* fix: unit tests

* fix: check if ingress is enabled or not

weird interaction with GetIngressEnabled, it returns true by default
this could be a problem when checking for other ingress options like aws ingress

* fix: ingress class & ingress host check

* feat: ingress class for alb

* fix: annotation quotes

* chore: fix style

* chore: fix typo

* feat: test check mattermost aws ingress

* fix: address review comments

* fix: use mattermost name for ingress class

* fix: address comments

* chore: test disable and enable lb

* chore: address review comments

* feat: get ports
  • Loading branch information
Hunter-Thompson authored Dec 5, 2022
1 parent ed6c02d commit aa3dcab
Show file tree
Hide file tree
Showing 11 changed files with 438 additions and 8 deletions.
25 changes: 25 additions & 0 deletions apis/mattermost/v1beta1/mattermost_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ type MattermostSpec struct {
// Ingress defines configuration for Ingress resource created by the Operator.
// +optional
Ingress *Ingress `json:"ingress,omitempty"`

// +optional
AWSLoadBalancerController *AWSLoadBalancerController `json:"awsLoadBalancerController,omitempty"`
// Volumes allows for mounting volumes from various sources into the
// Mattermost application pods.
// +optional
Expand Down Expand Up @@ -177,6 +180,28 @@ type Ingress struct {
IngressClass *string `json:"ingressClass,omitempty"`
}

type AWSLoadBalancerController struct {
// An AWS ALB Ingress will be created instead of nginx
// +optional
Enabled bool `json:"enabled,omitempty"`

// Certificate arn for the ALB, required if SSL enabled
// +optional
CertificateARN string `json:"certificateARN,omitempty"`

// Whether the Ingress will be internetfacing, default is false
// +optional
InternetFacing bool `json:"internetFacing,omitempty"`

// Hosts allows specifying additional domain names for Mattermost to use.
// +optional
Hosts []IngressHost `json:"hosts,omitempty"`

// IngressClassName for your ingress
// +optional
IngressClassName string `json:"ingressClassName,omitempty"`
}

// IngressHost specifies additional hosts configuration.
type IngressHost struct {
HostName string `json:"hostName,omitempty"`
Expand Down
25 changes: 24 additions & 1 deletion apis/mattermost/v1beta1/mattermost_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ const (

// SetDefaults set the missing values in the manifest to the default ones
func (mm *Mattermost) SetDefaults() error {
if mm.IngressEnabled() && mm.GetIngressHost() == "" {
if mm.AWSLoadBalancerEnabled() && len(mm.Spec.AWSLoadBalancerController.Hosts) == 0 {
return errors.New("awsLoadBalancerController.hosts is required, but not set")
}

if !mm.AWSLoadBalancerEnabled() && mm.IngressEnabled() && mm.GetIngressHost() == "" {
return errors.New("ingress.host required, but not set")
}
if mm.Spec.Image == "" {
Expand All @@ -74,6 +78,13 @@ func (mm *Mattermost) IngressEnabled() bool {
return true
}

func (mm *Mattermost) AWSLoadBalancerEnabled() bool {
if mm.Spec.AWSLoadBalancerController != nil {
return mm.Spec.AWSLoadBalancerController.Enabled
}
return false
}

// GetIngressHost returns Mattermost primary Ingress host.
func (mm *Mattermost) GetIngressHost() string {
if mm.Spec.Ingress == nil {
Expand Down Expand Up @@ -113,6 +124,18 @@ func (mm *Mattermost) GetIngressHostNames() []string {
return hosts
}

func (mm *Mattermost) GetAWSLoadBalancerHostNames() []string {
hosts := []string{}

if mm.Spec.AWSLoadBalancerController != nil {
for _, host := range mm.Spec.AWSLoadBalancerController.Hosts {
hosts = append(hosts, host.HostName)
}
}

return hosts
}

// GetIngresAnnotations returns Mattermost Ingress annotations.
func (mm *Mattermost) GetIngresAnnotations() map[string]string {
if mm.Spec.Ingress == nil {
Expand Down
25 changes: 25 additions & 0 deletions apis/mattermost/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion apis/mattermost/v1beta1/zz_generated.openapi.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions config/crd/bases/installation.mattermost.com_mattermosts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,32 @@ spec:
spec:
description: MattermostSpec defines the desired state of Mattermost
properties:
awsLoadBalancerController:
properties:
certificateARN:
description: Certificate arn for the ALB, required if SSL enabled
type: string
enabled:
description: An AWS ALB Ingress will be created instead of nginx
type: boolean
hosts:
description: Hosts allows specifying additional domain names for
Mattermost to use.
items:
description: IngressHost specifies additional hosts configuration.
properties:
hostName:
type: string
type: object
type: array
ingressClassName:
description: IngressClassName for your ingress
type: string
internetFacing:
description: Whether the Ingress will be internetfacing, default
is false
type: boolean
type: object
database:
description: External Services
properties:
Expand Down
1 change: 1 addition & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ rules:
- networking.k8s.io
resources:
- ingresses
- ingressclasses
verbs:
- '*'
- apiGroups:
Expand Down
37 changes: 35 additions & 2 deletions controllers/mattermost/mattermost/mattermost.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ func (r *MattermostReconciler) checkMattermost(
}

if !mattermost.Spec.UseServiceLoadBalancer {
err = r.checkMattermostIngressClass(mattermost, reqLogger)
if err != nil {
return reconcileStatus{}, err
}

err = r.checkMattermostIngress(mattermost, reqLogger)
if err != nil {
return reconcileStatus{}, err
Expand Down Expand Up @@ -187,7 +192,11 @@ func (r *MattermostReconciler) checkMattermostRoleBinding(mattermost *mmv1beta.M
func (r *MattermostReconciler) checkMattermostIngress(mattermost *mmv1beta.Mattermost, reqLogger logr.Logger) error {
desired := mattermostApp.GenerateIngressV1Beta(mattermost)

if !mattermost.IngressEnabled() {
if mattermost.AWSLoadBalancerEnabled() {
desired = mattermostApp.GenerateALBIngressV1Beta(mattermost)
}

if !mattermost.IngressEnabled() && !mattermost.AWSLoadBalancerEnabled() {
err := r.Resources.DeleteIngress(types.NamespacedName{Namespace: desired.Namespace, Name: desired.Name}, reqLogger)
if err != nil {
return errors.Wrap(err, "failed to delete disabled ingress")
Expand All @@ -209,13 +218,37 @@ func (r *MattermostReconciler) checkMattermostIngress(mattermost *mmv1beta.Matte
return r.Resources.Update(current, desired, reqLogger)
}

func (r *MattermostReconciler) checkMattermostIngressClass(mattermost *mmv1beta.Mattermost, reqLogger logr.Logger) error {
desired := mattermostApp.GenerateALBIngressClassV1Beta(mattermost)

if !mattermost.AWSLoadBalancerEnabled() || mattermost.Spec.AWSLoadBalancerController.IngressClassName != "" {
err := r.Resources.DeleteIngressClass(types.NamespacedName{Namespace: desired.Namespace, Name: desired.Name}, reqLogger)
if err != nil {
return errors.Wrap(err, "failed to delete disabled ingressClass")
}
return nil
}

err := r.Resources.CreateIngressClassIfNotExists(mattermost, desired, reqLogger)
if err != nil {
return err
}

current := &networkingv1.IngressClass{}
err = r.Client.Get(context.TODO(), types.NamespacedName{Name: desired.Name, Namespace: desired.Namespace}, current)
if err != nil {
return err
}

return r.Resources.Update(current, desired, reqLogger)
}

func (r *MattermostReconciler) checkMattermostDeployment(
mattermost *mmv1beta.Mattermost,
dbConfig mattermostApp.DatabaseConfig,
fsConfig mattermostApp.FileStoreConfig,
status *mmv1beta.MattermostStatus,
reqLogger logr.Logger) (reconcileStatus, error) {

desired := mattermostApp.GenerateDeploymentV1Beta(
mattermost,
dbConfig,
Expand Down
Loading

0 comments on commit aa3dcab

Please sign in to comment.