Skip to content

Commit

Permalink
rebase and simplify controller code
Browse files Browse the repository at this point in the history
  • Loading branch information
gemmahou committed Sep 18, 2024
1 parent 7b0c022 commit db22625
Showing 1 changed file with 49 additions and 98 deletions.
147 changes: 49 additions & 98 deletions pkg/controller/direct/compute/forwardingrule_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,31 +261,12 @@ func (a *forwardingRuleAdapter) Find(ctx context.Context) (bool, error) {

var err error
forwardingRule := &computepb.ForwardingRule{}
if a.id.location == "global" {
req := &computepb.GetGlobalForwardingRuleRequest{
ForwardingRule: a.id.forwardingRule,
Project: a.id.project,
}
forwardingRule, err = a.globalForwardingRulesClient.Get(ctx, req)
if err != nil {
if direct.IsNotFound(err) {
return false, nil
}
return false, fmt.Errorf("getting ComputeForwardingRule %q: %w", a.fullyQualifiedName(), err)
}
} else {
req := &computepb.GetForwardingRuleRequest{
ForwardingRule: a.id.forwardingRule,
Region: a.id.location,
Project: a.id.project,
}
forwardingRule, err = a.forwardingRulesClient.Get(ctx, req)
if err != nil {
if direct.IsNotFound(err) {
return false, nil
}
return false, fmt.Errorf("getting ComputeForwardingRule %q: %w", a.fullyQualifiedName(), err)
forwardingRule, err = a.get(ctx)
if err != nil {
if direct.IsNotFound(err) {
return false, nil
}
return false, fmt.Errorf("getting ComputeForwardingRule %q: %w", a.fullyQualifiedName(), err)
}
a.actual = forwardingRule
return true, nil
Expand Down Expand Up @@ -344,42 +325,14 @@ func (a *forwardingRuleAdapter) Create(ctx context.Context, createOp *directbase

// Get the created resource
created := &computepb.ForwardingRule{}
if a.id.location == "global" {
getReq := &computepb.GetGlobalForwardingRuleRequest{
ForwardingRule: a.id.forwardingRule,
Project: a.id.project,
}
created, err = a.globalForwardingRulesClient.Get(ctx, getReq)
} else {
getReq := &computepb.GetForwardingRuleRequest{
ForwardingRule: a.id.forwardingRule,
Region: a.id.location,
Project: a.id.project,
}
created, err = a.forwardingRulesClient.Get(ctx, getReq)
}
created, err = a.get(ctx)
if err != nil {
return fmt.Errorf("getting ComputeForwardingRule %q: %w", a.fullyQualifiedName(), err)
}

// Set labels for the created forwarding rule
if forwardingRule.Labels != nil {
if a.id.location == "global" {
setLabelsReq := &computepb.SetLabelsGlobalForwardingRuleRequest{
Resource: a.id.forwardingRule,
GlobalSetLabelsRequestResource: &computepb.GlobalSetLabelsRequest{LabelFingerprint: created.LabelFingerprint, Labels: forwardingRule.Labels},
Project: a.id.project,
}
op, err = a.globalForwardingRulesClient.SetLabels(ctx, setLabelsReq)
} else {
setLabelsReq := &computepb.SetLabelsForwardingRuleRequest{
Resource: a.id.forwardingRule,
RegionSetLabelsRequestResource: &computepb.RegionSetLabelsRequest{LabelFingerprint: created.LabelFingerprint, Labels: forwardingRule.Labels},
Project: a.id.project,
Region: a.id.location,
}
op, err = a.forwardingRulesClient.SetLabels(ctx, setLabelsReq)
}
op, err := a.setLabels(ctx, created.LabelFingerprint, forwardingRule.Labels)
if err != nil {
return fmt.Errorf("adding ComputeForwardingRule labels %s: %w", a.fullyQualifiedName(), err)
}
Expand All @@ -392,20 +345,7 @@ func (a *forwardingRuleAdapter) Create(ctx context.Context, createOp *directbase
log.V(2).Info("successfully added ComputeForwardingRule labels", "name", a.fullyQualifiedName())

// Get the created resource with label added
if a.id.location == "global" {
getReq := &computepb.GetGlobalForwardingRuleRequest{
ForwardingRule: a.id.forwardingRule,
Project: a.id.project,
}
created, err = a.globalForwardingRulesClient.Get(ctx, getReq)
} else {
getReq := &computepb.GetForwardingRuleRequest{
ForwardingRule: a.id.forwardingRule,
Region: a.id.location,
Project: a.id.project,
}
created, err = a.forwardingRulesClient.Get(ctx, getReq)
}
created, err = a.get(ctx)
if err != nil {
return fmt.Errorf("getting ComputeForwardingRule %q: %w", a.fullyQualifiedName(), err)
}
Expand Down Expand Up @@ -445,22 +385,7 @@ func (a *forwardingRuleAdapter) Update(ctx context.Context, updateOp *directbase
op := &gcp.Operation{}
updated := &computepb.ForwardingRule{}
if !reflect.DeepEqual(forwardingRule.Labels, a.actual.Labels) {
if a.id.location == "global" {
setLabelsReq := &computepb.SetLabelsGlobalForwardingRuleRequest{
Resource: a.id.forwardingRule,
GlobalSetLabelsRequestResource: &computepb.GlobalSetLabelsRequest{LabelFingerprint: a.actual.LabelFingerprint, Labels: forwardingRule.Labels},
Project: a.id.project,
}
op, err = a.globalForwardingRulesClient.SetLabels(ctx, setLabelsReq)
} else {
setLabelsReq := &computepb.SetLabelsForwardingRuleRequest{
Resource: a.id.forwardingRule,
RegionSetLabelsRequestResource: &computepb.RegionSetLabelsRequest{LabelFingerprint: a.actual.LabelFingerprint, Labels: forwardingRule.Labels},
Project: a.id.project,
Region: a.id.location,
}
op, err = a.forwardingRulesClient.SetLabels(ctx, setLabelsReq)
}
op, err := a.setLabels(ctx, a.actual.LabelFingerprint, forwardingRule.Labels)
if err != nil {
return fmt.Errorf("updating ComputeForwardingRule labels %s: %w", a.fullyQualifiedName(), err)
}
Expand Down Expand Up @@ -503,20 +428,7 @@ func (a *forwardingRuleAdapter) Update(ctx context.Context, updateOp *directbase
log.V(2).Info("successfully updated ComputeForwardingRule target", "name", a.fullyQualifiedName())
}
// Get the updated resource
if a.id.location == "global" {
getReq := &computepb.GetGlobalForwardingRuleRequest{
ForwardingRule: a.id.forwardingRule,
Project: a.id.project,
}
updated, err = a.globalForwardingRulesClient.Get(ctx, getReq)
} else {
getReq := &computepb.GetForwardingRuleRequest{
ForwardingRule: a.id.forwardingRule,
Region: a.id.location,
Project: a.id.project,
}
updated, err = a.forwardingRulesClient.Get(ctx, getReq)
}
updated, err = a.get(ctx)
if err != nil {
return fmt.Errorf("getting ComputeForwardingRule %q: %w", a.id.forwardingRule, err)
}
Expand Down Expand Up @@ -593,6 +505,45 @@ func (a *forwardingRuleAdapter) Delete(ctx context.Context, deleteOp *directbase
return true, nil
}

func (a *forwardingRuleAdapter) get(ctx context.Context) (*computepb.ForwardingRule, error) {
if a.id.location == "global" {
getReq := &computepb.GetGlobalForwardingRuleRequest{
ForwardingRule: a.id.forwardingRule,
Project: a.id.project,
}
return a.globalForwardingRulesClient.Get(ctx, getReq)
} else {
getReq := &computepb.GetForwardingRuleRequest{
ForwardingRule: a.id.forwardingRule,
Region: a.id.location,
Project: a.id.project,
}
return a.forwardingRulesClient.Get(ctx, getReq)
}
}

func (a *forwardingRuleAdapter) setLabels(ctx context.Context, fingerprint *string, labels map[string]string) (*gcp.Operation, error) {
op := &gcp.Operation{}
var err error
if a.id.location == "global" {
setLabelsReq := &computepb.SetLabelsGlobalForwardingRuleRequest{
Resource: a.id.forwardingRule,
GlobalSetLabelsRequestResource: &computepb.GlobalSetLabelsRequest{LabelFingerprint: fingerprint, Labels: labels},
Project: a.id.project,
}
op, err = a.globalForwardingRulesClient.SetLabels(ctx, setLabelsReq)
} else {
setLabelsReq := &computepb.SetLabelsForwardingRuleRequest{
Resource: a.id.forwardingRule,
RegionSetLabelsRequestResource: &computepb.RegionSetLabelsRequest{LabelFingerprint: fingerprint, Labels: labels},
Project: a.id.project,
Region: a.id.location,
}
op, err = a.forwardingRulesClient.SetLabels(ctx, setLabelsReq)
}
return op, err
}

func (a *forwardingRuleAdapter) fullyQualifiedName() string {
if a.id.location == "global" {
return fmt.Sprintf("projects/%s/global/forwardingRules/%s", a.id.project, a.id.forwardingRule)
Expand Down

0 comments on commit db22625

Please sign in to comment.