Feat: change the interface

Signed-off-by: barnettZQG <barnett.zqg@gmail.com>
This commit is contained in:
barnettZQG 2022-06-06 20:49:29 +08:00
parent 62c03702af
commit ca039f85a7
9 changed files with 87 additions and 59 deletions

View File

@ -222,6 +222,21 @@ func (in *CanaryStrategy) DeepCopy() *CanaryStrategy {
return out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *GatewayTrafficRouting) DeepCopyInto(out *GatewayTrafficRouting) {
*out = *in
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new GatewayTrafficRouting.
func (in *GatewayTrafficRouting) DeepCopy() *GatewayTrafficRouting {
if in == nil {
return nil
}
out := new(GatewayTrafficRouting)
in.DeepCopyInto(out)
return out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *IngressTrafficRouting) DeepCopyInto(out *IngressTrafficRouting) {
*out = *in
@ -466,6 +481,11 @@ func (in *TrafficRouting) DeepCopyInto(out *TrafficRouting) {
*out = new(IngressTrafficRouting)
**out = **in
}
if in.Gateway != nil {
in, out := &in.Gateway, &out.Gateway
*out = new(GatewayTrafficRouting)
**out = **in
}
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new TrafficRouting.

View File

@ -202,26 +202,6 @@ rules:
- get
- patch
- update
- apiGroups:
- networking.k8s.io
resources:
- ingresses
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- networking.k8s.io
resources:
- ingresses/status
verbs:
- get
- patch
- update
- apiGroups:
- gateway.networking.k8s.io
resources:
@ -242,6 +222,26 @@ rules:
- get
- patch
- update
- apiGroups:
- networking.k8s.io
resources:
- ingresses
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- networking.k8s.io
resources:
- ingresses/status
verbs:
- get
- patch
- update
- apiGroups:
- rollouts.kruise.io
resources:

View File

@ -304,7 +304,7 @@ func (r *RolloutReconciler) verifyTrafficRouting(ns string, tr *rolloutv1alpha1.
}
// check the traffic routing configuration
health, err := c.TrafficRouting()
health, err := c.Validate(context.Background())
if err != nil {
return false, "", err
}

View File

@ -55,6 +55,8 @@ type RolloutReconciler struct {
//+kubebuilder:rbac:groups=core,resources=services/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=networking.k8s.io,resources=ingresses,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=networking.k8s.io,resources=ingresses/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=gateway.networking.k8s.io,resources=httproutes,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=gateway.networking.k8s.io,resources=httproutes/status,verbs=get;update;patch
// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.

View File

@ -125,12 +125,12 @@ func (r *rolloutContext) doCanaryTrafficRouting() (bool, error) {
steps := len(r.rollout.Spec.Strategy.Canary.Steps)
cond := util.GetRolloutCondition(*r.newStatus, rolloutv1alpha1.RolloutConditionProgressing)
cond.Message = fmt.Sprintf("Rollout is in step(%d/%d), and route traffic weight(%d)", canaryStatus.CurrentStepIndex, steps, desiredWeight)
verify, err := trController.Verify(desiredWeight)
verify, err := trController.CheckWeight(context.Background(), desiredWeight)
if err != nil {
return false, err
} else if !verify {
r.recorder.Eventf(r.rollout, corev1.EventTypeNormal, "Progressing", fmt.Sprintf("traffic route weight(%d) done", desiredWeight))
return false, trController.SetRoutes(context.Background(), desiredWeight)
return false, trController.SetWeight(context.Background(), desiredWeight)
}
klog.Infof("rollout(%s/%s) do step(%d) trafficRouting(%d) success", r.rollout.Namespace, r.rollout.Name, r.newStatus.CanaryStatus.CurrentStepIndex, desiredWeight)
return true, nil
@ -199,12 +199,12 @@ func (r *rolloutContext) doFinalisingTrafficRouting() (bool, error) {
klog.Errorf("rollout(%s/%s) newTrafficRoutingController failed: %s", r.rollout.Namespace, r.rollout.Name, err.Error())
return false, err
}
verify, err := trController.Verify(-1)
verify, err := trController.CheckWeight(context.Background(), -1)
if err != nil {
return false, err
} else if !verify {
r.newStatus.CanaryStatus.LastUpdateTime = &metav1.Time{Time: time.Now()}
err = trController.SetRoutes(context.Background(), 0)
err = trController.SetWeight(context.Background(), 0)
if err != nil && errors.IsNotFound(err) {
klog.Warningf("rollout(%s/%s) VerifyTrafficRouting(-1), and stable ingress not found", r.rollout.Namespace, r.rollout.Name)
return false, nil
@ -220,7 +220,7 @@ func (r *rolloutContext) doFinalisingTrafficRouting() (bool, error) {
}
}
// DoFinalising, such as delete nginx canary ingress
if err = trController.Finalise(); err != nil {
if err = trController.Finalise(context.Background()); err != nil {
return false, err
}

View File

@ -58,7 +58,7 @@ func NewGatewayTrafficRouting(client client.Client, newStatus *rolloutv1alpha1.R
return r, nil
}
func (r *gatewayController) SetRoutes(ctx context.Context, desiredWeight int32) error {
func (r *gatewayController) SetWeight(ctx context.Context, desiredWeight int32) error {
var httpRoute gatewayv1alpha2.HTTPRoute
err := r.Get(ctx, types.NamespacedName{Namespace: r.conf.RolloutNs, Name: r.conf.TrafficConf.HTTPRouteName}, &httpRoute)
if err != nil {
@ -84,10 +84,10 @@ func (r *gatewayController) SetRoutes(ctx context.Context, desiredWeight int32)
return nil
}
func (r *gatewayController) Verify(desiredWeight int32) (bool, error) {
func (r *gatewayController) CheckWeight(ctx context.Context, desiredWeight int32) (bool, error) {
// verify set weight routing
httpRoute := &gatewayv1alpha2.HTTPRoute{}
err := r.Get(context.TODO(), types.NamespacedName{Namespace: r.conf.RolloutNs, Name: r.conf.TrafficConf.HTTPRouteName}, httpRoute)
err := r.Get(ctx, types.NamespacedName{Namespace: r.conf.RolloutNs, Name: r.conf.TrafficConf.HTTPRouteName}, httpRoute)
if err != nil && !errors.IsNotFound(err) {
klog.Errorf("rollout(%s/%s) get canary HTTPRoute failed: %s", r.conf.RolloutNs, r.conf.RolloutName, err.Error())
return false, err
@ -123,26 +123,30 @@ func (r *gatewayController) Verify(desiredWeight int32) (bool, error) {
return true, nil
}
func (r *gatewayController) Finalise() error {
canaryRoute := &gatewayv1alpha2.HTTPRoute{}
err := r.Get(context.TODO(), types.NamespacedName{Namespace: r.conf.RolloutNs, Name: r.conf.TrafficConf.HTTPRouteName}, canaryRoute)
if err != nil && !errors.IsNotFound(err) {
klog.Errorf("rollout(%s/%s) get canary HTTPRoute(%s) failed: %s", r.conf.RolloutNs, r.conf.RolloutName, r.conf.TrafficConf.HTTPRouteName, err.Error())
func (r *gatewayController) Finalise(ctx context.Context) error {
httpRoute := &gatewayv1alpha2.HTTPRoute{}
err := r.Get(ctx, types.NamespacedName{Namespace: r.conf.RolloutNs, Name: r.conf.TrafficConf.HTTPRouteName}, httpRoute)
if err != nil {
if errors.IsNotFound(err) {
return nil
}
klog.Errorf("rollout(%s/%s) get canary HTTPRoute failed: %s", r.conf.RolloutNs, r.conf.RolloutName, err.Error())
return err
}
// immediate delete canary ingress
if err = r.Delete(context.TODO(), canaryRoute); err != nil {
klog.Errorf("rollout(%s/%s) remove canary HTTPRoute(%s) failed: %s", r.conf.RolloutNs, r.conf.RolloutName, canaryRoute.Name, err.Error())
if r.getHTTPRouteCanaryWeight(*httpRoute) == -1 {
return nil
}
r.buildCanaryHTTPRoute(httpRoute, -1)
if err := r.Update(ctx, httpRoute); err != nil {
klog.Errorf("rollout(%s/%s) update the canary HTTPRoute failed: %s", r.conf.RolloutNs, r.conf.RolloutName, err.Error())
return err
}
klog.Infof("rollout(%s/%s) remove canary HTTPRoute(%s) success", r.conf.RolloutNs, r.conf.RolloutName, canaryRoute.Name)
return nil
}
func (r *gatewayController) TrafficRouting() (bool, error) {
func (r *gatewayController) Validate(ctx context.Context) (bool, error) {
canaryRoute := &gatewayv1alpha2.HTTPRoute{}
err := r.Get(context.TODO(), types.NamespacedName{Namespace: r.conf.RolloutNs, Name: r.conf.TrafficConf.HTTPRouteName}, canaryRoute)
err := r.Get(ctx, types.NamespacedName{Namespace: r.conf.RolloutNs, Name: r.conf.TrafficConf.HTTPRouteName}, canaryRoute)
if err != nil {
if errors.IsNotFound(err) {
return false, fmt.Errorf("Ingress(%s/%s) is Not Found", r.conf.RolloutNs, r.conf.TrafficConf.HTTPRouteName)

View File

@ -63,7 +63,7 @@ func NewNginxTrafficRouting(client client.Client, newStatus *rolloutv1alpha1.Rol
return r, nil
}
func (r *nginxController) SetRoutes(ctx context.Context, desiredWeight int32) error {
func (r *nginxController) SetWeight(ctx context.Context, desiredWeight int32) error {
stableIngress := &netv1.Ingress{}
err := r.Get(ctx, types.NamespacedName{Namespace: r.conf.RolloutNs, Name: r.conf.TrafficConf.Name}, stableIngress)
if err != nil {
@ -106,10 +106,10 @@ func (r *nginxController) SetRoutes(ctx context.Context, desiredWeight int32) er
return nil
}
func (r *nginxController) Verify(desiredWeight int32) (bool, error) {
func (r *nginxController) CheckWeight(ctx context.Context, desiredWeight int32) (bool, error) {
// verify set weight routing
canaryIngress := &netv1.Ingress{}
err := r.Get(context.TODO(), types.NamespacedName{Namespace: r.conf.RolloutNs, Name: r.defaultCanaryIngressName()}, canaryIngress)
err := r.Get(ctx, types.NamespacedName{Namespace: r.conf.RolloutNs, Name: r.defaultCanaryIngressName()}, canaryIngress)
if err != nil && !errors.IsNotFound(err) {
klog.Errorf("rollout(%s/%s) get canary ingress failed: %s", r.conf.RolloutNs, r.conf.RolloutName, err.Error())
return false, err
@ -123,7 +123,7 @@ func (r *nginxController) Verify(desiredWeight int32) (bool, error) {
}
// stable ingress
stableIngress := &netv1.Ingress{}
err = r.Get(context.TODO(), types.NamespacedName{Namespace: r.conf.RolloutNs, Name: r.conf.TrafficConf.Name}, stableIngress)
err = r.Get(ctx, types.NamespacedName{Namespace: r.conf.RolloutNs, Name: r.conf.TrafficConf.Name}, stableIngress)
if err != nil && !errors.IsNotFound(err) {
return false, err
}
@ -155,9 +155,9 @@ func (r *nginxController) Verify(desiredWeight int32) (bool, error) {
return true, nil
}
func (r *nginxController) Finalise() error {
func (r *nginxController) Finalise(ctx context.Context) error {
canaryIngress := &netv1.Ingress{}
err := r.Get(context.TODO(), types.NamespacedName{Namespace: r.conf.RolloutNs, Name: r.defaultCanaryIngressName()}, canaryIngress)
err := r.Get(ctx, types.NamespacedName{Namespace: r.conf.RolloutNs, Name: r.defaultCanaryIngressName()}, canaryIngress)
if err != nil && !errors.IsNotFound(err) {
klog.Errorf("rollout(%s/%s) get canary ingress(%s) failed: %s", r.conf.RolloutNs, r.conf.RolloutName, r.defaultCanaryIngressName(), err.Error())
return err
@ -168,7 +168,7 @@ func (r *nginxController) Finalise() error {
}
// immediate delete canary ingress
if err = r.Delete(context.TODO(), canaryIngress); err != nil {
if err = r.Delete(ctx, canaryIngress); err != nil {
klog.Errorf("rollout(%s/%s) remove canary ingress(%s) failed: %s", r.conf.RolloutNs, r.conf.RolloutName, canaryIngress.Name, err.Error())
return err
}
@ -232,9 +232,9 @@ func (r *nginxController) defaultCanaryIngressName() string {
return fmt.Sprintf("%s-canary", r.conf.TrafficConf.Name)
}
func (r *nginxController) TrafficRouting() (bool, error) {
func (r *nginxController) Validate(ctx context.Context) (bool, error) {
ingress := &netv1.Ingress{}
err := r.Get(context.TODO(), types.NamespacedName{Namespace: r.conf.RolloutNs, Name: r.conf.TrafficConf.Name}, ingress)
err := r.Get(ctx, types.NamespacedName{Namespace: r.conf.RolloutNs, Name: r.conf.TrafficConf.Name}, ingress)
if err != nil {
if errors.IsNotFound(err) {
return false, fmt.Errorf("Ingress(%s/%s) is Not Found", r.conf.RolloutNs, r.conf.TrafficConf.Name)

View File

@ -20,12 +20,14 @@ import "context"
// Controller common function across all TrafficRouting implementation
type Controller interface {
// SetRoutes set canary desired weight
SetRoutes(ctx context.Context, desiredWeight int32) error
// Verify check if canary has been set desired weight
Verify(desiredWeight int32) (bool, error)
// Finalise will do some cleanup work, such as delete canary ingress
Finalise() error
// TrafficRouting will check the traffic routing resource
TrafficRouting() (bool, error)
// Validate will validate the traffic routing resource,
Validate(ctx context.Context) (bool, error)
// SetWeight set canary desired weight.
// set the desired weight to -1 means complete the canary rollout.
SetWeight(ctx context.Context, desiredWeight int32) error
// CheckWeight check if canary has been set desired weight.
CheckWeight(ctx context.Context, desiredWeight int32) (bool, error)
// Finalise will do some cleanup work after the canary rollout complete, such as delete canary ingress.
// Finalise is called with a 3-second delay after completing the canary.
Finalise(ctx context.Context) error
}

View File

@ -23,6 +23,6 @@ spec:
pause: {duration: 0}
trafficRoutings:
- service: echoserver
type: nginx
ingress:
classType: nginx
name: echoserver