Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple label selection ability in EtcdNodeSelectorLabels #5321

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/karmadactl/cmdinit/cmdinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func NewCmdInit(parentCommand string) *cobra.Command {
flags.StringVarP(&opts.EtcdInitImage, "etcd-init-image", "", kubernetes.DefaultInitImage, "etcd init container image")
flags.Int32VarP(&opts.EtcdReplicas, "etcd-replicas", "", 1, "etcd replica set, cluster 3,5...singular")
flags.StringVarP(&opts.EtcdHostDataPath, "etcd-data", "", "/var/lib/karmada-etcd", "etcd data path,valid in hostPath mode.")
flags.StringVarP(&opts.EtcdNodeSelectorLabels, "etcd-node-selector-labels", "", "", "etcd pod select the labels of the node. valid in hostPath mode ( e.g. --etcd-node-selector-labels karmada.io/etcd=true)")
flags.StringVarP(&opts.EtcdNodeSelectorLabels, "etcd-node-selector-labels", "", "", "the labels used for etcd pod to select nodes, valid in hostPath mode, and with each label separated by a comma. ( e.g. --etcd-node-selector-labels karmada.io/etcd=true,kubernetes.io/os=linux)")
flags.StringVarP(&opts.EtcdPersistentVolumeSize, "etcd-pvc-size", "", "5Gi", "etcd data path,valid in pvc mode.")
flags.StringVar(&opts.ExternalEtcdCACertPath, "external-etcd-ca-cert-path", "", "The path of CA certificate of the external etcd cluster in pem format.")
flags.StringVar(&opts.ExternalEtcdClientCertPath, "external-etcd-client-cert-path", "", "The path of client side certificate to the external etcd cluster in pem format.")
Expand Down
25 changes: 20 additions & 5 deletions pkg/karmadactl/cmdinit/kubernetes/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ type CommandInitOption struct {
EtcdStorageMode string
EtcdHostDataPath string
EtcdNodeSelectorLabels string
EtcdNodeSelectorLabelsMap map[string]string
EtcdPersistentVolumeSize string
ExternalEtcdCACertPath string
ExternalEtcdClientCertPath string
Expand Down Expand Up @@ -180,10 +181,6 @@ func (i *CommandInitOption) validateLocalEtcd(parentCommand string) error {
return fmt.Errorf("when etcd storage mode is hostPath, dataPath is not empty. See '%s init --help'", parentCommand)
}

if i.EtcdStorageMode == etcdStorageModeHostPath && i.EtcdNodeSelectorLabels != "" && utils.StringToMap(i.EtcdNodeSelectorLabels) == nil {
return fmt.Errorf("the label does not seem to be 'key=value'")
}

if i.EtcdStorageMode == etcdStorageModeHostPath && i.EtcdReplicas != 1 {
return fmt.Errorf("for data security,when etcd storage mode is hostPath,etcd-replicas can only be 1")
}
Expand Down Expand Up @@ -274,12 +271,15 @@ func (i *CommandInitOption) Complete() error {
}
klog.Infof("karmada apiserver ip: %s", i.KarmadaAPIServerIP)

if err := i.handleEtcdNodeSelectorLabels(); err != nil {
return err
}

if !i.isExternalEtcdProvided() && i.EtcdStorageMode == "hostPath" && i.EtcdNodeSelectorLabels != "" {
if !i.isNodeExist(i.EtcdNodeSelectorLabels) {
return fmt.Errorf("no node found by label %s", i.EtcdNodeSelectorLabels)
}
}

return initializeDirectory(i.KarmadaDataPath)
}

Expand Down Expand Up @@ -714,6 +714,21 @@ func (i *CommandInitOption) getImagePullSecrets() []corev1.LocalObjectReference
return imagePullSecrets
}

func (i *CommandInitOption) handleEtcdNodeSelectorLabels() error {
if i.EtcdStorageMode == etcdStorageModeHostPath && i.EtcdNodeSelectorLabels != "" {
selector, err := metav1.ParseToLabelSelector(i.EtcdNodeSelectorLabels)
if err != nil {
return fmt.Errorf("the etcdNodeSelector format is incorrect: %s", err)
}
labelMap, err := metav1.LabelSelectorAsMap(selector)
if err != nil {
return fmt.Errorf("failed to convert etcdNodeSelector labels to map: %v", err)
}
i.EtcdNodeSelectorLabelsMap = labelMap
}
return nil
}

