Skip to content

Commit

Permalink
More logging for exists method
Browse files Browse the repository at this point in the history
  • Loading branch information
enxebre committed May 10, 2019
1 parent f4bf6b3 commit ad2a8ef
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
3 changes: 2 additions & 1 deletion pkg/cloud/gcp/actuators/machine/actuator.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func (a *Actuator) Create(ctx context.Context, cluster *clusterv1.Cluster, machi
}

func (a *Actuator) Exists(ctx context.Context, cluster *clusterv1.Cluster, machine *machinev1.Machine) (bool, error) {
klog.Infof("Checking if machine %v exists", machine.Name)
scope, err := newMachineScope(machineScopeParams{
machineClient: a.machineClient,
coreClient: a.coreClient,
Expand All @@ -58,7 +59,7 @@ func (a *Actuator) Exists(ctx context.Context, cluster *clusterv1.Cluster, machi
return false, fmt.Errorf("failed to create scope for machine %q: %v", machine.Name, err)
}
defer scope.Close()
return newReconciler(scope).instanceExists()
return newReconciler(scope).Exists()
}

func (a *Actuator) Update(ctx context.Context, cluster *clusterv1.Cluster, machine *machinev1.Machine) error {
Expand Down
8 changes: 5 additions & 3 deletions pkg/cloud/gcp/actuators/machine/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,25 +198,27 @@ func validateMachine(machine machinev1.Machine, providerSpec v1beta1.GCPMachineP
}

// Returns true if machine exists.
func (r *Reconciler) instanceExists() (bool, error) {
func (r *Reconciler) Exists() (bool, error) {
if err := validateMachine(*r.machine, *r.providerSpec); err != nil {
return false, fmt.Errorf("failed validating machine provider spec: %v", err)
}
zone := r.providerSpec.Zone
// Need to verify that our project/zone exists before checking machine, as
// invalid project/zone produces same 404 error as no machine.
if err := r.validateZone(); err != nil {
return false, fmt.Errorf("Unable to verify project/zone exists: %v/%v; err: %v", r.projectID, zone, err)
return false, fmt.Errorf("unable to verify project/zone exists: %v/%v; err: %v", r.projectID, zone, err)
}
_, err := r.computeService.InstancesGet(r.projectID, zone, r.machine.Name)
if err == nil {
klog.Infof("Machine %q already exists", r.machine.Name)
return true, nil
}
if isNotFoundError(err) {
klog.Infof("Machine %q does not exists", r.machine.Name)
return false, nil
}

return false, fmt.Errorf("Error getting running instances: %v", err)
return false, fmt.Errorf("error getting running instances: %v", err)

}

Expand Down
2 changes: 1 addition & 1 deletion pkg/cloud/gcp/actuators/machine/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func TestExists(t *testing.T) {
computeService: mockComputeService,
}
reconciler := newReconciler(&machineScope)
exists, err := reconciler.instanceExists()
exists, err := reconciler.Exists()
if err != nil || exists != true {
t.Errorf("reconciler was not expected to return error: %v", err)
}
Expand Down

0 comments on commit ad2a8ef

Please sign in to comment.