Skip to content

Commit

Permalink
Use direct controller in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gemmahou committed Oct 30, 2024
1 parent 209539f commit 7fa9f2d
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 160 deletions.
4 changes: 2 additions & 2 deletions apis/compute/v1beta1/firewallpolicyrule_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import (

var (
ComputeFirewallPolicyRuleGVK = schema.GroupVersionKind{
Group: SchemeGroupVersion.Group,
Version: SchemeGroupVersion.Version,
Group: GroupVersion.Group,
Version: GroupVersion.Version,
Kind: "ComputeFirewallPolicyRule",
}
)
Expand Down
2 changes: 1 addition & 1 deletion dev/tasks/run-e2e
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ if [[ -z "${KUBEBUILDER_ASSETS:-}" ]]; then
fi

if [[ -z "${KCC_USE_DIRECT_RECONCILERS:-}" ]]; then
KCC_USE_DIRECT_RECONCILERS=ComputeForwardingRule,GKEHubFeatureMembership,SecretManagerSecret
KCC_USE_DIRECT_RECONCILERS=ComputeFirewallPolicyRule,ComputeForwardingRule,GKEHubFeatureMembership,SecretManagerSecret
fi
echo "Using direct controllers: $KCC_USE_DIRECT_RECONCILERS"
export KCC_USE_DIRECT_RECONCILERS
Expand Down
88 changes: 0 additions & 88 deletions pkg/controller/direct/compute/firewallpolicyrule/client.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"context"
"fmt"

"google.golang.org/api/option"

gcp "cloud.google.com/go/compute/apiv1"
computepb "cloud.google.com/go/compute/apiv1/computepb"
krm "github.com/GoogleCloudPlatform/k8s-config-connector/apis/compute/v1beta1"
Expand Down Expand Up @@ -59,6 +61,19 @@ type firewallPolicyRuleAdapter struct {

var _ directbase.Adapter = &firewallPolicyRuleAdapter{}

func (m *firewallPolicyRuleModel) client(ctx context.Context) (*gcp.FirewallPoliciesClient, error) {
var opts []option.ClientOption
opts, err := m.config.RESTClientOptions()
if err != nil {
return nil, err
}
gcpClient, err := gcp.NewFirewallPoliciesRESTClient(ctx, opts...)
if err != nil {
return nil, fmt.Errorf("building FirewallPolicy client: %w", err)
}
return gcpClient, err
}

func (m *firewallPolicyRuleModel) AdapterForObject(ctx context.Context, reader client.Reader, u *unstructured.Unstructured) (directbase.Adapter, error) {
obj := &krm.ComputeFirewallPolicyRule{}
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(u.Object, &obj); err != nil {
Expand Down Expand Up @@ -88,16 +103,11 @@ func (m *firewallPolicyRuleModel) AdapterForObject(ctx context.Context, reader c
}

// Get GCP client
gcpClient, err := newGCPClient(ctx, m.config)
gcpClient, err := m.client(ctx)
if err != nil {
return nil, fmt.Errorf("building gcp client: %w", err)
}

firewallPoliciesClient, err := gcpClient.firewallPoliciesClient(ctx)
if err != nil {
return nil, err
}
firewallPolicyRuleAdapter.firewallPoliciesClient = firewallPoliciesClient
firewallPolicyRuleAdapter.firewallPoliciesClient = gcpClient

return firewallPolicyRuleAdapter, nil
}
Expand All @@ -113,10 +123,10 @@ func (a *firewallPolicyRuleAdapter) Find(ctx context.Context) (bool, error) {

firewallPolicyRule, err := a.get(ctx)
if err != nil {
// When a certain rule does not exist, the error has code 400(invalid) instead of 404(not found)
// When a certain rule does not exist, the error has code 400(bad request) instead of 404(not found)
// example error message:
// "Invalid value for field 'priority': '9000'. The firewall policy does not contain a rule at priority 9000.",
if direct.IsInvalidValue(err) {
if direct.IsBadRequest(err) {
return false, nil
}
return false, fmt.Errorf("getting ComputeFirewallPolicyRule %d: %w", a.priority, err)
Expand All @@ -126,7 +136,6 @@ func (a *firewallPolicyRuleAdapter) Find(ctx context.Context) (bool, error) {
}

func (a *firewallPolicyRuleAdapter) Create(ctx context.Context, createOp *directbase.CreateOperation) error {
u := createOp.GetUnstructured()
var err error

err = resolveDependencies(ctx, a.reader, a.desired)
Expand Down Expand Up @@ -169,15 +178,12 @@ func (a *firewallPolicyRuleAdapter) Create(ctx context.Context, createOp *direct
return fmt.Errorf("getting ComputeFirewallPolicyRule %d: %w", a.priority, err)
}

status := &krm.ComputeFirewallPolicyRuleStatus{
RuleTupleCount: direct.PtrTo(int64(*created.RuleTupleCount)),
Kind: direct.PtrTo("compute#firewallPolicyRule"),
}
return setStatus(u, status)
status := &krm.ComputeFirewallPolicyRuleStatus{}
status = ComputeFirewallPolicyRuleStatus_FromProto(mapCtx, created)
return createOp.UpdateStatus(ctx, status, nil)
}

func (a *firewallPolicyRuleAdapter) Update(ctx context.Context, updateOp *directbase.UpdateOperation) error {
u := updateOp.GetUnstructured()
var err error

err = resolveDependencies(ctx, a.reader, a.desired)
Expand All @@ -194,17 +200,18 @@ func (a *firewallPolicyRuleAdapter) Update(ctx context.Context, updateOp *direct
if mapCtx.Err() != nil {
return mapCtx.Err()
}
// The field priority should be removed from the patch request body and included as a query parameter.
// See API doc: https://cloud.google.com/compute/docs/reference/rest/v1/firewallPolicies/patchRule#query-parameters
firewallPolicyRule.Priority = nil

op := &gcp.Operation{}
updated := &computepb.FirewallPolicyRule{}

updateReq := &computepb.PatchRuleFirewallPolicyRequest{
FirewallPolicyRuleResource: firewallPolicyRule,
FirewallPolicy: a.firewallPolicy,
Priority: direct.PtrTo(int32(a.priority)),
}
op, err = a.firewallPoliciesClient.PatchRule(ctx, updateReq)
op, err := a.firewallPoliciesClient.PatchRule(ctx, updateReq)
if err != nil {
return fmt.Errorf("updating ComputeFirewallPolicyRule %d: %w", a.priority, err)
}
Expand All @@ -222,11 +229,9 @@ func (a *firewallPolicyRuleAdapter) Update(ctx context.Context, updateOp *direct
return fmt.Errorf("getting ComputeFirewallPolicyRule %d: %w", a.priority, err)
}

status := &krm.ComputeFirewallPolicyRuleStatus{
RuleTupleCount: direct.PtrTo(int64(*updated.RuleTupleCount)),
Kind: direct.PtrTo("compute#firewallPolicyRule"),
}
return setStatus(u, status)
status := &krm.ComputeFirewallPolicyRuleStatus{}
status = ComputeFirewallPolicyRuleStatus_FromProto(mapCtx, updated)
return updateOp.UpdateStatus(ctx, status, nil)
}

func (a *firewallPolicyRuleAdapter) Export(ctx context.Context) (*unstructured.Unstructured, error) {
Expand Down Expand Up @@ -290,21 +295,3 @@ func (a *firewallPolicyRuleAdapter) get(ctx context.Context) (*computepb.Firewal
}
return a.firewallPoliciesClient.GetRule(ctx, getReq)
}

func setStatus(u *unstructured.Unstructured, typedStatus any) error {
status, err := runtime.DefaultUnstructuredConverter.ToUnstructured(typedStatus)
if err != nil {
return fmt.Errorf("error converting status to unstructured: %w", err)
}

old, _, _ := unstructured.NestedMap(u.Object, "status")
if old != nil {
status["conditions"] = old["conditions"]
status["observedGeneration"] = old["observedGeneration"]
status["externalRef"] = old["externalRef"]
}

u.Object["status"] = status

return nil
}

This file was deleted.

1 change: 1 addition & 0 deletions pkg/controller/direct/compute/firewallpolicyrule/refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package firewallpolicyrule
import (
"context"
"fmt"

krm "github.com/GoogleCloudPlatform/k8s-config-connector/apis/compute/v1beta1"

refs "github.com/GoogleCloudPlatform/k8s-config-connector/apis/refs/v1beta1"
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/direct/maputils.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,8 @@ func IsNotFound(err error) bool {
return HasHTTPCode(err, 404)
}

// IsInvalidValue returns true if the given error is an HTTP 400.
func IsInvalidValue(err error) bool {
// IsBadRequest returns true if the given error is an HTTP 400.
func IsBadRequest(err error) bool {
return HasHTTPCode(err, 400)
}

Expand Down

0 comments on commit 7fa9f2d

Please sign in to comment.