Skip to content

Commit

Permalink
Add events recorder framework
Browse files Browse the repository at this point in the history
Signed-off-by: Prajyot-Parab <[email protected]>
  • Loading branch information
Prajyot-Parab committed Apr 13, 2022
1 parent 232858b commit 9851c9a
Show file tree
Hide file tree
Showing 13 changed files with 208 additions and 29 deletions.
29 changes: 29 additions & 0 deletions cloud/scope/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

infrav1beta1 "sigs.k8s.io/cluster-api-provider-ibmcloud/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/record"
)

// ClusterScopeParams defines the input parameters used to create a new ClusterScope.
Expand Down Expand Up @@ -102,10 +103,12 @@ func (s *ClusterScope) CreateVPC() (*vpcv1.VPC, error) {
options.SetName(s.IBMVPCCluster.Spec.VPC)
vpc, _, err := s.IBMVPCClients.VPCService.CreateVPC(options)
if err != nil {
record.Warnf(s.IBMVPCCluster, "FailedCreate", "Failed vpc creation - %v", err)
return nil, err
} else if err := s.updateDefaultSG(*vpc.DefaultSecurityGroup.ID); err != nil {
return nil, err
}
record.Eventf(s.IBMVPCCluster, "SuccessfulCreate", "Created VPC %q", *vpc.Name)
return vpc, nil
}

Expand All @@ -114,6 +117,11 @@ func (s *ClusterScope) DeleteVPC() error {
deleteVpcOptions := &vpcv1.DeleteVPCOptions{}
deleteVpcOptions.SetID(s.IBMVPCCluster.Status.VPC.ID)
_, err := s.IBMVPCClients.VPCService.DeleteVPC(deleteVpcOptions)
if err != nil {
record.Warnf(s.IBMVPCCluster, "FailedDelete", "Failed vpc deletion - %v", err)
} else {
record.Eventf(s.IBMVPCCluster, "SuccessfulDelete", "Deleted VPC %q", s.IBMVPCCluster.Status.VPC.Name)
}

return err
}
Expand Down Expand Up @@ -141,6 +149,9 @@ func (s *ClusterScope) updateDefaultSG(sgID string) error {
IPVersion: core.StringPtr("ipv4"),
})
_, _, err := s.IBMVPCClients.VPCService.CreateSecurityGroupRule(options)
if err != nil {
record.Warnf(s.IBMVPCCluster, "FailedCreate", "Failed security group rule creation - %v", err)
}
return err
}

Expand All @@ -167,6 +178,9 @@ func (s *ClusterScope) ReserveFIP() (*vpcv1.FloatingIP, error) {
})

floatingIP, _, err := s.IBMVPCClients.VPCService.CreateFloatingIP(options)
if err != nil {
record.Warnf(s.IBMVPCCluster, "FailedCreate", "Failed floatingIP creation - %v", err)
}
return floatingIP, err
}

Expand All @@ -190,6 +204,9 @@ func (s *ClusterScope) DeleteFloatingIP() error {
deleteFIPOption := &vpcv1.DeleteFloatingIPOptions{}
deleteFIPOption.SetID(fipID)
_, err := s.IBMVPCClients.VPCService.DeleteFloatingIP(deleteFIPOption)
if err != nil {
record.Warnf(s.IBMVPCCluster, "FailedDelete", "Failed floatingIP deletion - %v", err)
}
return err
}
return nil
Expand Down Expand Up @@ -226,6 +243,9 @@ func (s *ClusterScope) CreateSubnet() (*vpcv1.Subnet, error) {
},
})
subnet, _, err := s.IBMVPCClients.VPCService.CreateSubnet(options)
if err != nil {
record.Warnf(s.IBMVPCCluster, "FailedCreate", "Failed subnet creation - %v", err)
}
if subnet != nil {
pgw, err := s.createPublicGateWay(s.IBMVPCCluster.Status.VPC.ID, s.IBMVPCCluster.Spec.Zone, s.IBMVPCCluster.Spec.ResourceGroup)
if err != nil {
Expand Down Expand Up @@ -293,6 +313,7 @@ func (s *ClusterScope) DeleteSubnet() error {
deleteSubnetOption.SetID(subnetID)
_, err = s.IBMVPCClients.VPCService.DeleteSubnet(deleteSubnetOption)
if err != nil {
record.Warnf(s.IBMVPCCluster, "FailedDelete", "Failed subnet deletion - %v", err)
return errors.Wrap(err, "Error when deleting subnet ")
}
return err
Expand All @@ -310,6 +331,9 @@ func (s *ClusterScope) createPublicGateWay(vpcID string, zoneName string, resour
ID: &resourceGroupID,
})
publicGateway, _, err := s.IBMVPCClients.VPCService.CreatePublicGateway(options)
if err != nil {
record.Warnf(s.IBMVPCCluster, "FailedCreate", "Failed publicgateway creation - %v", err)
}
return publicGateway, err
}

Expand All @@ -320,6 +344,9 @@ func (s *ClusterScope) attachPublicGateWay(subnetID string, pgwID string) (*vpcv
ID: &pgwID,
})
publicGateway, _, err := s.IBMVPCClients.VPCService.SetSubnetPublicGateway(options)
if err != nil {
record.Warnf(s.IBMVPCCluster, "FailedAttach", "Failed publicgateway attachment - %v", err)
}
return publicGateway, err
}

