chore: cleanup code

chore: cleanup code

chore: cleanup code

chore: cleanup code
This commit is contained in:
Cookiery 2024-10-04 12:17:26 +08:00
parent 9ffefe3da2
commit 4ce7f8547d
10 changed files with 22 additions and 21 deletions

View File

@ -36,10 +36,10 @@ func LoadConfigFromFile(logger klog.Logger, file string) (*config.KubeSchedulerC
return nil, err
}
return loadConfig(logger, data)
return loadConfig(data)
}
func loadConfig(logger klog.Logger, data []byte) (*config.KubeSchedulerConfiguration, error) {
func loadConfig(data []byte) (*config.KubeSchedulerConfiguration, error) {
// The UniversalDecoder runs defaulting and returns the internal type by default.
obj, gvk, err := scheme.Codecs.UniversalDecoder().Decode(data, nil, nil)
if err != nil {

View File

@ -18,6 +18,7 @@ package options
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
@ -103,13 +104,13 @@ func TestLoadConfigFromFile(t *testing.T) {
{
name: "Scheduler config with decode error",
path: decodeErrConfigFile,
expectedErr: fmt.Errorf(apiVersionMissing),
expectedErr: errors.New(apiVersionMissing),
expectedConfig: nil,
},
{
name: "Scheduler config version too old",
path: versionTooOldConfigFile,
expectedErr: fmt.Errorf(apiVersionTooOld),
expectedErr: errors.New(apiVersionTooOld),
expectedConfig: nil,
},
}

View File

@ -175,7 +175,7 @@ func SetDefaults_FileDiscovery(obj *FileDiscovery) {
// layer, but set to a random value later at runtime if not set before.
func SetDefaults_BootstrapTokens(obj *InitConfiguration) {
if obj.BootstrapTokens == nil || len(obj.BootstrapTokens) == 0 {
if len(obj.BootstrapTokens) == 0 {
obj.BootstrapTokens = []bootstraptokenv1.BootstrapToken{{}}
}

View File

@ -183,7 +183,7 @@ func SetDefaults_FileDiscovery(obj *FileDiscovery) {
// layer, but set to a random value later at runtime if not set before.
func SetDefaults_BootstrapTokens(obj *InitConfiguration) {
if obj.BootstrapTokens == nil || len(obj.BootstrapTokens) == 0 {
if len(obj.BootstrapTokens) == 0 {
obj.BootstrapTokens = []bootstraptokenv1.BootstrapToken{{}}
}

View File

@ -29,7 +29,7 @@ func TestNewCmdCompletion(t *testing.T) {
var out bytes.Buffer
shells := GetSupportedShells()
if len(shells) == 0 {
t.Errorf(shellsError)
t.Error(shellsError)
}
// test newCmdCompletion with a valid shell.
// use a dummy parent command as newCmdCompletion needs it.
@ -72,7 +72,7 @@ func TestRunCompletion(t *testing.T) {
// test all supported shells
shells := GetSupportedShells()
if len(shells) == 0 {
t.Errorf(shellsError)
t.Error(shellsError)
}
for _, shell := range shells {
test := TestCase{

View File

@ -184,7 +184,9 @@ func runDiff(fs *pflag.FlagSet, flags *diffFlags, args []string, fetchInitConfig
Context: cmdutil.ValueFromFlagsOrConfig(fs, "context-lines", upgradeCfg.Diff.DiffContextLines, flags.contextLines).(int),
}
difflib.WriteUnifiedDiff(flags.out, diff)
if err = difflib.WriteUnifiedDiff(flags.out, diff); err != nil {
return errors.Wrap(err, "error writing unified diff")
}
}
return nil
}

View File

@ -106,7 +106,7 @@ func newCmdNode(out io.Writer) *cobra.Command {
// sets the data builder function, that will be used by the runner
// both when running the entire workflow or single phases
nodeRunner.SetDataInitializer(func(cmd *cobra.Command, args []string) (workflow.RunData, error) {
data, err := newNodeData(cmd, args, nodeOptions, out)
data, err := newNodeData(cmd, nodeOptions, out)
if err != nil {
return nil, err
}
@ -145,7 +145,7 @@ func addUpgradeNodeFlags(flagSet *flag.FlagSet, nodeOptions *nodeOptions) {
// newNodeData returns a new nodeData struct to be used for the execution of the kubeadm upgrade node workflow.
// This func takes care of validating nodeOptions passed to the command, and then it converts
// options into the internal InitConfiguration type that is used as input all the phases in the kubeadm upgrade node workflow
func newNodeData(cmd *cobra.Command, args []string, nodeOptions *nodeOptions, out io.Writer) (*nodeData, error) {
func newNodeData(cmd *cobra.Command, nodeOptions *nodeOptions, out io.Writer) (*nodeData, error) {
// Checks if a node is a control-plane node by looking up the kube-apiserver manifest file
isControlPlaneNode := true
filepath := constants.GetStaticPodFilepath(constants.KubeAPIServer, constants.GetStaticPodDirectory())

View File

@ -63,7 +63,7 @@ func TestPruneXML(t *testing.T) {
writer := bufio.NewWriter(&output)
_ = streamXML(writer, suites)
_ = writer.Flush()
assert.Equal(t, outputXML, string(output.Bytes()), "xml was not pruned correctly")
assert.Equal(t, outputXML, output.String(), "xml was not pruned correctly")
}
func TestPruneTESTS(t *testing.T) {
@ -114,5 +114,5 @@ func TestPruneTESTS(t *testing.T) {
writer := bufio.NewWriter(&output)
_ = streamXML(writer, suites)
_ = writer.Flush()
assert.Equal(t, outputXML, string(output.Bytes()), "tests in xml was not pruned correctly")
assert.Equal(t, outputXML, output.String(), "tests in xml was not pruned correctly")
}

View File

@ -19,8 +19,9 @@ package main
import (
"bufio"
"bytes"
"github.com/stretchr/testify/assert"
"testing"
"github.com/stretchr/testify/assert"
)
func TestFetchYaml(t *testing.T) {
@ -48,5 +49,5 @@ labels:
writer := bufio.NewWriter(&output)
_ = streamYaml(writer, &indent, node)
_ = writer.Flush()
assert.Equal(t, outputYaml, string(output.Bytes()), "yaml was not formatted correctly")
assert.Equal(t, outputYaml, output.String(), "yaml was not formatted correctly")
}

View File

@ -145,10 +145,7 @@ func isOptionalAlias(t *types.Type) bool {
if t.Underlying == nil || (t.Underlying.Kind != types.Map && t.Underlying.Kind != types.Slice) {
return false
}
if extractBoolTagOrDie("protobuf.nullable", t.CommentLines) == false {
return false
}
return true
return extractBoolTagOrDie("protobuf.nullable", t.CommentLines)
}
func (g *genProtoIDL) Imports(c *generator.Context) (imports []string) {
@ -187,7 +184,7 @@ func (g *genProtoIDL) GenerateType(c *generator.Context, t *types.Type, w io.Wri
case types.Struct:
return b.doStruct(sw)
default:
return b.unknown(sw)
return b.unknown()
}
}
@ -262,7 +259,7 @@ type bodyGen struct {
t *types.Type
}
func (b bodyGen) unknown(sw *generator.SnippetWriter) error {
func (b bodyGen) unknown() error {
return fmt.Errorf("not sure how to generate: %#v", b.t)
}