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

*: update vc nodepool add subcommand #105

Merged
merged 1 commit into from
Mar 27, 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
13 changes: 13 additions & 0 deletions cmd/kperf/commands/utils/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ func KeyValuesMap(strs []string) (map[string][]string, error) {
return res, nil
}

// KeyValuesMap converts key=value into map[string]string.
func KeyValueMap(strs []string) (map[string]string, error) {
res := make(map[string]string, len(strs))
for _, str := range strs {
key, value, ok := strings.Cut(str, "=")
if !ok {
return nil, fmt.Errorf("expected key=value format, but got %s", str)
}
res[key] = value
}
return res, nil
}

// inCluster is to check if current process is in pod.
func inCluster() bool {
f, err := os.Stat("/var/run/secrets/kubernetes.io/serviceaccount/token")
Expand Down
16 changes: 16 additions & 0 deletions cmd/kperf/commands/virtualcluster/nodepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ var nodepoolAddCommand = cli.Command{
Name: "affinity",
Usage: "Deploy controllers to the nodes with a specific labels (FORMAT: KEY=VALUE[,VALUE])",
},
cli.StringSliceFlag{
Name: "node-labels",
Usage: "Additional labels to node (FORMAT: KEY=VALUE)",
},
cli.StringFlag{
Name: "shared-provider-id",
Usage: "Force all the virtual nodes using one provider ID",
Hidden: true,
},
},
Action: func(cliCtx *cli.Context) error {
if cliCtx.NArg() != 1 {
Expand All @@ -77,6 +86,11 @@ var nodepoolAddCommand = cli.Command{
return fmt.Errorf("failed to parse affinity: %w", err)
}

nodeLabels, err := utils.KeyValueMap(cliCtx.StringSlice("node-labels"))
if err != nil {
return fmt.Errorf("failed to parse node-labels: %w", err)
}

return virtualcluster.CreateNodepool(context.Background(),
kubeCfgPath,
nodepoolName,
Expand All @@ -85,6 +99,8 @@ var nodepoolAddCommand = cli.Command{
virtualcluster.WithNodepoolCountOpt(cliCtx.Int("nodes")),
virtualcluster.WithNodepoolMaxPodsOpt(cliCtx.Int("max-pods")),
virtualcluster.WithNodepoolNodeControllerAffinity(affinityLabels),
virtualcluster.WithNodepoolLabelsOpt(nodeLabels),
virtualcluster.WithNodepoolSharedProviderID(cliCtx.String("shared-provider-id")),
)
},
}
Expand Down
4 changes: 4 additions & 0 deletions manifests/virtualcluster/nodes/templates/nodes.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
{{- $memory := .Values.memory }}
{{- $maxPods := .Values.maxPods }}
{{- $labels := .Values.nodeLabels }}
{{- $sharedProviderID := .Values.sharedProviderID }}
{{- range $index := (untilStep 0 (int .Values.replicas) 1) }}
apiVersion: v1
kind: Node
Expand Down Expand Up @@ -32,6 +33,9 @@ spec:
- effect: NoSchedule
key: kperf.io/nodepool
value: fake
{{- if $sharedProviderID }}
providerID: {{ $sharedProviderID }}
{{- end}}
status:
allocatable:
cpu: {{ $cpu }}
Expand Down
56 changes: 51 additions & 5 deletions virtualcluster/nodes_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,16 @@ type nodepoolConfig struct {
// maxPods represents maximum Pods per node.
maxPods int
// labels is to be applied to each virtual node.
labels []string
labels map[string]string
// sharedProviderID is to force all the virtual nodes sharing one providerID.
//
// FIXME(weifu):
//
// EKS cloud provider will delete all the non-ready virtual nodes
// if they aren't managed by their cloud provider. The sharedProviderID
// can be linked to existing node provided by EKS so that cloud provider
// won't delete the virtual nodes. It's hack.
sharedProviderID string
// nodeSelectors forces virtual node's controller to nodes with that specific labels.
nodeSelectors map[string][]string
}
Expand Down Expand Up @@ -122,7 +131,7 @@ func WithNodepoolMaxPodsOpt(maxPods int) NodepoolOpt {
}

// WithNodepoolLabelsOpt updates node's labels.
func WithNodepoolLabelsOpt(labels []string) NodepoolOpt {
func WithNodepoolLabelsOpt(labels map[string]string) NodepoolOpt {
return func(cfg *nodepoolConfig) {
cfg.labels = labels
}
Expand All @@ -136,18 +145,55 @@ func WithNodepoolNodeControllerAffinity(nodeSelectors map[string][]string) Nodep
}
}

// WithNodepoolSharedProviderID forces virtual nodes to share unique providerID.
func WithNodepoolSharedProviderID(providerID string) NodepoolOpt {
return func(cfg *nodepoolConfig) {
cfg.sharedProviderID = providerID
}
}

// toNodeHelmValuesAppliers creates ValuesAppliers.
//
// NOTE: Please align with ../manifests/virtualcluster/nodes/values.yaml
func (cfg *nodepoolConfig) toNodeHelmValuesAppliers() []helmcli.ValuesApplier {
res := make([]string, 0, 5)
func (cfg *nodepoolConfig) toNodeHelmValuesAppliers() ([]helmcli.ValuesApplier, error) {
res := make([]string, 0, 6)

res = append(res, fmt.Sprintf("name=%s", cfg.name))
res = append(res, fmt.Sprintf("cpu=%d", cfg.cpu))
res = append(res, fmt.Sprintf("memory=%d", cfg.memory))
res = append(res, fmt.Sprintf("replicas=%d", cfg.count))
res = append(res, fmt.Sprintf("maxPods=%d", cfg.maxPods))
return []helmcli.ValuesApplier{helmcli.StringPathValuesApplier(res...)}
res = append(res, fmt.Sprintf("sharedProviderID=%s", cfg.sharedProviderID))

nodeLabelsYaml, err := cfg.renderNodeLabels()
if err != nil {
return nil, err
}

nodeLabelsApplier, err := helmcli.YAMLValuesApplier(nodeLabelsYaml)
if err != nil {
return nil, err
}

return []helmcli.ValuesApplier{
helmcli.StringPathValuesApplier(res...),
nodeLabelsApplier,
}, nil
}

// renderNodeLabels renders virtual node's labels into YAML string
//
// NOTE: Please align with ../manifests/virtualcluster/nodes/values.yaml
func (cfg *nodepoolConfig) renderNodeLabels() (string, error) {
target := map[string]interface{}{
"nodeLabels": cfg.labels,
}

rawData, err := yaml.Marshal(target)
if err != nil {
return "", fmt.Errorf("failed to render nodeLabels: %w", err)
}
return string(rawData), nil
}

// toNodeControllerHelmValuesAppliers creates ValuesAppliers.
Expand Down
7 changes: 6 additions & 1 deletion virtualcluster/nodes_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,18 @@ func CreateNodepool(ctx context.Context, kubeCfgPath string, nodepoolName string
return fmt.Errorf("failed to load virtual node chart: %w", err)
}

valueAppliers, err := cfg.toNodeHelmValuesAppliers()
if err != nil {
return err
}

releaseCli, err := helmcli.NewReleaseCli(
kubeCfgPath,
virtualnodeReleaseNamespace,
cfg.nodeHelmReleaseName(),
ch,
virtualnodeReleaseLabels,
cfg.toNodeHelmValuesAppliers()...,
valueAppliers...,
)
if err != nil {
return fmt.Errorf("failed to create helm release client: %w", err)
Expand Down
Loading