Expand All @@ -329,6 +356,7 @@ func (s *ClusterScope) detachPublicGateway(subnetID string, pgwID string) error
unsetPGWOption.SetID(subnetID)
_, err := s.IBMVPCClients.VPCService.UnsetSubnetPublicGateway(unsetPGWOption)
if err != nil {
record.Warnf(s.IBMVPCCluster, "FailedDetach", "Failed publicgateway detachment - %v", err)
return errors.Wrap(err, "Error when unsetting publicgateway for subnet "+subnetID)
}

Expand All @@ -337,6 +365,7 @@ func (s *ClusterScope) detachPublicGateway(subnetID string, pgwID string) error
deletePGWOption.SetID(pgwID)
_, err = s.IBMVPCClients.VPCService.DeletePublicGateway(deletePGWOption)
if err != nil {
record.Warnf(s.IBMVPCCluster, "FailedDelete", "Failed publicgateway deletion - %v", err)
return errors.Wrap(err, "Error when deleting publicgateway for subnet "+subnetID)
}
return err
Expand Down
11 changes: 11 additions & 0 deletions cloud/scope/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

infrav1beta1 "sigs.k8s.io/cluster-api-provider-ibmcloud/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/record"
)

// MachineScopeParams defines the input parameters used to create a new MachineScope.
Expand Down Expand Up @@ -143,6 +144,11 @@ func (m *MachineScope) CreateMachine() (*vpcv1.Instance, error) {

options.SetInstancePrototype(instancePrototype)
instance, response, err := m.IBMVPCClients.VPCService.CreateInstance(options)
if err != nil {
record.Warnf(m.IBMVPCMachine, "FailedCreate", "Failed instance creation - %v", err)
} else {
record.Eventf(m.IBMVPCMachine, "SuccessfulCreate", "Created Instance %q", *instance.Name)
}
fmt.Printf("%v\n", response)
return instance, err
}
Expand All @@ -152,6 +158,11 @@ func (m *MachineScope) DeleteMachine() error {
options := &vpcv1.DeleteInstanceOptions{}
options.SetID(m.IBMVPCMachine.Status.InstanceID)
_, err := m.IBMVPCClients.VPCService.DeleteInstance(options)
if err != nil {
record.Warnf(m.IBMVPCMachine, "FailedDelete", "Failed instance deletion - %v", err)
} else {
record.Eventf(m.IBMVPCMachine, "SuccessfulDelete", "Deleted Instance %q", m.IBMVPCMachine.Name)
}
return err
}

Expand Down
18 changes: 16 additions & 2 deletions cloud/scope/powervs_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/powervs"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/resourcecontroller"
servicesutils "sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/utils"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/record"
)

// BucketAccess indicates if the bucket has public or private access public access.
Expand Down Expand Up @@ -163,6 +164,7 @@ func (i *PowerVSImageScope) CreateImageCOSBucket() (*models.ImageReference, *mod
return nil, nil, err
} else if imageReply != nil {
i.Info("Image already exists")
record.Eventf(i.IBMPowerVSImage, "SuccessfulRetrive", "Retrieved Image %q", *imageReply.Name)
return imageReply, nil, nil
}

