-
Notifications
You must be signed in to change notification settings - Fork 888
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: tiansuo114 <[email protected]> Complete the print function and add unit tests Signed-off-by: tiansuo114 <[email protected]> Add comment Signed-off-by: tiansuo114 <[email protected]> fix Signed-off-by: tiansuo114 <[email protected]> fix ci Signed-off-by: tiansuo114 <[email protected]> fix Signed-off-by: tiansuo114 <[email protected]> add kube-image-country flag Signed-off-by: tiansuo114 <[email protected]> fix ci Signed-off-by: tiansuo114 <[email protected]> add e2e test Signed-off-by: tiansuo114 <[email protected]> fix Signed-off-by: tiansuo114 <[email protected]> test Signed-off-by: tiansuo114 <[email protected]> fix Signed-off-by: tiansuo114 <[email protected]> fix Signed-off-by: tiansuo114 <[email protected]> fix Signed-off-by: tiansuo114 <[email protected]> fix ci Signed-off-by: tiansuo114 <[email protected]>
- Loading branch information
1 parent
ca7438a
commit 8f1541d
Showing
9 changed files
with
474 additions
and
0 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,60 @@ | ||
/* | ||
Copyright 2024 The Karmada 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" | ||
|
||
"github.com/spf13/cobra" | ||
"k8s.io/kubectl/pkg/util/templates" | ||
|
||
"github.com/karmada-io/karmada/pkg/karmadactl/util" | ||
) | ||
|
||
var ( | ||
configLong = templates.LongDesc(` | ||
Manage Karmada configurations. | ||
The 'config' command allows you to manage the configurations required for | ||
deploying and running Karmada. This includes listing, setting, and modifying | ||
various configurations such as image registries, versions, and other settings.`) | ||
|
||
configExamples = templates.Examples(` | ||
# List all images required for deploying Karmada | ||
%[1]s config image list | ||
# Use a specific registry for Karmada images | ||
%[1]s config image list --private-image-registry registry.cn-hangzhou.aliyuncs.com/google_containers`) | ||
) | ||
|
||
// NewCmdConfig creates a new config command | ||
func NewCmdConfig(parentCommand string) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "config", | ||
Short: "Manage Karmada configurations", | ||
Long: configLong, | ||
Example: fmt.Sprintf(configExamples, parentCommand), | ||
Annotations: map[string]string{ | ||
util.TagCommandGroup: util.GroupAdvancedCommands, | ||
}, | ||
} | ||
|
||
configParentCommand := fmt.Sprintf("%s %s", parentCommand, "config") | ||
cmd.AddCommand(NewCmdConfigImage(configParentCommand)) | ||
|
||
return cmd | ||
} |
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,31 @@ | ||
/* | ||
Copyright 2024 The Karmada 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_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/karmada-io/karmada/pkg/karmadactl/config" | ||
) | ||
|
||
// TestNewCmdConfigImagesList verifies that the list command works as expected | ||
func TestNewCmdConfigImagesList(t *testing.T) { | ||
parentCommand := "karmadactl config image" | ||
cmd := config.NewCmdConfigImagesList(parentCommand) | ||
if cmd == nil { | ||
t.Errorf("NewCmdConfigImagesList() want return not nil, but 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,56 @@ | ||
/* | ||
Copyright 2024 The Karmada 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" | ||
|
||
"github.com/spf13/cobra" | ||
"k8s.io/kubectl/pkg/util/templates" | ||
) | ||
|
||
var ( | ||
imagesLong = templates.LongDesc(` | ||
Shows information about the images required for Karmada deployment. | ||
This command lists all the images needed to deploy and run Karmada, including | ||
images for the API server, controller manager, scheduler, and other components.`) | ||
|
||
imagesExamples = templates.Examples(` | ||
# List all images required for deploying Karmada | ||
%[1]s config image list | ||
# Use a specific registry for Karmada images | ||
%[1]s config image list --private-image-registry registry.cn-hangzhou.aliyuncs.com/google_containers`) | ||
) | ||
|
||
// NewCmdConfigImage creates a new image command | ||
func NewCmdConfigImage(parentCommand string) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "image", | ||
Short: "Shows information about the images required for Karmada deployment.", | ||
Long: imagesLong, | ||
Example: fmt.Sprintf(imagesExamples, parentCommand), | ||
SilenceUsage: true, | ||
DisableFlagsInUseLine: true, | ||
} | ||
|
||
imageParentCommand := fmt.Sprintf("%s %s", parentCommand, "image") | ||
cmd.AddCommand(NewCmdConfigImagesList(imageParentCommand)) | ||
|
||
return cmd | ||
} |
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,55 @@ | ||
/* | ||
Copyright 2024 The Karmada 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" | ||
"os" | ||
|
||
"github.com/karmada-io/karmada/pkg/karmadactl/cmdinit/kubernetes" | ||
) | ||
|
||
// CommandConfigImageOption options for components | ||
type CommandConfigImageOption struct { | ||
PrivateImageRegistry string | ||
KubeImageCountry string | ||
InitOption *kubernetes.CommandInitOption | ||
} | ||
|
||
// Complete sets up the CommandConfigImageOption with the provided options. | ||
func (o *CommandConfigImageOption) Complete() error { | ||
o.InitOption = kubernetes.NewDefaultCommandInitOption() | ||
if o.PrivateImageRegistry != "" { | ||
o.InitOption.ImageRegistry = o.PrivateImageRegistry | ||
} | ||
if o.KubeImageCountry != "" { | ||
o.InitOption.KubeImageMirrorCountry = o.KubeImageCountry | ||
} | ||
return nil | ||
} | ||
|
||
// Run generates and prints the required control plane images. | ||
func (o *CommandConfigImageOption) Run() error { | ||
imageList := o.InitOption.GenerateControlPlaneImages() | ||
for _, image := range imageList { | ||
_, err := fmt.Fprintf(os.Stdout, "%s\n", image) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
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,85 @@ | ||
/* | ||
Copyright 2024 The Karmada 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_test | ||
|
||
import ( | ||
"io" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/karmada-io/karmada/pkg/karmadactl/cmdinit/kubernetes" | ||
config "github.com/karmada-io/karmada/pkg/karmadactl/config/init" | ||
) | ||
|
||
// Helper function to capture stdout | ||
func captureOutput(f func()) string { | ||
old := os.Stdout | ||
r, w, _ := os.Pipe() | ||
os.Stdout = w | ||
|
||
f() | ||
|
||
w.Close() | ||
os.Stdout = old | ||
out, _ := io.ReadAll(r) | ||
return string(out) | ||
} | ||
|
||
func TestCommandConfigImageOption(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
privateImageRegistry string | ||
expectedImages []string | ||
}{ | ||
{ | ||
name: "Default execution", | ||
privateImageRegistry: "", | ||
expectedImages: kubernetes.NewDefaultCommandInitOption().GenerateControlPlaneImages(), | ||
}, | ||
{ | ||
name: "With Private Image Registry", | ||
privateImageRegistry: "myregistry.com", | ||
expectedImages: func() []string { | ||
opts := kubernetes.NewDefaultCommandInitOption() | ||
opts.ImageRegistry = "myregistry.com" | ||
return opts.GenerateControlPlaneImages() | ||
}(), | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
opts := config.CommandConfigImageOption{ | ||
PrivateImageRegistry: tt.privateImageRegistry, | ||
} | ||
err := opts.Complete() | ||
assert.NoError(t, err) | ||
assert.NotNil(t, opts.InitOption) | ||
|
||
output := captureOutput(func() { | ||
err := opts.Run() | ||
assert.NoError(t, err) | ||
}) | ||
|
||
for _, image := range tt.expectedImages { | ||
assert.Contains(t, output, image) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.