forked from Kuadrant/kuadrant-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* sotw: dnspolicy init Add basic setup for DNSPolicy state of the world tasks, dnsrecord types, watcher and linker function (Listener -> DNSRecord) * Update dns policy validator in preparation for status updates, adds correct errors for acceptance. * Add common labels that get applied to all dnsrecord resources created by the kuadrant operator * Add filter to topology for dnsrecords to only add records that contain * sotw: dnspolicy delete orphan records Move all logic to delete orphan dnsrecord resources for a DNSPolicy to the sotw reconciler and based all decisions on the current topology. Orphan record is one that no longer has a valid path between it's owner DNSPolicy and itself in the topology. This covers the following scenarios: * Listener is deleted from the Gateway * Gateway is deleted * Policy is deleted(K8s will also deal with this due to the owner relationship) * Policy ref is changed Does not deal with the removal of records based on the state of the gateway. * sotw dnspolicy: status and dnspolicies reconciliation * Bump policy-machinery v0.6.1 --------- Signed-off-by: Michael Nairn <mnairn@redhat.com> Signed-off-by: R-Lawton <rlawton@redhat.com>
- Loading branch information
Showing
21 changed files
with
856 additions
and
695 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package controllers | ||
|
||
const ( | ||
KuadrantAppName = "kuadrant" | ||
) | ||
|
||
var ( | ||
AppLabelKey = "app" | ||
AppLabelValue = KuadrantAppName | ||
) | ||
|
||
func CommonLabels() map[string]string { | ||
return map[string]string{ | ||
AppLabelKey: AppLabelValue, | ||
"app.kubernetes.io/component": KuadrantAppName, | ||
"app.kubernetes.io/managed-by": "kuadrant-operator", | ||
"app.kubernetes.io/instance": KuadrantAppName, | ||
"app.kubernetes.io/name": KuadrantAppName, | ||
"app.kubernetes.io/part-of": KuadrantAppName, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,141 @@ | ||
package controllers | ||
|
||
import "github.com/kuadrant/policy-machinery/controller" | ||
import ( | ||
"fmt" | ||
"sync" | ||
|
||
func NewDNSWorkflow() *controller.Workflow { | ||
return &controller.Workflow{} | ||
"github.com/samber/lo" | ||
|
||
"k8s.io/apimachinery/pkg/api/meta" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/client-go/dynamic" | ||
gwapiv1 "sigs.k8s.io/gateway-api/apis/v1" | ||
gatewayapiv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2" | ||
|
||
"github.com/kuadrant/policy-machinery/controller" | ||
"github.com/kuadrant/policy-machinery/machinery" | ||
|
||
kuadrantdnsv1alpha1 "github.com/kuadrant/dns-operator/api/v1alpha1" | ||
|
||
"github.com/kuadrant/kuadrant-operator/api/v1alpha1" | ||
"github.com/kuadrant/kuadrant-operator/pkg/library/utils" | ||
) | ||
|
||
const ( | ||
DNSRecordKind = "DNSRecord" | ||
StateDNSPolicyAcceptedKey = "DNSPolicyValid" | ||
StateDNSPolicyErrorsKey = "DNSPolicyErrors" | ||
) | ||
|
||
var ( | ||
DNSRecordResource = kuadrantdnsv1alpha1.GroupVersion.WithResource("dnsrecords") | ||
DNSRecordGroupKind = schema.GroupKind{Group: kuadrantdnsv1alpha1.GroupVersion.Group, Kind: DNSRecordKind} | ||
) | ||
|
||
//+kubebuilder:rbac:groups=core,resources=namespaces,verbs=get;list;watch | ||
//+kubebuilder:rbac:groups=kuadrant.io,resources=dnspolicies,verbs=get;list;watch;update;patch;delete | ||
//+kubebuilder:rbac:groups=kuadrant.io,resources=dnspolicies/status,verbs=get;update;patch | ||
//+kubebuilder:rbac:groups=kuadrant.io,resources=dnspolicies/finalizers,verbs=update | ||
|
||
//+kubebuilder:rbac:groups=kuadrant.io,resources=dnsrecords,verbs=get;list;watch;create;update;patch;delete | ||
//+kubebuilder:rbac:groups=kuadrant.io,resources=dnsrecords/status,verbs=get | ||
|
||
func NewDNSWorkflow(client *dynamic.DynamicClient, scheme *runtime.Scheme) *controller.Workflow { | ||
return &controller.Workflow{ | ||
Precondition: NewDNSPoliciesValidator().Subscription().Reconcile, | ||
Tasks: []controller.ReconcileFunc{ | ||
NewEffectiveDNSPoliciesReconciler(client, scheme).Subscription().Reconcile, | ||
}, | ||
Postcondition: NewDNSPolicyStatusUpdater(client).Subscription().Reconcile, | ||
} | ||
} | ||
|
||
func LinkListenerToDNSRecord(objs controller.Store) machinery.LinkFunc { | ||
gateways := lo.Map(objs.FilterByGroupKind(machinery.GatewayGroupKind), controller.ObjectAs[*gwapiv1.Gateway]) | ||
listeners := lo.FlatMap(lo.Map(gateways, func(g *gwapiv1.Gateway, _ int) *machinery.Gateway { | ||
return &machinery.Gateway{Gateway: g} | ||
}), machinery.ListenersFromGatewayFunc) | ||
|
||
return machinery.LinkFunc{ | ||
From: machinery.ListenerGroupKind, | ||
To: DNSRecordGroupKind, | ||
Func: func(child machinery.Object) []machinery.Object { | ||
return lo.FilterMap(listeners, func(l *machinery.Listener, _ int) (machinery.Object, bool) { | ||
if dnsRecord, ok := child.(*controller.RuntimeObject).Object.(*kuadrantdnsv1alpha1.DNSRecord); ok { | ||
return l, l.GetNamespace() == dnsRecord.GetNamespace() && | ||
dnsRecord.GetName() == dnsRecordName(l.Gateway.Name, string(l.Name)) | ||
} | ||
return nil, false | ||
}) | ||
}, | ||
} | ||
} | ||
|
||
func LinkDNSPolicyToDNSRecord(objs controller.Store) machinery.LinkFunc { | ||
policies := lo.Map(objs.FilterByGroupKind(v1alpha1.DNSPolicyGroupKind), controller.ObjectAs[*v1alpha1.DNSPolicy]) | ||
|
||
return machinery.LinkFunc{ | ||
From: v1alpha1.DNSPolicyGroupKind, | ||
To: DNSRecordGroupKind, | ||
Func: func(child machinery.Object) []machinery.Object { | ||
if dnsRecord, ok := child.(*controller.RuntimeObject).Object.(*kuadrantdnsv1alpha1.DNSRecord); ok { | ||
return lo.FilterMap(policies, func(dnsPolicy *v1alpha1.DNSPolicy, _ int) (machinery.Object, bool) { | ||
return dnsPolicy, utils.IsOwnedBy(dnsRecord, dnsPolicy) | ||
}) | ||
} | ||
return nil | ||
}, | ||
} | ||
} | ||
|
||
func dnsPolicyAcceptedStatusFunc(state *sync.Map) func(policy machinery.Policy) (bool, error) { | ||
validatedPolicies, validated := state.Load(StateDNSPolicyAcceptedKey) | ||
if !validated { | ||
return dnsPolicyAcceptedStatus | ||
} | ||
validatedPoliciesMap := validatedPolicies.(map[string]error) | ||
return func(policy machinery.Policy) (bool, error) { | ||
err, pValidated := validatedPoliciesMap[policy.GetLocator()] | ||
if pValidated { | ||
return err == nil, err | ||
} | ||
return dnsPolicyAcceptedStatus(policy) | ||
} | ||
} | ||
|
||
func dnsPolicyAcceptedStatus(policy machinery.Policy) (accepted bool, err error) { | ||
p, ok := policy.(*v1alpha1.DNSPolicy) | ||
if !ok { | ||
return | ||
} | ||
if condition := meta.FindStatusCondition(p.Status.Conditions, string(gatewayapiv1alpha2.PolicyConditionAccepted)); condition != nil { | ||
accepted = condition.Status == metav1.ConditionTrue | ||
if !accepted { | ||
err = fmt.Errorf(condition.Message) | ||
} | ||
return | ||
} | ||
return | ||
} | ||
|
||
func dnsPolicyErrorFunc(state *sync.Map) func(policy machinery.Policy) error { | ||
var policyErrorsMap map[string]error | ||
policyErrors, exists := state.Load(StateDNSPolicyErrorsKey) | ||
if exists { | ||
policyErrorsMap = policyErrors.(map[string]error) | ||
} | ||
return func(policy machinery.Policy) error { | ||
return policyErrorsMap[policy.GetLocator()] | ||
} | ||
} | ||
|
||
type dnsPolicyTypeFilter func(item machinery.Policy, index int) (*v1alpha1.DNSPolicy, bool) | ||
|
||
func dnsPolicyTypeFilterFunc() func(item machinery.Policy, _ int) (*v1alpha1.DNSPolicy, bool) { | ||
return func(item machinery.Policy, _ int) (*v1alpha1.DNSPolicy, bool) { | ||
p, ok := item.(*v1alpha1.DNSPolicy) | ||
return p, ok | ||
} | ||
} |
Oops, something went wrong.