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

Replace fmt.Printf/Println with logr.logger #487

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
74 changes: 38 additions & 36 deletions controllers/k8sgpt_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

corev1alpha1 "github.com/k8sgpt-ai/k8sgpt-operator/api/v1alpha1"

"github.com/go-logr/logr"
"github.com/prometheus/client_golang/prometheus"
v1 "k8s.io/api/apps/v1"
kcorev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -95,7 +96,8 @@ type K8sGPTReconciler struct {
// +kubebuilder:rbac:groups="*",resources="*",verbs="*"
// +kubebuilder:rbac:groups="apiextensions.k8s.io",resources="*",verbs="*"
func (r *K8sGPTReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
_ = log.FromContext(ctx)
log := log.FromContext(ctx)
log.Info("Reconciling K8sGPT")

// Look up the instance for this reconcile request
k8sgptConfig := &corev1alpha1.K8sGPT{}
Expand All @@ -108,7 +110,7 @@ func (r *K8sGPTReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
return ctrl.Result{}, client.IgnoreNotFound(err)
}

// Add a finaliser if there isn't one
// Add a finalizer if there isn't one
if k8sgptConfig.ObjectMeta.DeletionTimestamp.IsZero() {
// The object is not being deleted, so if it does not have our finalizer,
// then lets add the finalizer and update the object. This is equivalent
Expand All @@ -119,7 +121,7 @@ func (r *K8sGPTReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
k8sgptReconcileErrorCount.With(prometheus.Labels{
"k8sgpt": k8sgptConfig.Name,
}).Inc()
return r.finishReconcile(err, false)
return r.finishReconcile(log, err, false)
}
}
} else {
Expand All @@ -132,18 +134,18 @@ func (r *K8sGPTReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
k8sgptReconcileErrorCount.With(prometheus.Labels{
"k8sgpt": k8sgptConfig.Name,
}).Inc()
return r.finishReconcile(err, false)
return r.finishReconcile(log, err, false)
}
controllerutil.RemoveFinalizer(k8sgptConfig, FinalizerName)
if err := r.Update(ctx, k8sgptConfig); err != nil {
k8sgptReconcileErrorCount.With(prometheus.Labels{
"k8sgpt": k8sgptConfig.Name,
}).Inc()
return r.finishReconcile(err, false)
return r.finishReconcile(log, err, false)
}
}
// Stop reconciliation as the item is being deleted
return r.finishReconcile(nil, false)
return r.finishReconcile(log, nil, false)
}

if k8sgptConfig.Spec.AI.BackOff == nil {
Expand All @@ -155,7 +157,7 @@ func (r *K8sGPTReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
k8sgptReconcileErrorCount.With(prometheus.Labels{
"k8sgpt": k8sgptConfig.Name,
}).Inc()
return r.finishReconcile(err, false)
return r.finishReconcile(log, err, false)
}
}

Expand All @@ -167,14 +169,14 @@ func (r *K8sGPTReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
k8sgptReconcileErrorCount.With(prometheus.Labels{
"k8sgpt": k8sgptConfig.Name,
}).Inc()
return r.finishReconcile(err, false)
return r.finishReconcile(log, err, false)
}
err = resources.Sync(ctx, r.Client, *k8sgptConfig, resources.SyncOp)
if err != nil {
k8sgptReconcileErrorCount.With(prometheus.Labels{
"k8sgpt": k8sgptConfig.Name,
}).Inc()
return r.finishReconcile(err, false)
return r.finishReconcile(log, err, false)
}

if deployment.Status.ReadyReplicas > 0 {
Expand All @@ -196,10 +198,10 @@ func (r *K8sGPTReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
k8sgptReconcileErrorCount.With(prometheus.Labels{
"k8sgpt": k8sgptConfig.Name,
}).Inc()
return r.finishReconcile(err, false)
return r.finishReconcile(log, err, false)
}

return r.finishReconcile(nil, false)
return r.finishReconcile(log, nil, false)
}

