Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ingress): aws alb #294

Merged
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 {
mirshahriar marked this conversation as resolved.
Show resolved Hide resolved
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
41 changes: 38 additions & 3 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 @@ -185,9 +190,14 @@ func (r *MattermostReconciler) checkMattermostRoleBinding(mattermost *mmv1beta.M
}

func (r *MattermostReconciler) checkMattermostIngress(mattermost *mmv1beta.Mattermost, reqLogger logr.Logger) error {
desired := mattermostApp.GenerateIngressV1Beta(mattermost)
var desired *networkingv1.Ingress
Hunter-Thompson marked this conversation as resolved.
Show resolved Hide resolved
desired = mattermostApp.GenerateIngressV1Beta(mattermost)

if !mattermost.IngressEnabled() {
if mattermost.Spec.AWSLoadBalancerController != nil && mattermost.Spec.AWSLoadBalancerController.Enabled {
Hunter-Thompson marked this conversation as resolved.
Show resolved Hide resolved
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 +219,38 @@ 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 {
var desired *networkingv1.IngressClass
desired = mattermostApp.GenerateALBIngressClassV1Beta(mattermost)
Hunter-Thompson marked this conversation as resolved.
Show resolved Hide resolved

if mattermost.Spec.AWSLoadBalancerController == nil || mattermost.Spec.AWSLoadBalancerController.Enabled == false || mattermost.Spec.AWSLoadBalancerController.IngressClassName != "" {
mirshahriar marked this conversation as resolved.
Show resolved Hide resolved
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