Skip to content

Commit

Permalink
Add config list
Browse files Browse the repository at this point in the history
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
tiansuo114 committed Aug 30, 2024
1 parent ca7438a commit 8f1541d
Show file tree
Hide file tree
Showing 9 changed files with 474 additions and 0 deletions.
45 changes: 45 additions & 0 deletions pkg/karmadactl/cmdinit/kubernetes/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"github.com/karmada-io/karmada/pkg/karmadactl/cmdinit/cert"
"github.com/karmada-io/karmada/pkg/karmadactl/cmdinit/karmada"
"github.com/karmada-io/karmada/pkg/karmadactl/cmdinit/options"
cmdinitoptions "github.com/karmada-io/karmada/pkg/karmadactl/cmdinit/options"
"github.com/karmada-io/karmada/pkg/karmadactl/cmdinit/utils"
globaloptions "github.com/karmada-io/karmada/pkg/karmadactl/options"
"github.com/karmada-io/karmada/pkg/karmadactl/util"
Expand Down Expand Up @@ -729,3 +730,47 @@ func generateServerURL(serverIP string, nodePort int32) (string, error) {
func SupportedStorageMode() []string {
return []string{etcdStorageModeEmptyDir, etcdStorageModeHostPath, etcdStorageModePVC}
}

// NewDefaultCommandInitOption returns a CommandInitOption with default values
func NewDefaultCommandInitOption() *CommandInitOption {
return &CommandInitOption{
ImagePullPolicy: string(corev1.PullIfNotPresent),
KubeImageTag: "v1.29.6",
Namespace: "karmada-system",
EtcdStorageMode: "hostPath",
EtcdInitImage: DefaultInitImage,
EtcdReplicas: 1,
EtcdHostDataPath: "/var/lib/karmada-etcd",
EtcdPersistentVolumeSize: "5Gi",
CRDs: DefaultCrdURL,
KarmadaAPIServerNodePort: 32443,
KarmadaDataPath: "/etc/karmada",
KarmadaPkiPath: "/etc/karmada/pki",
KarmadaSchedulerImage: DefaultKarmadaSchedulerImage,
KarmadaSchedulerReplicas: 1,
KarmadaControllerManagerImage: DefaultKarmadaControllerManagerImage,
KarmadaControllerManagerReplicas: 1,
KarmadaWebhookImage: DefaultKarmadaWebhookImage,
KarmadaWebhookReplicas: 1,
KarmadaAggregatedAPIServerImage: DefaultKarmadaAggregatedAPIServerImage,
KarmadaAggregatedAPIServerReplicas: 1,
WaitComponentReadyTimeout: cmdinitoptions.WaitComponentReadyTimeout,
CertValidity: cert.Duration365d,
HostClusterDomain: globaloptions.DefaultHostClusterDomain,
}
}

// GenerateControlPlaneImages generated control plane all the image list
func (i *CommandInitOption) GenerateControlPlaneImages() []string {
images := []string{
i.kubeAPIServerImage(),
i.kubeControllerManagerImage(),
i.etcdImage(),
i.etcdInitImage(),
i.karmadaSchedulerImage(),
i.karmadaControllerManagerImage(),
i.karmadaWebhookImage(),
i.karmadaAggregatedAPIServerImage(),
}
return images
}
60 changes: 60 additions & 0 deletions pkg/karmadactl/config/config.go
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
}
31 changes: 31 additions & 0 deletions pkg/karmadactl/config/config_test.go
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")
}
}
56 changes: 56 additions & 0 deletions pkg/karmadactl/config/image.go
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
}
55 changes: 55 additions & 0 deletions pkg/karmadactl/config/init/list_option.go
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
}
85 changes: 85 additions & 0 deletions pkg/karmadactl/config/init/list_option_test.go
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)
}
})
}
}
Loading

0 comments on commit 8f1541d

Please sign in to comment.