diff --git a/internal/commands/loglevel/export_test.go b/internal/commands/loglevel/export_test.go index 96ee7aa..e013c66 100644 --- a/internal/commands/loglevel/export_test.go +++ b/internal/commands/loglevel/export_test.go @@ -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 ) diff --git a/internal/commands/loglevel/set.go b/internal/commands/loglevel/set.go index b125a0c..72cd59d 100644 --- a/internal/commands/loglevel/set.go +++ b/internal/commands/loglevel/set.go @@ -21,9 +21,13 @@ import ( "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, @@ -31,7 +35,7 @@ func updateDebuggingConfiguration(ctx context.Context, logSeverity libsveltosv1b cc, err := collectLogLevelConfiguration(ctx) if err != nil { - return nil + return nil // Question: this should return 'err' instead of nil? } found := false @@ -44,7 +48,6 @@ func updateDebuggingConfiguration(ctx context.Context, logSeverity libsveltosv1b LogLevel: logSeverity, } found = true - break } else { spec[i] = libsveltosv1beta1.ComponentConfiguration{ Component: c.component, @@ -65,19 +68,85 @@ func updateDebuggingConfiguration(ctx context.Context, logSeverity libsveltosv1b 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" + cluster.APIVersion = "cluster.x-k8s.io/v1beta1" + } else { + cluster.Kind = "SveltosCluster" + cluster.APIVersion = "lib.projectsveltos.io/v1beta1" + } + + 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= (--info|--debug|--verbose) + sveltosctl log-level set --component= (--info|--debug|--verbose) [--namespace=] [--clusterName=] [--clusterType=] Options: - -h --help Show this screen. - --component= 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 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= (Optional) Namespace where the managed cluster is located. + --clusterName= (Optional) Name of the managed cluster. + --clusterType= (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 { @@ -95,6 +164,24 @@ Description: 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" { + clusterType = libsveltosv1beta1.ClusterTypeSveltos + } + } + info := parsedArgs["--info"].(bool) debug := parsedArgs["--debug"].(bool) verbose := parsedArgs["--verbose"].(bool) @@ -108,5 +195,8 @@ Description: logSeverity = libsveltosv1beta1.LogLevelVerbose } + if namespace != "" && clusterName != "" { + return updateDebuggingConfigurationInManaged(ctx, logSeverity, component, namespace, clusterName, clusterType) + } return updateDebuggingConfiguration(ctx, logSeverity, component) } diff --git a/internal/commands/loglevel/set_test.go b/internal/commands/loglevel/set_test.go index 83a875a..daa1926 100644 --- a/internal/commands/loglevel/set_test.go +++ b/internal/commands/loglevel/set_test.go @@ -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" @@ -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)) + }) }) diff --git a/internal/commands/loglevel/show.go b/internal/commands/loglevel/show.go index c45caf7..d6619b9 100644 --- a/internal/commands/loglevel/show.go +++ b/internal/commands/loglevel/show.go @@ -22,8 +22,14 @@ import ( "os" "strings" - docopt "github.com/docopt/docopt-go" + "github.com/docopt/docopt-go" + "github.com/go-logr/logr" "github.com/olekukonko/tablewriter" + 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 showLogSettings(ctx context.Context) error { @@ -49,15 +55,66 @@ func showLogSettings(ctx context.Context) error { return nil } +func showLogSettingsInManaged(ctx context.Context, namespace, clusterName string, clusterType libsveltosv1beta1.ClusterType) error { + // Get client for the managed cluster using libsveltos 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" + cluster.APIVersion = "cluster.x-k8s.io/v1beta1" + } else { + cluster.Kind = "SveltosCluster" + cluster.APIVersion = "lib.projectsveltos.io/v1beta1" + } + + 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 configuration from the managed cluster + componentConfiguration, err := collectLogLevelConfigurationFromClient(ctx, managedClient) + if err != nil { + return err + } + + table := tablewriter.NewWriter(os.Stdout) + table.SetHeader([]string{"COMPONENT", "VERBOSITY"}) + genRow := func(component, verbosity string) []string { + return []string{ + component, + verbosity, + } + } + + for _, c := range componentConfiguration { + table.Append(genRow(string(c.component), string(c.logSeverity))) + } + + table.Render() + return nil +} + // Show displays information about log verbosity (if set) func Show(ctx context.Context, args []string) error { doc := `Usage: - sveltosctl log-level show + sveltosctl log-level show [--namespace=] [--clusterName=] [--clusterType=] Options: - -h --help Show this screen. + -h --help Show this screen. + --namespace= (Optional) Namespace where the managed cluster is located. + --clusterName= (Optional) Name of the managed cluster. + --clusterType= (Optional) Type of cluster: Capi or Sveltos. Description: The log-level show command shows information about current log verbosity. + If namespace and clusterName are provided, the log levels are shown from the managed cluster. + Otherwise, they are shown from the management cluster. ` parsedArgs, err := docopt.ParseArgs(doc, nil, "1.0") if err != nil { @@ -70,5 +127,26 @@ Description: return nil } + 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" { + clusterType = libsveltosv1beta1.ClusterTypeSveltos + } + } + + if namespace != "" && clusterName != "" { + return showLogSettingsInManaged(ctx, namespace, clusterName, clusterType) + } return showLogSettings(ctx) } diff --git a/internal/commands/loglevel/show_test.go b/internal/commands/loglevel/show_test.go index 7cb04af..5bc8ebf 100644 --- a/internal/commands/loglevel/show_test.go +++ b/internal/commands/loglevel/show_test.go @@ -25,6 +25,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" "sigs.k8s.io/controller-runtime/pkg/client/fake" @@ -82,4 +83,32 @@ var _ = Describe("Show", func() { Expect(found).To(BeTrue()) os.Stdout = old }) + + It("showLogSettingsInManaged displays log level settings from managed cluster", func() { + scheme, err := utils.GetScheme() + Expect(err).To(BeNil()) + + // Create a DebuggingConfiguration for the managed cluster + dc := &libsveltosv1beta1.DebuggingConfiguration{ + ObjectMeta: metav1.ObjectMeta{ + Name: "default", + }, + Spec: libsveltosv1beta1.DebuggingConfigurationSpec{ + Configuration: []libsveltosv1beta1.ComponentConfiguration{ + { + Component: libsveltosv1beta1.ComponentClassifier, + LogLevel: libsveltosv1beta1.LogLevelInfo, + }, + }, + }, + } + + managedClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(dc).Build() + + // Test the helper function directly with the managed client + componentConfiguration, err := loglevel.CollectLogLevelConfigurationFromClient(context.TODO(), managedClient) + Expect(err).To(BeNil()) + Expect(componentConfiguration).ToNot(BeNil()) + Expect(len(componentConfiguration)).To(Equal(1)) + }) }) diff --git a/internal/commands/loglevel/unset.go b/internal/commands/loglevel/unset.go index b8a2cad..4e75db6 100644 --- a/internal/commands/loglevel/unset.go +++ b/internal/commands/loglevel/unset.go @@ -21,9 +21,13 @@ import ( "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 unsetDebuggingConfiguration(ctx context.Context, component string) error { @@ -55,16 +59,73 @@ func unsetDebuggingConfiguration(ctx context.Context, component string) error { return nil } +func unsetDebuggingConfigurationInManaged(ctx context.Context, component, namespace, clusterName string, clusterType libsveltosv1beta1.ClusterType) error { + // Get client for the managed cluster using libsveltos 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" + cluster.APIVersion = "cluster.x-k8s.io/v1beta1" + } else { + cluster.Kind = "SveltosCluster" + cluster.APIVersion = "lib.projectsveltos.io/v1beta1" + } + + 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 nil + } + + found := false + spec := make([]libsveltosv1beta1.ComponentConfiguration, 0) + + for _, c := range cc { + if string(c.component) == component { + found = true + continue + } else { + spec = append(spec, + libsveltosv1beta1.ComponentConfiguration{ + Component: c.component, + LogLevel: c.logSeverity, + }, + ) + } + } + + if found { + return updateLogLevelConfigurationWithClient(ctx, managedClient, spec) + } + return nil +} + // Unset resets log verbosity for a given component func Unset(ctx context.Context, args []string) error { doc := `Usage: - sveltosctl log-level unset --component= + sveltosctl log-level unset --component= [--namespace=] [--clusterName=] [--clusterType=] Options: - -h --help Show this screen. - --component= Name of the component for which log severity is being set. + -h --help Show this screen. + --component= Name of the component for which log severity is being unset. + --namespace= (Optional) Namespace where the managed cluster is located. + --clusterName= (Optional) Name of the managed cluster. + --clusterType= (Optional) Type of cluster: Capi or Sveltos. Description: - The log-level set command set log severity for the specified component. + The log-level unset command unsets log severity for the specified component. + If namespace and clusterName are provided, the log level is unset in the managed cluster. + Otherwise, it is unset in the management cluster. ` parsedArgs, err := docopt.ParseArgs(doc, nil, "1.0") if err != nil { @@ -82,5 +143,26 @@ Description: 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" { + clusterType = libsveltosv1beta1.ClusterTypeSveltos + } + } + + if namespace != "" && clusterName != "" { + return unsetDebuggingConfigurationInManaged(ctx, component, namespace, clusterName, clusterType) + } return unsetDebuggingConfiguration(ctx, component) } diff --git a/internal/commands/loglevel/unset_test.go b/internal/commands/loglevel/unset_test.go index 19d12d4..4af04b3 100644 --- a/internal/commands/loglevel/unset_test.go +++ b/internal/commands/loglevel/unset_test.go @@ -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" "sigs.k8s.io/controller-runtime/pkg/client/fake" @@ -58,4 +59,47 @@ var _ = Describe("Unset", func() { Expect(currentDC.Spec.Configuration[0].LogLevel).To(Equal(libsveltosv1beta1.LogLevelInfo)) }) + + It("unsetDebuggingConfigurationInManaged removes log level settings from managed cluster", func() { + scheme, err := utils.GetScheme() + Expect(err).To(BeNil()) + + // Create a DebuggingConfiguration with multiple components for the managed cluster + 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, + }, + }, + }, + } + + managedClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(dc).Build() + + // Test removing one component using the helper function + spec := []libsveltosv1beta1.ComponentConfiguration{ + { + Component: libsveltosv1beta1.ComponentAddonManager, + LogLevel: libsveltosv1beta1.LogLevelInfo, + }, + } + + err = loglevel.UpdateLogLevelConfigurationWithClient(context.TODO(), managedClient, spec) + Expect(err).To(BeNil()) + + // Verify the configuration was updated (one component removed) + configs, err := loglevel.CollectLogLevelConfigurationFromClient(context.TODO(), managedClient) + Expect(err).To(BeNil()) + Expect(configs).ToNot(BeNil()) + Expect(len(configs)).To(Equal(1)) + }) }) diff --git a/internal/commands/loglevel/utils.go b/internal/commands/loglevel/utils.go index 3058208..163716c 100644 --- a/internal/commands/loglevel/utils.go +++ b/internal/commands/loglevel/utils.go @@ -22,6 +22,7 @@ import ( apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "sigs.k8s.io/controller-runtime/pkg/client" libsveltosv1beta1 "github.com/projectsveltos/libsveltos/api/v1beta1" "github.com/projectsveltos/sveltosctl/internal/utils" @@ -93,3 +94,60 @@ func updateLogLevelConfiguration( return instance.UpdateDebuggingConfiguration(ctx, dc) } + +// Collects configuration from a specific client +func collectLogLevelConfigurationFromClient(ctx context.Context, c client.Client) ([]*componentConfiguration, error) { + dc := &libsveltosv1beta1.DebuggingConfiguration{} + err := c.Get(ctx, client.ObjectKey{Name: "default"}, dc) + if err != nil { + if apierrors.IsNotFound(err) { + return make([]*componentConfiguration, 0), nil + } + return nil, err + } + + configurationSettings := make([]*componentConfiguration, len(dc.Spec.Configuration)) + + for i, c := range dc.Spec.Configuration { + configurationSettings[i] = &componentConfiguration{ + component: c.Component, + logSeverity: c.LogLevel, + } + } + + // Sorting this by component name first. Component/node is higher priority than Component + sort.Sort(byComponent(configurationSettings)) + + return configurationSettings, nil +} + +// Updates configuration using a specific client +func updateLogLevelConfigurationWithClient( + ctx context.Context, + c client.Client, + spec []libsveltosv1beta1.ComponentConfiguration, +) error { + dc := &libsveltosv1beta1.DebuggingConfiguration{} + err := c.Get(ctx, client.ObjectKey{Name: "default"}, dc) + if err != nil { + if apierrors.IsNotFound(err) { + dc = &libsveltosv1beta1.DebuggingConfiguration{ + ObjectMeta: metav1.ObjectMeta{ + Name: "default", + }, + } + dc.Spec = libsveltosv1beta1.DebuggingConfigurationSpec{ + Configuration: spec, + } + return c.Create(ctx, dc) + } else { + return err + } + } + + dc.Spec = libsveltosv1beta1.DebuggingConfigurationSpec{ + Configuration: spec, + } + + return c.Update(ctx, dc) +}