-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce new command to generate kubeconfig
``` 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
Showing
10 changed files
with
566 additions
and
542 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
Oops, something went wrong.