Skip to content

Commit

Permalink
sotw dnspolicy: init updates
Browse files Browse the repository at this point in the history
* 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 the correct label.
* Adds predicates to only trigger on events to owned policies (might be pointless)

Signed-off-by: Michael Nairn <[email protected]>
  • Loading branch information
mikenairn committed Oct 22, 2024
1 parent 639df0b commit ab8d5f4
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 21 deletions.
21 changes: 21 additions & 0 deletions controllers/common.go
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,
}
}
2 changes: 1 addition & 1 deletion controllers/dns_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type dnsHelper struct {
}

func commonDNSRecordLabels(gwKey client.ObjectKey, p *v1alpha1.DNSPolicy) map[string]string {
commonLabels := map[string]string{}
commonLabels := CommonLabels()
for k, v := range policyDNSRecordLabels(p) {
commonLabels[k] = v
}
Expand Down
16 changes: 10 additions & 6 deletions controllers/dns_workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ import (
"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"

kuadrantdnsv1alpha1 "github.com/kuadrant/dns-operator/api/v1alpha1"
)

const (
DNSRecordKind = "DNSRecord"
StateDNSPolicyAcceptedKey = "DNSPolicyValid"
)

var (
Expand All @@ -28,11 +34,9 @@ var (
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},
Tasks: []controller.ReconcileFunc{
NewEffectiveDNSPoliciesReconciler(client).Subscription().Reconcile,
},
Postcondition: NewDNSPolicyStatusUpdater(client).Subscription().Reconcile,
}
}
Expand Down
31 changes: 21 additions & 10 deletions controllers/dnspolicies_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import (
"context"
"sync"

"github.com/samber/lo"

apierrors "k8s.io/apimachinery/pkg/api/errors"

"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{}{}
"github.com/kuadrant/kuadrant-operator/pkg/library/kuadrant"
)

func NewDNSPoliciesValidator() *DNSPoliciesValidator {
Expand All @@ -31,14 +31,25 @@ func (r *DNSPoliciesValidator) Subscription() controller.Subscription {
}
}

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
func (r *DNSPoliciesValidator) validate(ctx context.Context, _ []controller.ResourceEvent, topology *machinery.Topology, _ error, state *sync.Map) error {
logger := controller.LoggerFromContext(ctx).WithName("DNSPoliciesValidator")

policies := lo.FilterMap(topology.Policies().Items(), func(item machinery.Policy, index int) (*kuadrantv1alpha1.DNSPolicy, bool) {
p, ok := item.(*kuadrantv1alpha1.DNSPolicy)
return p, ok
})

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
logger.V(1).Info("validating dns policies", "policies", len(policies))

state.Store(StateDNSPolicyAcceptedKey, lo.SliceToMap(policies, func(policy *kuadrantv1alpha1.DNSPolicy) (string, error) {
if len(policy.GetTargetRefs()) == 0 || len(topology.Targetables().Children(policy)) == 0 {
return policy.GetLocator(), kuadrant.NewErrTargetNotFound(policy.Kind(), policy.GetTargetRef(),
apierrors.NewNotFound(kuadrantv1alpha1.DNSPoliciesResource.GroupResource(), policy.GetName()))
}
return policy.GetLocator(), nil
}))

logger.V(1).Info("finished validating dns policies")

return nil
}
7 changes: 4 additions & 3 deletions controllers/dnspolicy_status_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"context"
"sync"

"k8s.io/client-go/dynamic"

"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"
)
Expand All @@ -21,7 +22,7 @@ type DNSPolicyStatusUpdater struct {

func (r *DNSPolicyStatusUpdater) Subscription() controller.Subscription {
return controller.Subscription{
ReconcileFunc: r.update,
ReconcileFunc: r.updateStatus,
Events: []controller.ResourceEventMatcher{
{Kind: &machinery.GatewayGroupKind},
{Kind: &kuadrantv1alpha1.DNSPolicyGroupKind},
Expand All @@ -30,7 +31,7 @@ func (r *DNSPolicyStatusUpdater) Subscription() controller.Subscription {
}
}

func (r *DNSPolicyStatusUpdater) update(_ context.Context, _ []controller.ResourceEvent, _ *machinery.Topology, _ error, _ *sync.Map) error {
func (r *DNSPolicyStatusUpdater) updateStatus(_ context.Context, _ []controller.ResourceEvent, _ *machinery.Topology, _ error, _ *sync.Map) error {
//ToDo Implement implement me !!!
return nil
}
9 changes: 8 additions & 1 deletion controllers/state_of_the_world.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,16 @@ func (b *BootOptionsBuilder) getConsolePluginOptions() []controller.ControllerOp
}

func (b *BootOptionsBuilder) getDNSOperatorOptions() []controller.ControllerOption {
isDNSRecordOwnedByDNSPolicy := func(c *kuadrantdnsv1alpha1.DNSRecord) bool {
return true
}

var opts []controller.ControllerOption
opts = append(opts,
controller.WithRunnable("dnsrecord watcher", controller.Watch(&kuadrantdnsv1alpha1.DNSRecord{}, DNSRecordResource, metav1.NamespaceAll)),
controller.WithRunnable("dnsrecord watcher", controller.Watch(
&kuadrantdnsv1alpha1.DNSRecord{}, DNSRecordResource, metav1.NamespaceAll,
controller.FilterResourcesByLabel[*kuadrantdnsv1alpha1.DNSRecord](fmt.Sprintf("%s=%s", AppLabelKey, AppLabelValue)),
controller.WithPredicates(ctrlruntimepredicate.NewTypedPredicateFuncs(isDNSRecordOwnedByDNSPolicy)))),
controller.WithObjectKinds(
DNSRecordGroupKind,
),
Expand Down

0 comments on commit ab8d5f4

Please sign in to comment.