-
Notifications
You must be signed in to change notification settings - Fork 917
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]>
- Loading branch information
1 parent
21467f8
commit 8b5e785
Showing
7 changed files
with
332 additions
and
7 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
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,41 @@ | ||
/* | ||
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 show | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/karmada-io/karmada/pkg/karmadactl/cmdinit/kubernetes" | ||
) | ||
|
||
// CommandConfigImageOption options for components | ||
type CommandConfigImageOption struct { | ||
InitOption *kubernetes.CommandInitOption | ||
} | ||
|
||
// 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,84 @@ | ||
/* | ||
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 show_test | ||
|
||
import ( | ||
"io" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/karmada-io/karmada/pkg/karmadactl/cmdinit/kubernetes" | ||
"github.com/karmada-io/karmada/pkg/karmadactl/cmdinit/show" | ||
) | ||
|
||
// 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 := show.CommandConfigImageOption{ | ||
InitOption: kubernetes.NewDefaultCommandInitOption(), | ||
} | ||
opts.InitOption.ImageRegistry = tt.privateImageRegistry | ||
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) | ||
} | ||
}) | ||
} | ||
} |
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,68 @@ | ||
/* | ||
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 cmdinit | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
"k8s.io/kubectl/pkg/util/templates" | ||
|
||
"github.com/karmada-io/karmada/pkg/karmadactl/cmdinit/kubernetes" | ||
configInit "github.com/karmada-io/karmada/pkg/karmadactl/cmdinit/show" | ||
) | ||
|
||
var ( | ||
showImagesLong = 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.`) | ||
|
||
showImagesExample = templates.Examples(` | ||
# List all required images | ||
%[1]s init show-images | ||
# List all required images from a specific private image registry | ||
%[1]s init show-images --private-image-registry registry.cn-hangzhou.aliyuncs.com/google_containers | ||
# List all required images with a specific country code for the image registry | ||
%[1]s init show-images --kube-image-country cn`) | ||
) | ||
|
||
// NewCmdShowImages creates a new show-images command | ||
func NewCmdShowImages(parentCommand string) *cobra.Command { | ||
opts := configInit.CommandConfigImageOption{ | ||
InitOption: kubernetes.NewDefaultCommandInitOption(), | ||
} | ||
cmd := &cobra.Command{ | ||
Use: "show-images", | ||
Short: "List the images required for Karmada deployment", | ||
Long: showImagesLong, | ||
Example: fmt.Sprintf(showImagesExample, parentCommand), | ||
RunE: func(_ *cobra.Command, _ []string) error { | ||
return opts.Run() | ||
}, | ||
SilenceUsage: true, | ||
DisableFlagsInUseLine: true, | ||
} | ||
|
||
cmd.Flags().StringVarP(&opts.InitOption.ImageRegistry, "private-image-registry", "", "", "Private image registry to pull images from. If set, all required images will be downloaded from it, useful for offline installation scenarios.") | ||
cmd.Flags().StringVarP(&opts.InitOption.KubeImageMirrorCountry, "kube-image-country", "", "", "The country code of the image registry, such as 'global' or 'cn'.") | ||
|
||
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