// If the deployment is active, we will query it directly for sis data
Expand All @@ -208,17 +210,18 @@ func (r *K8sGPTReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
k8sgptReconcileErrorCount.With(prometheus.Labels{
"k8sgpt": k8sgptConfig.Name,
}).Inc()
return r.finishReconcile(err, false)
return r.finishReconcile(log, err, false)
}

// Log address
fmt.Printf("K8sGPT address: %s\n", address)
log.Info(fmt.Sprintf("K8sGPT address: %s", address))

k8sgptClient, err := kclient.NewClient(address)
if err != nil {
k8sgptReconcileErrorCount.With(prometheus.Labels{
"k8sgpt": k8sgptConfig.Name,
}).Inc()
return r.finishReconcile(err, false)
return r.finishReconcile(log, err, false)
}

defer k8sgptClient.Close()
Expand All @@ -230,16 +233,16 @@ func (r *K8sGPTReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
k8sgptReconcileErrorCount.With(prometheus.Labels{
"k8sgpt": k8sgptConfig.Name,
}).Inc()
return r.finishReconcile(err, false)
return r.finishReconcile(log, err, false)
}
}
if k8sgptConfig.Spec.Integrations != nil {
err = k8sgptClient.AddIntegration(k8sgptConfig)
err = k8sgptClient.AddIntegration(log, k8sgptConfig)
if err != nil {
k8sgptReconcileErrorCount.With(prometheus.Labels{
"k8sgpt": k8sgptConfig.Name,
}).Inc()
return r.finishReconcile(err, false)
return r.finishReconcile(log, err, false)
}
}

