Skip to content

[tmpnet] Source tmpnet defaults from a configmap #4057

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
14 changes: 5 additions & 9 deletions tests/fixture/tmpnet/flags/kube_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ const (
)

var (
errKubeNamespaceRequired = errors.New("--kube-namespace is required")
errKubeImageRequired = errors.New("--kube-image is required")
errKubeMinVolumeSizeRequired = fmt.Errorf("--kube-volume-size must be >= %d", tmpnet.MinimumVolumeSizeGB)
errKubeSchedulingLabelRequired = errors.New("--kube-scheduling-label-key and --kube-scheduling-label-value are required when --kube-use-exclusive-scheduling is enabled")
errKubeNamespaceRequired = errors.New("--kube-namespace is required")
errKubeImageRequired = errors.New("--kube-image is required")
errKubeMinVolumeSizeRequired = fmt.Errorf("--kube-volume-size must be >= %d", tmpnet.MinimumVolumeSizeGB)
)

type kubeRuntimeVars struct {
Expand Down Expand Up @@ -77,13 +76,13 @@ func (v *kubeRuntimeVars) register(stringVar varFunc[string], uintVar varFunc[ui
stringVar(
&v.schedulingLabelKey,
"kube-scheduling-label-key",
"purpose",
"",
kubeDocPrefix+"The label key to use for exclusive scheduling for node selection and toleration",
)
stringVar(
&v.schedulingLabelValue,
"kube-scheduling-label-value",
"higher-spec",
"",
kubeDocPrefix+"The label value to use for exclusive scheduling for node selection and toleration",
)
}
Expand All @@ -98,9 +97,6 @@ func (v *kubeRuntimeVars) getKubeRuntimeConfig() (*tmpnet.KubeRuntimeConfig, err
if v.volumeSizeGB < tmpnet.MinimumVolumeSizeGB {
return nil, errKubeMinVolumeSizeRequired
}
if v.useExclusiveScheduling && (len(v.schedulingLabelKey) == 0 || len(v.schedulingLabelValue) == 0) {
return nil, errKubeSchedulingLabelRequired
}
return &tmpnet.KubeRuntimeConfig{
ConfigPath: v.config.Path,
ConfigContext: v.config.Context,
Expand Down
53 changes: 53 additions & 0 deletions tests/fixture/tmpnet/kube_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/ava-labs/avalanchego/config"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/logging"

corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -49,8 +50,13 @@ const (
// are never scheduled to the same nodes.
antiAffinityLabelKey = "tmpnet-scheduling"
antiAffinityLabelValue = "exclusive"

// Name of config map containing tmpnet defaults
defaultsConfigMapName = "tmpnet-defaults"
)

var errMissingSchedulingLabels = errors.New("--kube-scheduling-label-key and --kube-scheduling-label-value are required when exclusive scheduling is enabled")

type KubeRuntimeConfig struct {
// Path to the kubeconfig file identifying the target cluster
ConfigPath string `json:"configPath,omitempty"`
Expand All @@ -72,6 +78,53 @@ type KubeRuntimeConfig struct {
SchedulingLabelValue string `json:"schedulingLabelValue,omitempty"`
}

// ensureDefaults sets cluster-specific defaults for fields not already set by flags.
func (c *KubeRuntimeConfig) ensureDefaults(ctx context.Context, log logging.Logger) error {
requireSchedulingDefaults := c.UseExclusiveScheduling && (len(c.SchedulingLabelKey) == 0 || len(c.SchedulingLabelValue) == 0)
if !requireSchedulingDefaults {
return nil
}

clientset, err := GetClientset(log, c.ConfigPath, c.ConfigContext)
if err != nil {
return err
}

log.Info("attempting to retrieve configmap containing tmpnet defaults",
zap.String("namespace", c.Namespace),
zap.String("configMap", defaultsConfigMapName),
)

configMap, err := clientset.CoreV1().ConfigMaps(c.Namespace).Get(ctx, defaultsConfigMapName, metav1.GetOptions{})
if err != nil {
return fmt.Errorf("failed to get ConfigMap: %w", err)
}

var (
schedulingLabelKey = configMap.Data["schedulingLabelKey"]
schedulingLabelValue = configMap.Data["schedulingLabelValue"]
)
if len(c.SchedulingLabelKey) == 0 && len(schedulingLabelKey) > 0 {
log.Info("setting default value for SchedulingLabelKey",
zap.String("schedulingLabelKey", schedulingLabelKey),
)
c.SchedulingLabelKey = schedulingLabelKey
}
if len(c.SchedulingLabelValue) == 0 && len(schedulingLabelValue) > 0 {
log.Info("setting default value for SchedulingLabelValue",
zap.String("schedulingLabelValue", schedulingLabelValue),
)
c.SchedulingLabelValue = schedulingLabelValue
}
Comment on lines +107 to +118
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a benefit to mix-and-matching between flag values and ConfigMap values here? I would think that the KubeRuntimeConfig would go with exclusively with flag values or exclusively with ConfigMap values.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Configmap values are defaults and only used when a flag has an empty value (as per the logging communicating their usage). The intention is to ensure that a cluster can provide appropriate defaults rather than to prevent a user from supplying values via flags.


// Validate that the scheduling labels are now set
if len(c.SchedulingLabelKey) == 0 || len(c.SchedulingLabelValue) == 0 {
return errMissingSchedulingLabels
}

return nil
}

type KubeRuntime struct {
node *Node
}
Expand Down
11 changes: 9 additions & 2 deletions tests/fixture/tmpnet/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func BootstrapNewNetwork(
if err := checkVMBinaries(log, network.Subnets, network.DefaultRuntimeConfig.Process); err != nil {
return err
}
if err := network.EnsureDefaultConfig(log); err != nil {
if err := network.EnsureDefaultConfig(ctx, log); err != nil {
return err
}
if err := network.Create(rootNetworkDir); err != nil {
Expand Down Expand Up @@ -234,7 +234,14 @@ func ReadNetwork(ctx context.Context, log logging.Logger, dir string) (*Network,
}

// Initializes a new network with default configuration.
func (n *Network) EnsureDefaultConfig(log logging.Logger) error {
func (n *Network) EnsureDefaultConfig(ctx context.Context, log logging.Logger) error {
// Populate runtime defaults before logging it
if n.DefaultRuntimeConfig.Kube != nil {
if err := n.DefaultRuntimeConfig.Kube.ensureDefaults(ctx, log); err != nil {
return err
}
}

log.Info("preparing configuration for new network",
zap.Any("runtimeConfig", n.DefaultRuntimeConfig),
)
Expand Down
2 changes: 1 addition & 1 deletion tests/fixture/tmpnet/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestNetworkSerialization(t *testing.T) {
network.PrimarySubnetConfig = ConfigMap{
"validatorOnly": true,
}
require.NoError(network.EnsureDefaultConfig(logging.NoLog{}))
require.NoError(network.EnsureDefaultConfig(ctx, logging.NoLog{}))
require.NoError(network.Create(tmpDir))
// Ensure node runtime is initialized
require.NoError(network.readNodes(ctx))
Expand Down