Skip to content

Commit 5e4bcaf

Browse files
committed
refactored the nomos commands for better separation of concerns
1 parent 8b47860 commit 5e4bcaf

File tree

24 files changed

+948
-432
lines changed

24 files changed

+948
-432
lines changed

cmd/nomos/bugreport/bugreport.go

Lines changed: 0 additions & 79 deletions
This file was deleted.

cmd/nomos/bugreport/cmd.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package bugreport
16+
17+
import (
18+
"fmt"
19+
20+
"github.com/spf13/cobra"
21+
"kpt.dev/configsync/cmd/nomos/flags"
22+
"kpt.dev/configsync/pkg/api/configmanagement"
23+
)
24+
25+
// Cmd retrieves readers for all relevant nomos container logs and cluster state commands and writes them to a zip file
26+
var Cmd = &cobra.Command{
27+
Use: "bugreport",
28+
Short: fmt.Sprintf("Generates a zip file of relevant %v debug information.", configmanagement.CLIName),
29+
Long: "Generates a zip file in your current directory containing an aggregate of the logs and cluster state for debugging purposes.",
30+
RunE: func(cmd *cobra.Command, _ []string) error {
31+
// Don't show usage on error, as argument validation passed.
32+
cmd.SilenceUsage = true
33+
34+
// Create execution parameters from parsed flags
35+
params := ExecParams{
36+
ClientTimeout: flags.ClientTimeout,
37+
}
38+
39+
// Execute the bugreport command logic
40+
return ExecuteBugreport(cmd.Context(), params)
41+
},
42+
}
43+
44+
func init() {
45+
// Initialize flags for the bugreport command
46+
// This separation keeps flag definitions isolated from command execution logic
47+
flags.AddClientTimeout(Cmd)
48+
}

cmd/nomos/bugreport/exec.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package bugreport
16+
17+
import (
18+
"context"
19+
"fmt"
20+
"time"
21+
22+
"k8s.io/client-go/kubernetes"
23+
"kpt.dev/configsync/pkg/bugreport"
24+
"kpt.dev/configsync/pkg/client/restconfig"
25+
"sigs.k8s.io/controller-runtime/pkg/client"
26+
)
27+
28+
// ExecParams contains all parameters needed to execute the bugreport command
29+
// This struct is completely independent of cobra command structures
30+
type ExecParams struct {
31+
ClientTimeout time.Duration
32+
}
33+
34+
// ExecuteBugreport executes the core bugreport command logic without any cobra dependencies
35+
// This function encapsulates all the business logic for the bugreport command
36+
func ExecuteBugreport(ctx context.Context, params ExecParams) error {
37+
38+
cfg, err := restconfig.NewRestConfig(params.ClientTimeout)
39+
if err != nil {
40+
return fmt.Errorf("failed to create rest config: %w", err)
41+
}
42+
cs, err := kubernetes.NewForConfig(cfg)
43+
if err != nil {
44+
return fmt.Errorf("failed to create kubernetes client set: %w", err)
45+
}
46+
c, err := client.New(cfg, client.Options{})
47+
if err != nil {
48+
return fmt.Errorf("failed to create kubernetes client: %w", err)
49+
}
50+
51+
report, err := bugreport.New(ctx, c, cs)
52+
if err != nil {
53+
return fmt.Errorf("failed to initialize bug reporter: %w", err)
54+
}
55+
56+
if err = report.Open(); err != nil {
57+
return err
58+
}
59+
60+
report.WriteRawInZip(report.FetchLogSources(ctx))
61+
report.WriteRawInZip(report.FetchResources(ctx))
62+
report.WriteRawInZip(report.FetchCMSystemPods(ctx))
63+
report.AddNomosStatusToZip(ctx)
64+
report.AddNomosVersionToZip(ctx)
65+
66+
report.Close()
67+
return nil
68+
}

cmd/nomos/flags/flags.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
// Package flags contains flags used by several CLI commands.
16+
// The local flags will be found in the command folder in the flags.go file.
1517
package flags
1618

1719
import (
@@ -119,6 +121,12 @@ func AddOutputFormat(cmd *cobra.Command) {
119121
`Output format. Accepts 'yaml' and 'json'.`)
120122
}
121123

124+
// AddClientTimeout adds the --timeout flag
125+
func AddClientTimeout(cmd *cobra.Command) {
126+
cmd.Flags().DurationVar(&ClientTimeout, "timeout", restconfig.DefaultTimeout,
127+
fmt.Sprintf("Timeout for client connections; defaults to %s", restconfig.DefaultTimeout))
128+
}
129+
122130
// AddAPIServerTimeout adds the --api-server-timeout flag
123131
func AddAPIServerTimeout(cmd *cobra.Command) {
124132
cmd.Flags().DurationVar(&APIServerTimeout, "api-server-timeout", restconfig.DefaultTimeout, fmt.Sprintf("Client-side timeout for talking to the API server; defaults to %s", restconfig.DefaultTimeout))

cmd/nomos/hydrate/cmd.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package hydrate
16+
17+
import (
18+
"github.com/spf13/cobra"
19+
"kpt.dev/configsync/cmd/nomos/flags"
20+
"kpt.dev/configsync/pkg/api/configsync"
21+
)
22+
23+
// localFlags holds the hydrate command flags
24+
var localFlags = NewFlags()
25+
26+
func init() {
27+
// Initialize flags for the hydrate command
28+
// This separation keeps flag definitions isolated from command execution logic
29+
localFlags.AddFlags(Cmd)
30+
}
31+
32+
// Cmd is the Cobra object representing the hydrate command.
33+
var Cmd = &cobra.Command{
34+
Use: "hydrate",
35+
Short: "Compiles the local repository to the exact form that would be sent to the APIServer.",
36+
Long: `Compiles the local repository to the exact form that would be sent to the APIServer.
37+
38+
The output directory consists of one directory per declared Cluster, and defaultcluster/ for
39+
clusters without declarations. Each directory holds the full set of configs for a single cluster,
40+
which you could kubectl apply -fR to the cluster, or have Config Sync sync to the cluster.`,
41+
Args: cobra.ExactArgs(0),
42+
RunE: func(cmd *cobra.Command, _ []string) error {
43+
// Don't show usage on error, as argument validation passed.
44+
cmd.SilenceUsage = true
45+
46+
// Create execution parameters from parsed flags
47+
params := ExecParams{
48+
Clusters: flags.Clusters,
49+
Path: flags.Path,
50+
SkipAPIServer: flags.SkipAPIServer,
51+
SourceFormat: configsync.SourceFormat(flags.SourceFormat),
52+
OutputFormat: flags.OutputFormat,
53+
APIServerTimeout: flags.APIServerTimeout,
54+
Flat: localFlags.Flat,
55+
OutPath: localFlags.OutPath,
56+
}
57+
58+
// Execute the hydrate command logic
59+
return ExecuteHydrate(cmd.Context(), params)
60+
},
61+
}

0 commit comments

Comments
 (0)