Skip to content

Commit

Permalink
Implement events
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelgugino committed Jun 19, 2019
1 parent 12d1607 commit 864807b
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 7 deletions.
1 change: 1 addition & 0 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func main() {
machineActuator := machine.NewActuator(machine.ActuatorParams{
MachineClient: cs.MachineV1beta1(),
CoreClient: mgr.GetClient(),
EventRecorder: mgr.GetEventRecorderFor("gcpcontroller"),
})

if err := apis.AddToScheme(mgr.GetScheme()); err != nil {
Expand Down
50 changes: 43 additions & 7 deletions pkg/cloud/gcp/actuators/machine/actuator.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,55 @@ import (
clusterv1 "github.com/openshift/cluster-api/pkg/apis/cluster/v1alpha1"
machinev1 "github.com/openshift/cluster-api/pkg/apis/machine/v1beta1"
mapiclient "github.com/openshift/cluster-api/pkg/client/clientset_generated/clientset/typed/machine/v1beta1"
apierrors "github.com/openshift/cluster-api/pkg/errors"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/tools/record"
"k8s.io/klog"
controllerclient "sigs.k8s.io/controller-runtime/pkg/client"
)

const (
scopeFailFmt = "failed to create scope for machine %q: %v"
scopeFailFmt = "failed to create scope for machine %q: %v"
createEventAction = "Create"
updateEventAction = "Update"
deleteEventAction = "Delete"
noEventAction = ""
)

// Actuator is responsible for performing machine reconciliation.
type Actuator struct {
machineClient mapiclient.MachineV1beta1Interface
coreClient controllerclient.Client
eventRecorder record.EventRecorder
}

// ActuatorParams holds parameter information for Actuator.
type ActuatorParams struct {
MachineClient mapiclient.MachineV1beta1Interface
CoreClient controllerclient.Client
EventRecorder record.EventRecorder
}

// NewActuator returns an actuator.
func NewActuator(params ActuatorParams) *Actuator {
return &Actuator{
machineClient: params.MachineClient,
coreClient: params.CoreClient,
eventRecorder: params.EventRecorder,
}
}

// Set corresponding event based on error. It also returns the original error
// for convenience, so callers can do "return handleMachineError(...)".
func (a *Actuator) handleMachineError(machine *machinev1.Machine, err *apierrors.MachineError, eventAction string) error {
if eventAction != noEventAction {
a.eventRecorder.Eventf(machine, corev1.EventTypeWarning, "Failed"+eventAction, "%v", err.Reason)
}

klog.Errorf("Machine error: %v", err.Message)
return err
}

// Create creates a machine and is invoked by the machine controller.
func (a *Actuator) Create(ctx context.Context, cluster *clusterv1.Cluster, machine *machinev1.Machine) error {
klog.Infof("Creating machine %q", machine.Name)
Expand All @@ -47,10 +68,15 @@ func (a *Actuator) Create(ctx context.Context, cluster *clusterv1.Cluster, machi
machine: machine,
})
if err != nil {
return fmt.Errorf(scopeFailFmt, machine.Name, err)
fmtErr := fmt.Sprintf(scopeFailFmt, machine.Name, err)
return a.handleMachineError(machine, apierrors.CreateMachine(fmtErr), createEventAction)
}
defer scope.Close()
return newReconciler(scope).create()
if err := newReconciler(scope).create(); err != nil {
return a.handleMachineError(machine, apierrors.CreateMachine(err.Error()), createEventAction)
}
a.eventRecorder.Eventf(machine, corev1.EventTypeNormal, createEventAction, "Created Machine %v", machine.Name)
return nil
}

func (a *Actuator) Exists(ctx context.Context, cluster *clusterv1.Cluster, machine *machinev1.Machine) (bool, error) {
Expand Down Expand Up @@ -79,10 +105,15 @@ func (a *Actuator) Update(ctx context.Context, cluster *clusterv1.Cluster, machi
machine: machine,
})
if err != nil {
return fmt.Errorf("failed to create scope for machine %q: %v", machine.Name, err)
fmtErr := fmt.Sprintf(scopeFailFmt, machine.Name, err)
return a.handleMachineError(machine, apierrors.UpdateMachine(fmtErr), updateEventAction)
}
defer scope.Close()
return newReconciler(scope).update()
if err := newReconciler(scope).update(); err != nil {
return a.handleMachineError(machine, apierrors.UpdateMachine(err.Error()), updateEventAction)
}
a.eventRecorder.Eventf(machine, corev1.EventTypeNormal, updateEventAction, "Updated Machine %v", machine.Name)
return nil
}

func (a *Actuator) Delete(ctx context.Context, cluster *clusterv1.Cluster, machine *machinev1.Machine) error {
Expand All @@ -93,7 +124,12 @@ func (a *Actuator) Delete(ctx context.Context, cluster *clusterv1.Cluster, machi
machine: machine,
})
if err != nil {
return fmt.Errorf(scopeFailFmt, machine.Name, err)
fmtErr := fmt.Sprintf(scopeFailFmt, machine.Name, err)
return a.handleMachineError(machine, apierrors.DeleteMachine(fmtErr), deleteEventAction)
}
if err := newReconciler(scope).delete(); err != nil {
return a.handleMachineError(machine, apierrors.DeleteMachine(err.Error()), deleteEventAction)
}
return newReconciler(scope).delete()
a.eventRecorder.Eventf(machine, corev1.EventTypeNormal, deleteEventAction, "Deleted machine %v", machine.Name)
return nil
}
33 changes: 33 additions & 0 deletions pkg/cloud/gcp/actuators/machine/actuator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package machine

import (
"testing"

machinev1 "github.com/openshift/cluster-api/pkg/apis/machine/v1beta1"
capifake "github.com/openshift/cluster-api/pkg/client/clientset_generated/clientset/fake"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

func init() {
// Add types to scheme
machinev1.AddToScheme(scheme.Scheme)
}

func TestActuatorCreate(t *testing.T) {
eventsChannel := make(chan string, 1)
recorder := &record.FakeRecorder{
Events: eventsChannel,
}
cs := capifake.NewSimpleClientset()
// Initialize machine actuator.
machineActuator := NewActuator(ActuatorParams{
MachineClient: cs.MachineV1beta1(),
CoreClient: fake.NewFakeClient(),
EventRecorder: recorder,
})
if machineActuator == nil {
t.Errorf("expected machine not nil")
}
}

0 comments on commit 864807b

Please sign in to comment.