Skip to content
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
11 changes: 8 additions & 3 deletions internal/commands/loglevel/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ limitations under the License.
package loglevel

var (
ShowLogSettings = showLogSettings
UpdateDebuggingConfiguration = updateDebuggingConfiguration
UnsetDebuggingConfiguration = unsetDebuggingConfiguration
ShowLogSettings = showLogSettings
UpdateDebuggingConfiguration = updateDebuggingConfiguration
UnsetDebuggingConfiguration = unsetDebuggingConfiguration
UpdateDebuggingConfigurationInManaged = updateDebuggingConfigurationInManaged
CollectLogLevelConfigurationFromClient = collectLogLevelConfigurationFromClient
UpdateLogLevelConfigurationWithClient = updateLogLevelConfigurationWithClient
ShowLogSettingsInManaged = showLogSettingsInManaged
UnsetDebuggingConfigurationInManaged = unsetDebuggingConfigurationInManaged
)
108 changes: 99 additions & 9 deletions internal/commands/loglevel/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,21 @@
"fmt"
"strings"

docopt "github.com/docopt/docopt-go"
"github.com/docopt/docopt-go"
"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"

libsveltosv1beta1 "github.com/projectsveltos/libsveltos/api/v1beta1"
"github.com/projectsveltos/libsveltos/lib/clusterproxy"
"github.com/projectsveltos/sveltosctl/internal/utils"
)

