Skip to content

Commit

Permalink
GCE machine controller events (#387)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkjelland authored and k8s-ci-robot committed Jun 25, 2018
1 parent e407412 commit 3d9767c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const (
gceMachineControllerName = "gce-controller"
)

func StartMachineController(server *options.MachineControllerServer, shutdown <-chan struct{}) {
func StartMachineController(server *options.MachineControllerServer, recorder record.EventRecorder, shutdown <-chan struct{}) {
config, err := controller.GetConfig(server.CommonConfig.Kubeconfig)
if err != nil {
glog.Fatalf("Could not create Config for talking to the apiserver: %v", err)
Expand All @@ -63,6 +63,7 @@ func StartMachineController(server *options.MachineControllerServer, shutdown <-
params := google.MachineActuatorParams{
V1Alpha1Client: client.ClusterV1alpha1(),
MachineSetupConfigGetter: configWatch,
EventRecorder: recorder,
}
actuator, err := google.NewMachineActuator(params)

Expand All @@ -86,23 +87,23 @@ func RunMachineController(server *options.MachineControllerServer) error {
return err
}

kubeClientControl, err := kubernetes.NewForConfig(
clientSet, err := kubernetes.NewForConfig(
rest.AddUserAgent(kubeConfig, "machine-controller-manager"),
)
if err != nil {
glog.Errorf("Invalid API configuration for kubeconfig-control: %v", err)
return err
}

recorder, err := createRecorder(kubeClientControl)
recorder, err := createRecorder(clientSet)
if err != nil {
glog.Errorf("Could not create event recorder : %v", err)
return err
}

// run function will block and never return.
run := func(stop <-chan struct{}) {
StartMachineController(server, stop)
StartMachineController(server, recorder, stop)
}

leaderElectConfig := config.GetLeaderElectionConfig()
Expand Down Expand Up @@ -155,7 +156,7 @@ func createRecorder(kubeClient *kubernetes.Clientset) (record.EventRecorder, err
if err := corev1.AddToScheme(eventsScheme); err != nil {
return nil, err
}
// We also emit events for our own types
// We also emit events for our own types.
clusterapiclientsetscheme.AddToScheme(eventsScheme)

eventBroadcaster := record.NewBroadcaster()
Expand Down
45 changes: 31 additions & 14 deletions cloud/google/machineactuator.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import (
apierrors "sigs.k8s.io/cluster-api/pkg/errors"
"sigs.k8s.io/cluster-api/pkg/kubeadm"
"sigs.k8s.io/cluster-api/pkg/util"
"k8s.io/client-go/tools/record"
)

const (
Expand All @@ -65,6 +66,12 @@ const (
MachineSetupConfigsFilename = "machine_setup_configs.yaml"
)

const (
createEventAction = "Create"
deleteEventAction = "Delete"
noEventAction = ""
)

type SshCreds struct {
user string
privateKeyPath string
Expand All @@ -89,6 +96,7 @@ type GCEClient struct {
sshCreds SshCreds
v1Alpha1Client client.ClusterV1alpha1Interface
machineSetupConfigGetter GCEClientMachineSetupConfigGetter
eventRecorder record.EventRecorder
}

type MachineActuatorParams struct {
Expand All @@ -97,6 +105,7 @@ type MachineActuatorParams struct {
Kubeadm GCEClientKubeadm
V1Alpha1Client client.ClusterV1alpha1Interface
MachineSetupConfigGetter GCEClientMachineSetupConfigGetter
EventRecorder record.EventRecorder
}

func NewMachineActuator(params MachineActuatorParams) (*GCEClient, error) {
Expand Down Expand Up @@ -142,6 +151,7 @@ func NewMachineActuator(params MachineActuatorParams) (*GCEClient, error) {
},
v1Alpha1Client: params.V1Alpha1Client,
machineSetupConfigGetter: params.MachineSetupConfigGetter,
eventRecorder: params.EventRecorder,
}, nil
}

Expand Down Expand Up @@ -203,16 +213,16 @@ func (gce *GCEClient) Create(cluster *clusterv1.Cluster, machine *clusterv1.Mach
machineConfig, err := gce.machineproviderconfig(machine.Spec.ProviderConfig)
if err != nil {
return gce.handleMachineError(machine, apierrors.InvalidMachineConfiguration(
"Cannot unmarshal machine's providerConfig field: %v", err))
"Cannot unmarshal machine's providerConfig field: %v", err), createEventAction)
}
clusterConfig, err := gce.gceProviderConfigCodec.ClusterProviderFromProviderConfig(cluster.Spec.ProviderConfig)
if err != nil {
return gce.handleMachineError(machine, apierrors.InvalidMachineConfiguration(
"Cannot unmarshal cluster's providerConfig field: %v", err))
"Cannot unmarshal cluster's providerConfig field: %v", err), createEventAction)
}

if verr := gce.validateMachine(machine, machineConfig); verr != nil {
return gce.handleMachineError(machine, verr)
return gce.handleMachineError(machine, verr, createEventAction)
}

configParams := &machinesetup.ConfigParams{
Expand Down Expand Up @@ -288,9 +298,10 @@ func (gce *GCEClient) Create(cluster *clusterv1.Cluster, machine *clusterv1.Mach

if err != nil {
return gce.handleMachineError(machine, apierrors.CreateMachine(
"error creating GCE instance: %v", err))
"error creating GCE instance: %v", err), createEventAction)
}

gce.eventRecorder.Eventf(machine, corev1.EventTypeNormal, "Created", "Created Machine %v", machine.Name)
// If we have a v1Alpha1Client, then annotate the machine so that we
// remember exactly what VM we created for it.
if gce.v1Alpha1Client != nil {
Expand All @@ -317,17 +328,17 @@ func (gce *GCEClient) Delete(cluster *clusterv1.Cluster, machine *clusterv1.Mach
machineConfig, err := gce.machineproviderconfig(machine.Spec.ProviderConfig)
if err != nil {
return gce.handleMachineError(machine,
apierrors.InvalidMachineConfiguration("Cannot unmarshal machine's providerConfig field: %v", err))
apierrors.InvalidMachineConfiguration("Cannot unmarshal machine's providerConfig field: %v", err), deleteEventAction)
}

clusterConfig, err := gce.gceProviderConfigCodec.ClusterProviderFromProviderConfig(cluster.Spec.ProviderConfig)
if err != nil {
return gce.handleMachineError(machine,
apierrors.InvalidMachineConfiguration("Cannot unmarshal cluster's providerConfig field: %v", err))
apierrors.InvalidMachineConfiguration("Cannot unmarshal cluster's providerConfig field: %v", err), deleteEventAction)
}

if verr := gce.validateMachine(machine, machineConfig); verr != nil {
return gce.handleMachineError(machine, verr)
return gce.handleMachineError(machine, verr, deleteEventAction)
}

var project, zone, name string
Expand All @@ -351,9 +362,11 @@ func (gce *GCEClient) Delete(cluster *clusterv1.Cluster, machine *clusterv1.Mach
}
if err != nil {
return gce.handleMachineError(machine, apierrors.DeleteMachine(
"error deleting GCE instance: %v", err))
"error deleting GCE instance: %v", err), deleteEventAction)
}

gce.eventRecorder.Eventf(machine, corev1.EventTypeNormal, "Deleted", "Deleted Machine %v", name)

return err
}

Expand Down Expand Up @@ -401,10 +414,10 @@ func (gce *GCEClient) Update(cluster *clusterv1.Cluster, goalMachine *clusterv1.
config, err := gce.machineproviderconfig(goalMachine.Spec.ProviderConfig)
if err != nil {
return gce.handleMachineError(goalMachine,
apierrors.InvalidMachineConfiguration("Cannot unmarshal machine's providerConfig field: %v", err))
apierrors.InvalidMachineConfiguration("Cannot unmarshal machine's providerConfig field: %v", err), noEventAction)
}
if verr := gce.validateMachine(goalMachine, config); verr != nil {
return gce.handleMachineError(goalMachine, verr)
return gce.handleMachineError(goalMachine, verr, noEventAction)
}

status, err := gce.instanceStatus(goalMachine)
Expand Down Expand Up @@ -516,14 +529,14 @@ func (gce *GCEClient) updateAnnotations(cluster *clusterv1.Cluster, machine *clu
zone := machineConfig.Zone
if err != nil {
return gce.handleMachineError(machine,
apierrors.InvalidMachineConfiguration("Cannot unmarshal machine's providerConfig field: %v", err))
apierrors.InvalidMachineConfiguration("Cannot unmarshal machine's providerConfig field: %v", err), noEventAction)
}

clusterConfig, err := gce.gceProviderConfigCodec.ClusterProviderFromProviderConfig(cluster.Spec.ProviderConfig)
project := clusterConfig.Project
if err != nil {
return gce.handleMachineError(machine,
apierrors.InvalidMachineConfiguration("Cannot unmarshal cluster's providerConfig field: %v", err))
apierrors.InvalidMachineConfiguration("Cannot unmarshal cluster's providerConfig field: %v", err), noEventAction)
}

if machine.ObjectMeta.Annotations == nil {
Expand Down Expand Up @@ -651,7 +664,7 @@ func (gce *GCEClient) validateMachine(machine *clusterv1.Machine, config *gcecon
// the appropriate reason/message on the Machine.Status. If not, such as during
// cluster installation, it will operate as a no-op. It also returns the
// original error for convenience, so callers can do "return handleMachineError(...)".
func (gce *GCEClient) handleMachineError(machine *clusterv1.Machine, err *apierrors.MachineError) error {
func (gce *GCEClient) handleMachineError(machine *clusterv1.Machine, err *apierrors.MachineError, eventAction string) error {
if gce.v1Alpha1Client != nil {
reason := err.Reason
message := err.Message
Expand All @@ -660,6 +673,10 @@ func (gce *GCEClient) handleMachineError(machine *clusterv1.Machine, err *apierr
gce.v1Alpha1Client.Machines(machine.Namespace).UpdateStatus(machine)
}

if eventAction != noEventAction {
gce.eventRecorder.Eventf(machine, corev1.EventTypeWarning, "Failed"+eventAction, "%v", err.Reason)
}

glog.Errorf("Machine error: %v", err.Message)
return err
}
Expand Down Expand Up @@ -773,7 +790,7 @@ func (gce *GCEClient) getMetadata(cluster *clusterv1.Cluster, machine *clusterv1
if util.IsMaster(machine) {
if machine.Spec.Versions.ControlPlane == "" {
return nil, gce.handleMachineError(machine, apierrors.InvalidMachineConfiguration(
"invalid master configuration: missing Machine.Spec.Versions.ControlPlane"))
"invalid master configuration: missing Machine.Spec.Versions.ControlPlane"), createEventAction)
}
var err error
metadataMap, err = masterMetadata(cluster, machine, clusterConfig.Project, &machineSetupMetadata)
Expand Down
2 changes: 2 additions & 0 deletions cloud/google/machineactuator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"sigs.k8s.io/cluster-api/pkg/test-cmd-runner"
"strings"
"testing"
"k8s.io/client-go/tools/record"
)

func init() {
Expand Down Expand Up @@ -256,6 +257,7 @@ func createCluster(t *testing.T, machine *v1alpha1.Machine, computeServiceMock *
ComputeService: computeServiceMock,
Kubeadm: kubeadm,
MachineSetupConfigGetter: configWatch,
EventRecorder: &record.FakeRecorder{},
}
gce, err := google.NewMachineActuator(params)
if err != nil {
Expand Down

0 comments on commit 3d9767c

Please sign in to comment.