Skip to content

Commit

Permalink
Introduce new command to generate kubeconfig
Browse files Browse the repository at this point in the history
```
sveltosctl generate kubeconfig --help
Usage:
  sveltosctl generate kubeconfig [options] [--namespace=<name>] [--serviceaccount=<name>] [--create] [--verbose]

     --namespace=<name>      The namespace of the ServiceAccount. If not specified, projectsveltos namespace will be used.
     --serviceaccount=<name> The name of the ServiceAccount. If not specified, projectsveltos will be used.
     --create                If a ServiceAccount with enough permissions is already present, do not set this flag.
                             Sveltos will generate a Kubeconfig associated to that ServiceAccount.
                             If a ServiceAccount with cluster admin permissions needs to be created, use this option.
                             When this option is set, this command will create necessary resources:
                             1. namespace if not existing already
                             2. serviceAccount if not existing already
                             3. ClusterRole with cluster admin permission
                             4. ClusterRoleBinding granting the serviceAccount cluster admin permissions
                             5. TokenRequest for the ServiceAccount

Options:
  -h --help                  Show this screen.
     --verbose               Verbose mode. Print each step.

Description:
  The generate kubeconfig command will generate a Kubeconfig that can later on be used to register the cluster.
```
  • Loading branch information
mgianluc committed Apr 11, 2024
1 parent 9f19b85 commit 9883e1f
Show file tree
Hide file tree
Showing 10 changed files with 566 additions and 542 deletions.
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

0 comments on commit 9883e1f

Please sign in to comment.