Expand All @@ -185,9 +187,11 @@ func (i *PowerVSImageScope) CreateImageCOSBucket() (*models.ImageReference, *mod
jobRef, err := i.IBMPowerVSClient.CreateCosImage(body)
if err != nil {
i.Info("Unable to create new import job request")
record.Warnf(i.IBMPowerVSImage, "FailedCreate", "Failed image import job creation - %v", err)
return nil, nil, err
}
i.Info("New import job request created")
record.Eventf(i.IBMPowerVSImage, "SuccessfulCreate", "Created image import job %q", *jobRef.ID)
return nil, jobRef, nil
}

Expand All @@ -203,7 +207,12 @@ func (i *PowerVSImageScope) Close() error {

// DeleteImage will delete the image.
func (i *PowerVSImageScope) DeleteImage() error {
return i.IBMPowerVSClient.DeleteImage(i.IBMPowerVSImage.Status.ImageID)
if err := i.IBMPowerVSClient.DeleteImage(i.IBMPowerVSImage.Status.ImageID); err != nil {
record.Warnf(i.IBMPowerVSImage, "FailedDelete", "Failed image deletion - %v", err)
return err
}
record.Eventf(i.IBMPowerVSImage, "SuccessfulDelete", "Deleted Image %q", i.IBMPowerVSImage.Status.ImageID)
return nil
}

// GetImportJob will get the image import job.
Expand All @@ -213,7 +222,12 @@ func (i *PowerVSImageScope) GetImportJob() (*models.Job, error) {

// DeleteImportJob will delete the image import job.
func (i *PowerVSImageScope) DeleteImportJob() error {
return i.IBMPowerVSClient.DeleteJob(i.IBMPowerVSImage.Status.JobID)
if err := i.IBMPowerVSClient.DeleteJob(i.IBMPowerVSImage.Status.JobID); err != nil {
record.Warnf(i.IBMPowerVSImage, "FailedDelete", "Failed image import job deletion - %v", err)
return err
}
record.Eventf(i.IBMPowerVSImage, "SuccessfulDelete", "Deleted image import job %q", i.IBMPowerVSImage.Status.JobID)
return nil
}

// SetReady will set the status as ready for the image.
Expand Down
12 changes: 11 additions & 1 deletion cloud/scope/powervs_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import (
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/resourcecontroller"
servicesutils "sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/utils"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/options"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/record"
)

// PowerVSMachineScopeParams defines the input parameters used to create a new PowerVSMachineScope.
Expand Down Expand Up @@ -213,12 +214,14 @@ func (m *PowerVSMachineScope) CreateMachine() (*models.PVMInstanceReference, err
} else {
imageID, err = getImageID(s.Image, m)
if err != nil {
record.Warnf(m.IBMPowerVSMachine, "FailedRetrive", "Failed image retrival - %v", err)
return nil, fmt.Errorf("error getting image ID: %v", err)
}
}

networkID, err := getNetworkID(s.Network, m)
if err != nil {
record.Warnf(m.IBMPowerVSMachine, "FailedRetrive", "Failed network retrival - %v", err)
return nil, fmt.Errorf("error getting network ID: %v", err)
}

Expand All @@ -242,8 +245,10 @@ func (m *PowerVSMachineScope) CreateMachine() (*models.PVMInstanceReference, err
}
_, err = m.IBMPowerVSClient.CreateInstance(params.Body)
if err != nil {
record.Warnf(m.IBMPowerVSMachine, "FailedCreate", "Failed instance creation - %v", err)
return nil, err
}
record.Eventf(m.IBMPowerVSMachine, "SuccessfulCreate", "Created Instance %q", m.IBMPowerVSMachine.Name)
return nil, nil
}

Expand All @@ -259,7 +264,12 @@ func (m *PowerVSMachineScope) PatchObject() error {

// DeleteMachine deletes the power vs machine associated with machine instance id and service instance id.
func (m *PowerVSMachineScope) DeleteMachine() error {
return m.IBMPowerVSClient.DeleteInstance(m.IBMPowerVSMachine.Status.InstanceID)
if err := m.IBMPowerVSClient.DeleteInstance(m.IBMPowerVSMachine.Status.InstanceID); err != nil {
record.Warnf(m.IBMPowerVSMachine, "FailedDelete", "Failed instance deletion - %v", err)
return err
}
record.Eventf(m.IBMPowerVSMachine, "SuccessfulDelete", "Deleted Instance %q", m.IBMPowerVSMachine.Name)
return nil
}

// GetBootstrapData returns the base64 encoded bootstrap data from the secret in the Machine's bootstrap.dataSecretName.
Expand Down
6 changes: 4 additions & 2 deletions controllers/ibmpowervscluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
kerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/client-go/tools/record"
capiv1beta1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/util"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -41,8 +42,9 @@ import (
// IBMPowerVSClusterReconciler reconciles a IBMPowerVSCluster object.
type IBMPowerVSClusterReconciler struct {
client.Client
Log logr.Logger
Scheme *runtime.Scheme
Log logr.Logger
Recorder record.EventRecorder
Scheme *runtime.Scheme
}

// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=ibmpowervsclusters,verbs=get;list;watch;create;update;patch;delete
Expand Down
6 changes: 4 additions & 2 deletions controllers/ibmpowervsimage_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
capiv1beta1 "sigs.k8s.io/cluster-api/api/v1beta1"
clusterv1util "sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/conditions"
Expand All @@ -43,8 +44,9 @@ import (
// IBMPowerVSImageReconciler reconciles a IBMPowerVSImage object.
type IBMPowerVSImageReconciler struct {
client.Client
Log logr.Logger
Scheme *runtime.Scheme
Log logr.Logger
Recorder record.EventRecorder
Scheme *runtime.Scheme
}

//+kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=ibmpowervsimages,verbs=get;list;watch;create;update;patch;delete
Expand Down
6 changes: 4 additions & 2 deletions controllers/ibmpowervsmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/pkg/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
capiv1beta1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/conditions"
Expand All @@ -39,8 +40,9 @@ import (
// IBMPowerVSMachineReconciler reconciles a IBMPowerVSMachine object.
type IBMPowerVSMachineReconciler struct {
client.Client
Log logr.Logger
Scheme *runtime.Scheme
Log logr.Logger
Recorder record.EventRecorder
Scheme *runtime.Scheme
}

// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=ibmpowervsmachines,verbs=get;list;watch;create;update;patch;delete
Expand Down
6 changes: 4 additions & 2 deletions controllers/ibmvpccluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/pkg/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
capiv1beta1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/predicates"
Expand All @@ -40,8 +41,9 @@ import (
// IBMVPCClusterReconciler reconciles a IBMVPCCluster object.
type IBMVPCClusterReconciler struct {
client.Client
Log logr.Logger
Scheme *runtime.Scheme
Log logr.Logger
Recorder record.EventRecorder
Scheme *runtime.Scheme
}

// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=ibmvpcclusters,verbs=get;list;watch;create;update;patch;delete
Expand Down
6 changes: 4 additions & 2 deletions controllers/ibmvpcmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
"k8s.io/utils/pointer"
capiv1beta1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/util"
Expand All @@ -41,8 +42,9 @@ import (
// IBMVPCMachineReconciler reconciles a IBMVPCMachine object.
type IBMVPCMachineReconciler struct {
client.Client
Log logr.Logger
Scheme *runtime.Scheme
Log logr.Logger
Recorder record.EventRecorder
Scheme *runtime.Scheme
}

// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=ibmvpcmachines,verbs=get;list;watch;create;update;patch;delete
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/pkg/errors v0.9.1
github.com/ppc64le-cloud/powervs-utils v0.0.0-20210415051532-4cdd6a79c8fa
github.com/spf13/pflag v1.0.5
golang.org/x/text v0.3.7
k8s.io/api v0.23.5
k8s.io/apimachinery v0.23.5
k8s.io/client-go v0.23.5
Expand Down Expand Up @@ -121,7 +122,6 @@ require (
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect
golang.org/x/sys v0.0.0-20220330033206-e17cdc41300f // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
gomodules.xyz/jsonpatch/v2 v2.2.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
Expand Down
Loading

0 comments on commit 9851c9a

Please sign in to comment.