|
| 1 | +package ingress |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/google/go-cmp/cmp" |
| 8 | + "github.com/google/go-cmp/cmp/cmpopts" |
| 9 | + |
| 10 | + operatorv1 "github.com/openshift/api/operator/v1" |
| 11 | + "github.com/openshift/cluster-ingress-operator/pkg/manifests" |
| 12 | + controller "github.com/openshift/cluster-ingress-operator/pkg/operator/controller" |
| 13 | + |
| 14 | + networkingv1 "k8s.io/api/networking/v1" |
| 15 | + |
| 16 | + "k8s.io/apimachinery/pkg/api/errors" |
| 17 | +) |
| 18 | + |
| 19 | +// ensureRouterNetworkPolicy ensures the per-ingresscontroller NetworkPolicy that |
| 20 | +// allows ingress to router pods and egress to the network exists and is up to date. |
| 21 | +func (r *reconciler) ensureRouterNetworkPolicy(ic *operatorv1.IngressController) error { |
| 22 | + desired := desiredRouterNetworkPolicy(ic) |
| 23 | + |
| 24 | + have, current, err := r.currentRouterNetworkPolicy(ic) |
| 25 | + if err != nil { |
| 26 | + return err |
| 27 | + } |
| 28 | + |
| 29 | + switch { |
| 30 | + case !have: |
| 31 | + if err := r.client.Create(context.TODO(), desired); err != nil { |
| 32 | + return fmt.Errorf("failed to create router network policy: %v", err) |
| 33 | + } |
| 34 | + log.Info("created router network policy", "networkpolicy", desired) |
| 35 | + return nil |
| 36 | + default: |
| 37 | + if updated, err := r.updateRouterNetworkPolicy(current, desired); err != nil { |
| 38 | + return fmt.Errorf("failed to update router network policy: %v", err) |
| 39 | + } else if updated { |
| 40 | + return nil |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + return nil |
| 45 | +} |
| 46 | + |
| 47 | +// desiredRouterNetworkPolicy returns the desired per-ingresscontroller NetworkPolicy. |
| 48 | +func desiredRouterNetworkPolicy(ic *operatorv1.IngressController) *networkingv1.NetworkPolicy { |
| 49 | + np := manifests.RouterNetworkPolicyAllow() |
| 50 | + |
| 51 | + name := controller.RouterNetworkPolicyName(ic) |
| 52 | + np.Namespace = name.Namespace |
| 53 | + np.Name = name.Name |
| 54 | + |
| 55 | + if np.Labels == nil { |
| 56 | + np.Labels = map[string]string{} |
| 57 | + } |
| 58 | + np.Labels[manifests.OwningIngressControllerLabel] = ic.Name |
| 59 | + |
| 60 | + // Select router pods for this ingresscontroller. |
| 61 | + np.Spec.PodSelector = *controller.IngressControllerDeploymentPodSelector(ic) |
| 62 | + |
| 63 | + return np |
| 64 | +} |
| 65 | + |
| 66 | +// currentRouterNetworkPolicy returns the current per-ingresscontroller NetworkPolicy, if it exists. |
| 67 | +func (r *reconciler) currentRouterNetworkPolicy(ic *operatorv1.IngressController) (bool, *networkingv1.NetworkPolicy, error) { |
| 68 | + current := &networkingv1.NetworkPolicy{} |
| 69 | + if err := r.client.Get(context.TODO(), controller.RouterNetworkPolicyName(ic), current); err != nil { |
| 70 | + if errors.IsNotFound(err) { |
| 71 | + return false, nil, nil |
| 72 | + } |
| 73 | + return false, nil, err |
| 74 | + } |
| 75 | + return true, current, nil |
| 76 | +} |
| 77 | + |
| 78 | +// updateRouterNetworkPolicy updates the NetworkPolicy if it differs from the desired state. |
| 79 | +func (r *reconciler) updateRouterNetworkPolicy(current, desired *networkingv1.NetworkPolicy) (bool, error) { |
| 80 | + changed, updated := routerNetworkPolicyChanged(current, desired) |
| 81 | + if !changed { |
| 82 | + return false, nil |
| 83 | + } |
| 84 | + |
| 85 | + // Diff before updating because the client may mutate the object. |
| 86 | + diff := cmp.Diff(current, updated, cmpopts.EquateEmpty()) |
| 87 | + if err := r.client.Update(context.TODO(), updated); err != nil { |
| 88 | + return false, err |
| 89 | + } |
| 90 | + log.Info("updated router network policy", "namespace", updated.Namespace, "name", updated.Name, "diff", diff) |
| 91 | + return true, nil |
| 92 | +} |
| 93 | + |
| 94 | +// routerNetworkPolicyChanged checks whether the current NetworkPolicy matches the expected |
| 95 | +// state and, if not, returns an updated NetworkPolicy. |
| 96 | +func routerNetworkPolicyChanged(current, expected *networkingv1.NetworkPolicy) (bool, *networkingv1.NetworkPolicy) { |
| 97 | + changed := false |
| 98 | + |
| 99 | + if !cmp.Equal(current.Spec, expected.Spec, cmpopts.EquateEmpty()) { |
| 100 | + changed = true |
| 101 | + } |
| 102 | + |
| 103 | + // Ensure the owning-ingresscontroller label value is as expected while preserving other labels. |
| 104 | + if current.Labels == nil || current.Labels[manifests.OwningIngressControllerLabel] != expected.Labels[manifests.OwningIngressControllerLabel] { |
| 105 | + changed = true |
| 106 | + } |
| 107 | + |
| 108 | + if !changed { |
| 109 | + return false, nil |
| 110 | + } |
| 111 | + |
| 112 | + updated := current.DeepCopy() |
| 113 | + updated.Spec = expected.Spec |
| 114 | + |
| 115 | + if updated.Labels == nil { |
| 116 | + updated.Labels = map[string]string{} |
| 117 | + } |
| 118 | + updated.Labels[manifests.OwningIngressControllerLabel] = expected.Labels[manifests.OwningIngressControllerLabel] |
| 119 | + |
| 120 | + return true, updated |
| 121 | +} |
0 commit comments