func updateDebuggingConfiguration(ctx context.Context, logSeverity libsveltosv1beta1.LogLevel,
component string) error {

cc, err := collectLogLevelConfiguration(ctx)
if err != nil {
return nil
return nil // Question: this should return 'err' instead of nil?
}

found := false
Expand All @@ -44,7 +48,6 @@
LogLevel: logSeverity,
}
found = true
break
} else {
spec[i] = libsveltosv1beta1.ComponentConfiguration{
Component: c.component,
Expand All @@ -65,19 +68,85 @@
return updateLogLevelConfiguration(ctx, spec)
}

func updateDebuggingConfigurationInManaged(ctx context.Context, logSeverity libsveltosv1beta1.LogLevel,
component, namespace, clusterName string, clusterType libsveltosv1beta1.ClusterType) error {

// Get client for the managed cluster using libsveltos the clusterproxy
cluster := &corev1.ObjectReference{
Namespace: namespace,
Name: clusterName,
}

// Set the appropriate Kind and APIVersion based on the clusterType
if clusterType == libsveltosv1beta1.ClusterTypeCapi {
cluster.Kind = "Cluster"

Check failure on line 82 in internal/commands/loglevel/set.go

View workflow job for this annotation

GitHub Actions / build-static-test

string `Cluster` has 3 occurrences, make it a constant (goconst)
cluster.APIVersion = "cluster.x-k8s.io/v1beta1"

Check failure on line 83 in internal/commands/loglevel/set.go

View workflow job for this annotation

GitHub Actions / build-static-test

string `cluster.x-k8s.io/v1beta1` has 3 occurrences, make it a constant (goconst)
} else {
cluster.Kind = "SveltosCluster"

Check failure on line 85 in internal/commands/loglevel/set.go

View workflow job for this annotation

GitHub Actions / build-static-test

string `SveltosCluster` has 3 occurrences, make it a constant (goconst)
cluster.APIVersion = "lib.projectsveltos.io/v1beta1"

Check failure on line 86 in internal/commands/loglevel/set.go

View workflow job for this annotation

GitHub Actions / build-static-test

string `lib.projectsveltos.io/v1beta1` has 3 occurrences, make it a constant (goconst)
}

logger := logr.Discard() // Use a discard logger for simplicity
managedClient, err := clusterproxy.GetKubernetesClient(ctx, utils.GetAccessInstance().GetClient(),
cluster.Namespace, cluster.Name, "", "", clusterType, logger)
if err != nil {
return fmt.Errorf("failed to get client for managed cluster %s/%s: %w", namespace, clusterName, err)
}

// Get existing configuration from the managed cluster
cc, err := collectLogLevelConfigurationFromClient(ctx, managedClient)
if err != nil {
return err
}

found := false
spec := make([]libsveltosv1beta1.ComponentConfiguration, len(cc))

for i, c := range cc {
if string(c.component) == component {
spec[i] = libsveltosv1beta1.ComponentConfiguration{
Component: c.component,
LogLevel: logSeverity,
}
found = true
} else {
spec[i] = libsveltosv1beta1.ComponentConfiguration{
Component: c.component,
LogLevel: c.logSeverity,
}
}
}

if !found {
spec = append(spec,
libsveltosv1beta1.ComponentConfiguration{
Component: libsveltosv1beta1.Component(component),
LogLevel: logSeverity,
},
)
}

return updateLogLevelConfigurationWithClient(ctx, managedClient, spec)
}

// Set displays/changes log verbosity for a given component
func Set(ctx context.Context, args []string) error {
doc := `Usage:
sveltosctl log-level set --component=<name> (--info|--debug|--verbose)
sveltosctl log-level set --component=<name> (--info|--debug|--verbose) [--namespace=<namespace>] [--clusterName=<cluster-name>] [--clusterType=<cluster-type>]
Options:
-h --help Show this screen.
--component=<name> Name of the component for which log severity is being set.
--info Set log severity to info.
--debug Set log severity to debug.
--verbose Set log severity to verbose.
-h --help Show this screen.
--component=<name> Name of the component for which log severity is being set.
--info Set log severity to info.
--debug Set log severity to debug.
--verbose Set log severity to verbose.
--namespace=<namespace> (Optional) Namespace where the managed cluster is located.
--clusterName=<cluster-name> (Optional) Name of the managed cluster.
--clusterType=<cluster-type> (Optional) Type of cluster: Capi or Sveltos.

Description:
The log-level set command set log severity for the specified component.
If namespace and clusterName are provided, the log level is set in the managed cluster.
Otherwise, it is set in the management cluster.
`
parsedArgs, err := docopt.ParseArgs(doc, nil, "1.0")
if err != nil {
Expand All @@ -95,6 +164,24 @@
component = passedComponent.(string)
}

namespace := ""
if passedNamespace := parsedArgs["--namespace"]; passedNamespace != nil {
namespace = passedNamespace.(string)
}

clusterName := ""
if passedClusterName := parsedArgs["--clusterName"]; passedClusterName != nil {
clusterName = passedClusterName.(string)
}

clusterType := libsveltosv1beta1.ClusterTypeCapi // default
if passedClusterType := parsedArgs["--clusterType"]; passedClusterType != nil {
clusterTypeStr := passedClusterType.(string)
if clusterTypeStr == "Sveltos" {

Check failure on line 180 in internal/commands/loglevel/set.go

View workflow job for this annotation

GitHub Actions / build-static-test

string `Sveltos` has 3 occurrences, make it a constant (goconst)
clusterType = libsveltosv1beta1.ClusterTypeSveltos
}
}

info := parsedArgs["--info"].(bool)
debug := parsedArgs["--debug"].(bool)
verbose := parsedArgs["--verbose"].(bool)
Expand All @@ -108,5 +195,8 @@
logSeverity = libsveltosv1beta1.LogLevelVerbose
}

if namespace != "" && clusterName != "" {
return updateDebuggingConfigurationInManaged(ctx, logSeverity, component, namespace, clusterName, clusterType)
}
return updateDebuggingConfiguration(ctx, logSeverity, component)
}
108 changes: 108 additions & 0 deletions internal/commands/loglevel/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client/fake"

libsveltosv1beta1 "github.com/projectsveltos/libsveltos/api/v1beta1"
Expand Down Expand Up @@ -59,4 +60,111 @@ var _ = Describe("Set", func() {
Expect(currentDC.Spec.Configuration[0].Component).To(Equal(libsveltosv1beta1.ComponentAddonManager))
Expect(currentDC.Spec.Configuration[0].LogLevel).To(Equal(libsveltosv1beta1.LogLevelInfo))
})

