Skip to content

Commit

Permalink
Update log message format (#866)
Browse files Browse the repository at this point in the history
Based on golang Golang convention:
https://go.dev/wiki/CodeReviewComments#error-strings
it should make the first letter of the log message sentence as upper case.
  • Loading branch information
timdengyun authored Nov 5, 2024
1 parent 21bf52f commit 2b6d1b7
Show file tree
Hide file tree
Showing 32 changed files with 258 additions and 258 deletions.
47 changes: 23 additions & 24 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,20 +93,20 @@ func init() {
}

func StartNSXServiceAccountController(mgr ctrl.Manager, commonService common.Service) {
log.Info("starting NSXServiceAccountController")
log.Info("Starting NSXServiceAccountController")
nsxServiceAccountReconcile := &nsxserviceaccountcontroller.NSXServiceAccountReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Recorder: mgr.GetEventRecorderFor("nsxserviceaccount-controller"),
}
nsxServiceAccountService, err := nsxserviceaccount.InitializeNSXServiceAccount(commonService)
if err != nil {
log.Error(err, "failed to initialize service", "controller", "NSXServiceAccount")
log.Error(err, "Failed to initialize service", "controller", "NSXServiceAccount")
os.Exit(1)
}
nsxServiceAccountReconcile.Service = nsxServiceAccountService
if err := nsxServiceAccountReconcile.Start(mgr); err != nil {
log.Error(err, "failed to create controller", "controller", "NSXServiceAccount")
log.Error(err, "Failed to create controller", "controller", "NSXServiceAccount")
os.Exit(1)
}
go commonctl.GenericGarbageCollector(make(chan bool), common.GCInterval, nsxServiceAccountReconcile.CollectGarbage)
Expand All @@ -121,7 +121,7 @@ func StartNetworkInfoController(mgr ctrl.Manager, vpcService *vpc.VPCService, ip
networkInfoReconciler.Service = vpcService
networkInfoReconciler.IPBlocksInfoService = ipblocksInfoService
if err := networkInfoReconciler.Start(mgr); err != nil {
log.Error(err, "failed to create networkinfo controller", "controller", "NetworkInfo")
log.Error(err, "Failed to create networkinfo controller", "controller", "NetworkInfo")
os.Exit(1)
}
go commonctl.GenericGarbageCollector(make(chan bool), common.GCInterval, networkInfoReconciler.CollectGarbage)
Expand All @@ -136,7 +136,7 @@ func StartNamespaceController(mgr ctrl.Manager, cf *config.NSXOperatorConfig, vp
}

if err := nsReconciler.Start(mgr); err != nil {
log.Error(err, "failed to create namespace controller", "controller", "Namespace")
log.Error(err, "Failed to create namespace controller", "controller", "Namespace")
os.Exit(1)
}
}
Expand All @@ -151,7 +151,7 @@ func StartIPAddressAllocationController(mgr ctrl.Manager, ipAddressAllocationSer
}

if err := ipAddressAllocationReconciler.SetupWithManager(mgr); err != nil {
log.Error(err, "failed to create ipaddressallocation controller")
log.Error(err, "Failed to create ipaddressallocation controller")
os.Exit(1)
}
go commonctl.GenericGarbageCollector(make(chan bool), common.GCInterval, ipAddressAllocationReconciler.CollectGarbage)
Expand Down Expand Up @@ -190,31 +190,31 @@ func startServiceController(mgr manager.Manager, nsxClient *nsx.Client) {
var err error
vpcService, err = vpc.InitializeVPC(commonService)
if err != nil {
log.Error(err, "failed to initialize vpc commonService", "controller", "VPC")
log.Error(err, "Failed to initialize vpc commonService", "controller", "VPC")
os.Exit(1)
}
subnetService, err := subnetservice.InitializeSubnetService(commonService)
if err != nil {
log.Error(err, "failed to initialize subnet commonService")
log.Error(err, "Failed to initialize subnet commonService")
os.Exit(1)
}
ipAddressAllocationService, err := ipaddressallocationservice.InitializeIPAddressAllocation(commonService, vpcService, false)
if err != nil {
log.Error(err, "failed to initialize ipaddressallocation commonService", "controller", "IPAddressAllocation")
log.Error(err, "Failed to initialize ipaddressallocation commonService", "controller", "IPAddressAllocation")
}
subnetPortService, err := subnetportservice.InitializeSubnetPort(commonService)
if err != nil {
log.Error(err, "failed to initialize subnetport commonService", "controller", "SubnetPort")
log.Error(err, "Failed to initialize subnetport commonService", "controller", "SubnetPort")
os.Exit(1)
}
nodeService, err := nodeservice.InitializeNode(commonService)
if err != nil {
log.Error(err, "failed to initialize node commonService", "controller", "Node")
log.Error(err, "Failed to initialize node commonService", "controller", "Node")
os.Exit(1)
}
staticRouteService, err := staticroute.InitializeStaticRoute(commonService, vpcService)
if err != nil {
log.Error(err, "failed to initialize staticroute commonService", "controller", "StaticRoute")
log.Error(err, "Failed to initialize staticroute commonService", "controller", "StaticRoute")
os.Exit(1)
}
ipblocksInfoService := ipblocksinfo.InitializeIPBlocksInfoService(commonService)
Expand All @@ -224,14 +224,14 @@ func startServiceController(mgr manager.Manager, nsxClient *nsx.Client) {

var hookServer webhook.Server
if _, err := os.Stat(config.WebhookCertDir); errors.Is(err, os.ErrNotExist) {
log.Error(err, "server cert not found, disabling webhook server", "cert", config.WebhookCertDir)
log.Error(err, "Server cert not found, disabling webhook server", "cert", config.WebhookCertDir)
} else {
hookServer = webhook.NewServer(webhook.Options{
Port: config.WebhookServerPort,
CertDir: config.WebhookCertDir,
})
if err := mgr.Add(hookServer); err != nil {
log.Error(err, "failed to add hook server")
log.Error(err, "Failed to add hook server")
os.Exit(1)
}
}
Expand All @@ -258,7 +258,6 @@ func startServiceController(mgr manager.Manager, nsxClient *nsx.Client) {
if cf.EnableAntreaNSXInterworking {
StartNSXServiceAccountController(mgr, commonService)
}

}

func electMaster(mgr manager.Manager, nsxClient *nsx.Client) {
Expand All @@ -272,13 +271,13 @@ func electMaster(mgr manager.Manager, nsxClient *nsx.Client) {
// the new master operator is configured to wait for 15 seconds, which is
// slightly longer than the default Leader Election Renew Deadline (10 seconds),
// ensuring a smooth transition.
log.Info("waiting a 15-second delay to let the old instance know that it has lost its lease")
log.Info("Waiting a 15-second delay to let the old instance know that it has lost its lease")
time.Sleep(15 * time.Second)
startServiceController(mgr, nsxClient)
}

func main() {
log.Info("starting NSX Operator")
log.Info("Starting NSX Operator")
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
HealthProbeBindAddress: config.ProbeAddr,
Expand All @@ -288,14 +287,14 @@ func main() {
LeaderElectionID: "nsx-operator",
})
if err != nil {
log.Error(err, "failed to init manager")
log.Error(err, "Failed to init manager")
os.Exit(1)
}

// nsxClient is used to interact with NSX API.
nsxClient := nsx.GetClient(cf)
if nsxClient == nil {
log.Error(nil, "failed to get nsx client")
log.Error(nil, "Failed to get nsx client")
os.Exit(1)
}

Expand All @@ -310,17 +309,17 @@ func main() {
}

if err := mgr.AddHealthzCheck("healthz", nsxClient.NSXChecker.CheckNSXHealth); err != nil {
log.Error(err, "failed to set up health check")
log.Error(err, "Failed to set up health check")
os.Exit(1)
}
if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
log.Error(err, "failed to set up ready check")
log.Error(err, "Failed to set up ready check")
os.Exit(1)
}

log.Info("starting manager")
log.Info("Starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
log.Error(err, "failed to start manager")
log.Error(err, "Failed to start manager")
os.Exit(1)
}
}
Expand All @@ -340,7 +339,7 @@ func getHealthStatus(nsxClient *nsx.Client) error {
func updateHealthMetricsPeriodically(nsxClient *nsx.Client) {
for {
if err := getHealthStatus(nsxClient); err != nil {
log.Error(err, "failed to fetch health info")
log.Error(err, "Failed to fetch health info")
}
select {
case <-time.After(metrics.ScrapeTimeout):
Expand Down
2 changes: 1 addition & 1 deletion cmd_clean/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func main() {
log = logger.ZapLogger(cf.DefaultConfig.Debug, config.LogLevel)
err := clean.Clean(ctx, cf, &log, cf.DefaultConfig.Debug, config.LogLevel)
if err != nil {
log.Error(err, "failed to clean nsx resources")
log.Error(err, "Failed to clean nsx resources")
os.Exit(1)
}
os.Exit(0)
Expand Down
12 changes: 6 additions & 6 deletions pkg/clean/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func Clean(ctx context.Context, cf *config.NSXOperatorConfig, log *logr.Logger,
}
logger.InitLog(log)

log.Info("starting NSX cleanup")
log.Info("Starting NSX cleanup")
if err := cf.ValidateConfigFromCmd(); err != nil {
return errors.Join(nsxutil.ValidationFailed, err)
}
Expand All @@ -71,7 +71,7 @@ func Clean(ctx context.Context, cf *config.NSXOperatorConfig, log *logr.Logger,

retriable := func(err error) bool {
if err != nil && !errors.As(err, &nsxutil.TimeoutFailed) {
log.Info("retrying to clean up NSX resources", "error", err)
log.Info("Retrying to clean up NSX resources", "error", err)
return true
}
return false
Expand All @@ -97,20 +97,20 @@ func Clean(ctx context.Context, cf *config.NSXOperatorConfig, log *logr.Logger,
// delete DLB group -> delete virtual servers -> DLB services -> DLB pools -> persistent profiles for DLB
if err := retry.OnError(retry.DefaultRetry, func(err error) bool {
if err != nil {
log.Info("retrying to clean up DLB resources", "error", err)
log.Info("Retrying to clean up DLB resources", "error", err)
return true
}
return false
}, func() error {
if err := CleanDLB(ctx, nsxClient.Cluster, cf, log); err != nil {
return fmt.Errorf("failed to clean up specific resource: %w", err)
return fmt.Errorf("Failed to clean up specific resource: %w", err)
}
return nil
}); err != nil {
return err
}

log.Info("cleanup NSX resources successfully")
log.Info("Cleanup NSX resources successfully")
return nil
}

Expand All @@ -127,7 +127,7 @@ func wrapCleanFunc(ctx context.Context, clean cleanup) func() error {
func InitializeCleanupService(cf *config.NSXOperatorConfig, nsxClient *nsx.Client) (*CleanupService, error) {
cleanupService := NewCleanupService()

var commonService = common.Service{
commonService := common.Service{
NSXClient: nsxClient,
NSXConfig: cf,
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/controllers/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ func AllocateSubnetFromSubnetSet(subnetSet *v1alpha1.SubnetSet, vpcService servi
if tags == nil {
return "", errors.New("failed to generate subnet tags")
}
log.Info("the existing subnets are not available, creating new subnet", "subnetList", subnetList, "subnetSet.Name", subnetSet.Name, "subnetSet.Namespace", subnetSet.Namespace)
log.Info("The existing subnets are not available, creating new subnet", "subnetList", subnetList, "subnetSet.Name", subnetSet.Name, "subnetSet.Namespace", subnetSet.Namespace)
vpcInfoList := vpcService.ListVPCInfo(subnetSet.Namespace)
if len(vpcInfoList) == 0 {
err := errors.New("no VPC found")
log.Error(err, "failed to allocate Subnet")
log.Error(err, "Failed to allocate Subnet")
return "", err
}
return subnetService.CreateOrUpdateSubnet(subnetSet, vpcInfoList[0], tags)
Expand All @@ -60,14 +60,14 @@ func getSharedNamespaceForNamespace(client k8sclient.Client, ctx context.Context
namespace := &v1.Namespace{}
namespacedName := types.NamespacedName{Name: namespaceName}
if err := client.Get(ctx, namespacedName, namespace); err != nil {
log.Error(err, "failed to get target namespace during getting VPC for namespace")
log.Error(err, "Failed to get target namespace during getting VPC for namespace")
return "", err
}
sharedNamespaceName, exists := namespace.Annotations[servicecommon.AnnotationSharedVPCNamespace]
if !exists {
return "", nil
}
log.Info("got shared VPC namespace", "current namespace", namespaceName, "shared namespace", sharedNamespaceName)
log.Info("Got shared VPC namespace", "current namespace", namespaceName, "shared namespace", sharedNamespaceName)
return sharedNamespaceName, nil
}

Expand Down
16 changes: 8 additions & 8 deletions pkg/controllers/networkpolicy/networkpolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func cleanNetworkPolicyErrorAnnotation(ctx context.Context, networkPolicy *netwo
}
updateErr := client.Update(ctx, networkPolicy)
if updateErr != nil {
log.Error(updateErr, "failed to clean NetworkPolicy annotation")
log.Error(updateErr, "Failed to clean NetworkPolicy annotation")
}
log.Info("Clean NetworkPolicy annotation")
}
Expand All @@ -108,13 +108,13 @@ func (r *NetworkPolicyReconciler) Reconcile(ctx context.Context, req ctrl.Reques
if err := r.Client.Get(ctx, req.NamespacedName, networkPolicy); err != nil {
if apierrors.IsNotFound(err) {
if err := r.deleteNetworkPolicyByName(req.Namespace, req.Name); err != nil {
log.Error(err, "failed to delete NetworkPolicy", "networkpolicy", req.NamespacedName)
log.Error(err, "Failed to delete NetworkPolicy", "networkpolicy", req.NamespacedName)
return ResultRequeue, err
}
return ResultNormal, nil
}
// In case that client is unable to check CR
log.Error(err, "client is unable to fetch NetworkPolicy CR", "req", req.NamespacedName)
log.Error(err, "Failed to fetch NetworkPolicy CR", "req", req.NamespacedName)
return ResultRequeue, err
}

Expand All @@ -134,7 +134,7 @@ func (r *NetworkPolicyReconciler) Reconcile(ctx context.Context, req ctrl.Reques
setNetworkPolicyErrorAnnotation(ctx, networkPolicy, r.Client, common.ErrorNoDFWLicense)
os.Exit(1)
}
log.Error(err, "create or update failed, would retry exponentially", "networkpolicy", req.NamespacedName)
log.Error(err, "Failed to create or update, would retry exponentially", "networkpolicy", req.NamespacedName)
updateFail(r, ctx, networkPolicy, &err)
return ResultRequeue, err
}
Expand All @@ -145,7 +145,7 @@ func (r *NetworkPolicyReconciler) Reconcile(ctx context.Context, req ctrl.Reques
metrics.CounterInc(r.Service.NSXConfig, metrics.ControllerDeleteTotal, MetricResType)

if err := r.Service.DeleteSecurityPolicy(networkPolicy, false, false, servicecommon.ResourceTypeNetworkPolicy); err != nil {
log.Error(err, "deletion failed, would retry exponentially", "networkpolicy", req.NamespacedName)
log.Error(err, "Failed to Delete, would retry exponentially", "networkpolicy", req.NamespacedName)
deleteFail(r, ctx, networkPolicy, &err)
return ResultRequeue, err
}
Expand Down Expand Up @@ -217,7 +217,7 @@ func (r *NetworkPolicyReconciler) deleteNetworkPolicyByName(ns, name string) err

log.Info("Deleting NetworkPolicy", "networkPolicyUID", uid, "nsxSecurityPolicyId", *item.Id)
if err := r.Service.DeleteSecurityPolicy(types.UID(uid), false, false, servicecommon.ResourceTypeNetworkPolicy); err != nil {
log.Error(err, "failed to delete NetworkPolicy", "networkPolicyUID", uid, "nsxSecurityPolicyId", *item.Id)
log.Error(err, "Failed to delete NetworkPolicy", "networkPolicyUID", uid, "nsxSecurityPolicyId", *item.Id)
return err
}
log.Info("Successfully deleted NetworkPolicy", "networkPolicyUID", uid, "nsxSecurityPolicyId", *item.Id)
Expand All @@ -229,7 +229,7 @@ func (r *NetworkPolicyReconciler) listNetworkPolciyCRIDs() (sets.Set[string], er
networkPolicyList := &networkingv1.NetworkPolicyList{}
err := r.Client.List(context.Background(), networkPolicyList)
if err != nil {
log.Error(err, "failed to list NetworkPolicy CRs")
log.Error(err, "Failed to list NetworkPolicy CRs")
return nil, err
}

Expand All @@ -249,7 +249,7 @@ func StartNetworkPolicyController(mgr ctrl.Manager, commonService servicecommon.
}
networkPolicyReconcile.Service = securitypolicy.GetSecurityService(commonService, vpcService)
if err := networkPolicyReconcile.Start(mgr); err != nil {
log.Error(err, "failed to create controller", "controller", "NetworkPolicy")
log.Error(err, "Failed to create controller", "controller", "NetworkPolicy")
os.Exit(1)
}
go common.GenericGarbageCollector(make(chan bool), servicecommon.GCInterval, networkPolicyReconcile.CollectGarbage)
Expand Down
6 changes: 3 additions & 3 deletions pkg/controllers/securitypolicy/namespace_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (e *EnqueueRequestForNamespace) Generic(_ context.Context, _ event.GenericE
func (e *EnqueueRequestForNamespace) Update(_ context.Context, updateEvent event.UpdateEvent, l workqueue.RateLimitingInterface) {
obj := updateEvent.ObjectNew.(*v1.Namespace)
if isInSysNs, err := util.IsSystemNamespace(nil, "", obj); err != nil {
log.Error(err, "failed to fetch namespace", "namespace", obj.Name)
log.Error(err, "Failed to fetch namespace", "namespace", obj.Name)
return
} else if isInSysNs {
log.V(2).Info("NameSpace is in system namespace, ignore it", "namespace", obj.Name)
Expand All @@ -50,7 +50,7 @@ func (e *EnqueueRequestForNamespace) Update(_ context.Context, updateEvent event
podList := &v1.PodList{}
err := e.Client.List(context.Background(), podList, client.InNamespace(obj.Name))
if err != nil {
log.Error(err, "failed to list pod in namespace", "namespace", obj.Name)
log.Error(err, "Failed to list pod in namespace", "namespace", obj.Name)
return
}

Expand All @@ -68,7 +68,7 @@ func (e *EnqueueRequestForNamespace) Update(_ context.Context, updateEvent event

err = reconcileSecurityPolicy(e.SecurityPolicyReconciler, e.Client, podList.Items, l)
if err != nil {
log.Error(err, "failed to reconcile security policy for namedport check")
log.Error(err, "Failed to reconcile security policy for namedport check")
}
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/controllers/securitypolicy/pod_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,21 @@ func (e *EnqueueRequestForPod) Raw(evt interface{}, q workqueue.RateLimitingInte
case event.GenericEvent:
obj = et.Object.(*v1.Pod)
default:
log.Error(nil, "unknown event type", "event", evt)
log.Error(nil, "Unknown event type", "event", evt)
}

pod := obj.(*v1.Pod)
if isInSysNs, err := util.IsSystemNamespace(e.Client, pod.Namespace, nil); err != nil {
log.Error(err, "failed to fetch namespace", "namespace", pod.Namespace)
log.Error(err, "Failed to fetch namespace", "namespace", pod.Namespace)
return
} else if isInSysNs {
log.V(2).Info("pod is in system namespace, do nothing")
log.V(2).Info("POD is in system namespace, do nothing")
return
}
pods = append(pods, *pod)
err := reconcileSecurityPolicy(e.SecurityPolicyReconciler, e.Client, pods, q)
if err != nil {
log.Error(err, "failed to reconcile security policy")
log.Error(err, "Failed to reconcile security policy")
}
}

Expand Down
Loading

0 comments on commit 2b6d1b7

Please sign in to comment.