Skip to content

Commit

Permalink
refactor worker (#226)
Browse files Browse the repository at this point in the history
  • Loading branch information
matheusfm authored Aug 10, 2023
1 parent db12033 commit 1009639
Show file tree
Hide file tree
Showing 21 changed files with 994 additions and 1,282 deletions.
24 changes: 13 additions & 11 deletions cmd/worker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,32 @@
package main

import (
"context"
"flag"
"os"
"time"

"github.com/go-logr/logr"
"go.uber.org/zap/zapcore"

ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

"github.com/undistro/zora/pkg/worker"
)

var log = ctrl.Log.WithName("worker")

func main() {
opts := zap.Options{
Development: true,
TimeEncoder: zapcore.TimeEncoderOfLayout(time.RFC3339),
}
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))

log.Info("Starting worker")
if err := worker.Run(log); err != nil {
log.Info("Worker crashed")
panic(err)
opts.BindFlags(flag.CommandLine)
log := zap.New(zap.UseFlagOptions(&opts)).WithName("worker")
ctx := logr.NewContext(context.Background(), log)

log.Info("starting worker")
if err := worker.Run(ctx); err != nil {
log.Error(err, "failed to run worker")
os.Exit(1)
}
log.Info("Worker finished successfully")
log.Info("Stopping worker")
log.Info("worker finished successfully")
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/undistro/zora
go 1.20

require (
github.com/caarlos0/env/v9 v9.0.0
github.com/go-logr/logr v1.2.4
github.com/google/go-cmp v0.5.9
github.com/onsi/ginkgo/v2 v2.9.5
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLj
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/caarlos0/env/v9 v9.0.0 h1:SI6JNsOA+y5gj9njpgybykATIylrRMklbs5ch6wO6pc=
github.com/caarlos0/env/v9 v9.0.0/go.mod h1:ye5mlCVMYh6tZ+vCgrs/B95sj88cg5Tlnc0XIzgZ020=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
Expand Down
14 changes: 12 additions & 2 deletions pkg/plugins/cronjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ var (
Name: "DONE_DIR",
Value: resultsDir,
},
{
Name: "DONE_FILE",
Value: filepath.Join(resultsDir, "done"),
},
{
Name: "ERROR_FILE",
Value: filepath.Join(resultsDir, "error"),
},
}
// commonVolumeMounts represents the volume mounts to be used in worker and plugin containers
commonVolumeMounts = []corev1.VolumeMount{
Expand Down Expand Up @@ -259,8 +267,10 @@ func (r *CronJobMutator) workerEnv() []corev1.EnvVar {
Value: r.ClusterScan.Spec.ClusterRef.Name,
},
corev1.EnvVar{
Name: "CLUSTER_ISSUES_NAMESPACE",
Value: r.ClusterScan.Namespace,
Name: "NAMESPACE",
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{FieldPath: "metadata.namespace", APIVersion: "v1"},
},
},
corev1.EnvVar{
Name: "PLUGIN_NAME",
Expand Down
49 changes: 49 additions & 0 deletions pkg/worker/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2023 Undistro Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package worker

import (
"strings"
"time"

"github.com/caarlos0/env/v9"
)

// config represents worker configuration
type config struct {
DoneFile string `env:"DONE_FILE" envDefault:"/tmp/zora/results/done"`
ErrorFile string `env:"ERROR_FILE" envDefault:"/tmp/zora/results/error"`
PluginName string `env:"PLUGIN_NAME,required"`
ClusterName string `env:"CLUSTER_NAME,required"`
Namespace string `env:"NAMESPACE,required"`
JobName string `env:"JOB_NAME,required"`
JobUID string `env:"JOB_UID,required"`
PodName string `env:"POD_NAME,required"`
WaitInterval time.Duration `env:"WAIT_INTERVAL" envDefault:"1s"`

suffix string
}

// configFromEnv returns a config from environment variables
func configFromEnv() (*config, error) {
cfg := &config{}
if err := env.Parse(cfg); err != nil {
return nil, err
}
if i := strings.LastIndex(cfg.PodName, "-"); i > 0 && len(cfg.PodName) > i+1 {
cfg.suffix = cfg.PodName[i+1:]
}
return cfg, nil
}
182 changes: 0 additions & 182 deletions pkg/worker/config/config.go

This file was deleted.

Loading

0 comments on commit 1009639

Please sign in to comment.