It("collectLogLevelConfigurationFromClient returns empty configuration when DebuggingConfiguration does not exist", func() {
scheme, err := utils.GetScheme()
Expect(err).To(BeNil())
c := fake.NewClientBuilder().WithScheme(scheme).Build()

configs, err := loglevel.CollectLogLevelConfigurationFromClient(context.TODO(), c)
Expect(err).To(BeNil())
Expect(configs).ToNot(BeNil())
Expect(len(configs)).To(Equal(0))
})

It("collectLogLevelConfigurationFromClient returns existing configuration", func() {
scheme, err := utils.GetScheme()
Expect(err).To(BeNil())

// Create a DebuggingConfiguration with some initial settings
dc := &libsveltosv1beta1.DebuggingConfiguration{
ObjectMeta: metav1.ObjectMeta{
Name: "default",
},
Spec: libsveltosv1beta1.DebuggingConfigurationSpec{
Configuration: []libsveltosv1beta1.ComponentConfiguration{
{
Component: libsveltosv1beta1.ComponentClassifier,
LogLevel: libsveltosv1beta1.LogLevelDebug,
},
{
Component: libsveltosv1beta1.ComponentAddonManager,
LogLevel: libsveltosv1beta1.LogLevelInfo,
},
},
},
}

c := fake.NewClientBuilder().WithScheme(scheme).WithObjects(dc).Build()

configs, err := loglevel.CollectLogLevelConfigurationFromClient(context.TODO(), c)
Expect(err).To(BeNil())
Expect(configs).ToNot(BeNil())
Expect(len(configs)).To(Equal(2))
})

It("updateLogLevelConfigurationWithClient creates new DebuggingConfiguration when it does not exist", func() {
scheme, err := utils.GetScheme()
Expect(err).To(BeNil())
c := fake.NewClientBuilder().WithScheme(scheme).Build()

spec := []libsveltosv1beta1.ComponentConfiguration{
{
Component: libsveltosv1beta1.ComponentClassifier,
LogLevel: libsveltosv1beta1.LogLevelDebug,
},
}

err = loglevel.UpdateLogLevelConfigurationWithClient(context.TODO(), c, spec)
Expect(err).To(BeNil())

// Verify the configuration was created
configs, err := loglevel.CollectLogLevelConfigurationFromClient(context.TODO(), c)
Expect(err).To(BeNil())
Expect(configs).ToNot(BeNil())
Expect(len(configs)).To(Equal(1))
})

It("updateLogLevelConfigurationWithClient updates existing DebuggingConfiguration", func() {
scheme, err := utils.GetScheme()
Expect(err).To(BeNil())

// Create a DebuggingConfiguration with initial settings
dc := &libsveltosv1beta1.DebuggingConfiguration{
ObjectMeta: metav1.ObjectMeta{
Name: "default",
},
Spec: libsveltosv1beta1.DebuggingConfigurationSpec{
Configuration: []libsveltosv1beta1.ComponentConfiguration{
{
Component: libsveltosv1beta1.ComponentClassifier,
LogLevel: libsveltosv1beta1.LogLevelInfo,
},
},
},
}

c := fake.NewClientBuilder().WithScheme(scheme).WithObjects(dc).Build()

// Update the configuration
spec := []libsveltosv1beta1.ComponentConfiguration{
{
Component: libsveltosv1beta1.ComponentClassifier,
LogLevel: libsveltosv1beta1.LogLevelDebug,
},
{
Component: libsveltosv1beta1.ComponentAddonManager,
LogLevel: libsveltosv1beta1.LogLevelVerbose,
},
}

err = loglevel.UpdateLogLevelConfigurationWithClient(context.TODO(), c, spec)
Expect(err).To(BeNil())

// Verify the configuration was updated
configs, err := loglevel.CollectLogLevelConfigurationFromClient(context.TODO(), c)
Expect(err).To(BeNil())
Expect(configs).ToNot(BeNil())
Expect(len(configs)).To(Equal(2))
})
})
Loading
Loading