Skip to content

Commit

Permalink
new approach in utils.go
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamSadek committed May 14, 2024
1 parent c556426 commit 346348c
Showing 1 changed file with 47 additions and 16 deletions.
63 changes: 47 additions & 16 deletions internal/commands/loglevel/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@ package loglevel

import (
"context"
"fmt"
"sort"
"path/filepath"

apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"

libsveltosv1alpha1 "github.com/projectsveltos/libsveltos/api/v1alpha1"
"github.com/projectsveltos/sveltosctl/internal/utils"
)

type componentConfiguration struct {
Expand All @@ -41,57 +46,83 @@ func (c byComponent) Less(i, j int) bool {
return c[i].component < c[j].component
}

func collectLogLevelConfiguration(ctx context.Context, namespace, clusterName string) ([]*componentConfiguration, error) {
// configures the Kubernetes client to target the correct cluster based on namespace and clusterName.
// func ConfigureClient(ctx context.Context, namespace, clusterName string) (*kubernetes.Clientset, error) {
// kubeconfigPath := filepath.Join(homedir.HomeDir(), ".kube", "config")
// config, err := clientcmd.LoadFromFile(kubeconfigPath)
// if err != nil {
// return nil, fmt.Errorf("failed to load kubeconfig from %s: %v", kubeconfigPath, err)
// }

// // use it to change the context if clusterName is specified
// if clusterName != "" {
// contextName := fmt.Sprintf("%s-context", clusterName)
// if _, exists := config.Contexts[contextName]; !exists {
// return nil, fmt.Errorf("no context found for the specified cluster name: %s", clusterName)
// }
// config.CurrentContext = contextName
// }

// // build a rest.Config from the kubeconfig and the overridden current context.
// restConfig, err := clientcmd.NewDefaultClientConfig(*config, &clientcmd.ConfigOverrides{CurrentContext: config.CurrentContext}).ClientConfig()
// if err != nil {
// return nil, fmt.Errorf("failed to create Kubernetes REST client configuration: %v", err)
// }

// // create the Kubernetes client using the configured REST config
// clientset, err := kubernetes.NewForConfig(restConfig)
// if err != nil {
// return nil, fmt.Errorf("failed to create Kubernetes clientset: %v", err)
// }

// return clientset, nil
// }

func collectLogLevelConfiguration(ctx context.Context, namespace, clusterName string) ([]*componentConfiguration, string, string, error) {
instance := utils.GetAccessInstance()

dc, err := instance.GetDebuggingConfiguration(ctx, namespace, clusterName)
if err != nil {
if apierrors.IsNotFound(err) {
return make([]*componentConfiguration, 0), nil
}
return nil, err
return nil, namespace, clusterName, err
}

configurationSettings := make([]*componentConfiguration, len(dc.Spec.Configuration))

for i, c := range dc.Spec.Configuration {
configurationSettings[i] = &componentConfiguration{
component: c.Component,
logSeverity: c.LogLevel,
}
}

// Sort this by component name first. Component/node is higher priority than Component
sort.Sort(byComponent(configurationSettings))

return configurationSettings, nil
return configurationSettings, namespace, clusterName, nil
}

func updateLogLevelConfiguration(
ctx context.Context,
namespace, clusterName string,
spec []libsveltosv1alpha1.ComponentConfiguration,
) error {
instance := utils.GetAccessInstance()
) (string, string, error) {

instance := utils.GetAccessInstance()
dc, err := instance.GetDebuggingConfiguration(ctx, namespace, clusterName)
if err != nil {
if apierrors.IsNotFound(err) {
dc = &libsveltosv1alpha1.DebuggingConfiguration{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: clusterName,
Name: clusterName,
},
}
} else {
return err
return namespace, clusterName, err
}
}

Check failure on line 121 in internal/commands/loglevel/utils.go

View workflow job for this annotation

GitHub Actions / build-ut

not enough arguments in call to instance.UpdateDebuggingConfiguration

Check failure on line 121 in internal/commands/loglevel/utils.go

View workflow job for this annotation

GitHub Actions / build-static-test

not enough arguments in call to instance.UpdateDebuggingConfiguration
dc.Spec = libsveltosv1alpha1.DebuggingConfigurationSpec{
Configuration: spec,
}

return instance.UpdateDebuggingConfiguration(ctx, dc)
err = instance.UpdateDebuggingConfiguration(ctx, dc)
return namespace, clusterName, err
}

0 comments on commit 346348c

Please sign in to comment.