Skip to content

Commit

Permalink
Merge pull request #70 from zqzten/cronjob
Browse files Browse the repository at this point in the history
HorizontalPortrait: Support service account defaulting for algorithm job
  • Loading branch information
dayko2019 authored Oct 19, 2023
2 parents 4e6365b + 7a25c88 commit 786848e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
13 changes: 8 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ type promConfig struct {

type algorithmJobConfig struct {
Namespace string
DefaultServiceAccount string
DefaultMetricsServerAddr string
DefaultImagePredictiveHorizontal string
DefaultImageHorizontalPredictive string
}

func main() {
Expand Down Expand Up @@ -150,10 +151,12 @@ func main() {
flag.DurationVar(&promConfig.MetricsMaxAge, "prometheus-metrics-max-age", 0,
"The period for which to query the set of available metrics from Prometheus. If not set, it defaults to prometheus-metrics-relist-interval.")
flag.StringVar(&algorithmJobConfig.Namespace, "algorithm-job-namespace", "", "The namespace where algorithm jobs should run in.")
flag.StringVar(&algorithmJobConfig.DefaultServiceAccount, "algorithm-job-default-service-account", "",
"The default service account for algorithm jobs.")
flag.StringVar(&algorithmJobConfig.DefaultMetricsServerAddr, "algorithm-job-default-metrics-server-addr", "",
"The default metrics server address to inject to env of algorithm jobs.")
flag.StringVar(&algorithmJobConfig.DefaultImagePredictiveHorizontal, "algorithm-job-default-image-predictive-horizontal", "",
"The default image for algorithm jobs of predictive horizontal portrait.")
flag.StringVar(&algorithmJobConfig.DefaultImageHorizontalPredictive, "algorithm-job-default-image-horizontal-predictive", "",
"The default image for algorithm jobs of horizontal predictive portrait.")
opts := zap.Options{
Development: true,
TimeEncoder: zapcore.RFC3339TimeEncoder,
Expand Down Expand Up @@ -445,8 +448,8 @@ func initExternalHorizontalPortraitAlgorithmJobControllers(client client.Client,
return nil, fmt.Errorf("shall set namespace for algorithm jobs")
}
controllers := make(map[autoscalingv1alpha1.PortraitAlgorithmJobType]jobcontroller.Horizontal)
controllers[autoscalingv1alpha1.CronJobPortraitAlgorithmJobType] = jobcontroller.NewCronJobHorizontal(client, config.Namespace, config.DefaultMetricsServerAddr, map[autoscalingv1alpha1.PortraitType]string{
autoscalingv1alpha1.PredictivePortraitType: config.DefaultImagePredictiveHorizontal,
controllers[autoscalingv1alpha1.CronJobPortraitAlgorithmJobType] = jobcontroller.NewCronJobHorizontal(client, config.Namespace, config.DefaultServiceAccount, config.DefaultMetricsServerAddr, map[autoscalingv1alpha1.PortraitType]string{
autoscalingv1alpha1.PredictivePortraitType: config.DefaultImageHorizontalPredictive,
})
return controllers, nil
}
Expand Down
8 changes: 7 additions & 1 deletion pkg/portrait/algorithm/externaljob/jobcontroller/cronjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,16 @@ const (
type CronJobHorizontal struct {
client client.Client
namespace string
defaultServiceAccount string
defaultMetricsServerAddr string
defaultImages map[autoscalingv1alpha1.PortraitType]string
}

func NewCronJobHorizontal(client client.Client, namespace, defaultMetricsServerAddr string, defaultImages map[autoscalingv1alpha1.PortraitType]string) Horizontal {
func NewCronJobHorizontal(client client.Client, namespace, defaultServiceAccount, defaultMetricsServerAddr string, defaultImages map[autoscalingv1alpha1.PortraitType]string) Horizontal {
return &CronJobHorizontal{
client: client,
namespace: namespace,
defaultServiceAccount: defaultServiceAccount,
defaultMetricsServerAddr: defaultMetricsServerAddr,
defaultImages: defaultImages,
}
Expand Down Expand Up @@ -135,6 +137,10 @@ func (h *CronJobHorizontal) buildCronJobNamespacedName(hp *autoscalingv1alpha1.H
}

func (h *CronJobHorizontal) defaultingTemplatePodSpec(spec *corev1.PodSpec, hp *autoscalingv1alpha1.HorizontalPortrait) {
if h.defaultServiceAccount != "" && spec.ServiceAccountName == "" && spec.DeprecatedServiceAccount == "" {
spec.ServiceAccountName = h.defaultServiceAccount
spec.DeprecatedServiceAccount = h.defaultServiceAccount
}
for i := range spec.Containers {
container := &spec.Containers[i]
if container.Name != defaultAlgorithmContainerName {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func TestCronJobHorizontal_Create(t *testing.T) {
},
}

cronJobHorizontal := NewCronJobHorizontal(fakeClient, cronJobNamespace, "", nil)
cronJobHorizontal := NewCronJobHorizontal(fakeClient, cronJobNamespace, "", "", nil)
err := cronJobHorizontal.UpdateJob(ctx, horizontalPortrait, cfg)
assert.Nil(t, err)

Expand Down Expand Up @@ -126,7 +126,7 @@ func TestCronJobHorizontal_Update(t *testing.T) {
}
_ = fakeClient.Create(ctx, cronJob)

cronJobHorizontal := NewCronJobHorizontal(fakeClient, cronJobNamespace, "", nil)
cronJobHorizontal := NewCronJobHorizontal(fakeClient, cronJobNamespace, "", "", nil)
err := cronJobHorizontal.UpdateJob(ctx, horizontalPortrait, cfg)
assert.Nil(t, err)

Expand Down

0 comments on commit 786848e

Please sign in to comment.