Skip to content

Commit

Permalink
feat: remove the direct usage of logrus and logr
Browse files Browse the repository at this point in the history
  • Loading branch information
pengpeng committed Dec 15, 2024
1 parent 3692319 commit 4a583e9
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 43 deletions.
76 changes: 37 additions & 39 deletions controllers/cluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package kubekey

import (
"bytes"
"bytetrade.io/web3os/installer/pkg/core/logger"
"context"
"encoding/base64"
"encoding/json"
Expand All @@ -34,7 +35,6 @@ import (
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/util/wait"

"github.com/go-logr/logr"
yamlV2 "gopkg.in/yaml.v2"
kubeErr "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -59,7 +59,6 @@ const (
// ClusterReconciler reconciles a Cluster object
type ClusterReconciler struct {
client.Client
Log logr.Logger
Scheme *runtime.Scheme
}

Expand Down Expand Up @@ -94,7 +93,6 @@ type ClusterReconciler struct {
// +kubebuilder:rbac:groups=cluster.kubesphere.io,resources=*,verbs=*

func (r *ClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
log := r.Log.WithValues("cluster", req.NamespacedName)

cluster := &kubekeyv1alpha2.Cluster{}
cmFound := &corev1.ConfigMap{}
Expand All @@ -106,10 +104,10 @@ func (r *ClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
// Fetch the Cluster object
if err := r.Get(ctx, req.NamespacedName, cluster); err != nil {
if kubeErr.IsNotFound(err) {
log.Info("Cluster resource not found. Ignoring since object must be deleted")
logger.Info("Cluster resource not found. Ignoring since object must be deleted")
return ctrl.Result{}, nil
}
log.Error(err, "Failed to get Cluster")
logger.Error(err, "Failed to get Cluster")
return ctrl.Result{}, err
}

Expand All @@ -131,7 +129,7 @@ func (r *ClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
return ctrl.Result{RequeueAfter: 2 * time.Second}, err
}

if err := updateClusterConfigMap(r, ctx, cluster, cmFound, log); err != nil {
if err := updateClusterConfigMap(r, ctx, cluster, cmFound); err != nil {
return ctrl.Result{RequeueAfter: 2 * time.Second}, err
}
return ctrl.Result{RequeueAfter: 1 * time.Second}, nil
Expand All @@ -143,7 +141,7 @@ func (r *ClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
return ctrl.Result{RequeueAfter: 2 * time.Second}, err
}
if len(nodes) != 0 {
log.Info("Cluster resource defines current cluster")
logger.Info("Cluster resource defines current cluster")
if err := adaptExistedCluster(nodes, cluster); err != nil {
return ctrl.Result{RequeueAfter: 2 * time.Second}, err
}
Expand All @@ -160,7 +158,7 @@ func (r *ClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
return ctrl.Result{RequeueAfter: 2 * time.Second}, err
}
if len(otherClusterNodes) != 0 {
log.Info("Cluster resource defines other existed cluster")
logger.Info("Cluster resource defines other existed cluster")
if err := adaptExistedCluster(otherClusterNodes, cluster); err != nil {
return ctrl.Result{RequeueAfter: 2 * time.Second}, err
}
Expand All @@ -171,12 +169,12 @@ func (r *ClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
return ctrl.Result{RequeueAfter: 1 * time.Second}, nil
}

if err := updateRunJob(r, req, ctx, cluster, jobFound, log, CreateCluster); err != nil {
if err := updateRunJob(r, req, ctx, cluster, jobFound, CreateCluster); err != nil {
return ctrl.Result{RequeueAfter: 2 * time.Second}, err
}

addHosts = cluster.Spec.Hosts
sendHostsAction(1, addHosts, log)
sendHostsAction(1, addHosts)

// Ensure that the cluster has been created successfully, otherwise re-enter Reconcile.
return ctrl.Result{RequeueAfter: 3 * time.Second}, nil
Expand All @@ -189,10 +187,10 @@ func (r *ClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
return ctrl.Result{RequeueAfter: 3 * time.Second}, nil
}

if err := updateClusterConfigMap(r, ctx, cluster, cmFound, log); err != nil {
if err := updateClusterConfigMap(r, ctx, cluster, cmFound); err != nil {
return ctrl.Result{}, err
}
if err := updateRunJob(r, req, ctx, cluster, jobFound, log, AddNodes); err != nil {
if err := updateRunJob(r, req, ctx, cluster, jobFound, AddNodes); err != nil {
return ctrl.Result{Requeue: true}, err
}

Expand All @@ -206,7 +204,7 @@ func (r *ClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
addHosts = append(addHosts, host)
}
}
sendHostsAction(1, addHosts, log)
sendHostsAction(1, addHosts)

// Ensure that the nodes has been added successfully, otherwise re-enter Reconcile.
return ctrl.Result{RequeueAfter: 3 * time.Second}, nil
Expand Down Expand Up @@ -282,7 +280,7 @@ func (r *ClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
}
}

sendHostsAction(0, removeHosts, log)
sendHostsAction(0, removeHosts)

var newEtcd, newMaster, newWorker []string
for _, node := range newNodes {
Expand Down Expand Up @@ -311,10 +309,10 @@ func (r *ClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
// Fetch the Cluster object
if err := r.Get(ctx, req.NamespacedName, cluster); err != nil {
if kubeErr.IsNotFound(err) {
log.Info("Cluster resource not found. Ignoring since object must be deleted")
logger.Info("Cluster resource not found. Ignoring since object must be deleted")
return ctrl.Result{}, nil
}
log.Error(err, "Failed to get Cluster")
logger.Error(err, "Failed to get Cluster")
return ctrl.Result{}, err
}

Expand Down Expand Up @@ -380,7 +378,7 @@ func (r *ClusterReconciler) configMapForCluster(c *kubekeyv1alpha2.Cluster) *cor
return cm
}

func (r *ClusterReconciler) jobForCluster(c *kubekeyv1alpha2.Cluster, action string, log logr.Logger) *batchv1.Job {
func (r *ClusterReconciler) jobForCluster(c *kubekeyv1alpha2.Cluster, action string) *batchv1.Job {
var (
backoffLimit int32 = 0
name string
Expand All @@ -401,7 +399,7 @@ func (r *ClusterReconciler) jobForCluster(c *kubekeyv1alpha2.Cluster, action str
}
err := r.List(context.TODO(), podlist, listOpts...)
if err != nil {
log.Error(err, "Failed to list kubekey controller-manager pod")
logger.Error(err, "Failed to list kubekey controller-manager pod")
}
nodeName := podlist.Items[0].Spec.NodeName
var image string
Expand Down Expand Up @@ -552,29 +550,29 @@ func updateStatusRunner(r *ClusterReconciler, req ctrl.Request, cluster *kubekey
return nil
}

func updateClusterConfigMap(r *ClusterReconciler, ctx context.Context, cluster *kubekeyv1alpha2.Cluster, cmFound *corev1.ConfigMap, log logr.Logger) error {
func updateClusterConfigMap(r *ClusterReconciler, ctx context.Context, cluster *kubekeyv1alpha2.Cluster, cmFound *corev1.ConfigMap) error {
// Check if the configmap already exists, if not create a new one
if err := r.Get(ctx, types.NamespacedName{Name: cluster.Name, Namespace: cluster.Namespace}, cmFound); err != nil && !kubeErr.IsNotFound(err) {
log.Error(err, "Failed to get ConfigMap", "ConfigMap.Namespace", cmFound.Namespace, "ConfigMap.Name", cmFound.Name)
logger.Error(err, "Failed to get ConfigMap", "ConfigMap.Namespace", cmFound.Namespace, "ConfigMap.Name", cmFound.Name)
return err
} else if err == nil {
if err := r.Delete(ctx, cmFound); err != nil {
log.Error(err, "Failed to delete old ConfigMap", "ConfigMap.Namespace", cmFound.Namespace, "ConfigMap.Name", cmFound.Name)
logger.Error(err, "Failed to delete old ConfigMap", "ConfigMap.Namespace", cmFound.Namespace, "ConfigMap.Name", cmFound.Name)
return err
}
}

// Define a new configmap
cmCluster := r.configMapForCluster(cluster)
log.Info("Creating a new ConfigMap", "ConfigMap.Namespace", cmCluster.Namespace, "ConfigMap.Name", cmCluster.Name)
logger.Info("Creating a new ConfigMap", "ConfigMap.Namespace", cmCluster.Namespace, "ConfigMap.Name", cmCluster.Name)
if err := r.Create(ctx, cmCluster); err != nil {
log.Error(err, "Failed to create new ConfigMap", "ConfigMap.Namespace", cmCluster.Namespace, "ConfigMap.Name", cmCluster.Name)
logger.Error(err, "Failed to create new ConfigMap", "ConfigMap.Namespace", cmCluster.Namespace, "ConfigMap.Name", cmCluster.Name)
return err
}
return nil
}

func updateRunJob(r *ClusterReconciler, req ctrl.Request, ctx context.Context, cluster *kubekeyv1alpha2.Cluster, jobFound *batchv1.Job, log logr.Logger, action string) error {
func updateRunJob(r *ClusterReconciler, req ctrl.Request, ctx context.Context, cluster *kubekeyv1alpha2.Cluster, jobFound *batchv1.Job, action string) error {
var (
name string
)
Expand All @@ -599,15 +597,15 @@ func updateRunJob(r *ClusterReconciler, req ctrl.Request, ctx context.Context, c
_ = r.Delete(ctx, &pod)
}
}
log.Info("Prepare to delete old job", "Job.Namespace", jobFound.Namespace, "Job.Name", jobFound.Name)
logger.Info("Prepare to delete old job", "Job.Namespace", jobFound.Namespace, "Job.Name", jobFound.Name)
if err := r.Delete(ctx, jobFound); err != nil {
log.Error(err, "Failed to delete old Job", "Job.Namespace", jobFound.Namespace, "Job.Name", jobFound.Name)
logger.Error(err, "Failed to delete old Job", "Job.Namespace", jobFound.Namespace, "Job.Name", jobFound.Name)
return err
}
log.Info("Deleting old job success", "Job.Namespace", jobFound.Namespace, "Job.Name", jobFound.Name)
logger.Info("Deleting old job success", "Job.Namespace", jobFound.Namespace, "Job.Name", jobFound.Name)

err := wait.PollInfinite(1*time.Second, func() (bool, error) {
log.Info("Checking old job is deleted", "Job.Namespace", jobFound.Namespace, "Job.Name", jobFound.Name)
logger.Info("Checking old job is deleted", "Job.Namespace", jobFound.Namespace, "Job.Name", jobFound.Name)
if e := r.Get(ctx, types.NamespacedName{Name: name, Namespace: req.Namespace}, jobFound); e != nil {
if kubeErr.IsNotFound(e) {
return true, nil
Expand All @@ -617,21 +615,21 @@ func updateRunJob(r *ClusterReconciler, req ctrl.Request, ctx context.Context, c
return false, nil
})
if err != nil {
log.Error(err, "Failed to loop check old job is deleted", "Job.Namespace", jobFound.Namespace, "Job.Name", jobFound.Name)
logger.Error(err, "Failed to loop check old job is deleted", "Job.Namespace", jobFound.Namespace, "Job.Name", jobFound.Name)
return err
}

jobCluster := r.jobForCluster(cluster, action, log)
log.Info("Creating a new Job to scale cluster", "Job.Namespace", jobCluster.Namespace, "Job.Name", jobCluster.Name)
jobCluster := r.jobForCluster(cluster, action)
logger.Info("Creating a new Job to scale cluster", "Job.Namespace", jobCluster.Namespace, "Job.Name", jobCluster.Name)
if err := r.Create(ctx, jobCluster); err != nil {
log.Error(err, "Failed to create new Job", "Job.Namespace", jobCluster.Namespace, "Job.Name", jobCluster.Name)
logger.Error(err, "Failed to create new Job", "Job.Namespace", jobCluster.Namespace, "Job.Name", jobCluster.Name)
return err
}
} else if kubeErr.IsNotFound(err) {
jobCluster := r.jobForCluster(cluster, action, log)
log.Info("Creating a new Job to create cluster", "Job.Namespace", jobCluster.Namespace, "Job.Name", jobCluster.Name)
jobCluster := r.jobForCluster(cluster, action)
logger.Info("Creating a new Job to create cluster", "Job.Namespace", jobCluster.Namespace, "Job.Name", jobCluster.Name)
if err := r.Create(ctx, jobCluster); err != nil {
log.Error(err, "Failed to create new Job", "Job.Namespace", jobCluster.Namespace, "Job.Name", jobCluster.Name)
logger.Error(err, "Failed to create new Job", "Job.Namespace", jobCluster.Namespace, "Job.Name", jobCluster.Name)
return err
}
}
Expand All @@ -641,7 +639,7 @@ func updateRunJob(r *ClusterReconciler, req ctrl.Request, ctx context.Context, c
return nil
}

func sendHostsAction(action int, hosts []kubekeyv1alpha2.HostCfg, log logr.Logger) {
func sendHostsAction(action int, hosts []kubekeyv1alpha2.HostCfg) {
if os.Getenv("HOSTS_MANAGER") == "true" {
type HostsAction struct {
Hosts []kubekeyv1alpha2.HostCfg `json:"hosts,omitempty"`
Expand All @@ -655,20 +653,20 @@ func sendHostsAction(action int, hosts []kubekeyv1alpha2.HostCfg, log logr.Logge
fmt.Println(newHostsAction)
hostsInfoBytes, err := json.Marshal(newHostsAction)
if err != nil {
log.Error(err, "Failed to marshal hosts info")
logger.Error(err, "Failed to marshal hosts info")
}

fmt.Println(string(hostsInfoBytes))
req, err := http.NewRequest("POST", "http://localhost:8090/api/v1alpha2/hosts", bytes.NewReader(hostsInfoBytes))
if err != nil {
log.Error(err, "Failed to create request")
logger.Error(err, "Failed to create request")
}

req.Header.Add("Content-Type", "application/json")

_, err = http.DefaultClient.Do(req)
if err != nil {
log.Error(err, "Failed to send hosts info")
logger.Error(err, "Failed to send hosts info")
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ require (
github.com/schollz/progressbar/v3 v3.17.1
github.com/shirou/gopsutil/v4 v4.24.5
github.com/shurcooL/httpfs v0.0.0-20230704072500-f1e31cf0ba5c
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.8.0
go.uber.org/zap v1.27.0
golang.org/x/crypto v0.28.0
Expand Down Expand Up @@ -163,6 +162,7 @@ require (
github.com/russross/blackfriday v1.6.0 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/smartystreets/goconvey v1.8.1 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/stretchr/testify v1.9.0 // indirect
Expand Down Expand Up @@ -196,7 +196,7 @@ require (
)

require (
github.com/go-logr/logr v1.4.1
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-openapi/jsonpointer v0.21.0 // indirect
github.com/go-openapi/jsonreference v0.21.0 // indirect
github.com/go-openapi/swag v0.23.0 // indirect
Expand Down
Binary file added main
Binary file not shown.
4 changes: 2 additions & 2 deletions pkg/utils/certs/certs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package certs

import (
"bytetrade.io/web3os/installer/pkg/core/logger"
"crypto"
cryptorand "crypto/rand"
"crypto/x509"
Expand All @@ -26,7 +27,6 @@ import (

"bytetrade.io/web3os/installer/pkg/common"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/util/sets"
certutil "k8s.io/client-go/util/cert"
)
Expand Down Expand Up @@ -282,7 +282,7 @@ func CheckCertificatePeriodValidity(baseName string, cert *x509.Certificate) {
certPeriodValidation[baseName] = struct{}{}

if err := ValidateCertPeriod(cert, 0); err != nil {
logrus.Warningf("WARNING: could not validate bounds for certificate %s: %v", baseName, err)
logger.Warnf("WARNING: could not validate bounds for certificate %s: %v", baseName, err)
}
}

Expand Down

0 comments on commit 4a583e9

Please sign in to comment.