Expand All @@ -256,7 +259,7 @@ func (r *K8sGPTReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
if k8sgptConfig.Spec.AI.BackOff.Enabled {
if analysisRetryCount > k8sgptConfig.Spec.AI.BackOff.MaxRetries {
allowBackendAIRequest = false
fmt.Printf("Disabled AI backend %s due to failures exceeding max retries\n", k8sgptConfig.Spec.AI.Backend)
log.Info(fmt.Sprintf("Disabled AI backend %s due to failures exceeding max retries", k8sgptConfig.Spec.AI.Backend))
analysisRetryCount = 0
}
analysisRetryCount++
Expand All @@ -265,7 +268,7 @@ func (r *K8sGPTReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
k8sgptReconcileErrorCount.With(prometheus.Labels{
"k8sgpt": k8sgptConfig.Name,
}).Inc()
return r.finishReconcile(err, false)
return r.finishReconcile(log, err, false)
}
// Reset analysisRetryCount
analysisRetryCount = 0
Expand All @@ -290,7 +293,7 @@ func (r *K8sGPTReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
k8sgptReconcileErrorCount.With(prometheus.Labels{
"k8sgpt": k8sgptConfig.Name,
}).Inc()
return r.finishReconcile(err, false)
return r.finishReconcile(log, err, false)
}
// Prior to creating or updating any results we will delete any stale results that
// no longer are relevent, we can do this by using the resultSpec composed name against
Expand All @@ -304,19 +307,19 @@ func (r *K8sGPTReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
k8sgptReconcileErrorCount.With(prometheus.Labels{
"k8sgpt": k8sgptConfig.Name,
}).Inc()
return r.finishReconcile(err, false)
return r.finishReconcile(log, err, false)
}
if len(resultList.Items) > 0 {
// If the result does not exist in the map we will delete it
for _, result := range resultList.Items {
fmt.Printf("Checking if %s is still relevant\n", result.Name)
log.Info(fmt.Sprintf("Checking if %s is still relevant", result.Name))
if _, ok := rawResults[result.Name]; !ok {
err = r.Delete(ctx, &result)
if err != nil {
k8sgptReconcileErrorCount.With(prometheus.Labels{
"k8sgpt": k8sgptConfig.Name,
}).Inc()
return r.finishReconcile(err, false)
return r.finishReconcile(log, err, false)
} else {
k8sgptNumberOfResultsByType.With(prometheus.Labels{
"kind": result.Spec.Kind,
Expand All @@ -335,7 +338,7 @@ func (r *K8sGPTReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
k8sgptReconcileErrorCount.With(prometheus.Labels{
"k8sgpt": k8sgptConfig.Name,
}).Inc()
return r.finishReconcile(err, false)
return r.finishReconcile(log, err, false)

}
// Update metrics
Expand All @@ -346,9 +349,8 @@ func (r *K8sGPTReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
"k8sgpt": k8sgptConfig.Name,
}).Inc()
} else if operation == resources.UpdatedResult {
fmt.Printf("Updated successfully %s \n", result.Name)
log.Info(fmt.Sprintf("Updated successfully %s", result.Name))
}

}

// We emit when result Status is not historical
Expand All @@ -358,10 +360,10 @@ func (r *K8sGPTReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
"k8sgpts.k8sgpt.ai/name": k8sgptConfig.Name,
"k8sgpts.k8sgpt.ai/namespace": k8sgptConfig.Namespace,
})); err != nil {
return r.finishReconcile(err, false)
return r.finishReconcile(log, err, false)
}
if len(latestResultList.Items) == 0 {
return r.finishReconcile(nil, false)
return r.finishReconcile(log, nil, false)
}

sinkEnabled := k8sgptConfig.Spec.Sink != nil && k8sgptConfig.Spec.Sink.Type != "" && (k8sgptConfig.Spec.Sink.Endpoint != "" || k8sgptConfig.Spec.Sink.Secret != nil)
Expand All @@ -380,7 +382,7 @@ func (r *K8sGPTReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
k8sgptReconcileErrorCount.With(prometheus.Labels{
"k8sgpt": k8sgptConfig.Name,
}).Inc()
return r.finishReconcile(fmt.Errorf("could not find sink secret: %w", err), false)
return r.finishReconcile(log, fmt.Errorf("could not find sink secret: %w", err), false)
}

sinkSecretValue = string(secret.Data[k8sgptConfig.Spec.Sink.Secret.Key])
Expand All @@ -392,7 +394,7 @@ func (r *K8sGPTReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
for _, result := range latestResultList.Items {
var res corev1alpha1.Result
if err := r.Get(ctx, client.ObjectKey{Namespace: result.Namespace, Name: result.Name}, &res); err != nil {
return r.finishReconcile(err, false)
return r.finishReconcile(log, err, false)
}

if sinkEnabled {
Expand All @@ -401,7 +403,7 @@ func (r *K8sGPTReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
k8sgptReconcileErrorCount.With(prometheus.Labels{
"k8sgpt": k8sgptConfig.Name,
}).Inc()
return r.finishReconcile(err, false)
return r.finishReconcile(log, err, false)
}
res.Status.Webhook = k8sgptConfig.Spec.Sink.Endpoint
}
Expand All @@ -413,12 +415,12 @@ func (r *K8sGPTReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
k8sgptReconcileErrorCount.With(prometheus.Labels{
"k8sgpt": k8sgptConfig.Name,
}).Inc()
return r.finishReconcile(err, false)
return r.finishReconcile(log, err, false)
}
}
}

return r.finishReconcile(nil, false)
return r.finishReconcile(log, nil, false)
}

// SetupWithManager sets up the controller with the Manager.
Expand All @@ -435,20 +437,20 @@ func (r *K8sGPTReconciler) SetupWithManager(mgr ctrl.Manager) error {
return c
}

func (r *K8sGPTReconciler) finishReconcile(err error, requeueImmediate bool) (ctrl.Result, error) {
func (r *K8sGPTReconciler) finishReconcile(logger logr.Logger, err error, requeueImmediate bool) (ctrl.Result, error) {
if err != nil {
interval := ReconcileErrorInterval
if requeueImmediate {
interval = 0
}
fmt.Printf("Finished Reconciling k8sGPT with error: %s\n", err.Error())
logger.Error(err, "Finished Reconciling k8sGPT with error")
return ctrl.Result{Requeue: true, RequeueAfter: interval}, err
}
interval := ReconcileSuccessInterval
if requeueImmediate {
interval = 0
}
fmt.Println("Finished Reconciling k8sGPT")
logger.Info("Finished Reconciling k8sGPT")
return ctrl.Result{Requeue: true, RequeueAfter: interval}, nil
}

Expand Down
9 changes: 6 additions & 3 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"google.golang.org/grpc"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
)

// This is the client for communicating with the K8sGPT in cluster deployment
Expand Down Expand Up @@ -68,16 +69,18 @@ func GenerateAddress(ctx context.Context, cli client.Client, k8sgptConfig *v1alp
}
}

fmt.Printf("Creating new client for %s\n", address)
log := log.FromContext(ctx)
log.Info(fmt.Sprintf("Creating new client for %s", address))

// Test if the port is open
conn, err := net.DialTimeout("tcp", address, 1*time.Second)
if err != nil {
return "", err
}
defer conn.Close()

fmt.Printf("Connection established between %s and localhost with time out of %d seconds.\n", address, int64(1))
fmt.Printf("Remote Address : %s \n", conn.RemoteAddr().String())
log.WithValues("Remote Address", conn.RemoteAddr().String()).
Info(fmt.Sprintf("Connection established between %s and localhost with time out of %d seconds.", address, int64(1)))

return address, nil
}
5 changes: 3 additions & 2 deletions pkg/client/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import (

rpc "buf.build/gen/go/k8sgpt-ai/k8sgpt/grpc/go/schema/v1/schemav1grpc"
schemav1 "buf.build/gen/go/k8sgpt-ai/k8sgpt/protocolbuffers/go/schema/v1"
"github.com/go-logr/logr"
"github.com/k8sgpt-ai/k8sgpt-operator/api/v1alpha1"
)