func generateServerURL(serverIP string, nodePort int32) (string, error) {
_, ipType, err := utils.ParseIP(serverIP)
if err != nil {
Expand Down
12 changes: 6 additions & 6 deletions pkg/karmadactl/cmdinit/kubernetes/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"k8s.io/utils/ptr"

"github.com/karmada-io/karmada/pkg/karmadactl/cmdinit/options"
"github.com/karmada-io/karmada/pkg/karmadactl/cmdinit/utils"
)

const (
Expand Down Expand Up @@ -300,11 +299,12 @@ func (i *CommandInitOption) makeETCDStatefulSet() *appsv1.StatefulSet {
Volumes: *Volumes,
}

if i.EtcdStorageMode == "hostPath" && i.EtcdNodeSelectorLabels != "" {
podSpec.NodeSelector = utils.StringToMap(i.EtcdNodeSelectorLabels)
}
if i.EtcdStorageMode == "hostPath" && i.EtcdNodeSelectorLabels == "" {
podSpec.NodeSelector = map[string]string{"karmada.io/etcd": ""}
if i.EtcdStorageMode == "hostPath" {
if i.EtcdNodeSelectorLabelsMap != nil {
podSpec.NodeSelector = i.EtcdNodeSelectorLabelsMap
} else {
podSpec.NodeSelector = map[string]string{"karmada.io/etcd": ""}
}
}

// InitContainers
Expand Down
50 changes: 32 additions & 18 deletions pkg/karmadactl/cmdinit/kubernetes/statefulset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,43 +78,55 @@ func TestCommandInitIOption_etcdInitContainerCommand(t *testing.T) {

func TestCommandInitIOption_makeETCDStatefulSet(t *testing.T) {
tests := []struct {
name string
opt CommandInitOption
expectedNSValue string
expectedNSLabel string
name string
opt CommandInitOption
expectedNSMap map[string]string
}{
{
name: "EtcdStorageMode is etcdStorageModeHostPath, EtcdNodeSelectorLabels is set",
name: "EtcdStorageMode is etcdStorageModeHostPath, single valid EtcdNodeSelectorLabel",
opt: CommandInitOption{
EtcdStorageMode: etcdStorageModeHostPath,
Namespace: "karmada",
StorageClassesName: "StorageClassesName",
EtcdPersistentVolumeSize: "1024",
EtcdNodeSelectorLabels: "label=value",
EtcdNodeSelectorLabelsMap: map[string]string{
"label": "value",
},
},
expectedNSMap: map[string]string{
"label": "value",
},
expectedNSValue: "value",
expectedNSLabel: "label",
},
{
name: "EtcdStorageMode is etcdStorageModeHostPath, EtcdNodeSelectorLabels is not set",
name: "EtcdStorageMode is etcdStorageModeHostPath, multiple valid EtcdNodeSelectorLabels",
opt: CommandInitOption{
EtcdStorageMode: etcdStorageModeHostPath,
Namespace: "karmada",
StorageClassesName: "StorageClassesName",
EtcdPersistentVolumeSize: "1024",
EtcdNodeSelectorLabels: "",
EtcdNodeSelectorLabels: "label1=value1,label2=value2,kubernetes.io/os=linux",
EtcdNodeSelectorLabelsMap: map[string]string{
"label1": "value1",
"label2": "value2",
"kubernetes.io/os": "linux",
},
},
expectedNSMap: map[string]string{
"label1": "value1",
"label2": "value2",
"kubernetes.io/os": "linux",
},
expectedNSValue: "",
expectedNSLabel: "karmada.io/etcd",
},
{
name: "EtcdStorageMode is etcdStorageModePVC",
opt: CommandInitOption{
EtcdStorageMode: etcdStorageModePVC,
Namespace: "karmada",
StorageClassesName: "StorageClassesName",
EtcdPersistentVolumeSize: "1024",
EtcdNodeSelectorLabels: "",
EtcdStorageMode: etcdStorageModePVC,
Namespace: "karmada",
StorageClassesName: "StorageClassesName",
EtcdPersistentVolumeSize: "1024",
EtcdNodeSelectorLabels: "",
EtcdNodeSelectorLabelsMap: map[string]string{},
},
},
}
Expand All @@ -128,8 +140,10 @@ func TestCommandInitIOption_makeETCDStatefulSet(t *testing.T) {
}
} else {
nodeSelector := etcd.Spec.Template.Spec.NodeSelector
if val, ok := nodeSelector[tt.expectedNSLabel]; !ok || val != tt.expectedNSValue {
t.Errorf("CommandInitOption.makeETCDStatefulSet() returns wrong nodeSelector %v", nodeSelector)
for label, value := range tt.expectedNSMap {
if val, ok := nodeSelector[label]; !ok || val != value {
t.Errorf("CommandInitOption.makeETCDStatefulSet() returns wrong nodeSelector %v, expected %v=%v", nodeSelector, label, value)
}
}

if len(etcd.Spec.VolumeClaimTemplates) != 0 {
Expand Down