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

Create network policy during knative setup and add knative ports #623

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
18 changes: 18 additions & 0 deletions controllers/runtimecomponent_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,24 @@ func (r *RuntimeComponentReconciler) Reconcile(ctx context.Context, req ctrl.Req

if isKnativeSupported {
reqLogger.Info("Knative is supported and Knative Service is enabled")

networkPolicy := &networkingv1.NetworkPolicy{ObjectMeta: defaultMeta}
if np := instance.Spec.NetworkPolicy; np == nil || np != nil && !np.IsDisabled() {
err = r.CreateOrUpdate(networkPolicy, instance, func() error {
appstacksutils.CustomizeNetworkPolicy(networkPolicy, r.IsOpenShift(), instance)
return nil
})
if err != nil {
reqLogger.Error(err, "Failed to reconcile network policy")
return r.ManageError(err, common.StatusConditionTypeReconciled, instance)
}
} else {
if err := r.DeleteResource(networkPolicy); err != nil {
reqLogger.Error(err, "Failed to delete network policy")
return r.ManageError(err, common.StatusConditionTypeReconciled, instance)
}
}

ksvc := &servingv1.Service{ObjectMeta: defaultMeta}
err = r.CreateOrUpdate(ksvc, instance, func() error {
appstacksutils.CustomizeKnativeService(ksvc, instance)
Expand Down
13 changes: 11 additions & 2 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,13 +449,22 @@ func createNetworkPolicyPeer(appName string, namespace string, networkPolicy com

func customizeNetworkPolicyPorts(ingress *networkingv1.NetworkPolicyIngressRule, ba common.BaseComponent) {
var ports []int32
ports = append(ports, ba.GetService().GetPort())

for _, port := range ba.GetService().GetPorts() {
ports = append(ports, port.Port)
}

currentLen := len(ingress.Ports)
desiredLen := len(ba.GetService().GetPorts()) + 1 // Add one for normal port
desiredLen := len(ba.GetService().GetPorts())

if ba.GetCreateKnativeService() != nil && *ba.GetCreateKnativeService() {
knativeports := []int32{8012, 8013, 8112, 8022, 9090, 9091}
ports = append(ports, knativeports...)
desiredLen += len(knativeports)
} else {
ports = append(ports, ba.GetService().GetPort())
desiredLen += 1 // Add one for normal port
}

// Shrink if needed
if currentLen > desiredLen {
Expand Down