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

Introduce new command to generate kubeconfig #226

Merged
merged 1 commit into from
Apr 11, 2024
Merged
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
4 changes: 4 additions & 0 deletions cmd/sveltosctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ func main() {
snapshot Displays collected snaphost. Visualize diffs between two collected snapshots.
techsupport Displays collected techsupport.
register Onboard an existing non CAPI cluster by creating all necessary internal resources.
generate Generates a Kubeconfig that can later be used to register a cluster.
Run this command with sveltosctl pointing to the cluster you want Sveltos to manage.
log-level Allows changing the log verbosity.
version Display the version of sveltosctl.

Expand Down Expand Up @@ -105,6 +107,8 @@ Description:
err = commands.Techsupport(ctx, args, logger)
case "register":
err = commands.RegisterCluster(ctx, args, logger)
case "generate":
err = commands.Generate(ctx, args, logger)
case "log-level":
err = commands.LogLevel(ctx, args, logger)
case "version":
Expand Down
78 changes: 78 additions & 0 deletions internal/commands/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
Copyright 2024. projectsveltos.io. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package commands

import (
"context"
"errors"
"fmt"
"os"
"strings"

docopt "github.com/docopt/docopt-go"
"github.com/go-logr/logr"

logs "github.com/projectsveltos/libsveltos/lib/logsettings"
"github.com/projectsveltos/sveltosctl/internal/commands/generate"
)

// Generate takes care generating a kubeconfig
func Generate(ctx context.Context, args []string, logger logr.Logger) error {
doc := `Usage:
sveltosctl generate <command> [<args>...]

kubeconfig Generates a Kubeconfig. The generated Kubeconfig can then be used to register a cluster.
Run this command while pointing to the managed cluster.

Options:
-h --help Show this screen.

Description:
See 'sveltosctl generate kubeconfig --help' to read about a specific subcommand.
`

parser := &docopt.Parser{
HelpHandler: docopt.PrintHelpAndExit,
OptionsFirst: true,
SkipHelpFlags: false,
}

opts, err := parser.ParseArgs(doc, nil, "1.0")
if err != nil {
var userError docopt.UserError
if errors.As(err, &userError) {
logger.V(logs.LogInfo).Info(fmt.Sprintf(
"Invalid option: 'sveltosctl %s'. Use flag '--help' to read about a specific subcommand.\n",
strings.Join(os.Args[1:], " "),
))
}
os.Exit(1)
}

command := opts["<command>"].(string)
arguments := append([]string{"logLevel", command}, opts["<args>"].([]string)...)

switch command {
case "kubeconfig":
return generate.GenerateKubeconfig(ctx, arguments, logger)
default:
//nolint: forbidigo // print doc
fmt.Println(doc)
}

return nil
}
27 changes: 27 additions & 0 deletions internal/commands/generate/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
Copyright 2024. projectsveltos.io. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package generate

const (
Projectsveltos = projectsveltos
)

var (
CreateNamespace = createNamespace
CreateClusterRole = createClusterRole
CreateClusterRoleBinding = createClusterRoleBinding
)
Loading