|
| 1 | +package controller |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/google/go-cmp/cmp" |
| 8 | + "github.com/google/go-cmp/cmp/cmpopts" |
| 9 | + operatorv1 "github.com/openshift/api/operator/v1" |
| 10 | + "github.com/openshift/cluster-dns-operator/pkg/manifests" |
| 11 | + |
| 12 | + "github.com/sirupsen/logrus" |
| 13 | + |
| 14 | + networkingv1 "k8s.io/api/networking/v1" |
| 15 | + "k8s.io/apimachinery/pkg/api/errors" |
| 16 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 17 | +) |
| 18 | + |
| 19 | +// ensureDNSNetworkPolicy ensures that a NetworkPolicy exists for the given DNS. |
| 20 | +func (r *reconciler) ensureDNSNetworkPolicy(dns *operatorv1.DNS) (bool, *networkingv1.NetworkPolicy, error) { |
| 21 | + haveNP, current, err := r.currentDNSNetworkPolicy(dns) |
| 22 | + if err != nil { |
| 23 | + return false, nil, err |
| 24 | + } |
| 25 | + |
| 26 | + desired := desiredDNSNetworkPolicy(dns) |
| 27 | + |
| 28 | + switch { |
| 29 | + case !haveNP: |
| 30 | + if err := r.client.Create(context.TODO(), desired); err != nil { |
| 31 | + return false, nil, fmt.Errorf("failed to create dns networkpolicy: %v", err) |
| 32 | + } |
| 33 | + logrus.Infof("created dns networkpolicy: %s/%s", desired.Namespace, desired.Name) |
| 34 | + return r.currentDNSNetworkPolicy(dns) |
| 35 | + case haveNP: |
| 36 | + if updated, err := r.updateDNSNetworkPolicy(current, desired); err != nil { |
| 37 | + return true, current, err |
| 38 | + } else if updated { |
| 39 | + return r.currentDNSNetworkPolicy(dns) |
| 40 | + } |
| 41 | + } |
| 42 | + return true, current, nil |
| 43 | +} |
| 44 | + |
| 45 | +func (r *reconciler) currentDNSNetworkPolicy(dns *operatorv1.DNS) (bool, *networkingv1.NetworkPolicy, error) { |
| 46 | + current := &networkingv1.NetworkPolicy{} |
| 47 | + if err := r.client.Get(context.TODO(), DNSNetworkPolicyName(dns), current); err != nil { |
| 48 | + if errors.IsNotFound(err) { |
| 49 | + return false, nil, nil |
| 50 | + } |
| 51 | + return false, nil, err |
| 52 | + } |
| 53 | + return true, current, nil |
| 54 | +} |
| 55 | + |
| 56 | +func desiredDNSNetworkPolicy(dns *operatorv1.DNS) *networkingv1.NetworkPolicy { |
| 57 | + np := manifests.DNSNetworkPolicy() |
| 58 | + |
| 59 | + name := DNSNetworkPolicyName(dns) |
| 60 | + np.Namespace = name.Namespace |
| 61 | + np.Name = name.Name |
| 62 | + np.SetOwnerReferences([]metav1.OwnerReference{dnsOwnerRef(dns)}) |
| 63 | + |
| 64 | + if np.Labels == nil { |
| 65 | + np.Labels = map[string]string{} |
| 66 | + } |
| 67 | + np.Labels[manifests.OwningDNSLabel] = DNSDaemonSetLabel(dns) |
| 68 | + |
| 69 | + // Ensure pod selector matches the DNS daemonset pods for this instance. |
| 70 | + if sel := DNSDaemonSetPodSelector(dns); sel != nil { |
| 71 | + np.Spec.PodSelector = *sel |
| 72 | + } |
| 73 | + |
| 74 | + return np |
| 75 | +} |
| 76 | + |
| 77 | +func (r *reconciler) updateDNSNetworkPolicy(current, desired *networkingv1.NetworkPolicy) (bool, error) { |
| 78 | + changed, updated := networkPolicyChanged(current, desired) |
| 79 | + if !changed { |
| 80 | + return false, nil |
| 81 | + } |
| 82 | + |
| 83 | + // Diff before updating because the client may mutate the object. |
| 84 | + diff := cmp.Diff(current, updated, cmpopts.EquateEmpty()) |
| 85 | + if err := r.client.Update(context.TODO(), updated); err != nil { |
| 86 | + return false, fmt.Errorf("failed to update dns networkpolicy %s/%s: %v", updated.Namespace, updated.Name, err) |
| 87 | + } |
| 88 | + logrus.Infof("updated dns networkpolicy %s/%s: %v", updated.Namespace, updated.Name, diff) |
| 89 | + return true, nil |
| 90 | +} |
| 91 | + |
| 92 | +func networkPolicyChanged(current, expected *networkingv1.NetworkPolicy) (bool, *networkingv1.NetworkPolicy) { |
| 93 | + // Ignore fields that the API, other controllers, or users may modify. |
| 94 | + npCmpOpts := []cmp.Option{ |
| 95 | + cmpopts.EquateEmpty(), |
| 96 | + } |
| 97 | + |
| 98 | + currentLabels := current.Labels |
| 99 | + if currentLabels == nil { |
| 100 | + currentLabels = map[string]string{} |
| 101 | + } |
| 102 | + expectedLabels := expected.Labels |
| 103 | + if expectedLabels == nil { |
| 104 | + expectedLabels = map[string]string{} |
| 105 | + } |
| 106 | + |
| 107 | + if cmp.Equal(current.Spec, expected.Spec, npCmpOpts...) && cmp.Equal(currentLabels, expectedLabels, npCmpOpts...) { |
| 108 | + return false, nil |
| 109 | + } |
| 110 | + |
| 111 | + updated := current.DeepCopy() |
| 112 | + updated.Spec = expected.Spec |
| 113 | + if updated.Labels == nil { |
| 114 | + updated.Labels = map[string]string{} |
| 115 | + } |
| 116 | + for k, v := range expectedLabels { |
| 117 | + updated.Labels[k] = v |
| 118 | + } |
| 119 | + |
| 120 | + return true, updated |
| 121 | +} |
0 commit comments