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

Add Ingress support #79

Merged
merged 2 commits into from
Apr 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions deploy/cluster_role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,11 @@ rules:
resources:
- servicebindingrequests
verbs:
- '*'
- apiGroups:
- networking.k8s.io
- extensions
resources:
- ingresses
verbs:
- '*'
7 changes: 7 additions & 0 deletions deploy/releases/daily/runtime-component-cluster-rbac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ rules:
- servicebindingrequests
verbs:
- '*'
- apiGroups:
- networking.k8s.io
- extensions
resources:
- ingresses
verbs:
- '*'
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
Expand Down
7 changes: 7 additions & 0 deletions deploy/releases/daily/runtime-component-operator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ rules:
- servicebindingrequests
verbs:
- '*'
- apiGroups:
- networking.k8s.io
- extensions
resources:
- ingresses
verbs:
- '*'
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
Expand Down
7 changes: 7 additions & 0 deletions deploy/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,11 @@ rules:
resources:
- servicebindingrequests
verbs:
- '*'
- apiGroups:
- networking.k8s.io
- extensions
resources:
- ingresses
verbs:
- '*'
25 changes: 23 additions & 2 deletions pkg/controller/runtimecomponent/runtimecomponent_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"os"

networkingv1beta1 "k8s.io/api/networking/v1beta1"

"github.com/application-stacks/runtime-component-operator/pkg/common"

"github.com/operator-framework/operator-sdk/pkg/k8sutil"
Expand Down Expand Up @@ -479,6 +481,7 @@ func (r *ReconcileRuntimeComponent) Reconcile(request reconcile.Request) (reconc
&appsv1.Deployment{ObjectMeta: defaultMeta},
&appsv1.StatefulSet{ObjectMeta: defaultMeta},
&autoscalingv1.HorizontalPodAutoscaler{ObjectMeta: defaultMeta},
&networkingv1beta1.Ingress{ObjectMeta: defaultMeta},
}
err = r.DeleteResources(resources)
if err != nil {
Expand Down Expand Up @@ -508,9 +511,8 @@ func (r *ReconcileRuntimeComponent) Reconcile(request reconcile.Request) (reconc
return r.ManageError(err, common.StatusConditionTypeReconciled, instance)
}
return r.ManageSuccess(common.StatusConditionTypeReconciled, instance)
} else {
return r.ManageError(errors.New("failed to reconcile Knative service as operator could not find Knative CRDs"), common.StatusConditionTypeReconciled, instance)
}
return r.ManageError(errors.New("failed to reconcile Knative service as operator could not find Knative CRDs"), common.StatusConditionTypeReconciled, instance)
}

if isKnativeSupported {
Expand Down Expand Up @@ -655,6 +657,25 @@ func (r *ReconcileRuntimeComponent) Reconcile(request reconcile.Request) (reconc
}
} else {
reqLogger.V(1).Info(fmt.Sprintf("%s is not supported", routev1.SchemeGroupVersion.String()))
if instance.Spec.Expose != nil && *instance.Spec.Expose {
ing := &networkingv1beta1.Ingress{ObjectMeta: defaultMeta}
navidsh marked this conversation as resolved.
Show resolved Hide resolved
err = r.CreateOrUpdate(ing, instance, func() error {
appstacksutils.CustomizeIngress(ing, instance)
return nil
})
if err != nil {
reqLogger.Error(err, "Failed to reconcile Ingress")
return r.ManageError(err, common.StatusConditionTypeReconciled, instance)
}
} else {
ing := &networkingv1beta1.Ingress{ObjectMeta: defaultMeta}
err = r.DeleteResource(ing)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the Knative scenario, please delete the ingress resource

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch

if err != nil {
reqLogger.Error(err, "Failed to delete Ingress")
return r.ManageError(err, common.StatusConditionTypeReconciled, instance)
}
}

}

if ok, err := r.IsGroupVersionSupported(prometheusv1.SchemeGroupVersion.String(), "ServiceMonitor"); err != nil {
Expand Down
53 changes: 53 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"strconv"
"strings"

networkingv1beta1 "k8s.io/api/networking/v1beta1"

"github.com/application-stacks/runtime-component-operator/pkg/common"
prometheusv1 "github.com/coreos/prometheus-operator/pkg/apis/monitoring/v1"

Expand Down Expand Up @@ -856,3 +858,54 @@ func GetAppContainer(containerList []corev1.Container) *corev1.Container {
}
return &containerList[0]
}

// CustomizeIngress customizes ingress resource
func CustomizeIngress(ing *networkingv1beta1.Ingress, ba common.BaseComponent) {
obj := ba.(metav1.Object)
ing.Labels = ba.GetLabels()
ing.Annotations = MergeMaps(ing.Annotations, ba.GetAnnotations(), ba.GetRoute().GetAnnotations())
servicePort := strconv.Itoa(int(ba.GetService().GetPort())) + "-tcp"
rt := ba.GetRoute()
if ba.GetService().GetPortName() != "" {
servicePort = ba.GetService().GetPortName()
}
ing.Spec.Rules = []networkingv1beta1.IngressRule{
{
Host: rt.GetHost(),
IngressRuleValue: networkingv1beta1.IngressRuleValue{
HTTP: &networkingv1beta1.HTTPIngressRuleValue{
Paths: []networkingv1beta1.HTTPIngressPath{
{
Path: rt.GetPath(),
Backend: networkingv1beta1.IngressBackend{
ServiceName: obj.GetName(),
ServicePort: intstr.IntOrString{Type: intstr.String, StrVal: servicePort},
},
},
},
},
},
},
}

tlsSecretName := ""
if rt.GetCertificate() != nil {
tlsSecretName = obj.GetName() + "-route-tls"
if rt.GetCertificate().GetSpec().SecretName != "" {
tlsSecretName = obj.GetName() + rt.GetCertificate().GetSpec().SecretName
}
}
if rt.GetCertificateSecretRef() != nil && *rt.GetCertificateSecretRef() != "" {
tlsSecretName = *rt.GetCertificateSecretRef()
}
if tlsSecretName != "" {
ing.Spec.TLS = []networkingv1beta1.IngressTLS{
{
Hosts: []string{rt.GetHost()},
SecretName: tlsSecretName,
},
}
} else {
ing.Spec.TLS = nil
}
}