Skip to content

Commit

Permalink
Merge pull request #20 from oviceinc/sre-1760/watch-argo
Browse files Browse the repository at this point in the history
SRE-1760: Watch ArgoRollout resources that is specified in RolloutSca…
  • Loading branch information
h3poteto authored Dec 5, 2023
2 parents 0c86457 + 0ea5d20 commit 5bddf82
Show file tree
Hide file tree
Showing 11 changed files with 398 additions and 77 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ help: ## Display this help.

.PHONY: manifests
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./api/..." output:crd:artifacts:config=config/crd/bases
$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="{./api/..., ./internal/controller/...}" output:crd:artifacts:config=config/crd/bases

.PHONY: generate
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
Expand Down
6 changes: 4 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log/zap"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"

argorolloutsapiv1alpha1 "github.com/argoproj/argo-rollouts/pkg/apis/rollouts/v1alpha1"
rolloutv1alpha1 "github.com/oviceinc/rollout-optimizer-controller/api/v1alpha1"
"github.com/oviceinc/rollout-optimizer-controller/internal/controller"
//+kubebuilder:scaffold:imports
Expand All @@ -46,6 +47,7 @@ func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))

utilruntime.Must(rolloutv1alpha1.AddToScheme(scheme))
utilruntime.Must(argorolloutsapiv1alpha1.AddToScheme(scheme))
//+kubebuilder:scaffold:scheme
}

Expand Down Expand Up @@ -89,11 +91,11 @@ func main() {
os.Exit(1)
}

if err = (&controller.RolloutScaleDownReconciler{
if err = (&controller.ArgoRolloutReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "RolloutScaleDown")
setupLog.Error(err, "unable to create controller", "controller", "ArgoRollout")
os.Exit(1)
}
//+kubebuilder:scaffold:builder
Expand Down
14 changes: 14 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@ kind: ClusterRole
metadata:
name: manager-role
rules:
- apiGroups:
- argoproj.io
resources:
- rollouts
verbs:
- get
- list
- watch
- apiGroups:
- argoproj.io
resources:
- rollouts/status
verbs:
- get
- apiGroups:
- rollout.ovice.com
resources:
Expand Down
28 changes: 28 additions & 0 deletions config/samples/argorollout.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
apiVersion: argoproj.io/v1alpha1
kind: Rollout
metadata:
name: rollout-sample
spec:
replicas: 2
revisionHistoryLimit: 2
selector:
matchLabels:
app: rollout-sample
template:
metadata:
labels:
app: rollout-sample
spec:
containers:
- name: nginx
image: nginx:1.25.0
imagePullPolicy: Always
ports:
- containerPort: 80
strategy:
blueGreen:
activeService: rollout-bluegreen-active
previewService: rollout-bluegreen-preview
autoPromotionEnabled: true
# Do not scaleDown from ArgoRollout
scaleDownDelaySeconds: 0
4 changes: 4 additions & 0 deletions config/samples/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
## Append samples of your project ##
namespace: rollout-test

resources:
- service.yaml
- argorollout.yaml
- rollout_v1alpha1_rolloutscaledown.yaml
#+kubebuilder:scaffold:manifestskustomizesamples
4 changes: 2 additions & 2 deletions config/samples/rollout_v1alpha1_rolloutscaledown.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ metadata:
app.kubernetes.io/created-by: rollout-optimizer-controller
name: rolloutscaledown-sample
spec:
targetRollout: "example"
targetRollout: "rollout-sample"
terminatePerOnce: 1
coolTimeSeconds: 40
coolTimeSeconds: 40
26 changes: 26 additions & 0 deletions config/samples/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
apiVersion: v1
kind: Service
metadata:
name: rollout-bluegreen-active
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: rollout-sample
type: ClusterIP
---
apiVersion: v1
kind: Service
metadata:
name: rollout-bluegreen-preview
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: rollout-sample
type: ClusterIP
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
k8s.io/apimachinery v0.28.3
k8s.io/client-go v0.28.3
sigs.k8s.io/controller-runtime v0.16.3
github.com/argoproj/argo-rollouts v1.6.2
)

require (
Expand Down
253 changes: 253 additions & 0 deletions go.work.sum

Large diffs are not rendered by default.

65 changes: 65 additions & 0 deletions internal/controller/argorollout_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package controller

import (
"context"

argorolloutsapiv1alpha1 "github.com/argoproj/argo-rollouts/pkg/apis/rollouts/v1alpha1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"

optimizerv1alpha1 "github.com/oviceinc/rollout-optimizer-controller/api/v1alpha1"
)

type ArgoRolloutReconciler struct {
client.Client
Scheme *runtime.Scheme
}

//+kubebuilder:rbac:groups=argoproj.io,resources=rollouts,verbs=get;list;watch
//+kubebuilder:rbac:groups=argoproj.io,resources=rollouts/status,verbs=get
//+kubebuilder:rbac:groups=rollout.ovice.com,resources=rolloutscaledowns,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=rollout.ovice.com,resources=rolloutscaledowns/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=rollout.ovice.com,resources=rolloutscaledowns/finalizers,verbs=update

func (r *ArgoRolloutReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
logger := log.FromContext(ctx)
scaleDownList := &optimizerv1alpha1.RolloutScaleDownList{}
err := r.List(ctx, scaleDownList)
if err != nil {
logger.V(1).Error(err, "Faild to list RolloutScaleDown")
return ctrl.Result{}, err
}
var targetScaleDown *optimizerv1alpha1.RolloutScaleDown
for i := range scaleDownList.Items {
item := scaleDownList.Items[i]
if item.Namespace == req.Namespace && item.Spec.TargetRollout == req.Name {
targetScaleDown = &item
}
}
if targetScaleDown == nil {
logger.V(2).Info("Not target of ScaleDown", req.Namespace, req.Name)
return ctrl.Result{}, nil
}
argoRollout := &argorolloutsapiv1alpha1.Rollout{}
err = r.Get(ctx, types.NamespacedName{Name: req.Name, Namespace: req.Namespace}, argoRollout)
if err != nil && errors.IsNotFound(err) {
logger.V(1).Info("Not found ArgoRollout", req.Namespace, req.Name)
return ctrl.Result{}, nil
} else if err != nil {
logger.V(1).Error(err, "Failed to get ArgoRollout", req.Namespace, req.Name)
return ctrl.Result{}, err
}

logger.V(1).Info("Target ArgoRollout", argoRollout.Namespace, argoRollout.Name)
// TODO: Check ArgoRollout status and scaledown if needed

return ctrl.Result{}, nil
}

func (r *ArgoRolloutReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).For(&argorolloutsapiv1alpha1.Rollout{}).Complete(r)
}
72 changes: 0 additions & 72 deletions internal/controller/rolloutscaledown_controller.go

This file was deleted.

0 comments on commit 5bddf82

Please sign in to comment.