Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add events recorder framework #632

Merged
merged 1 commit into from
Apr 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 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,13 @@ 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, "FailedCreateVPC", "Failed vpc creation - %v", err)
return nil, err
} else if err := s.updateDefaultSG(*vpc.DefaultSecurityGroup.ID); err != nil {
record.Warnf(s.IBMVPCCluster, "FailedUpdateDefaultSecurityGroup", "Failed to update default security group - %v", err)
return nil, err
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need Warnf for this failure as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Added Warnf for above failure.
  2. Updated record reasons to be more specific.

How about we merge this version of the code and add fake recorder + enhance test cases as part of another PR?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better to write test cases with the code itself, for this case we can add later

}
record.Eventf(s.IBMVPCCluster, "SuccessfulCreateVPC", "Created VPC %q", *vpc.Name)
return vpc, nil
}

Expand All @@ -114,6 +118,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, "FailedDeleteVPC", "Failed vpc deletion - %v", err)
} else {
record.Eventf(s.IBMVPCCluster, "SuccessfulDeleteVPC", "Deleted VPC %q", s.IBMVPCCluster.Status.VPC.Name)
}

return err
}
Expand Down Expand Up @@ -141,6 +150,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, "FailedCreateSecurityGroupRule", "Failed security group rule creation - %v", err)
}
return err
}

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

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

Expand All @@ -190,6 +205,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, "FailedDeleteFloatingIP", "Failed floatingIP deletion - %v", err)
}
return err
}
return nil
Expand Down Expand Up @@ -226,6 +244,9 @@ func (s *ClusterScope) CreateSubnet() (*vpcv1.Subnet, error) {
},
})
subnet, _, err := s.IBMVPCClients.VPCService.CreateSubnet(options)
if err != nil {
record.Warnf(s.IBMVPCCluster, "FailedCreateSubnet", "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 +314,7 @@ func (s *ClusterScope) DeleteSubnet() error {
deleteSubnetOption.SetID(subnetID)
_, err = s.IBMVPCClients.VPCService.DeleteSubnet(deleteSubnetOption)
if err != nil {
record.Warnf(s.IBMVPCCluster, "FailedDeleteSubnet", "Failed subnet deletion - %v", err)
return errors.Wrap(err, "Error when deleting subnet ")
}
return err
Expand All @@ -310,6 +332,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, "FailedCreatePublicGateway", "Failed publicgateway creation - %v", err)
}
return publicGateway, err
}

Expand All @@ -320,6 +345,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, "FailedAttachPublicGateway", "Failed publicgateway attachment - %v", err)
}
return publicGateway, err
}

Expand All @@ -329,6 +357,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, "FailedDetachPublicGateway", "Failed publicgateway detachment - %v", err)
return errors.Wrap(err, "Error when unsetting publicgateway for subnet "+subnetID)
}

Expand All @@ -337,6 +366,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, "FailedDeletePublicGateway", "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, "FailedCreateInstance", "Failed instance creation - %v", err)
} else {
record.Eventf(m.IBMVPCMachine, "SuccessfulCreateInstance", "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, "FailedDeleteInstance", "Failed instance deletion - %v", err)
} else {
record.Eventf(m.IBMVPCMachine, "SuccessfulDeleteInstance", "Deleted Instance %q", m.IBMVPCMachine.Name)
}
return err
}

Expand Down
19 changes: 17 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 @@ -160,9 +161,11 @@ func (i *PowerVSImageScope) CreateImageCOSBucket() (*models.ImageReference, *mod

imageReply, err := i.ensureImageUnique(m.Name)
if err != nil {
record.Warnf(i.IBMPowerVSImage, "FailedRetriveImage", "Failed to retrieve image %q", *imageReply.Name)
return nil, nil, err
Prajyot-Parab marked this conversation as resolved.
Show resolved Hide resolved
} else if imageReply != nil {
i.Info("Image already exists")
record.Eventf(i.IBMPowerVSImage, "SuccessfulRetriveImage", "Retrieved Image %q", *imageReply.Name)
return imageReply, nil, nil
}

Expand All @@ -185,9 +188,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, "FailedCreateImageImportJob", "Failed image import job creation - %v", err)
return nil, nil, err
}
i.Info("New import job request created")
record.Eventf(i.IBMPowerVSImage, "SuccessfulCreateImageImportJob", "Created image import job %q", *jobRef.ID)
return nil, jobRef, nil
}

Expand All @@ -203,7 +208,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, "FailedDeleteImage", "Failed image deletion - %v", err)
return err
}
record.Eventf(i.IBMPowerVSImage, "SuccessfulDeleteImage", "Deleted Image %q", i.IBMPowerVSImage.Status.ImageID)
return nil
}

// GetImportJob will get the image import job.
Expand All @@ -213,7 +223,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, "FailedDeleteImageImoprtJob", "Failed image import job deletion - %v", err)
return err
}
record.Eventf(i.IBMPowerVSImage, "SuccessfulDeleteImageImoprtJob", "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, "FailedRetriveImage", "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, "FailedRetriveNetwork", "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, "FailedCreateInstance", "Failed instance creation - %v", err)
return nil, err
}
record.Eventf(m.IBMPowerVSMachine, "SuccessfulCreateInstance", "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, "FailedDeleteInstance", "Failed instance deletion - %v", err)
return err
}
record.Eventf(m.IBMPowerVSMachine, "SuccessfulDeleteInstance", "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