Skip to content

Commit

Permalink
Put constant for the default KatibConfig value on /pkg/apis/config/v1…
Browse files Browse the repository at this point in the history
…beta1

Signed-off-by: Yuki Iwai <yuki.iwai.tz@gmail.com>
  • Loading branch information
tenzen-y committed Jul 21, 2023
1 parent a240683 commit 199aa8b
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 86 deletions.
62 changes: 43 additions & 19 deletions pkg/apis/config/v1beta1/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,46 @@ import (
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/runtime"

"github.com/kubeflow/katib/pkg/controller.v1beta1/consts"
)

const (
// DefaultExperimentSuggestionName is the default name for the suggestions.
DefaultExperimentSuggestionName = "default"
DefaultMetricsAddr = ":8080"
DefaultHealthzAddr = ":18080"
DefaultLeaderElectionID = "3fbc96e9.katib.kubeflow.org"
// DefaultMetricsAddr is the default address for the prometheus metrics.
DefaultMetricsAddr = ":8080"
// DefaultHealthzAddr is the default address for the health probe.
DefaultHealthzAddr = ":18080"
// DefaultLeaderElectionID is the default LeaderElectionID for the controller.
DefaultLeaderElectionID = "3fbc96e9.katib.kubeflow.org"
// DefaultContainerSuggestionVolumeMountPath is the default mount path in suggestion container.
DefaultContainerSuggestionVolumeMountPath = "/opt/katib/data"
// DefaultSuggestionVolumeAccessMode is the default value for suggestion's volume access mode.
DefaultSuggestionVolumeAccessMode = corev1.ReadWriteOnce
// DefaultSuggestionVolumeStorage is the default value for suggestion's volume storage.
DefaultSuggestionVolumeStorage = "1Gi"
// DefaultImagePullPolicy is the default value for image pull policy.
DefaultImagePullPolicy = corev1.PullIfNotPresent
// DefaultCPULimit is the default value for CPU limit.
DefaultCPULimit = "500m"
// DefaultCPURequest is the default value for CPU request.
DefaultCPURequest = "50m"
// DefaultMemLimit is the default value for memory limit.
DefaultMemLimit = "100Mi"
// DefaultMemRequest is the default value for memory request.
DefaultMemRequest = "10Mi"
// DefaultDiskLimit is the default value for disk limit.
DefaultDiskLimit = "5Gi"
// DefaultDiskRequest is the default value for disk request.
DefaultDiskRequest = "500Mi"
)

var (
DefaultInjectSecurityContext = false
// DefaultEnableGRPCProbeInSuggestion is the default value whether enable to gRPC probe in suggestions.
DefaultEnableGRPCProbeInSuggestion = true
DefaultWebhookPort = 8443
DefaultEnableLeaderElection = false
DefaultTrialResources = []string{"Job.v1.batch"}
// DefaultWebhookPort is the default port for the admission webhook.
DefaultWebhookPort = 8443
// DefaultTrialResources is the default resource which can be used as a trial template.
DefaultTrialResources = []string{"Job.v1.batch"}
)

