Skip to content

Commit

Permalink
Feat: add controller require for canary (#84)
Browse files Browse the repository at this point in the history
Signed-off-by: FogDong <[email protected]>

Signed-off-by: FogDong <[email protected]>
  • Loading branch information
FogDong authored Nov 11, 2022
1 parent 420b2ea commit 7f1c99c
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 15 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ jobs:
else
echo ::set-output name=TAG::${GITHUB_REF#refs/tags/}
fi
echo ::set-output name=GITVERSION::git-$(git rev-parse --short HEAD)
- name: Login Docker Hub
uses: docker/login-action@v1
with:
Expand All @@ -46,6 +47,8 @@ jobs:
push: ${{ github.event_name != 'pull_request' }}
build-args: |
GOPROXY=https://proxy.golang.org
VERSION=${{ steps.vars.outputs.TAG }}
GIT_VERSION=${{ steps.vars.outputs.GITVERSION }}
tags: |-
docker.io/oamdev/vela-workflow:${{ steps.vars.outputs.TAG }}
Expand Down
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ COPY version/ version/

# Build
ARG TARGETARCH

ARG VERSION
ARG GITVERSION
RUN GO111MODULE=on CGO_ENABLED=0 GOOS=linux GOARCH=${TARGETARCH} \
go build -a -ldflags "-s -w" \
go build -a -ldflags "-s -w -X github.com/kubevela/workflow/version.VelaVersion=${VERSION:-undefined} -X github.com/kubevela/workflow/version.GitRevision=${GITVERSION:-undefined}" \
-o vela-workflow-${TARGETARCH} cmd/main.go

FROM ${BASE_IMAGE:-alpine:3.15}
Expand Down
1 change: 1 addition & 0 deletions charts/vela-workflow/templates/workflow-controller.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ spec:
- "--leader-elect"
- "--health-probe-bind-address=:{{ .Values.healthCheck.port }}"
- "--concurrent-reconciles={{ .Values.concurrentReconciles }}"
- "--ignore-workflow-without-controller-requirement={{ .Values.ignoreWorkflowWithoutControllerRequirement }}"
- "--kube-api-qps={{ .Values.kubeClient.qps }}"
- "--kube-api-burst={{ .Values.kubeClient.burst }}"
- "--max-workflow-wait-backoff-time={{ .Values.workflow.backoff.maxTime.waitState }}"
Expand Down
2 changes: 2 additions & 0 deletions charts/vela-workflow/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ systemDefinitionNamespace:

## @param concurrentReconciles concurrentReconciles is the concurrent reconcile number of the controller
concurrentReconciles: 4
## @param ignoreWorkflowWithoutControllerRequirement will determine whether to process the workflowrun without 'workflowrun.oam.dev/controller-version-require' annotation
ignoreWorkflowWithoutControllerRequirement: false

## @section KubeVela workflow parameters

Expand Down
12 changes: 7 additions & 5 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func main() {
"The duration the LeaderElector clients should wait between tries of actions")
flag.IntVar(&webhookPort, "webhook-port", 9443, "admission webhook listen address")
flag.IntVar(&controllerArgs.ConcurrentReconciles, "concurrent-reconciles", 4, "concurrent-reconciles is the concurrent reconcile number of the controller. The default value is 4")
flag.BoolVar(&controllerArgs.IgnoreWorkflowWithoutControllerRequirement, "ignore-workflow-without-controller-requirement", false, "If true, workflow controller will not process the workflowrun without 'workflowrun.oam.dev/controller-version-require' annotation")
flag.Float64Var(&qps, "kube-api-qps", 50, "the qps for reconcile clients. Low qps may lead to low throughput. High qps may give stress to api-server. Raise this value if concurrent-reconciles is set to be high.")
flag.IntVar(&burst, "kube-api-burst", 100, "the burst for reconcile clients. Recommend setting it qps*2.")
flag.StringVar(&pprofAddr, "pprof-addr", "", "The address for pprof to use while exporting profiling results. The default value is empty which means do not expose it. Set it to address like :6666 to expose it.")
Expand Down Expand Up @@ -201,11 +202,12 @@ func main() {
kubeClient := mgr.GetClient()

if err = (&controllers.WorkflowRunReconciler{
Client: kubeClient,
Scheme: mgr.GetScheme(),
PackageDiscover: pd,
Recorder: event.NewAPIRecorder(mgr.GetEventRecorderFor("WorkflowRun")),
Args: controllerArgs,
Client: kubeClient,
Scheme: mgr.GetScheme(),
PackageDiscover: pd,
Recorder: event.NewAPIRecorder(mgr.GetEventRecorderFor("WorkflowRun")),
ControllerVersion: version.VelaVersion,
Args: controllerArgs,
}).SetupWithManager(mgr); err != nil {
klog.Error(err, "unable to create controller", "controller", "WorkflowRun")
os.Exit(1)
Expand Down
26 changes: 23 additions & 3 deletions controllers/workflowrun_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,17 @@ import (
type Args struct {
// ConcurrentReconciles is the concurrent reconcile number of the controller
ConcurrentReconciles int
// IgnoreWorkflowWithoutControllerRequirement indicates that workflow controller will not process the workflowrun without 'workflowrun.oam.dev/controller-version-require' annotation.
IgnoreWorkflowWithoutControllerRequirement bool
}

// WorkflowRunReconciler reconciles a WorkflowRun object
type WorkflowRunReconciler struct {
client.Client
Scheme *runtime.Scheme
PackageDiscover *packages.PackageDiscover
Recorder event.Recorder
Scheme *runtime.Scheme
PackageDiscover *packages.PackageDiscover
Recorder event.Recorder
ControllerVersion string
Args
}

Expand Down Expand Up @@ -90,6 +93,11 @@ func (r *WorkflowRunReconciler) Reconcile(ctx context.Context, req ctrl.Request)
return ctrl.Result{}, client.IgnoreNotFound(err)
}

if !r.matchControllerRequirement(run) {
logCtx.Info("skip workflowrun: not match the controller requirement of workflowrun")
return ctrl.Result{}, nil
}

timeReporter := timeReconcile(run)
defer timeReporter()

Expand Down Expand Up @@ -163,6 +171,18 @@ func (r *WorkflowRunReconciler) Reconcile(ctx context.Context, req ctrl.Request)
return ctrl.Result{}, nil
}

func (r *WorkflowRunReconciler) matchControllerRequirement(wr *v1alpha1.WorkflowRun) bool {
if wr.Annotations != nil {
if requireVersion, ok := wr.Annotations[types.AnnotationControllerRequirement]; ok {
return requireVersion == r.ControllerVersion
}
}
if r.IgnoreWorkflowWithoutControllerRequirement {
return false
}
return true
}

// SetupWithManager sets up the controller with the Manager.
func (r *WorkflowRunReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
Expand Down
6 changes: 3 additions & 3 deletions e2e/definition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"os"
"time"

"github.com/kubevela/workflow/api/v1alpha1"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
Expand All @@ -30,7 +29,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/yaml"

"github.com/oam-dev/kubevela/pkg/oam/util"
"github.com/kubevela/workflow/api/v1alpha1"
"github.com/kubevela/workflow/pkg/utils"
)

var _ = Describe("Test the workflow run with the built-in definitions", func() {
Expand All @@ -45,7 +45,7 @@ var _ = Describe("Test the workflow run with the built-in definitions", func() {

Eventually(func() error {
return k8sClient.Create(ctx, &ns)
}, time.Second*3, time.Microsecond*300).Should(SatisfyAny(BeNil(), &util.AlreadyExistMatcher{}))
}, time.Second*3, time.Microsecond*300).Should(SatisfyAny(BeNil(), &utils.AlreadyExistMatcher{}))
})

It("Test the workflow with config definition", func() {
Expand Down
4 changes: 2 additions & 2 deletions makefiles/const.mk
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ VELA_VERSION ?= master
# Repo info
GIT_COMMIT ?= git-$(shell git rev-parse --short HEAD)
GIT_COMMIT_LONG ?= $(shell git rev-parse HEAD)
VELA_VERSION_KEY := github.com/oam-dev/kubevela/version.VelaVersion
VELA_GITVERSION_KEY := github.com/oam-dev/kubevela/version.GitRevision
VELA_VERSION_KEY := github.com/kubevela/workflow/version.VelaVersion
VELA_GITVERSION_KEY := github.com/kubevela/workflow/version.GitRevision
LDFLAGS ?= "-s -w -X $(VELA_VERSION_KEY)=$(VELA_VERSION) -X $(VELA_GITVERSION_KEY)=$(GIT_COMMIT)"


Expand Down
2 changes: 2 additions & 0 deletions pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ const (
const (
// AnnotationWorkflowRunDebug is the annotation for debug
AnnotationWorkflowRunDebug = "workflowrun.oam.dev/debug"
// AnnotationControllerRequirement indicates the controller version that can process the workflow run
AnnotationControllerRequirement = "workflowrun.oam.dev/controller-version-require"
)

// IsStepFinish will decide whether step is finish.
Expand Down

0 comments on commit 7f1c99c

Please sign in to comment.