func (c *Client) AddIntegration(config *v1alpha1.K8sGPT) error {
func (c *Client) AddIntegration(logger logr.Logger, config *v1alpha1.K8sGPT) error {

// Check if the integration is active already
client := rpc.NewServerServiceClient(c.conn)
Expand All @@ -22,7 +23,7 @@ func (c *Client) AddIntegration(config *v1alpha1.K8sGPT) error {
}

if resp.Trivy.Enabled == config.Spec.Integrations.Trivy.Enabled {
fmt.Println("Skipping trivy installation, already enabled")
logger.Info("Skipping trivy installation, already enabled")
return nil
}
// If the integration is inactive, make it active
Expand Down
9 changes: 6 additions & 3 deletions pkg/integrations/integrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"k8s.io/cli-runtime/pkg/genericclioptions"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
)

const (
Expand All @@ -37,13 +38,15 @@ func NewIntegrations(client client.Client, ctx context.Context) (*Integrations,
}

func (i *Integrations) BackstageLabel(result v1alpha1.ResultSpec) map[string]string {
log := log.FromContext(i.ctx, "result", result)

namespace, resourceName, _ := strings.Cut(result.Name, "/")
// Log and don't propagate errors so we won't trigger a new reconciliation
gvr, err := i.restMapper.ResourceFor(schema.GroupVersionResource{
Resource: result.Kind,
})
if err != nil {
fmt.Printf("Unable to find Kind '%s'\n", result.Kind)
log.Error(err, "Unable to find GVR for Kind", "kind", result.Kind)
return map[string]string{}
}

Expand All @@ -58,13 +61,13 @@ func (i *Integrations) BackstageLabel(result v1alpha1.ResultSpec) map[string]str
err = i.client.Get(i.ctx, client.ObjectKey{Name: resourceName, Namespace: namespace}, obj)
// if we don't find the K8s object we won't trigger a new reconciliation and just log a message
if err != nil {
fmt.Printf("Fail to retrieve resource %s for namespace %s\n", resourceName, namespace)
log.Error(err, "Fail to retrieve resource", "resource", resourceName, "namespace", namespace)
return map[string]string{}
}
labels := obj.GetLabels()
value, exists := labels[backstageLabelKey]
if !exists {
fmt.Printf("Label key '%s' does not exist in %s resource: %s\n", backstageLabelKey, result.Kind, resourceName)
log.Info(fmt.Sprintf("Label key '%s' does not exist in %s resource: %s", backstageLabelKey, result.Kind, resourceName))
}
// Assign the same label key/value to result CR
return map[string]string{backstageLabelKey: value}
Expand Down
Loading