-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic setup for DNSPolicy state of the world tasks, dnsrecord types, watcher and linker function (Listener -> DNSRecord) Signed-off-by: Michael Nairn <mnairn@redhat.com>
- Loading branch information
Showing
6 changed files
with
188 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,59 @@ | ||
package controllers | ||
|
||
import "github.com/kuadrant/policy-machinery/controller" | ||
import ( | ||
"github.com/samber/lo" | ||
|
||
func NewDNSWorkflow() *controller.Workflow { | ||
return &controller.Workflow{} | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/client-go/dynamic" | ||
gwapiv1 "sigs.k8s.io/gateway-api/apis/v1" | ||
|
||
kuadrantdnsv1alpha1 "github.com/kuadrant/dns-operator/api/v1alpha1" | ||
"github.com/kuadrant/policy-machinery/controller" | ||
"github.com/kuadrant/policy-machinery/machinery" | ||
|
||
kuadrantv1alpha1 "github.com/kuadrant/kuadrant-operator/api/v1alpha1" | ||
) | ||
|
||
var ( | ||
DNSRecordResource = kuadrantdnsv1alpha1.GroupVersion.WithResource("dnsrecords") | ||
DNSRecordGroupKind = schema.GroupKind{Group: kuadrantdnsv1alpha1.GroupVersion.Group, Kind: "DNSRecord"} | ||
) | ||
|
||
//+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) *controller.Workflow { | ||
return &controller.Workflow{ | ||
Precondition: NewDNSPoliciesValidator().Subscription().Reconcile, | ||
Tasks: []controller.ReconcileFunc{(&controller.Workflow{ | ||
Tasks: []controller.ReconcileFunc{ | ||
NewEffectiveDNSPoliciesReconciler(client).Subscription().Reconcile, | ||
}, | ||
}).Run}, | ||
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) { | ||
o := child.(*controller.RuntimeObject) | ||
return l, isObjectOwnedByGroupKind(o, kuadrantv1alpha1.DNSPolicyGroupKind) && | ||
l.GetNamespace() == child.GetNamespace() && | ||
child.GetName() == dnsRecordName(l.Gateway.Name, string(l.Name)) | ||
}) | ||
}, | ||
} | ||
} |
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,44 @@ | ||
package controllers | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
|
||
"github.com/kuadrant/policy-machinery/controller" | ||
"github.com/kuadrant/policy-machinery/machinery" | ||
"github.com/samber/lo" | ||
|
||
kuadrantv1alpha1 "github.com/kuadrant/kuadrant-operator/api/v1alpha1" | ||
) | ||
|
||
var ( | ||
StateDNSPolicyValid = struct{}{} | ||
) | ||
|
||
func NewDNSPoliciesValidator() *DNSPoliciesValidator { | ||
return &DNSPoliciesValidator{} | ||
} | ||
|
||
type DNSPoliciesValidator struct{} | ||
|
||
func (r *DNSPoliciesValidator) Subscription() controller.Subscription { | ||
return controller.Subscription{ | ||
ReconcileFunc: r.validate, | ||
Events: []controller.ResourceEventMatcher{ | ||
{Kind: &machinery.GatewayGroupKind}, | ||
{Kind: &kuadrantv1alpha1.DNSPolicyGroupKind}, | ||
}, | ||
} | ||
} | ||
|
||
func (r *DNSPoliciesValidator) validate(_ context.Context, _ []controller.ResourceEvent, topology *machinery.Topology, _ error, state *sync.Map) error { | ||
policies := topology.Policies().Items(func(o machinery.Object) bool { | ||
return o.GroupVersionKind().GroupKind() == kuadrantv1alpha1.DNSPolicyGroupKind | ||
}) | ||
|
||
state.Store(StateDNSPolicyValid, lo.SliceToMap(policies, func(policy machinery.Policy) (string, bool) { | ||
return policy.GetLocator(), len(policy.GetTargetRefs()) == 0 || len(topology.Targetables().Parents(policy)) > 0 | ||
})) | ||
|
||
return nil | ||
} |
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package controllers | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
|
||
"github.com/kuadrant/policy-machinery/controller" | ||
"github.com/kuadrant/policy-machinery/machinery" | ||
"k8s.io/client-go/dynamic" | ||
|
||
kuadrantv1alpha1 "github.com/kuadrant/kuadrant-operator/api/v1alpha1" | ||
) | ||
|
||
func NewDNSPolicyStatusUpdater(client *dynamic.DynamicClient) *DNSPolicyStatusUpdater { | ||
return &DNSPolicyStatusUpdater{client: client} | ||
} | ||
|
||
type DNSPolicyStatusUpdater struct { | ||
client *dynamic.DynamicClient | ||
} | ||
|
||
func (r *DNSPolicyStatusUpdater) Subscription() controller.Subscription { | ||
return controller.Subscription{ | ||
ReconcileFunc: r.update, | ||
Events: []controller.ResourceEventMatcher{ | ||
{Kind: &machinery.GatewayGroupKind}, | ||
{Kind: &kuadrantv1alpha1.DNSPolicyGroupKind}, | ||
{Kind: &DNSRecordGroupKind}, | ||
}, | ||
} | ||
} | ||
|
||
func (r *DNSPolicyStatusUpdater) update(_ context.Context, _ []controller.ResourceEvent, _ *machinery.Topology, _ error, _ *sync.Map) error { | ||
//ToDo Implement implement me !!! | ||
return nil | ||
} |
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,35 @@ | ||
package controllers | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
|
||
"github.com/kuadrant/policy-machinery/controller" | ||
"github.com/kuadrant/policy-machinery/machinery" | ||
"k8s.io/client-go/dynamic" | ||
|
||
kuadrantv1alpha1 "github.com/kuadrant/kuadrant-operator/api/v1alpha1" | ||
) | ||
|
||
func NewEffectiveDNSPoliciesReconciler(client *dynamic.DynamicClient) *EffectiveDNSPoliciesReconciler { | ||
return &EffectiveDNSPoliciesReconciler{client: client} | ||
} | ||
|
||
type EffectiveDNSPoliciesReconciler struct { | ||
client *dynamic.DynamicClient | ||
} | ||
|
||
func (r *EffectiveDNSPoliciesReconciler) Subscription() controller.Subscription { | ||
return controller.Subscription{ | ||
ReconcileFunc: r.reconcile, | ||
Events: []controller.ResourceEventMatcher{ | ||
{Kind: &machinery.GatewayGroupKind}, | ||
{Kind: &kuadrantv1alpha1.DNSPolicyGroupKind}, | ||
{Kind: &DNSRecordGroupKind}, | ||
}, | ||
} | ||
} | ||
|
||
func (r *EffectiveDNSPoliciesReconciler) reconcile(ctx context.Context, _ []controller.ResourceEvent, topology *machinery.Topology, _ error, state *sync.Map) error { | ||
return nil | ||
} |
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