-
Notifications
You must be signed in to change notification settings - Fork 407
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support yurtadm config command (#1709)
Signed-off-by: Liang Deng <[email protected]>
- Loading branch information
Showing
6 changed files
with
166 additions
and
1 deletion.
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
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,116 @@ | ||
/* | ||
Copyright 2023 The OpenYurt Authors. | ||
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 config | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"path/filepath" | ||
|
||
"github.com/lithammer/dedent" | ||
"github.com/spf13/cobra" | ||
"k8s.io/apimachinery/pkg/util/sets" | ||
|
||
"github.com/openyurtio/openyurt/pkg/projectinfo" | ||
"github.com/openyurtio/openyurt/pkg/util/templates" | ||
"github.com/openyurtio/openyurt/pkg/yurtadm/constants" | ||
"github.com/openyurtio/openyurt/pkg/yurtadm/util" | ||
"github.com/openyurtio/openyurt/pkg/yurtadm/util/edgenode" | ||
) | ||
|
||
// NewCmdConfig returns cobra.Command for "yurtadm config" command | ||
func NewCmdConfig(in io.Reader, out io.Writer, outErr io.Writer) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "config", | ||
Short: "Manage configuration for a openyurt cluster", | ||
Run: util.SubCmdRun(), | ||
} | ||
|
||
cmd.AddCommand(newCmdConfigPrint(out)) | ||
return cmd | ||
} | ||
|
||
// newCmdConfigPrint returns cobra.Command for "yurtadm config print" command | ||
func newCmdConfigPrint(out io.Writer) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "print", | ||
Short: "Print configuration", | ||
Run: util.SubCmdRun(), | ||
} | ||
cmd.AddCommand(newCmdConfigPrintJoinDefaults(out)) | ||
return cmd | ||
} | ||
|
||
// newCmdConfigPrintJoinDefaults returns cobra.Command for "yurtadm config print join-defaults" command | ||
func newCmdConfigPrintJoinDefaults(out io.Writer) *cobra.Command { | ||
return newCmdConfigPrintActionDefaults(out, "join", getDefaultNodeConfigBytes) | ||
} | ||
|
||
func newCmdConfigPrintActionDefaults(out io.Writer, action string, configBytesProc func() (string, error)) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: fmt.Sprintf("%s-defaults", action), | ||
Short: fmt.Sprintf("Print default %s configuration, that can be used for 'yurtadm %s'", action, action), | ||
Long: fmt.Sprintf(dedent.Dedent(` | ||
This command prints objects such as the default %s configuration that is used for 'yurtadm %s'. | ||
Note that sensitive values like the Bootstrap Token fields are replaced with placeholder values like %q in order to pass validation but | ||
not perform the real computation for creating a token. | ||
`), action, action, constants.PlaceholderToken), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return runConfigPrintActionDefaults(out, configBytesProc) | ||
}, | ||
Args: cobra.NoArgs, | ||
} | ||
return cmd | ||
} | ||
|
||
func runConfigPrintActionDefaults(out io.Writer, configBytesProc func() (string, error)) error { | ||
initialConfig, err := configBytesProc() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Fprint(out, initialConfig) | ||
return nil | ||
} | ||
|
||
func getDefaultNodeConfigBytes() (string, error) { | ||
KubeadmJoinDiscoveryFilePath := filepath.Join(constants.KubeletWorkdir, constants.KubeadmJoinDiscoveryFileName) | ||
ignoreErrors := sets.String{} | ||
name, err := edgenode.GetHostname("") | ||
if err != nil { | ||
return "", err | ||
} | ||
ctx := map[string]interface{}{ | ||
"kubeConfigPath": KubeadmJoinDiscoveryFilePath, | ||
"tlsBootstrapToken": constants.PlaceholderToken, | ||
"ignorePreflightErrors": ignoreErrors.List(), | ||
"podInfraContainerImage": constants.PauseImagePath, | ||
"nodeLabels": fmt.Sprintf("%s=true", projectinfo.GetEdgeWorkerLabelKey()), | ||
"criSocket": constants.DefaultDockerCRISocket, | ||
"name": name, | ||
"networkPlugin": "cni", | ||
"apiVersion": "kubeadm.k8s.io/v1beta3", | ||
} | ||
|
||
kubeadmJoinTemplate, err := templates.SubsituteTemplate(constants.KubeadmJoinConf, ctx) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return kubeadmJoinTemplate, 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
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,45 @@ | ||
/* | ||
Copyright 2023 The OpenYurt Authors. | ||
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 util | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/spf13/cobra" | ||
|
||
util "github.com/openyurtio/openyurt/pkg/yurtadm/util/error" | ||
) | ||
|
||
// SubCmdRun returns a function that handles a case where a subcommand must be specified | ||
// Without this callback, if a user runs just the command without a subcommand, | ||
// or with an invalid subcommand, cobra will print usage information, but still exit cleanly. | ||
func SubCmdRun() func(c *cobra.Command, args []string) { | ||
return func(c *cobra.Command, args []string) { | ||
if len(args) > 0 { | ||
util.CheckErr(usageErrorf(c, "invalid subcommand %q", strings.Join(args, " "))) | ||
} | ||
c.Help() | ||
util.CheckErr(util.ErrExit) | ||
} | ||
} | ||
|
||
func usageErrorf(c *cobra.Command, format string, args ...interface{}) error { | ||
msg := fmt.Sprintf(format, args...) | ||
return errors.Errorf("%s\nSee '%s -h' for help and examples", msg, c.CommandPath()) | ||
} |