func addDefaultingFuncs(scheme *runtime.Scheme) error {
Expand Down Expand Up @@ -86,7 +109,8 @@ func setSuggestionConfigs(suggestionConfigs []SuggestionConfig) {

// Set default suggestion container volume mount path
if suggestionConfigs[i].VolumeMountPath == "" {
suggestionConfigs[i].VolumeMountPath = consts.DefaultContainerSuggestionVolumeMountPath
suggestionConfigs[i].VolumeMountPath = DefaultContainerSuggestionVolumeMountPath

}

// Get persistent volume claim spec from config
Expand All @@ -95,13 +119,13 @@ func setSuggestionConfigs(suggestionConfigs []SuggestionConfig) {
// Set default access modes
if len(pvcSpec.AccessModes) == 0 {
pvcSpec.AccessModes = []corev1.PersistentVolumeAccessMode{
consts.DefaultSuggestionVolumeAccessMode,
DefaultSuggestionVolumeAccessMode,
}
}

// Set default resources
if len(pvcSpec.Resources.Requests) == 0 {
defaultVolumeStorage, _ := resource.ParseQuantity(consts.DefaultSuggestionVolumeStorage)
defaultVolumeStorage, _ := resource.ParseQuantity(DefaultSuggestionVolumeStorage)
pvcSpec.Resources.Requests = make(map[corev1.ResourceName]resource.Quantity)
pvcSpec.Resources.Requests[corev1.ResourceStorage] = defaultVolumeStorage
}
Expand Down Expand Up @@ -142,7 +166,7 @@ func setEarlyStoppingConfigs(earlyStoppingConfigs []EarlyStoppingConfig) {

func setImagePullPolicy(imagePullPolicy corev1.PullPolicy) corev1.PullPolicy {
if imagePullPolicy != corev1.PullAlways && imagePullPolicy != corev1.PullIfNotPresent && imagePullPolicy != corev1.PullNever {
return consts.DefaultImagePullPolicy
return DefaultImagePullPolicy
}
return imagePullPolicy
}
Expand All @@ -160,15 +184,15 @@ func setResourceRequirements(configResource corev1.ResourceRequirements) corev1.

// If resource is empty set default value for CPU, Memory, Disk
if cpuRequest.IsZero() {
defaultCPURequest, _ := resource.ParseQuantity(consts.DefaultCPURequest)
defaultCPURequest, _ := resource.ParseQuantity(DefaultCPURequest)
configResource.Requests[corev1.ResourceCPU] = defaultCPURequest
}
if memRequest.IsZero() {
defaultMemRequest, _ := resource.ParseQuantity(consts.DefaultMemRequest)
defaultMemRequest, _ := resource.ParseQuantity(DefaultMemRequest)
configResource.Requests[corev1.ResourceMemory] = defaultMemRequest
}
if diskRequest.IsZero() {
defaultDiskRequest, _ := resource.ParseQuantity(consts.DefaultDiskRequest)
defaultDiskRequest, _ := resource.ParseQuantity(DefaultDiskRequest)
configResource.Requests[corev1.ResourceEphemeralStorage] = defaultDiskRequest
}

Expand All @@ -184,15 +208,15 @@ func setResourceRequirements(configResource corev1.ResourceRequirements) corev1.

// If limit is empty set default value for CPU, Memory, Disk
if cpuLimit.IsZero() {
defaultCPULimit, _ := resource.ParseQuantity(consts.DefaultCPULimit)
defaultCPULimit, _ := resource.ParseQuantity(DefaultCPULimit)
configResource.Limits[corev1.ResourceCPU] = defaultCPULimit
}
if memLimit.IsZero() {
defaultMemLimit, _ := resource.ParseQuantity(consts.DefaultMemLimit)
defaultMemLimit, _ := resource.ParseQuantity(DefaultMemLimit)
configResource.Limits[corev1.ResourceMemory] = defaultMemLimit
}
if diskLimit.IsZero() {
defaultDiskLimit, _ := resource.ParseQuantity(consts.DefaultDiskLimit)
defaultDiskLimit, _ := resource.ParseQuantity(DefaultDiskLimit)
configResource.Limits[corev1.ResourceEphemeralStorage] = defaultDiskLimit
}

Expand Down
40 changes: 17 additions & 23 deletions pkg/apis/config/v1beta1/defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"k8s.io/utils/pointer"

commonv1beta1 "github.com/kubeflow/katib/pkg/apis/controller/common/v1beta1"
"github.com/kubeflow/katib/pkg/controller.v1beta1/consts"
)

func TestSetSuggestionConfigs(t *testing.T) {
Expand All @@ -34,7 +33,7 @@ func TestSetSuggestionConfigs(t *testing.T) {
return []SuggestionConfig{*c}
}(),
},
fmt.Sprintf("GetSuggestionConfigData sets %s to imagePullPolicy", consts.DefaultImagePullPolicy): {
fmt.Sprintf("GetSuggestionConfigData sets %s to imagePullPolicy", DefaultImagePullPolicy): {
config: func() []SuggestionConfig {
suggestion := newFakeSuggestionConfig(testAlgorithmName)
suggestion.ImagePullPolicy = ""
Expand All @@ -50,7 +49,7 @@ func TestSetSuggestionConfigs(t *testing.T) {
}(),
wantConfig: []SuggestionConfig{*newFakeSuggestionConfig(testAlgorithmName)},
},
fmt.Sprintf("GetSuggestionConfigData sets %s to volumeMountPath", consts.DefaultContainerSuggestionVolumeMountPath): {
fmt.Sprintf("GetSuggestionConfigData sets %s to volumeMountPath", DefaultContainerSuggestionVolumeMountPath): {
config: func() []SuggestionConfig {
suggestion := newFakeSuggestionConfig(testAlgorithmName)
suggestion.VolumeMountPath = ""
Expand Down Expand Up @@ -113,7 +112,7 @@ func TestSetEarlyStoppingConfigs(t *testing.T) {
return []EarlyStoppingConfig{*c}
}(),
},
fmt.Sprintf("GetEarlyStoppingConfigData sets %s to imagePullPolicy", consts.DefaultImagePullPolicy): {
fmt.Sprintf("GetEarlyStoppingConfigData sets %s to imagePullPolicy", DefaultImagePullPolicy): {
config: func() []EarlyStoppingConfig {
c := newFakeEarlyStoppingConfig(testAlgorithmName)
c.ImagePullPolicy = ""
Expand All @@ -138,10 +137,7 @@ func TestSetEarlyStoppingConfigs(t *testing.T) {
}

func TestSetMetricsCollectorConfigs(t *testing.T) {
const (
invalidCollectorKind commonv1beta1.CollectorKind = "invalidCollector"
testCollectorKind commonv1beta1.CollectorKind = "testCollector"
)
const testCollectorKind commonv1beta1.CollectorKind = "testCollector"
nukeResource, _ := resource.ParseQuantity("-1")
nukeResourceRequirements := map[corev1.ResourceName]resource.Quantity{
corev1.ResourceCPU: nukeResource,
Expand All @@ -164,7 +160,7 @@ func TestSetMetricsCollectorConfigs(t *testing.T) {
return []MetricsCollectorConfig{*c}
}(),
},
fmt.Sprintf("GetMetricsConfigData sets %s to imagePullPolicy", consts.DefaultImagePullPolicy): {
fmt.Sprintf("GetMetricsConfigData sets %s to imagePullPolicy", DefaultImagePullPolicy): {
config: func() []MetricsCollectorConfig {
c := newFakeMetricsCollectorConfig(testCollectorKind)
c.ImagePullPolicy = ""
Expand Down Expand Up @@ -254,11 +250,9 @@ func TestSetInitConfig(t *testing.T) {
ExperimentSuggestionName: DefaultExperimentSuggestionName,
MetricsAddr: DefaultMetricsAddr,
HealthzAddr: DefaultHealthzAddr,
InjectSecurityContext: DefaultInjectSecurityContext,
EnableGRPCProbeInSuggestion: &DefaultEnableGRPCProbeInSuggestion,
TrialResources: DefaultTrialResources,
WebhookPort: &DefaultWebhookPort,
EnableLeaderElection: DefaultEnableLeaderElection,
LeaderElectionID: DefaultLeaderElectionID,
},
},
Expand All @@ -278,19 +272,19 @@ func TestSetInitConfig(t *testing.T) {
}

func newFakeSuggestionConfig(algorithmName string) *SuggestionConfig {
defaultVolumeStorage, _ := resource.ParseQuantity(consts.DefaultSuggestionVolumeStorage)
defaultVolumeStorage, _ := resource.ParseQuantity(DefaultSuggestionVolumeStorage)

return &SuggestionConfig{
AlgorithmName: algorithmName,
Container: corev1.Container{
Image: "suggestion-image",
ImagePullPolicy: consts.DefaultImagePullPolicy,
ImagePullPolicy: DefaultImagePullPolicy,
Resources: *setFakeResourceRequirements(),
},
VolumeMountPath: consts.DefaultContainerSuggestionVolumeMountPath,
VolumeMountPath: DefaultContainerSuggestionVolumeMountPath,
PersistentVolumeClaimSpec: corev1.PersistentVolumeClaimSpec{
AccessModes: []corev1.PersistentVolumeAccessMode{
consts.DefaultSuggestionVolumeAccessMode,
DefaultSuggestionVolumeAccessMode,
},
Resources: corev1.ResourceRequirements{
Requests: map[corev1.ResourceName]resource.Quantity{
Expand All @@ -308,7 +302,7 @@ func newFakeEarlyStoppingConfig(algorithmName string) *EarlyStoppingConfig {
return &EarlyStoppingConfig{
AlgorithmName: algorithmName,
Image: "early-stopping-image",
ImagePullPolicy: consts.DefaultImagePullPolicy,
ImagePullPolicy: DefaultImagePullPolicy,
Resource: *setFakeResourceRequirements(),
}
}
Expand All @@ -317,19 +311,19 @@ func newFakeMetricsCollectorConfig(collectorKind commonv1beta1.CollectorKind) *M
return &MetricsCollectorConfig{
CollectorKind: string(collectorKind),
Image: "metrics-collector-image",
ImagePullPolicy: consts.DefaultImagePullPolicy,
ImagePullPolicy: DefaultImagePullPolicy,
Resource: *setFakeResourceRequirements(),
}
}

func setFakeResourceRequirements() *corev1.ResourceRequirements {
defaultCPURequest, _ := resource.ParseQuantity(consts.DefaultCPURequest)
defaultMemoryRequest, _ := resource.ParseQuantity(consts.DefaultMemRequest)
defaultEphemeralStorageRequest, _ := resource.ParseQuantity(consts.DefaultDiskRequest)
defaultCPURequest, _ := resource.ParseQuantity(DefaultCPURequest)
defaultMemoryRequest, _ := resource.ParseQuantity(DefaultMemRequest)
defaultEphemeralStorageRequest, _ := resource.ParseQuantity(DefaultDiskRequest)

defaultCPULimit, _ := resource.ParseQuantity(consts.DefaultCPULimit)
defaultMemoryLimit, _ := resource.ParseQuantity(consts.DefaultMemLimit)
defaultEphemeralStorageLimit, _ := resource.ParseQuantity(consts.DefaultDiskLimit)
defaultCPULimit, _ := resource.ParseQuantity(DefaultCPULimit)
defaultMemoryLimit, _ := resource.ParseQuantity(DefaultMemLimit)
defaultEphemeralStorageLimit, _ := resource.ParseQuantity(DefaultDiskLimit)

return &corev1.ResourceRequirements{
Requests: map[corev1.ResourceName]resource.Quantity{
Expand Down
25 changes: 0 additions & 25 deletions pkg/controller.v1beta1/consts/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ package consts
import (
"time"

corev1 "k8s.io/api/core/v1"

"github.com/kubeflow/katib/pkg/util/v1beta1/env"
)

Expand Down Expand Up @@ -114,29 +112,6 @@ const (
KatibConfigMapName = "katib-config"
// LabelKatibConfigTag is the name of KatibConfig in Katib copnfigmap.
LabelKatibConfigTag = "katib-config.yaml"
// DefaultImagePullPolicy is the default value for image pull policy.
DefaultImagePullPolicy = corev1.PullIfNotPresent
// DefaultCPULimit is the default value for CPU limit.
DefaultCPULimit = "500m"
// DefaultCPURequest is the default value for CPU request.
DefaultCPURequest = "50m"
// DefaultMemLimit is the default value for memory limit.
DefaultMemLimit = "100Mi"
// DefaultMemRequest is the default value for memory request.
DefaultMemRequest = "10Mi"
// DefaultDiskLimit is the default value for disk limit.
DefaultDiskLimit = "5Gi"
// DefaultDiskRequest is the default value for disk request.
DefaultDiskRequest = "500Mi"

// DefaultContainerSuggestionVolumeMountPath is the default mount path in suggestion container
DefaultContainerSuggestionVolumeMountPath = "/opt/katib/data"

// DefaultSuggestionVolumeStorage is the default value for suggestion's volume storage
DefaultSuggestionVolumeStorage = "1Gi"

// DefaultSuggestionVolumeAccessMode is the default value for suggestion's volume access mode
DefaultSuggestionVolumeAccessMode = corev1.ReadWriteOnce

// SuggestionVolumeMountKey specifies the AlgorithmSettings key used to toggle Suggestion managed trial storage
SuggestionVolumeMountKey = "suggestion_trial_dir"
Expand Down
14 changes: 7 additions & 7 deletions pkg/controller.v1beta1/suggestion/composer/composer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ func TestDesiredDeployment(t *testing.T) {
}(),
expectedDeployment: func() *appsv1.Deployment {
deploy := newFakeDeployment()
deploy.Spec.Template.Spec.Containers[0].ImagePullPolicy = consts.DefaultImagePullPolicy
deploy.Spec.Template.Spec.Containers[1].ImagePullPolicy = consts.DefaultImagePullPolicy
deploy.Spec.Template.Spec.Containers[0].ImagePullPolicy = configv1beta1.DefaultImagePullPolicy
deploy.Spec.Template.Spec.Containers[1].ImagePullPolicy = configv1beta1.DefaultImagePullPolicy
return deploy
}(),
err: false,
Expand Down Expand Up @@ -853,7 +853,7 @@ func newFakeContainers() []corev1.Container {
VolumeMounts: []corev1.VolumeMount{
{
Name: consts.ContainerSuggestionVolumeName,
MountPath: consts.DefaultContainerSuggestionVolumeMountPath,
MountPath: configv1beta1.DefaultContainerSuggestionVolumeMountPath,
},
},
},
Expand Down Expand Up @@ -885,7 +885,7 @@ func newFakeContainers() []corev1.Container {

func newFakePVC() *corev1.PersistentVolumeClaim {

volumeStorage, _ := resource.ParseQuantity(consts.DefaultSuggestionVolumeStorage)
volumeStorage, _ := resource.ParseQuantity(configv1beta1.DefaultSuggestionVolumeStorage)

return &corev1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -903,7 +903,7 @@ func newFakePVC() *corev1.PersistentVolumeClaim {
},
Spec: corev1.PersistentVolumeClaimSpec{
AccessModes: []corev1.PersistentVolumeAccessMode{
consts.DefaultSuggestionVolumeAccessMode,
configv1beta1.DefaultSuggestionVolumeAccessMode,
},
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{
Expand All @@ -916,7 +916,7 @@ func newFakePVC() *corev1.PersistentVolumeClaim {

func newFakePV() *corev1.PersistentVolume {
pvName := suggestionName + "-" + suggestionAlgorithm + "-" + namespace
volumeStorage, _ := resource.ParseQuantity(consts.DefaultSuggestionVolumeStorage)
volumeStorage, _ := resource.ParseQuantity(configv1beta1.DefaultSuggestionVolumeStorage)

return &corev1.PersistentVolume{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -927,7 +927,7 @@ func newFakePV() *corev1.PersistentVolume {
StorageClassName: storageClassName,
PersistentVolumeReclaimPolicy: corev1.PersistentVolumeReclaimDelete,
AccessModes: []corev1.PersistentVolumeAccessMode{
consts.DefaultSuggestionVolumeAccessMode,
configv1beta1.DefaultSuggestionVolumeAccessMode,
},
PersistentVolumeSource: corev1.PersistentVolumeSource{
HostPath: &corev1.HostPathVolumeSource{
Expand Down
Loading

0 comments on commit 199aa8b

Please sign in to comment.