diff --git a/cmd/manager/main.go b/cmd/manager/main.go index 73e12aca8..3db4ce353 100644 --- a/cmd/manager/main.go +++ b/cmd/manager/main.go @@ -14,6 +14,7 @@ import ( prvd "k8s.io/cloud-provider-alibaba-cloud/pkg/provider" "k8s.io/cloud-provider-alibaba-cloud/pkg/provider/alibaba" "k8s.io/cloud-provider-alibaba-cloud/pkg/provider/dryrun" + utildryrun "k8s.io/cloud-provider-alibaba-cloud/pkg/util/dryrun" "k8s.io/cloud-provider-alibaba-cloud/version" "k8s.io/klog/v2/klogr" "sigs.k8s.io/controller-runtime/pkg/client/config" @@ -98,6 +99,18 @@ func main() { os.Exit(1) } + if ctrlCfg.ControllerCFG.DryRun { + go func() { + <-utildryrun.Done() + log.Info("ccm initial process finished.") + err := utildryrun.ResultEvent(mgr.GetClient(), utildryrun.SUCCESS, "ccm initial process finished") + if err != nil { + log.Error(err, "write precheck event failed") + } + os.Exit(0) + }() + } + if err := mgr.Start(signals.SetupSignalHandler()); err != nil { log.Error(err, "Manager exited non-zero: %s", err.Error()) os.Exit(1) diff --git a/pkg/controller/service/clbv1/dryrun.go b/pkg/controller/service/clbv1/dryrun.go index cdcb9ff13..13891dc22 100644 --- a/pkg/controller/service/clbv1/dryrun.go +++ b/pkg/controller/service/clbv1/dryrun.go @@ -2,13 +2,14 @@ package clbv1 import ( "context" + "os" + "sync" + v1 "k8s.io/api/core/v1" - "k8s.io/cloud-provider-alibaba-cloud/pkg/provider/dryrun" "k8s.io/cloud-provider-alibaba-cloud/pkg/util" + "k8s.io/cloud-provider-alibaba-cloud/pkg/util/dryrun" "k8s.io/klog/v2" - "os" "sigs.k8s.io/controller-runtime/pkg/client" - "sync" ) var initial = sync.Map{} @@ -31,11 +32,7 @@ func initMap(client client.Client) { } util.ServiceLog.Info("ccm initial process finished.", "length", length) if length == 0 { - err := dryrun.ResultEvent(client, dryrun.SUCCESS, "ccm initial process finished") - if err != nil { - util.ServiceLog.Error(err, "fail to write precheck event") - } - os.Exit(0) + dryrun.Finish(dryrun.SLB) } } diff --git a/pkg/controller/service/clbv1/listeners.go b/pkg/controller/service/clbv1/listeners.go index feddc5e92..de888a453 100644 --- a/pkg/controller/service/clbv1/listeners.go +++ b/pkg/controller/service/clbv1/listeners.go @@ -17,8 +17,8 @@ import ( "strconv" "strings" - "k8s.io/cloud-provider-alibaba-cloud/pkg/provider/dryrun" "k8s.io/cloud-provider-alibaba-cloud/pkg/util" + "k8s.io/cloud-provider-alibaba-cloud/pkg/util/dryrun" v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/util/intstr" diff --git a/pkg/controller/service/clbv1/service_controller.go b/pkg/controller/service/clbv1/service_controller.go index f05acc397..76edf69e7 100644 --- a/pkg/controller/service/clbv1/service_controller.go +++ b/pkg/controller/service/clbv1/service_controller.go @@ -5,12 +5,12 @@ import ( "encoding/json" "errors" "fmt" - "os" "strings" "time" "k8s.io/apimachinery/pkg/types" utilerrors "k8s.io/apimachinery/pkg/util/errors" + "k8s.io/cloud-provider-alibaba-cloud/pkg/util/dryrun" "github.com/go-logr/logr" "golang.org/x/time/rate" @@ -42,11 +42,13 @@ import ( "k8s.io/cloud-provider-alibaba-cloud/pkg/controller/helper" "k8s.io/cloud-provider-alibaba-cloud/pkg/model" "k8s.io/cloud-provider-alibaba-cloud/pkg/provider/alibaba/vpc" - "k8s.io/cloud-provider-alibaba-cloud/pkg/provider/dryrun" "k8s.io/cloud-provider-alibaba-cloud/pkg/util" ) func Add(mgr manager.Manager, ctx *shared.SharedContext) error { + if ctrlCfg.ControllerCFG.DryRun { + dryrun.RegisterDryRun(dryrun.SLB) + } r, err := newReconciler(mgr, ctx) if err != nil { return err @@ -116,6 +118,10 @@ func add(mgr manager.Manager, r *ReconcileService) error { return fmt.Errorf("watch resource svc error: %s", err.Error()) } + if ctrlCfg.ControllerCFG.DryRun { + return mgr.Add(&serviceController{c: c, recon: r}) + } + if utilfeature.DefaultFeatureGate.Enabled(ctrlCfg.EndpointSlice) { // watch endpointslice if err := c.Watch(source.Kind(mgr.GetCache(), &discovery.EndpointSlice{}), @@ -169,12 +175,8 @@ func (m *ReconcileService) reconcile(c context.Context, request reconcile.Reques initial.Store(request.String(), 1) util.ServiceLog.Info("DryRun: reconcile finished", "service", request.NamespacedName.String()) if mapfull() { - util.ServiceLog.Info("ccm initial process finished.") - err := dryrun.ResultEvent(m.kubeClient, dryrun.SUCCESS, "ccm initial process finished") - if err != nil { - util.ServiceLog.Error(err, "write precheck event failed", "service", request.NamespacedName.String()) - } - os.Exit(0) + util.ServiceLog.Info("ccm slb dryrun process finished") + dryrun.Finish(dryrun.SLB) } err = nil } diff --git a/pkg/controller/service/clbv1/vgroups_test.go b/pkg/controller/service/clbv1/vgroups_test.go index 38e935cc4..0fce19b54 100644 --- a/pkg/controller/service/clbv1/vgroups_test.go +++ b/pkg/controller/service/clbv1/vgroups_test.go @@ -11,7 +11,7 @@ import ( "k8s.io/cloud-provider-alibaba-cloud/pkg/controller/service/reconcile/backend" svcCtx "k8s.io/cloud-provider-alibaba-cloud/pkg/controller/service/reconcile/context" "k8s.io/cloud-provider-alibaba-cloud/pkg/model" - "k8s.io/cloud-provider-alibaba-cloud/pkg/provider/dryrun" + "k8s.io/cloud-provider-alibaba-cloud/pkg/util/dryrun" "k8s.io/klog/v2/klogr" ) diff --git a/pkg/controller/service/nlbv2/dryrun.go b/pkg/controller/service/nlbv2/dryrun.go new file mode 100644 index 000000000..b7bc21685 --- /dev/null +++ b/pkg/controller/service/nlbv2/dryrun.go @@ -0,0 +1,57 @@ +package nlbv2 + +import ( + "context" + "os" + "sync" + + v1 "k8s.io/api/core/v1" + "k8s.io/cloud-provider-alibaba-cloud/pkg/util" + "k8s.io/cloud-provider-alibaba-cloud/pkg/util/dryrun" + "k8s.io/klog/v2" + "sigs.k8s.io/controller-runtime/pkg/client" +) + +var initial = sync.Map{} + +func initMap(client client.Client) { + svcs := v1.ServiceList{} + err := client.List(context.TODO(), &svcs) + if err != nil { + klog.Infof("init Map error: %s", err.Error()) + os.Exit(1) + } + + length := 0 + for _, m := range svcs.Items { + if !needAdd(&m) { + continue + } + length++ + initial.Store(util.NamespacedName(&m).String(), 0) + } + util.NLBLog.Info("ccm initial process finished.", "length", length) + if length == 0 { + dryrun.Finish(dryrun.NLB) + } +} + +func mapfull() bool { + total, unsync := 0, 0 + initial.Range( + func(key, value interface{}) bool { + val, ok := value.(int) + if !ok { + // not supposed + return true + } + if val != 1 { + unsync += 1 + } + total += 1 + return true + }, + ) + util.NLBLog.Info("Reconcile process", "total", total, "unsync", unsync) + return unsync == 0 +} diff --git a/pkg/controller/service/nlbv2/dryrun_test.go b/pkg/controller/service/nlbv2/dryrun_test.go new file mode 100644 index 000000000..e46553ce4 --- /dev/null +++ b/pkg/controller/service/nlbv2/dryrun_test.go @@ -0,0 +1,23 @@ +package nlbv2 + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestMapFull(t *testing.T) { + assert.Equal(t, mapfull(), true) + + initial.Store("default/test-svc", 0) + assert.Equal(t, mapfull(), false) + + initial.Store("default/test-svc", 1) + assert.Equal(t, mapfull(), true) +} + +//func TestInitMap(t *testing.T) { +// initMap(getFakeKubeClient()) +// _, ok := initial.Load(fmt.Sprintf("%s/%s", NS, SvcName)) +// assert.Equal(t, ok, true) +//} diff --git a/pkg/controller/service/nlbv2/listeners.go b/pkg/controller/service/nlbv2/listeners.go index ec4065aca..762826b9b 100644 --- a/pkg/controller/service/nlbv2/listeners.go +++ b/pkg/controller/service/nlbv2/listeners.go @@ -22,8 +22,8 @@ import ( prvd "k8s.io/cloud-provider-alibaba-cloud/pkg/provider" "k8s.io/cloud-provider-alibaba-cloud/pkg/provider/alibaba/base" "k8s.io/cloud-provider-alibaba-cloud/pkg/provider/alibaba/nlb" - "k8s.io/cloud-provider-alibaba-cloud/pkg/provider/dryrun" "k8s.io/cloud-provider-alibaba-cloud/pkg/util" + "k8s.io/cloud-provider-alibaba-cloud/pkg/util/dryrun" "k8s.io/klog/v2" ) diff --git a/pkg/controller/service/nlbv2/model_applier.go b/pkg/controller/service/nlbv2/model_applier.go index a27a9a59a..9c6f15228 100644 --- a/pkg/controller/service/nlbv2/model_applier.go +++ b/pkg/controller/service/nlbv2/model_applier.go @@ -14,8 +14,8 @@ import ( nlbmodel "k8s.io/cloud-provider-alibaba-cloud/pkg/model/nlb" "k8s.io/cloud-provider-alibaba-cloud/pkg/model/tag" "k8s.io/cloud-provider-alibaba-cloud/pkg/provider/alibaba/base" - "k8s.io/cloud-provider-alibaba-cloud/pkg/provider/dryrun" "k8s.io/cloud-provider-alibaba-cloud/pkg/util" + "k8s.io/cloud-provider-alibaba-cloud/pkg/util/dryrun" ) type serverGroupApplyResult struct { diff --git a/pkg/controller/service/nlbv2/nlb_controller.go b/pkg/controller/service/nlbv2/nlb_controller.go index dd8905755..2250d2336 100644 --- a/pkg/controller/service/nlbv2/nlb_controller.go +++ b/pkg/controller/service/nlbv2/nlb_controller.go @@ -27,8 +27,8 @@ import ( svcCtx "k8s.io/cloud-provider-alibaba-cloud/pkg/controller/service/reconcile/context" nlbmodel "k8s.io/cloud-provider-alibaba-cloud/pkg/model/nlb" prvd "k8s.io/cloud-provider-alibaba-cloud/pkg/provider" - "k8s.io/cloud-provider-alibaba-cloud/pkg/provider/dryrun" "k8s.io/cloud-provider-alibaba-cloud/pkg/util" + "k8s.io/cloud-provider-alibaba-cloud/pkg/util/dryrun" "k8s.io/cloud-provider-alibaba-cloud/pkg/util/metric" "k8s.io/klog/v2" v1helper "k8s.io/kubernetes/pkg/apis/core/v1/helper" @@ -41,6 +41,9 @@ import ( ) func Add(mgr manager.Manager, ctx *shared.SharedContext) error { + if ctrlCfg.ControllerCFG.DryRun { + dryrun.RegisterDryRun(dryrun.NLB) + } reconciler, err := newReconciler(mgr, ctx) if err != nil { return fmt.Errorf("new nlb reconciler error: %s", err.Error()) @@ -75,6 +78,9 @@ type nlbController struct { } func (n nlbController) Start(ctx context.Context) error { + if ctrlCfg.ControllerCFG.DryRun { + initMap(n.recon.kubeClient) + } return n.c.Start(ctx) } @@ -104,6 +110,10 @@ func add(mgr manager.Manager, r *ReconcileNLB) error { return fmt.Errorf("watch resource svc error: %s", err.Error()) } + if ctrlCfg.ControllerCFG.DryRun { + return mgr.Add(&nlbController{c: c, recon: r}) + } + if utilfeature.DefaultFeatureGate.Enabled(ctrlCfg.EndpointSlice) { // watch endpointslice if err := c.Watch(source.Kind(mgr.GetCache(), &discovery.EndpointSlice{}), @@ -148,15 +158,27 @@ func (m *ReconcileNLB) Reconcile(ctx context.Context, request reconcile.Request) return util.HandleReconcileResult(request, m.reconcile(ctx, request)) } -func (m *ReconcileNLB) reconcile(c context.Context, request reconcile.Request) error { +func (m *ReconcileNLB) reconcile(c context.Context, request reconcile.Request) (err error) { startTime := time.Now() + defer func() { + if ctrlCfg.ControllerCFG.DryRun { + initial.Store(request.String(), 1) + util.NLBLog.Info("DryRun: reconcile finished", "service", request.NamespacedName.String()) + if mapfull() { + util.NLBLog.Info("ccm nlb dryrun process finished") + dryrun.Finish(dryrun.NLB) + } + err = nil + } + }() + reconcileID := controller.ReconcileIDFromContext(c) nlbLog := util.NLBLog.WithValues("service", request.NamespacedName.String(), "reconcileID", reconcileID) nlbLog.Info("starting reconcile service") svc := &v1.Service{} - err := m.kubeClient.Get(context.Background(), request.NamespacedName, svc) + err = m.kubeClient.Get(context.Background(), request.NamespacedName, svc) if err != nil { if apierrors.IsNotFound(err) { m.logger.Info("service not found, skip", "service", request.NamespacedName) @@ -239,7 +261,6 @@ func (m *ReconcileNLB) cleanupLoadBalancerResources(reqCtx *svcCtx.RequestContex } func (m *ReconcileNLB) reconcileLoadBalancerResources(req *svcCtx.RequestContext) error { - if err := m.finalizerManager.AddFinalizers(req.Ctx, req.Service, helper.NLBFinalizer); err != nil { m.record.Event(req.Service, v1.EventTypeWarning, helper.FailedAddFinalizer, fmt.Sprintf("Error adding finalizer: %s", err.Error())) diff --git a/pkg/provider/alibaba/nlb/cert.go b/pkg/provider/alibaba/nlb/cert.go index 138b6250e..1843ef222 100644 --- a/pkg/provider/alibaba/nlb/cert.go +++ b/pkg/provider/alibaba/nlb/cert.go @@ -61,7 +61,7 @@ func (p *NLBProvider) AssociateAdditionalCertificatesWithNLBListener(ctx context klog.V(5).Infof("RequestId: %s, API: %s", tea.StringValue(resp.Body.RequestId), "AssociateAdditionalCertificatesWithListener") jobId := tea.StringValue(resp.Body.JobId) if jobId != "" { - return p.WaitJobFinish("AssociateAdditionalCertificatesWithListener", jobId) + return p.WaitJobFinish(nil, "AssociateAdditionalCertificatesWithListener", jobId) } return nil @@ -81,7 +81,7 @@ func (p *NLBProvider) DisassociateAdditionalCertificatesWithNLBListener(ctx cont jobId := tea.StringValue(resp.Body.JobId) if jobId != "" { - return p.WaitJobFinish("DisassociateAdditionalCertificatesWithListener", jobId) + return p.WaitJobFinish(nil, "DisassociateAdditionalCertificatesWithListener", jobId) } return nil } diff --git a/pkg/provider/alibaba/nlb/listener.go b/pkg/provider/alibaba/nlb/listener.go index ec0ad54fb..d7b57a261 100644 --- a/pkg/provider/alibaba/nlb/listener.go +++ b/pkg/provider/alibaba/nlb/listener.go @@ -116,7 +116,7 @@ func (p *NLBProvider) CreateNLBListener(ctx context.Context, lbId string, lis *n if err != nil { return err } - return p.WaitJobFinish("CreateListener", jobId) + return p.WaitJobFinish(nil, "CreateListener", jobId) } func (p *NLBProvider) UpdateNLBListener(ctx context.Context, lis *nlbmodel.ListenerAttribute) error { @@ -124,7 +124,7 @@ func (p *NLBProvider) UpdateNLBListener(ctx context.Context, lis *nlbmodel.Liste if err != nil { return err } - return p.WaitJobFinish("UpdateListener", jobId) + return p.WaitJobFinish(nil, "UpdateListener", jobId) } func (p *NLBProvider) DeleteNLBListener(ctx context.Context, listenerId string) error { @@ -132,7 +132,7 @@ func (p *NLBProvider) DeleteNLBListener(ctx context.Context, listenerId string) if err != nil { return err } - return p.WaitJobFinish("DeleteListener", jobId) + return p.WaitJobFinish(nil, "DeleteListener", jobId) } func (p *NLBProvider) StartNLBListener(ctx context.Context, listenerId string) error { diff --git a/pkg/provider/alibaba/nlb/loadbalancer.go b/pkg/provider/alibaba/nlb/loadbalancer.go index 027defd21..d494e3eb4 100644 --- a/pkg/provider/alibaba/nlb/loadbalancer.go +++ b/pkg/provider/alibaba/nlb/loadbalancer.go @@ -233,7 +233,7 @@ func (p *NLBProvider) DeleteNLB(ctx context.Context, mdl *nlbmodel.NetworkLoadBa } klog.V(5).Infof("RequestId: %s, API: %s", tea.StringValue(resp.Body.RequestId), "DeleteLoadBalancer") - return p.WaitJobFinish("DeleteLoadBalancer", tea.StringValue(resp.Body.JobId), 20*time.Second, 3*time.Minute) + return p.WaitJobFinish(ctx, "DeleteLoadBalancer", tea.StringValue(resp.Body.JobId), 20*time.Second, 3*time.Minute) } func (p *NLBProvider) UpdateNLB(ctx context.Context, mdl *nlbmodel.NetworkLoadBalancer) error { @@ -388,7 +388,7 @@ func (p *NLBProvider) NLBJoinSecurityGroup(ctx context.Context, lbId string, sgI } klog.V(5).Infof("RequestId: %s, API: %s", tea.StringValue(resp.Body.RequestId), "LoadBalancerJoinSecurityGroup") - err = p.WaitJobFinish("LoadBalancerJoinSecurityGroup", tea.StringValue(resp.Body.JobId)) + err = p.WaitJobFinish(ctx, "LoadBalancerJoinSecurityGroup", tea.StringValue(resp.Body.JobId)) if err != nil { return err } @@ -413,7 +413,7 @@ func (p *NLBProvider) NLBLeaveSecurityGroup(ctx context.Context, lbId string, sg } klog.V(5).Infof("RequestId: %s, API: %s", tea.StringValue(resp.Body.RequestId), "LoadBalancerLeaveSecurityGroup") - err = p.WaitJobFinish("LoadBalancerLeaveSecurityGroup", tea.StringValue(resp.Body.JobId)) + err = p.WaitJobFinish(ctx, "LoadBalancerLeaveSecurityGroup", tea.StringValue(resp.Body.JobId)) if err != nil { return err } @@ -441,7 +441,7 @@ func (p *NLBProvider) UpdateNLBSecurityGroupIds(ctx context.Context, mdl *nlbmod } klog.V(5).Infof("RequestId: %s, API: %s", tea.StringValue(resp.Body.RequestId), "LoadBalancerJoinSecurityGroup") - err = p.WaitJobFinish("LoadBalancerJoinSecurityGroup", tea.StringValue(resp.Body.JobId)) + err = p.WaitJobFinish(nil, "LoadBalancerJoinSecurityGroup", tea.StringValue(resp.Body.JobId)) if err != nil { return err } @@ -465,7 +465,7 @@ func (p *NLBProvider) UpdateNLBSecurityGroupIds(ctx context.Context, mdl *nlbmod } klog.V(5).Infof("RequestId: %s, API: %s", tea.StringValue(resp.Body.RequestId), "LoadBalancerLeaveSecurityGroup") - err = p.WaitJobFinish("LoadBalancerLeaveSecurityGroup", tea.StringValue(resp.Body.JobId)) + err = p.WaitJobFinish(ctx, "LoadBalancerLeaveSecurityGroup", tea.StringValue(resp.Body.JobId)) if err != nil { return err } @@ -572,7 +572,7 @@ func (p *NLBProvider) DetachCommonBandwidthPackageFromLoadBalancer(ctx context.C } klog.V(5).Infof("RequestId: %s, API: %s", tea.StringValue(resp.Body.RequestId), "DetachCommonBandwidthPackageFromLoadBalancer") - err = p.WaitJobFinish("DetachCommonBandwidthPackageFromLoadBalancer", tea.StringValue(resp.Body.JobId)) + err = p.WaitJobFinish(nil, "DetachCommonBandwidthPackageFromLoadBalancer", tea.StringValue(resp.Body.JobId)) if err != nil { return err } @@ -834,7 +834,7 @@ const ( DefaultRetryTimeout = 30 * time.Second ) -func (p *NLBProvider) WaitJobFinish(api, jobId string, args ...time.Duration) error { +func (p *NLBProvider) WaitJobFinish(ctx context.Context, api, jobId string, args ...time.Duration) error { var interval, timeout time.Duration if len(args) < 2 { interval = DefaultRetryInterval diff --git a/pkg/provider/alibaba/nlb/servergroup.go b/pkg/provider/alibaba/nlb/servergroup.go index 0670c6076..6a96b46a2 100644 --- a/pkg/provider/alibaba/nlb/servergroup.go +++ b/pkg/provider/alibaba/nlb/servergroup.go @@ -180,7 +180,7 @@ func (p *NLBProvider) CreateNLBServerGroup(ctx context.Context, sg *nlbmodel.Ser if err != nil { return err } - return p.WaitJobFinish("CreateServerGroup", jobId, 1200*time.Millisecond, DefaultRetryTimeout) + return p.WaitJobFinish(ctx, "CreateServerGroup", jobId, 1200*time.Millisecond, DefaultRetryTimeout) } func (p *NLBProvider) DeleteNLBServerGroup(ctx context.Context, sgId string) error { @@ -201,7 +201,7 @@ func (p *NLBProvider) AddNLBServers(ctx context.Context, sgId string, backends [ if err != nil { return err } - return p.WaitJobFinish("AddServersToServerGroup", jobId, 1200*time.Millisecond, DefaultRetryTimeout) + return p.WaitJobFinish(ctx, "AddServersToServerGroup", jobId, 1200*time.Millisecond, DefaultRetryTimeout) } func (p *NLBProvider) RemoveNLBServers(ctx context.Context, sgId string, backends []nlbmodel.ServerGroupServer, @@ -210,7 +210,7 @@ func (p *NLBProvider) RemoveNLBServers(ctx context.Context, sgId string, backend if err != nil { return err } - return p.WaitJobFinish("RemoveServersFromServerGroup", jobId, 1200*time.Millisecond, DefaultRetryTimeout) + return p.WaitJobFinish(ctx, "RemoveServersFromServerGroup", jobId, 1200*time.Millisecond, DefaultRetryTimeout) } func (p *NLBProvider) UpdateNLBServers(ctx context.Context, sgId string, backends []nlbmodel.ServerGroupServer, @@ -219,7 +219,7 @@ func (p *NLBProvider) UpdateNLBServers(ctx context.Context, sgId string, backend if err != nil { return err } - return p.WaitJobFinish("UpdateServerGroupServersAttribute", jobId, 1200*time.Millisecond, DefaultRetryTimeout) + return p.WaitJobFinish(ctx, "UpdateServerGroupServersAttribute", jobId, 1200*time.Millisecond, DefaultRetryTimeout) } func (p *NLBProvider) ListNLBServers(ctx context.Context, sgId string) ([]nlbmodel.ServerGroupServer, error) { diff --git a/pkg/provider/alibaba/vpc/vpc.go b/pkg/provider/alibaba/vpc/vpc.go index d0a7c6aeb..a3542784d 100644 --- a/pkg/provider/alibaba/vpc/vpc.go +++ b/pkg/provider/alibaba/vpc/vpc.go @@ -226,8 +226,7 @@ func (r *VPCProvider) DeleteRoutes(ctx context.Context, table string, routes []* return nil, err } - // TODO: V(5) - klog.Infof("RequestId: %s, API: %s, table: %s, elapsedTime: %f", resp.RequestId, "DeleteRouteEntries", table, time.Since(s).Seconds()) + klog.V(5).Infof("RequestId: %s, API: %s, table: %s, elapsedTime: %f", resp.RequestId, "DeleteRouteEntries", table, time.Since(s).Seconds()) var statuses []prvd.RouteUpdateStatus for _, r := range routes { diff --git a/pkg/provider/dryrun/contants.go b/pkg/provider/dryrun/contants.go new file mode 100644 index 000000000..e7f0a3412 --- /dev/null +++ b/pkg/provider/dryrun/contants.go @@ -0,0 +1,59 @@ +package dryrun + +const ( + CodeAddServers = "AddServers" + CodeAddVServerGroupBackendServers = "AddVServerGroupBackendServers" + CodeAssociateAdditionalCertificatesWithListener = "AssociateAdditionalCertificatesWithListener" + CodeAttachCommonBandwidthPackage = "AttachCommonBandwidthPackage" + CodeAuthorizeSecurityGroup = "AuthorizeSecurityGroup" + CodeBatchWaitJobsFinish = "BatchWaitJobsFinish" + CodeCreateDomainExtension = "CreateDomainExtension" + CodeCreateListener = "CreateListener" + CodeCreateNLB = "CreateNLB" + CodeCreateSecurityGroup = "CreateSecurityGroup" + CodeCreateServerGroup = "CreateServerGroup" + CodeCreateSLB = "CreateSLB" + CodeCreateVgroup = "CreateVgroup" + CodeDeleteDomainExtension = "DeleteDomainExtension" + CodeDeleteListener = "DeleteListener" + CodeDeleteNLB = "DeleteNLB" + CodeDeleteSecurityGroup = "DeleteSecurityGroup" + CodeDeleteServerGroup = "DeleteServerGroup" + CodeDeleteSLB = "DeleteSLB" + CodeDeleteVgroup = "DeleteVgroup" + CodeDetachCommonBandwidthPackage = "DetachCommonBandwidthPackage" + CodeDisassociateAdditionalCertificatesWithListener = "DisassociateAdditionalCertificatesWithListener" + CodeModifyInternetSpec = "ModifyInternetSpec" + CodeModifyLoadBalancerInstanceChargeType = "ModifyLoadBalancerInstanceChargeType" + CodeModifySecurityGroupAttribute = "ModifySecurityGroupAttribute" + CodeModifySecurityGroupRule = "ModifySecurityGroupRule" + CodeModifySLBSpec = "ModifySLBSpec" + CodeModifyVgroup = "ModifyVgroup" + CodeRemoveServers = "RemoveServers" + CodeRemoveVgroup = "RemoveVgroup" + CodeRevokeSecurityGroup = "RevokeSecurityGroup" + CodeSetDomainExtensionAttribute = "SetDomainExtensionAttribute" + CodeSetSLBDeleteProtection = "SetSLBDeleteProtection" + CodeSetSLBModificationProtection = "SetSLBModificationProtection" + CodeSetSLBName = "SetSLBName" + CodeStartListener = "StartListener" + CodeStopListener = "StopListener" + CodeTagResource = "TagResource" + CodeTagSLB = "TagSLB" + CodeUntagResources = "UntagResources" + CodeUntagSLB = "UntagSLB" + CodeUpdateAddressType = "UpdateAddressType" + CodeUpdateIPv6AddressType = "UpdateIPv6AddressType" + CodeUpdateListener = "UpdateListener" + CodeUpdateLoadBalancerProtection = "UpdateLoadBalancerProtection" + CodeUpdateNLB = "UpdateNLB" + CodeUpdateSecurityGroupIds = "UpdateSecurityGroupIds" + CodeUpdateServerGroup = "UpdateServerGroup" + CodeUpdateServers = "UpdateServers" + CodeUpdateZones = "UpdateZones" + CodeWaitJobFinish = "WaitJobFinish" +) + +const ( + MTypeUntagResources = "UntagResources" +) diff --git a/pkg/provider/dryrun/ecs.go b/pkg/provider/dryrun/ecs.go index fb9f26645..a545fdc39 100644 --- a/pkg/provider/dryrun/ecs.go +++ b/pkg/provider/dryrun/ecs.go @@ -5,6 +5,7 @@ import ( "fmt" "k8s.io/cloud-provider-alibaba-cloud/pkg/util" + "k8s.io/cloud-provider-alibaba-cloud/pkg/util/dryrun" "k8s.io/klog/v2" "k8s.io/cloud-provider-alibaba-cloud/pkg/model" @@ -27,6 +28,15 @@ type DryRunECS struct { ecs *ecs.ECSProvider } +const ( + MTypeAuthorizeSecurityGroup = "AuthorizeSecurityGroup" + MTypeDeleteSecurityGroup = "DeleteSecurityGroup" + MTypeRevokeSecurityGroup = "RevokeSecurityGroup" + MTypeModifySecurityGroupAttribute = "ModifySecurityGroupAttribute" + MTypeCreateSecurityGroup = "CreateSecurityGroup" + MTypeModifySecurityGroupRule = "ModifySecurityGroupRule" +) + var _ prvd.IInstance = &DryRunECS{} func (d *DryRunECS) ListInstances(ctx context.Context, ids []string) (map[string]*prvd.NodeAttribute, error) { @@ -51,9 +61,9 @@ func (d *DryRunECS) ModifyNetworkInterfaceSourceDestCheck(id string, enabled boo } func (d *DryRunECS) AuthorizeSecurityGroup(ctx context.Context, sgId string, permissions []ecsmodel.SecurityGroupPermission) error { - mtype := "AuthorizeSecurityGroup" + mtype := MTypeAuthorizeSecurityGroup svc := getService(ctx) - AddEvent(NLB, util.Key(svc), "", "AuthorizeSecurityGroup", ERROR, "") + dryrun.AddEvent(dryrun.NLB, util.Key(svc), "", CodeAuthorizeSecurityGroup, dryrun.ERROR, "") return hintError(mtype, fmt.Sprintf("need to authorize security group %s", sgId)) } @@ -62,9 +72,9 @@ func (d *DryRunECS) DescribeSecurityGroupAttribute(ctx context.Context, sgId str } func (d *DryRunECS) DeleteSecurityGroup(ctx context.Context, sgId string) error { - mtype := "DeleteSecurityGroup" + mtype := MTypeDeleteSecurityGroup svc := getService(ctx) - AddEvent(NLB, util.Key(svc), "", "DeleteSecurityGroup", ERROR, "") + dryrun.AddEvent(dryrun.NLB, util.Key(svc), "", CodeDeleteSecurityGroup, dryrun.ERROR, "") return hintError(mtype, fmt.Sprintf("need to delete security group %s", sgId)) } @@ -73,29 +83,29 @@ func (d *DryRunECS) DescribeSecurityGroups(ctx context.Context, tags []tag.Tag) } func (d *DryRunECS) RevokeSecurityGroup(ctx context.Context, sgId string, permissions []ecsmodel.SecurityGroupPermission) error { - mtype := "RevokeSecurityGroup" + mtype := MTypeRevokeSecurityGroup svc := getService(ctx) - AddEvent(NLB, util.Key(svc), "", "RevokeSecurityGroup", ERROR, "") + dryrun.AddEvent(dryrun.NLB, util.Key(svc), "", CodeRevokeSecurityGroup, dryrun.ERROR, "") return hintError(mtype, fmt.Sprintf("need to revoke security group %s", sgId)) } func (d *DryRunECS) ModifySecurityGroupAttribute(ctx context.Context, sgId string, sg *ecsmodel.SecurityGroup) error { - mtype := "ModifySecurityGroupAttribute" + mtype := MTypeModifySecurityGroupAttribute svc := getService(ctx) - AddEvent(NLB, util.Key(svc), "", "ModifySecurityGroupAttribute", ERROR, "") + dryrun.AddEvent(dryrun.NLB, util.Key(svc), "", CodeModifySecurityGroupAttribute, dryrun.ERROR, "") return hintError(mtype, fmt.Sprintf("need to modify security group attribute %s", sgId)) } func (d *DryRunECS) CreateSecurityGroup(ctx context.Context, sg ecsmodel.SecurityGroup) error { - mtype := "CreateSecurityGroup" + mtype := MTypeCreateSecurityGroup svc := getService(ctx) - AddEvent(NLB, util.Key(svc), "", "CreateSecurityGroup", ERROR, "") + dryrun.AddEvent(dryrun.NLB, util.Key(svc), "", CodeCreateSecurityGroup, dryrun.ERROR, "") return hintError(mtype, fmt.Sprintf("need to create security group %s", sg.Name)) } func (d *DryRunECS) ModifySecurityGroupRule(ctx context.Context, sgId string, permission ecsmodel.SecurityGroupPermission) error { - mtype := "ModifySecurityGroupRule" + mtype := MTypeModifySecurityGroupRule svc := getService(ctx) - AddEvent(NLB, util.Key(svc), "", "ModifySecurityGroupRule", ERROR, "") + dryrun.AddEvent(dryrun.NLB, util.Key(svc), "", CodeModifySecurityGroupRule, dryrun.ERROR, "") return hintError(mtype, fmt.Sprintf("need to modify security group rule %s", sgId)) } diff --git a/pkg/provider/dryrun/nlb.go b/pkg/provider/dryrun/nlb.go index fa5b811e2..c4eda9f2b 100644 --- a/pkg/provider/dryrun/nlb.go +++ b/pkg/provider/dryrun/nlb.go @@ -2,6 +2,7 @@ package dryrun import ( "context" + "fmt" "time" nlbmodel "k8s.io/cloud-provider-alibaba-cloud/pkg/model/nlb" @@ -9,6 +10,8 @@ import ( prvd "k8s.io/cloud-provider-alibaba-cloud/pkg/provider" "k8s.io/cloud-provider-alibaba-cloud/pkg/provider/alibaba/base" "k8s.io/cloud-provider-alibaba-cloud/pkg/provider/alibaba/nlb" + "k8s.io/cloud-provider-alibaba-cloud/pkg/util" + "k8s.io/cloud-provider-alibaba-cloud/pkg/util/dryrun" ) func NewDryRunNLB( @@ -24,29 +27,75 @@ type DryRunNLB struct { nlb *nlb.NLBProvider } +const ( + MTypeDeleteListener = "DeleteListener" + MTypeUpdateListener = "UpdateListener" + MTypeCreateListener = "CreateListener" + MTypeUpdateNLBSecurityGroupIds = "UpdateSecurityGroupIds" + MTypeTagResource = "TagResource" + MTypeCreateNLB = "CreateNLB" + MTypeDeleteNLB = "DeleteNLB" + MTypeUpdateNLB = "UpdateNLB" + MTypeUpdateAddressType = "UpdateAddressType" + MTypeUpdateNLBZones = "UpdateZones" + MTypeUpdateLoadBalancerProtection = "UpdateLoadBalancerProtection" + MTypeAttachCommonBandwidthPackage = "AttachCommonBandwidthPackage" + MTypeDetachCommonBandwidthPackage = "DetachCommonBandwidthPackage" + MTypeCreateServerGroup = "CreateServerGroup" + MTypeDeleteServerGroup = "DeleteServerGroup" + MTypeUpdateNLBServerGroup = "UpdateServerGroup" + MTypeAddServers = "AddServers" + MTypeRemoveNLBServers = "RemoveServers" + MTypeUpdateNLBServers = "UpdateServers" + MTypeStartListener = "StartListener" + MTypeStopListener = "StopListener" + MTypeUpdateIPv6AddressType = "UpdateIPv6AddressType" + MTypeBatchWaitJobsFinish = "BatchWaitJobsFinish" + MTypeAssociateAdditionalCertificatesWithListener = "AssociateAdditionalCertificatesWithListener" + MTypeDisassociateAdditionalCertificatesWithListener = "DisassociateAdditionalCertificatesWithListener" + MTypeWaitJobFinish = "WaitJobFinish" +) + func (d DryRunNLB) DeleteNLBListenerAsync(ctx context.Context, listenerId string) (string, error) { - //TODO implement me - panic("implement me") + mtype := MTypeDeleteListener + svc := getService(ctx) + dryrun.AddEvent(dryrun.NLB, util.Key(svc), "", CodeDeleteListener, dryrun.ERROR, "") + return "", hintError(mtype, fmt.Sprintf("listener %s should be deleted", listenerId)) } func (d DryRunNLB) UpdateNLBListenerAsync(ctx context.Context, lis *nlbmodel.ListenerAttribute) (string, error) { - //TODO implement me - panic("implement me") + mtype := MTypeUpdateListener + svc := getService(ctx) + dryrun.AddEvent(dryrun.NLB, util.Key(svc), lis.ListenerId, CodeUpdateListener, dryrun.ERROR, "") + return "", hintError(mtype, fmt.Sprintf("listener %s should be updated", lis.ListenerId)) } func (d DryRunNLB) CreateNLBListenerAsync(ctx context.Context, lbId string, lis *nlbmodel.ListenerAttribute) (string, error) { - //TODO implement me - panic("implement me") + mtype := MTypeCreateListener + svc := getService(ctx) + dryrun.AddEvent(dryrun.NLB, util.Key(svc), lbId, CodeCreateListener, dryrun.ERROR, "") + return "", hintError(mtype, fmt.Sprintf("listener for lb %s should be created", lbId)) +} + +func (d DryRunNLB) UpdateNLBSecurityGroupIds(ctx context.Context, mdl *nlbmodel.NetworkLoadBalancer, added, removed []string) error { + mtype := MTypeUpdateNLBSecurityGroupIds + svc := getService(ctx) + dryrun.AddEvent(dryrun.NLB, util.Key(svc), mdl.LoadBalancerAttribute.LoadBalancerId, CodeUpdateSecurityGroupIds, dryrun.ERROR, "") + return hintError(mtype, fmt.Sprintf("nlb %s security groups should be updated, added: %v, removed: %v", mdl.LoadBalancerAttribute.LoadBalancerId, added, removed)) } func (d DryRunNLB) TagNLBResource(ctx context.Context, resourceId string, resourceType nlbmodel.TagResourceType, tags []tag.Tag) error { - //TODO implement me - panic("implement me") + mtype := MTypeTagResource + svc := getService(ctx) + dryrun.AddEvent(dryrun.NLB, util.Key(svc), resourceId, CodeTagResource, dryrun.ERROR, "") + return hintError(mtype, fmt.Sprintf("resource %s type %s should be tagged with %v", resourceId, resourceType, tags)) } func (d DryRunNLB) UntagNLBResources(ctx context.Context, resourceId string, resourceType nlbmodel.TagResourceType, tagKey []*string) error { - //TODO implement me - panic("implement me") + mtype := MTypeUntagResources + svc := getService(ctx) + dryrun.AddEvent(dryrun.NLB, util.Key(svc), resourceId, CodeUntagResources, dryrun.ERROR, "") + return hintError(mtype, fmt.Sprintf("resource %s type %s should be untagged with %v", resourceId, resourceType, tagKey)) } func (d DryRunNLB) NLBJoinSecurityGroup(ctx context.Context, lbId string, sgIds []string) error { @@ -60,191 +109,240 @@ func (d DryRunNLB) NLBLeaveSecurityGroup(ctx context.Context, lbId string, sgIds } func (d DryRunNLB) ListNLBTagResources(ctx context.Context, lbId string) ([]tag.Tag, error) { - //TODO implement me - panic("implement me") + return d.nlb.ListNLBTagResources(ctx, lbId) } func (d DryRunNLB) FindNLB(ctx context.Context, mdl *nlbmodel.NetworkLoadBalancer) error { - //TODO implement me - panic("implement me") + return d.nlb.FindNLB(ctx, mdl) } func (d DryRunNLB) DescribeNLB(ctx context.Context, mdl *nlbmodel.NetworkLoadBalancer) error { - //TODO implement me - panic("implement me") + return d.nlb.DescribeNLB(ctx, mdl) } func (d DryRunNLB) CreateNLB(ctx context.Context, mdl *nlbmodel.NetworkLoadBalancer, clientToken string) error { - //TODO implement me - panic("implement me") + mtype := MTypeCreateNLB + svc := getService(ctx) + dryrun.AddEvent(dryrun.NLB, util.Key(svc), "", CodeCreateNLB, dryrun.ERROR, "") + return hintError(mtype, "nlb should be created") } func (d DryRunNLB) DeleteNLB(ctx context.Context, mdl *nlbmodel.NetworkLoadBalancer) error { - //TODO implement me - panic("implement me") + mtype := MTypeDeleteNLB + svc := getService(ctx) + dryrun.AddEvent(dryrun.NLB, util.Key(svc), mdl.LoadBalancerAttribute.LoadBalancerId, CodeDeleteNLB, dryrun.ERROR, "") + return hintError(mtype, fmt.Sprintf("nlb %s should be deleted", mdl.LoadBalancerAttribute.LoadBalancerId)) } func (d DryRunNLB) UpdateNLB(ctx context.Context, mdl *nlbmodel.NetworkLoadBalancer) error { - //TODO implement me - panic("implement me") + mtype := MTypeUpdateNLB + svc := getService(ctx) + dryrun.AddEvent(dryrun.NLB, util.Key(svc), mdl.LoadBalancerAttribute.LoadBalancerId, CodeUpdateNLB, dryrun.ERROR, "") + return hintError(mtype, fmt.Sprintf("nlb %s should be updated", mdl.LoadBalancerAttribute.LoadBalancerId)) } func (d DryRunNLB) UpdateNLBAddressType(ctx context.Context, mdl *nlbmodel.NetworkLoadBalancer) error { - //TODO implement me - panic("implement me") + mtype := MTypeUpdateAddressType + svc := getService(ctx) + dryrun.AddEvent(dryrun.NLB, util.Key(svc), mdl.LoadBalancerAttribute.LoadBalancerId, CodeUpdateAddressType, dryrun.ERROR, "") + return hintError(mtype, fmt.Sprintf("nlb %s address type should be updated to %s", mdl.LoadBalancerAttribute.LoadBalancerId, mdl.LoadBalancerAttribute.AddressType)) } func (d DryRunNLB) UpdateNLBZones(ctx context.Context, mdl *nlbmodel.NetworkLoadBalancer) error { - //TODO implement me - panic("implement me") + mtype := MTypeUpdateNLBZones + svc := getService(ctx) + dryrun.AddEvent(dryrun.NLB, util.Key(svc), mdl.LoadBalancerAttribute.LoadBalancerId, CodeUpdateZones, dryrun.ERROR, "") + return hintError(mtype, fmt.Sprintf("nlb %s zones should be updated", mdl.LoadBalancerAttribute.LoadBalancerId)) } func (d DryRunNLB) UpdateLoadBalancerProtection(ctx context.Context, lbId string, delCfg *nlbmodel.DeletionProtectionConfig, modCfg *nlbmodel.ModificationProtectionConfig) error { - //TODO implement me - panic("implement me") + mtype := MTypeUpdateLoadBalancerProtection + svc := getService(ctx) + dryrun.AddEvent(dryrun.NLB, util.Key(svc), lbId, CodeUpdateLoadBalancerProtection, dryrun.ERROR, "") + return hintError(mtype, fmt.Sprintf("nlb %s protection configs should be updated", lbId)) } func (d DryRunNLB) AttachCommonBandwidthPackageToLoadBalancer(ctx context.Context, lbId string, bandwidthPackageId string) error { - //TODO implement me - panic("implement me") + mtype := MTypeAttachCommonBandwidthPackage + svc := getService(ctx) + dryrun.AddEvent(dryrun.NLB, util.Key(svc), lbId, CodeAttachCommonBandwidthPackage, dryrun.ERROR, "") + return hintError(mtype, fmt.Sprintf("nlb %s should attach bandwidth package %s", lbId, bandwidthPackageId)) } func (d DryRunNLB) DetachCommonBandwidthPackageFromLoadBalancer(ctx context.Context, lbId string, bandwidthPackageId string) error { - //TODO implement me - panic("implement me") + mtype := MTypeDetachCommonBandwidthPackage + svc := getService(ctx) + dryrun.AddEvent(dryrun.NLB, util.Key(svc), lbId, CodeDetachCommonBandwidthPackage, dryrun.ERROR, "") + return hintError(mtype, fmt.Sprintf("nlb %s should detach bandwidth package %s", lbId, bandwidthPackageId)) } func (d DryRunNLB) ListNLBServerGroups(ctx context.Context, tags []tag.Tag) ([]*nlbmodel.ServerGroup, error) { - //TODO implement me - panic("implement me") + return d.nlb.ListNLBServerGroups(ctx, tags) } func (d DryRunNLB) CreateNLBServerGroup(ctx context.Context, sg *nlbmodel.ServerGroup) error { - //TODO implement me - panic("implement me") + mtype := MTypeCreateServerGroup + svc := getService(ctx) + dryrun.AddEvent(dryrun.NLB, fmt.Sprintf("%s/%s", util.Key(svc), sg.ServerGroupName), "", CodeCreateServerGroup, dryrun.ERROR, "") + return hintError(mtype, "server group should be created") } func (d DryRunNLB) DeleteNLBServerGroup(ctx context.Context, sgId string) error { - //TODO implement me - panic("implement me") + mtype := MTypeDeleteServerGroup + svc := getService(ctx) + dryrun.AddEvent(dryrun.NLB, util.Key(svc), sgId, CodeDeleteServerGroup, dryrun.ERROR, "") + return hintError(mtype, fmt.Sprintf("server group %s should be deleted", sgId)) } func (d DryRunNLB) UpdateNLBServerGroup(ctx context.Context, sg *nlbmodel.ServerGroup) error { - //TODO implement me - panic("implement me") + mtype := MTypeUpdateNLBServerGroup + svc := getService(ctx) + dryrun.AddEvent(dryrun.NLB, util.Key(svc), sg.ServerGroupId, CodeUpdateServerGroup, dryrun.ERROR, "") + return hintError(mtype, fmt.Sprintf("server group %s should be updated", sg.ServerGroupId)) } func (d DryRunNLB) AddNLBServers(ctx context.Context, sgId string, backends []nlbmodel.ServerGroupServer) error { - //TODO implement me - panic("implement me") + mtype := MTypeAddServers + svc := getService(ctx) + dryrun.AddEvent(dryrun.NLB, util.Key(svc), sgId, CodeAddServers, dryrun.ERROR, "") + return hintError(mtype, fmt.Sprintf("servers should be added to server group %s", sgId)) } func (d DryRunNLB) RemoveNLBServers(ctx context.Context, sgId string, backends []nlbmodel.ServerGroupServer) error { - //TODO implement me - panic("implement me") + mtype := MTypeRemoveNLBServers + svc := getService(ctx) + dryrun.AddEvent(dryrun.NLB, util.Key(svc), sgId, CodeRemoveServers, dryrun.ERROR, "") + return hintError(mtype, fmt.Sprintf("servers should be removed from server group %s", sgId)) } func (d DryRunNLB) UpdateNLBServers(ctx context.Context, sgId string, backends []nlbmodel.ServerGroupServer) error { - //TODO implement me - panic("implement me") + mtype := MTypeUpdateNLBServers + svc := getService(ctx) + dryrun.AddEvent(dryrun.NLB, util.Key(svc), sgId, CodeUpdateServers, dryrun.ERROR, "") + return hintError(mtype, fmt.Sprintf("servers should be updated in server group %s", sgId)) } func (d DryRunNLB) ListNLBListeners(ctx context.Context, lbId string) ([]*nlbmodel.ListenerAttribute, error) { - //TODO implement me - panic("implement me") + return d.nlb.ListNLBListeners(ctx, lbId) } func (d DryRunNLB) CreateNLBListener(ctx context.Context, lbId string, lis *nlbmodel.ListenerAttribute) error { - //TODO implement me - panic("implement me") + mtype := MTypeCreateListener + svc := getService(ctx) + dryrun.AddEvent(dryrun.NLB, fmt.Sprintf("%s/%d", util.Key(svc), lis.ListenerPort), lbId, CodeCreateListener, dryrun.ERROR, "") + return hintError(mtype, fmt.Sprintf("listener for nlb %s should be created", lbId)) } func (d DryRunNLB) UpdateNLBListener(ctx context.Context, lis *nlbmodel.ListenerAttribute) error { - //TODO implement me - panic("implement me") + mtype := MTypeUpdateListener + svc := getService(ctx) + dryrun.AddEvent(dryrun.NLB, fmt.Sprintf("%s/%d", util.Key(svc), lis.ListenerPort), lis.ListenerId, CodeUpdateListener, dryrun.ERROR, "") + return hintError(mtype, fmt.Sprintf("listener %s should be updated", lis.ListenerId)) } func (d DryRunNLB) DeleteNLBListener(ctx context.Context, listenerId string) error { - //TODO implement me - panic("implement me") + mtype := MTypeDeleteListener + svc := getService(ctx) + dryrun.AddEvent(dryrun.NLB, fmt.Sprintf("%s/%s", util.Key(svc), listenerId), listenerId, CodeDeleteListener, dryrun.ERROR, "") + return hintError(mtype, fmt.Sprintf("listener %s should be deleted", listenerId)) } func (d DryRunNLB) StartNLBListener(ctx context.Context, listenerId string) error { - //TODO implement me - panic("implement me") + mtype := MTypeStartListener + svc := getService(ctx) + dryrun.AddEvent(dryrun.NLB, fmt.Sprintf("%s/%s", util.Key(svc), listenerId), listenerId, CodeStartListener, dryrun.ERROR, "") + return hintError(mtype, fmt.Sprintf("listener %s should be started", listenerId)) } func (d DryRunNLB) StopNLBListener(ctx context.Context, listenerId string) error { - //TODO implement me - panic("implement me") + mtype := MTypeStopListener + svc := getService(ctx) + dryrun.AddEvent(dryrun.NLB, fmt.Sprintf("%s/%s", util.Key(svc), listenerId), listenerId, CodeStopListener, dryrun.ERROR, "") + return hintError(mtype, fmt.Sprintf("listener %s should be stopped", listenerId)) } func (d DryRunNLB) UpdateNLBIPv6AddressType(ctx context.Context, mdl *nlbmodel.NetworkLoadBalancer) error { - panic("implement me") + mtype := MTypeUpdateIPv6AddressType + svc := getService(ctx) + dryrun.AddEvent(dryrun.NLB, util.Key(svc), mdl.LoadBalancerAttribute.LoadBalancerId, CodeUpdateIPv6AddressType, dryrun.ERROR, "") + return hintError(mtype, fmt.Sprintf("nlb %s IPv6 address type should be updated", mdl.LoadBalancerAttribute.LoadBalancerId)) } func (d DryRunNLB) GetNLBServerGroup(ctx context.Context, sgId string) (*nlbmodel.ServerGroup, error) { - //TODO implement me - panic("implement me") + return d.nlb.GetNLBServerGroup(ctx, sgId) } func (d DryRunNLB) CreateNLBServerGroupAsync(ctx context.Context, sg *nlbmodel.ServerGroup) (string, error) { - //TODO implement me - panic("implement me") + mtype := MTypeCreateServerGroup + svc := getService(ctx) + dryrun.AddEvent(dryrun.NLB, fmt.Sprintf("%s/%s", util.Key(svc), sg.ServerGroupName), "", CodeCreateServerGroup, dryrun.ERROR, "") + return "", hintError(mtype, "server group should be created") } func (d DryRunNLB) DeleteNLBServerGroupAsync(ctx context.Context, sgId string) (string, error) { - //TODO implement me - panic("implement me") + mtype := MTypeDeleteServerGroup + svc := getService(ctx) + dryrun.AddEvent(dryrun.NLB, util.Key(svc), sgId, CodeDeleteServerGroup, dryrun.ERROR, "") + return "", hintError(mtype, fmt.Sprintf("server group %s should be deleted", sgId)) } func (d DryRunNLB) UpdateNLBServerGroupAsync(ctx context.Context, sg *nlbmodel.ServerGroup) (string, error) { - //TODO implement me - panic("implement me") + mtype := MTypeUpdateNLBServerGroup + svc := getService(ctx) + dryrun.AddEvent(dryrun.NLB, util.Key(svc), sg.ServerGroupId, CodeUpdateServerGroup, dryrun.ERROR, "") + return "", hintError(mtype, fmt.Sprintf("server group %s should be updated", sg.ServerGroupId)) } func (d DryRunNLB) AddNLBServersAsync(ctx context.Context, sgId string, backends []nlbmodel.ServerGroupServer) (string, error) { - //TODO implement me - panic("implement me") + mtype := MTypeAddServers + svc := getService(ctx) + dryrun.AddEvent(dryrun.NLB, util.Key(svc), sgId, CodeAddServers, dryrun.ERROR, "") + return "", hintError(mtype, fmt.Sprintf("servers should be added to server group %s", sgId)) } func (d DryRunNLB) RemoveNLBServersAsync(ctx context.Context, sgId string, backends []nlbmodel.ServerGroupServer) (string, error) { - //TODO implement me - panic("implement me") + mtype := MTypeRemoveNLBServers + svc := getService(ctx) + dryrun.AddEvent(dryrun.NLB, util.Key(svc), sgId, CodeRemoveServers, dryrun.ERROR, "") + return "", hintError(mtype, fmt.Sprintf("servers should be removed from server group %s", sgId)) } func (d DryRunNLB) UpdateNLBServersAsync(ctx context.Context, sgId string, backends []nlbmodel.ServerGroupServer) (string, error) { - //TODO implement me - panic("implement me") -} - -func (*DryRunCloud) CreateNLBListenerAsync(ctx context.Context, lbId string, lis *nlbmodel.ListenerAttribute) (string, error) { - //TODO implement me - panic("implement me") + mtype := MTypeUpdateNLBServers + svc := getService(ctx) + dryrun.AddEvent(dryrun.NLB, util.Key(svc), sgId, CodeUpdateServers, dryrun.ERROR, "") + return "", hintError(mtype, fmt.Sprintf("servers should be updated in server group %s", sgId)) } -func (*DryRunNLB) BatchWaitJobsFinish(ctx context.Context, api string, jobIds []string, args ...time.Duration) error { - //TODO implement me - panic("implement me") +func (d DryRunNLB) BatchWaitJobsFinish(ctx context.Context, api string, jobIds []string, args ...time.Duration) error { + mtype := MTypeBatchWaitJobsFinish + svc := getService(ctx) + dryrun.AddEvent(dryrun.NLB, util.Key(svc), "", CodeBatchWaitJobsFinish, dryrun.ERROR, "") + return hintError(mtype, fmt.Sprintf("jobs %v for api %s should finish", jobIds, api)) } func (d DryRunNLB) ListNLBListenerCertificates(ctx context.Context, listenerId string) ([]nlbmodel.ListenerCertificate, error) { - //TODO implement me - panic("implement me") + return d.nlb.ListNLBListenerCertificates(ctx, listenerId) } func (d DryRunNLB) AssociateAdditionalCertificatesWithNLBListener(ctx context.Context, listenerId string, certIds []string) error { - //TODO implement me - panic("implement me") + mtype := MTypeAssociateAdditionalCertificatesWithListener + svc := getService(ctx) + dryrun.AddEvent(dryrun.NLB, fmt.Sprintf("%s/%s", util.Key(svc), listenerId), listenerId, CodeAssociateAdditionalCertificatesWithListener, dryrun.ERROR, "") + return hintError(mtype, fmt.Sprintf("additional certificates should be associated with listener %s", listenerId)) } func (d DryRunNLB) DisassociateAdditionalCertificatesWithNLBListener(ctx context.Context, listenerId string, certIds []string) error { - //TODO implement me - panic("implement me") + mtype := MTypeDisassociateAdditionalCertificatesWithListener + svc := getService(ctx) + dryrun.AddEvent(dryrun.NLB, fmt.Sprintf("%s/%s", util.Key(svc), listenerId), listenerId, CodeDisassociateAdditionalCertificatesWithListener, dryrun.ERROR, "") + return hintError(mtype, fmt.Sprintf("additional certificates should be disassociated with listener %s", listenerId)) } -func (d DryRunNLB) WaitJobFinish(api, jobId string, args ...time.Duration) error { - //TODO implement me - panic("implement me") +func (d DryRunNLB) WaitJobFinish(ctx context.Context, api, jobId string, args ...time.Duration) error { + mtype := MTypeWaitJobFinish + svc := getService(ctx) + dryrun.AddEvent(dryrun.NLB, util.Key(svc), jobId, CodeWaitJobFinish, dryrun.ERROR, "") + return hintError(mtype, fmt.Sprintf("job %s for api %s should finish", jobId, api)) } diff --git a/pkg/provider/dryrun/slb.go b/pkg/provider/dryrun/slb.go index de70b63f3..5d9d501d1 100644 --- a/pkg/provider/dryrun/slb.go +++ b/pkg/provider/dryrun/slb.go @@ -7,12 +7,14 @@ import ( v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/cloud-provider-alibaba-cloud/pkg/controller/helper" "k8s.io/cloud-provider-alibaba-cloud/pkg/model" "k8s.io/cloud-provider-alibaba-cloud/pkg/model/tag" prvd "k8s.io/cloud-provider-alibaba-cloud/pkg/provider" "k8s.io/cloud-provider-alibaba-cloud/pkg/provider/alibaba/base" "k8s.io/cloud-provider-alibaba-cloud/pkg/provider/alibaba/slb" "k8s.io/cloud-provider-alibaba-cloud/pkg/util" + "k8s.io/cloud-provider-alibaba-cloud/pkg/util/dryrun" ) func NewDryRunSLB( @@ -28,14 +30,44 @@ type DryRunSLB struct { slb *slb.SLBProvider } +const ( + MTypeCreateLoadBalancer = "CreateLoadBalancer" + MTypeDeleteLoadBalancer = "DeleteLoadBalancer" + MTypeModifyLoadBalancerInstanceSpec = "ModifyLoadBalancerInstanceSpec" + MTypeSetLoadBalancerDeleteProtection = "SetLoadBalancerDeleteProtection" + MTypeSetLoadBalancerName = "SetLoadBalancerName" + MTypeModifyLoadBalancerInternetSpec = "ModifyLoadBalancerInternetSpec" + MTypeSetLoadBalancerModificationProtection = "SetLoadBalancerModificationProtection" + MTypeModifyLoadBalancerInstanceChargeType = "ModifyLoadBalancerInstanceChargeType" + MTypeStartLoadBalancerListener = "StartLoadBalancerListener" + MTypeStopLoadBalancerListener = "StopLoadBalancerListener" + MTypeDeleteLoadBalancerListener = "DeleteLoadBalancerListener" + MTypeCreateLoadBalancerTCPListener = "CreateLoadBalancerTCPListener" + MTypeSetLoadBalancerTCPListenerAttribute = "SetLoadBalancerTCPListenerAttribute" + MTypeCreateLoadBalancerUDPListener = "CreateLoadBalancerUDPListener" + MTypeSetLoadBalancerUDPListenerAttribute = "SetLoadBalancerUDPListenerAttribute" + MTypeCreateLoadBalancerHTTPListener = "CreateLoadBalancerHTTPListener" + MTypeSetLoadBalancerHTTPListenerAttribute = "SetLoadBalancerHTTPListenerAttribute" + MTypeCreateLoadBalancerHTTPSListener = "CreateLoadBalancerHTTPSListener" + MTypeSetLoadBalancerHTTPSListenerAttribute = "SetLoadBalancerHTTPSListenerAttribute" + MTypeCreateVServerGroup = "CreateVServerGroup" + MTypeDeleteVServerGroup = "DeleteVServerGroup" + MTypeAddVServerGroupBackendServers = "AddVServerGroupBackendServers" + MTypeRemoveVServerGroupBackendServers = "RemoveVServerGroupBackendServers" + MTypeModifyVServerGroupBackendServers = "ModifyVServerGroupBackendServers" + MTypeCreateDomainExtension = "CreateDomainExtension" + MTypeDeleteDomainExtension = "DeleteDomainExtension" + MTypeSetDomainExtensionAttribute = "SetDomainExtensionAttribute" +) + func (m *DryRunSLB) FindLoadBalancer(ctx context.Context, mdl *model.LoadBalancer) error { return m.slb.FindLoadBalancer(ctx, mdl) } func (m *DryRunSLB) CreateLoadBalancer(ctx context.Context, mdl *model.LoadBalancer, clientToken string) error { - mtype := "CreateLoadBalancer" + mtype := MTypeCreateLoadBalancer svc := getService(ctx) - AddEvent(SLB, util.Key(svc), "", "CreateSLB", ERROR, "") + dryrun.AddEvent(dryrun.SLB, util.Key(svc), "", CodeCreateSLB, dryrun.ERROR, "") return hintError(mtype, "need to create loadbalancer") } @@ -44,69 +76,73 @@ func (m *DryRunSLB) DescribeLoadBalancer(ctx context.Context, mdl *model.LoadBal } func (m *DryRunSLB) DeleteLoadBalancer(ctx context.Context, mdl *model.LoadBalancer) error { - mtype := "DeleteLoadBalancer" + mtype := MTypeDeleteLoadBalancer svc := getService(ctx) - AddEvent(SLB, util.Key(svc), mdl.LoadBalancerAttribute.LoadBalancerId, "DeleteSLB", ERROR, "") + dryrun.AddEvent(dryrun.SLB, util.Key(svc), mdl.LoadBalancerAttribute.LoadBalancerId, CodeDeleteSLB, dryrun.ERROR, "") return hintError(mtype, fmt.Sprintf("loadbalancer %s should be deleted", mdl.LoadBalancerAttribute.LoadBalancerId)) } func (m *DryRunSLB) ModifyLoadBalancerInstanceSpec(ctx context.Context, lbId string, spec string) error { - mtype := "ModifyLoadBalancerInstanceSpec" + mtype := MTypeModifyLoadBalancerInstanceSpec svc := getService(ctx) - AddEvent(SLB, util.Key(svc), lbId, "ModifySLBSpec", ERROR, "") + dryrun.AddEvent(dryrun.SLB, util.Key(svc), lbId, CodeModifySLBSpec, dryrun.ERROR, "") return hintError(mtype, fmt.Sprintf("loadbalancer %s spec should be %s", lbId, spec)) } func (m *DryRunSLB) SetLoadBalancerDeleteProtection(ctx context.Context, lbId string, flag string) error { - mtype := "SetLoadBalancerDeleteProtection" + mtype := MTypeSetLoadBalancerDeleteProtection svc := getService(ctx) - AddEvent(SLB, util.Key(svc), lbId, "SetSLBDeleteProtection", ERROR, "") + dryrun.AddEvent(dryrun.SLB, util.Key(svc), lbId, CodeSetSLBDeleteProtection, dryrun.ERROR, "") return hintError(mtype, fmt.Sprintf("loadbalancer %s DeleteProtection should be %s", lbId, flag)) } func (m *DryRunSLB) SetLoadBalancerName(ctx context.Context, lbId string, name string) error { - mtype := "SetLoadBalancerName" + mtype := MTypeSetLoadBalancerName svc := getService(ctx) - AddEvent(SLB, util.Key(svc), lbId, "SetSLBName", ERROR, "") + dryrun.AddEvent(dryrun.SLB, util.Key(svc), lbId, CodeSetSLBName, dryrun.ERROR, "") return hintError(mtype, fmt.Sprintf("loadbalancer %s name should be %s", lbId, name)) } func (m *DryRunSLB) ModifyLoadBalancerInternetSpec(ctx context.Context, lbId string, chargeType string, bandwidth int) error { - mtype := "ModifyLoadBalancerInternetSpec" + mtype := MTypeModifyLoadBalancerInternetSpec svc := getService(ctx) - AddEvent(SLB, util.Key(svc), lbId, "ModifyInternetSpec", ERROR, "") + dryrun.AddEvent(dryrun.SLB, util.Key(svc), lbId, CodeModifyInternetSpec, dryrun.ERROR, "") return hintError(mtype, fmt.Sprintf("loadbalancer %s chargeType should be %s, bandwidth %d", lbId, chargeType, bandwidth)) } func (m *DryRunSLB) SetLoadBalancerModificationProtection(ctx context.Context, lbId string, flag string) error { - mtype := "SetLoadBalancerModificationProtection" + mtype := MTypeSetLoadBalancerModificationProtection svc := getService(ctx) - AddEvent(SLB, util.Key(svc), lbId, "SetSLBModificationProtection", ERROR, "") + dryrun.AddEvent(dryrun.SLB, util.Key(svc), lbId, CodeSetSLBModificationProtection, dryrun.ERROR, "") return hintError(mtype, fmt.Sprintf("loadbalancer %s ModificationProtection should be %s", lbId, flag)) } func (m *DryRunSLB) ModifyLoadBalancerInstanceChargeType(ctx context.Context, lbId string, instanceChargeType string, spec string) error { - mtype := "ModifyLoadBalancerInstanceChargeType" + mtype := MTypeModifyLoadBalancerInstanceChargeType svc := getService(ctx) - AddEvent(SLB, util.Key(svc), lbId, "ModifyLoadBalancerInstanceChargeType", ERROR, "") + dryrun.AddEvent(dryrun.SLB, util.Key(svc), lbId, CodeModifyLoadBalancerInstanceChargeType, dryrun.ERROR, "") return hintError(mtype, fmt.Sprintf("loadbalancer %s ModifyLoadBalancerInstanceChargeType should be %s with spec [%s]", lbId, instanceChargeType, spec)) } // Tag func (m *DryRunSLB) TagCLBResource(ctx context.Context, resourceId string, tags []tag.Tag) error { - mtype := "UntagResources" + if len(tags) == 1 && tags[0].Key == helper.REUSEKEY { + // for older version compatibility, do not check reuse tag + return nil + } + mtype := MTypeUntagResources svc := getService(ctx) - AddEvent(SLB, util.Key(svc), "", "TagSLB", ERROR, "") + dryrun.AddEvent(dryrun.SLB, util.Key(svc), "", CodeTagSLB, dryrun.ERROR, "") return hintError(mtype, fmt.Sprintf("loadbalancer %s tags %q should be added", resourceId, getTagString(tags))) } func (m *DryRunSLB) UntagResources(ctx context.Context, lbId string, tagKey *[]string) error { - mtype := "UntagResources" + mtype := MTypeUntagResources svc := getService(ctx) - AddEvent(SLB, util.Key(svc), "", "UntagSLB", ERROR, "") + dryrun.AddEvent(dryrun.SLB, util.Key(svc), "", CodeUntagSLB, dryrun.ERROR, "") tagString := strings.Join(*tagKey, ",") return hintError(mtype, fmt.Sprintf("loadbalancer %s tags %q should be deleted", lbId, tagString)) @@ -122,100 +158,100 @@ func (m *DryRunSLB) DescribeLoadBalancerListeners(ctx context.Context, lbId stri } func (m *DryRunSLB) StartLoadBalancerListener(ctx context.Context, lbId string, port int, proto string) error { - mtype := "StartLoadBalancerListener" + mtype := MTypeStartLoadBalancerListener svc := getService(ctx) - AddEvent(SLB, fmt.Sprintf("%s/%d", util.Key(svc), port), lbId, "StartListener", - ERROR, "") + dryrun.AddEvent(dryrun.SLB, fmt.Sprintf("%s/%d", util.Key(svc), port), lbId, CodeStartListener, + dryrun.ERROR, "") return hintError(mtype, fmt.Sprintf("loadbalancer %s listener %d should be running", lbId, port)) } func (m *DryRunSLB) StopLoadBalancerListener(ctx context.Context, lbId string, port int, proto string) error { - mtype := "StopLoadBalancerListener" + mtype := MTypeStopLoadBalancerListener svc := getService(ctx) - AddEvent(SLB, fmt.Sprintf("%s/%d", util.Key(svc), port), lbId, "StopListener", - ERROR, "") + dryrun.AddEvent(dryrun.SLB, fmt.Sprintf("%s/%d", util.Key(svc), port), lbId, CodeStopListener, + dryrun.ERROR, "") return hintError(mtype, fmt.Sprintf("loadbalancer %s listener %d should be stopped", lbId, port)) } func (m *DryRunSLB) DeleteLoadBalancerListener(ctx context.Context, lbId string, port int, proto string) error { - mtype := "DeleteLoadBalancerListener" + mtype := MTypeDeleteLoadBalancerListener svc := getService(ctx) - AddEvent(SLB, fmt.Sprintf("%s/%d", util.Key(svc), port), lbId, "DeleteListener", ERROR, "") + dryrun.AddEvent(dryrun.SLB, fmt.Sprintf("%s/%d", util.Key(svc), port), lbId, CodeDeleteListener, dryrun.ERROR, "") return hintError(mtype, fmt.Sprintf("loadbalancer %s listener %d should be deleted", lbId, port)) } func (m *DryRunSLB) CreateLoadBalancerTCPListener(ctx context.Context, lbId string, listener model.ListenerAttribute) error { - mtype := "CreateLoadBalancerTCPListener" + mtype := MTypeCreateLoadBalancerTCPListener svc := getService(ctx) - AddEvent(SLB, fmt.Sprintf("%s/%d", util.Key(svc), listener.ListenerPort), lbId, - "CreateListener", ERROR, "") + dryrun.AddEvent(dryrun.SLB, fmt.Sprintf("%s/%d", util.Key(svc), listener.ListenerPort), lbId, + CodeCreateListener, dryrun.ERROR, "") return hintError(mtype, fmt.Sprintf("loadbalancer %s listener %d should be created", lbId, listener.ListenerPort)) } func (m *DryRunSLB) SetLoadBalancerTCPListenerAttribute(ctx context.Context, lbId string, listener model.ListenerAttribute) error { - mtype := "SetLoadBalancerTCPListenerAttribute" + mtype := MTypeSetLoadBalancerTCPListenerAttribute svc := getService(ctx) reason := getDryRunMsg(ctx) - AddEvent(SLB, fmt.Sprintf("%s/%d", util.Key(svc), listener.ListenerPort), lbId, - "UpdateListener", ERROR, reason) + dryrun.AddEvent(dryrun.SLB, fmt.Sprintf("%s/%d", util.Key(svc), listener.ListenerPort), lbId, + CodeUpdateListener, dryrun.ERROR, reason) return hintError(mtype, fmt.Sprintf("loadbalancer %s listener %d should be updated, %s", lbId, listener.ListenerPort, reason)) } func (m *DryRunSLB) CreateLoadBalancerUDPListener(ctx context.Context, lbId string, listener model.ListenerAttribute) error { - mtype := "CreateLoadBalancerUDPListener" + mtype := MTypeCreateLoadBalancerUDPListener svc := getService(ctx) - AddEvent(SLB, fmt.Sprintf("%s/%d", util.Key(svc), listener.ListenerPort), lbId, - "CreateListener", ERROR, "") + dryrun.AddEvent(dryrun.SLB, fmt.Sprintf("%s/%d", util.Key(svc), listener.ListenerPort), lbId, + CodeCreateListener, dryrun.ERROR, "") return hintError(mtype, fmt.Sprintf("loadbalancer %s listener %d should be created", lbId, listener.ListenerPort)) } func (m *DryRunSLB) SetLoadBalancerUDPListenerAttribute(ctx context.Context, lbId string, listener model.ListenerAttribute) error { - mtype := "SetLoadBalancerUDPListenerAttribute" + mtype := MTypeSetLoadBalancerUDPListenerAttribute svc := getService(ctx) reason := getDryRunMsg(ctx) - AddEvent(SLB, fmt.Sprintf("%s/%d", util.Key(svc), listener.ListenerPort), lbId, - "UpdateListener", ERROR, reason) + dryrun.AddEvent(dryrun.SLB, fmt.Sprintf("%s/%d", util.Key(svc), listener.ListenerPort), lbId, + CodeUpdateListener, dryrun.ERROR, reason) return hintError(mtype, fmt.Sprintf("loadbalancer %s listener %d should be updated, %s", lbId, listener.ListenerPort, reason)) } func (m *DryRunSLB) CreateLoadBalancerHTTPListener(ctx context.Context, lbId string, listener model.ListenerAttribute) error { - mtype := "CreateLoadBalancerHTTPListener" + mtype := MTypeCreateLoadBalancerHTTPListener svc := getService(ctx) - AddEvent(SLB, fmt.Sprintf("%s/%d", util.Key(svc), listener.ListenerPort), lbId, - "CreateListener", ERROR, "") + dryrun.AddEvent(dryrun.SLB, fmt.Sprintf("%s/%d", util.Key(svc), listener.ListenerPort), lbId, + CodeCreateListener, dryrun.ERROR, "") return hintError(mtype, fmt.Sprintf("loadbalancer %s listener %d should be created", lbId, listener.ListenerPort)) } func (m *DryRunSLB) SetLoadBalancerHTTPListenerAttribute(ctx context.Context, lbId string, listener model.ListenerAttribute) error { - mtype := "SetLoadBalancerHTTPListenerAttribute" + mtype := MTypeSetLoadBalancerHTTPListenerAttribute svc := getService(ctx) reason := getDryRunMsg(ctx) - AddEvent(SLB, fmt.Sprintf("%s/%d", util.Key(svc), listener.ListenerPort), lbId, - "UpdateListener", ERROR, reason) + dryrun.AddEvent(dryrun.SLB, fmt.Sprintf("%s/%d", util.Key(svc), listener.ListenerPort), lbId, + CodeUpdateListener, dryrun.ERROR, reason) return hintError(mtype, fmt.Sprintf("loadbalancer %s listener %d should be updated, %s", lbId, listener.ListenerPort, reason)) } func (m *DryRunSLB) CreateLoadBalancerHTTPSListener(ctx context.Context, lbId string, listener model.ListenerAttribute) error { - mtype := "CreateLoadBalancerHTTPSListener" + mtype := MTypeCreateLoadBalancerHTTPSListener svc := getService(ctx) - AddEvent(SLB, fmt.Sprintf("%s/%d", util.Key(svc), listener.ListenerPort), lbId, "CreateListener", - ERROR, "") + dryrun.AddEvent(dryrun.SLB, fmt.Sprintf("%s/%d", util.Key(svc), listener.ListenerPort), lbId, CodeCreateListener, + dryrun.ERROR, "") return hintError(mtype, fmt.Sprintf("loadbalancer %s listener %d should be created", lbId, listener.ListenerPort)) } func (m *DryRunSLB) SetLoadBalancerHTTPSListenerAttribute(ctx context.Context, lbId string, listener model.ListenerAttribute) error { - mtype := "SetLoadBalancerHTTPSListenerAttribute" + mtype := MTypeSetLoadBalancerHTTPSListenerAttribute svc := getService(ctx) reason := getDryRunMsg(ctx) - AddEvent(SLB, fmt.Sprintf("%s/%d", util.Key(svc), listener.ListenerPort), lbId, - "UpdateListener", ERROR, reason) + dryrun.AddEvent(dryrun.SLB, fmt.Sprintf("%s/%d", util.Key(svc), listener.ListenerPort), lbId, + CodeUpdateListener, dryrun.ERROR, reason) return hintError(mtype, fmt.Sprintf("loadbalancer %s listener %d should be updated, %s", lbId, listener.ListenerPort, reason)) } @@ -226,10 +262,10 @@ func (m *DryRunSLB) DescribeVServerGroups(ctx context.Context, lbId string) ([]m } func (m *DryRunSLB) CreateVServerGroup(ctx context.Context, vg *model.VServerGroup, lbId string) error { - mtype := "CreateVServerGroup" + mtype := MTypeCreateVServerGroup svc := getService(ctx) - AddEvent(SLB, fmt.Sprintf("%s/%s", util.Key(svc), vg.VGroupName), lbId, - "CreateVgroup", ERROR, "") + dryrun.AddEvent(dryrun.SLB, fmt.Sprintf("%s/%s", util.Key(svc), vg.VGroupName), lbId, + CodeCreateVgroup, dryrun.ERROR, "") return hintError(mtype, fmt.Sprintf("loadbalancer %s vgroup %s should be created", lbId, vg.VGroupName)) } @@ -238,30 +274,30 @@ func (m *DryRunSLB) DescribeVServerGroupAttribute(ctx context.Context, vGroupId } func (m *DryRunSLB) DeleteVServerGroup(ctx context.Context, vGroupId string) error { - mtype := "DeleteVServerGroup" + mtype := MTypeDeleteVServerGroup svc := getService(ctx) lbId := getSlb(ctx) - AddEvent(SLB, fmt.Sprintf("%s/%s", util.Key(svc), vGroupId), lbId, - "DeleteVgroup", ERROR, "") + dryrun.AddEvent(dryrun.SLB, fmt.Sprintf("%s/%s", util.Key(svc), vGroupId), lbId, + CodeDeleteVgroup, dryrun.ERROR, "") return hintError(mtype, fmt.Sprintf("loadbalancer %s vgroup %s should be deleted", lbId, vGroupId)) } func (m *DryRunSLB) AddVServerGroupBackendServers(ctx context.Context, vGroupId string, backends string) error { - mtype := "AddVServerGroupBackendServers" + mtype := MTypeAddVServerGroupBackendServers svc := getService(ctx) lbId := getSlb(ctx) - AddEvent(SLB, fmt.Sprintf("%s/%s", util.Key(svc), vGroupId), lbId, - "AddVServerGroupBackendServers", ERROR, "") + dryrun.AddEvent(dryrun.SLB, fmt.Sprintf("%s/%s", util.Key(svc), vGroupId), lbId, + CodeAddVServerGroupBackendServers, dryrun.ERROR, "") return hintError(mtype, fmt.Sprintf("loadbalancer %s vgroup %s backends %s should be added", lbId, vGroupId, backends)) } func (m *DryRunSLB) RemoveVServerGroupBackendServers(ctx context.Context, vGroupId string, backends string) error { - mtype := "RemoveVServerGroupBackendServers" + mtype := MTypeRemoveVServerGroupBackendServers svc := getService(ctx) lbId := getSlb(ctx) - AddEvent(SLB, fmt.Sprintf("%s/%s", util.Key(svc), vGroupId), lbId, - "RemoveVgroup", ERROR, "") + dryrun.AddEvent(dryrun.SLB, fmt.Sprintf("%s/%s", util.Key(svc), vGroupId), lbId, + CodeRemoveVgroup, dryrun.ERROR, "") return hintError(mtype, fmt.Sprintf("loadbalancer %s vgroup %s backends %s should be deleted", lbId, vGroupId, backends)) } @@ -272,11 +308,11 @@ func (m *DryRunSLB) SetVServerGroupAttribute(ctx context.Context, vGroupId strin } func (m *DryRunSLB) ModifyVServerGroupBackendServers(ctx context.Context, vGroupId string, old string, new string) error { - mtype := "ModifyVServerGroupBackendServers" + mtype := MTypeModifyVServerGroupBackendServers svc := getService(ctx) lbId := getSlb(ctx) - AddEvent(SLB, fmt.Sprintf("%s/VGroupID/%s", util.Key(svc), vGroupId), lbId, - "ModifyVgroup", ERROR, "") + dryrun.AddEvent(dryrun.SLB, fmt.Sprintf("%s/VGroupID/%s", util.Key(svc), vGroupId), lbId, + CodeModifyVgroup, dryrun.ERROR, "") return hintError(mtype, fmt.Sprintf("loadbalancer %s vgroup %s backends should be %s", lbId, vGroupId, new)) } @@ -289,26 +325,26 @@ func (m *DryRunSLB) DescribeDomainExtensions(ctx context.Context, lbId string, p } func (m *DryRunSLB) CreateDomainExtension(ctx context.Context, lbId string, port int, domain string, certId string) error { - mtype := "CreateDomainExtension" + mtype := MTypeCreateDomainExtension svc := getService(ctx) - AddEvent(SLB, fmt.Sprintf("%s/%d", util.Key(svc), port), lbId, - "CreateDomainExtension", ERROR, "") + dryrun.AddEvent(dryrun.SLB, fmt.Sprintf("%s/%d", util.Key(svc), port), lbId, + CodeCreateDomainExtension, dryrun.ERROR, "") return hintError(mtype, fmt.Sprintf("loadbalancer %s listener %d domain extension %s should be created", lbId, port, domain)) } func (m *DryRunSLB) DeleteDomainExtension(ctx context.Context, id string) error { - mtype := "DeleteDomainExtension" + mtype := MTypeDeleteDomainExtension svc := getService(ctx) - AddEvent(SLB, fmt.Sprintf("%s/%s", util.Key(svc), id), id, - "DeleteDomainExtension", ERROR, "") + dryrun.AddEvent(dryrun.SLB, fmt.Sprintf("%s/%s", util.Key(svc), id), id, + CodeDeleteDomainExtension, dryrun.ERROR, "") return hintError(mtype, fmt.Sprintf("domain extension %s should be deleted", id)) } func (m *DryRunSLB) SetDomainExtensionAttribute(ctx context.Context, id string, certId string) error { - mtype := "SetDomainExtensionAttribute" + mtype := MTypeSetDomainExtensionAttribute svc := getService(ctx) - AddEvent(SLB, fmt.Sprintf("%s/%s", util.Key(svc), id), id, - "SetDomainExtensionAttribute", ERROR, "") + dryrun.AddEvent(dryrun.SLB, fmt.Sprintf("%s/%s", util.Key(svc), id), id, + CodeSetDomainExtensionAttribute, dryrun.ERROR, "") return hintError(mtype, fmt.Sprintf("domain extension %s should be set", id)) } @@ -321,7 +357,7 @@ func getTagString(tags []tag.Tag) string { } func getService(ctx context.Context) *v1.Service { - isvc := ctx.Value(ContextService) + isvc := ctx.Value(dryrun.ContextService) if isvc == nil { return unknown() } @@ -333,7 +369,7 @@ func getService(ctx context.Context) *v1.Service { } func getSlb(ctx context.Context) string { - islb := ctx.Value(ContextSLB) + islb := ctx.Value(dryrun.ContextSLB) if islb == nil { return "" } @@ -354,7 +390,7 @@ func unknown() *v1.Service { } func getDryRunMsg(ctx context.Context) string { - isMsg := ctx.Value(ContextMessage) + isMsg := ctx.Value(dryrun.ContextMessage) if isMsg == nil { return "" } diff --git a/pkg/provider/provider.go b/pkg/provider/provider.go index 11a8614a0..2ac2800d2 100644 --- a/pkg/provider/provider.go +++ b/pkg/provider/provider.go @@ -287,7 +287,7 @@ type INLB interface { // Jobs BatchWaitJobsFinish(ctx context.Context, api string, jobIds []string, args ...time.Duration) error - WaitJobFinish(api, jobId string, args ...time.Duration) error + WaitJobFinish(ctx context.Context, api, jobId string, args ...time.Duration) error } // EFLONodeAttribute EFLO node attribute from cloud LingJun instance diff --git a/pkg/provider/vmock/nlb.go b/pkg/provider/vmock/nlb.go index 2b8908dc9..51329ffcc 100644 --- a/pkg/provider/vmock/nlb.go +++ b/pkg/provider/vmock/nlb.go @@ -71,8 +71,7 @@ func (m MockNLB) TagNLBResource(ctx context.Context, resourceId string, resource } func (m MockNLB) UntagNLBResources(ctx context.Context, resourceId string, resourceType nlbmodel.TagResourceType, tagKey []*string) error { - //TODO implement me - panic("implement me") + return nil } func (m MockNLB) ListNLBTagResources(ctx context.Context, lbId string) ([]tag.Tag, error) { @@ -408,7 +407,7 @@ func (m MockNLB) BatchWaitJobsFinish(ctx context.Context, api string, jobIds []s return nil } -func (m MockNLB) WaitJobFinish(api, jobId string, args ...time.Duration) error { +func (m MockNLB) WaitJobFinish(ctx context.Context, api, jobId string, args ...time.Duration) error { return nil } diff --git a/pkg/provider/vmock/slb.go b/pkg/provider/vmock/slb.go index feb102187..feaa1a0cb 100644 --- a/pkg/provider/vmock/slb.go +++ b/pkg/provider/vmock/slb.go @@ -11,7 +11,7 @@ import ( "k8s.io/cloud-provider-alibaba-cloud/pkg/model/tag" prvd "k8s.io/cloud-provider-alibaba-cloud/pkg/provider" "k8s.io/cloud-provider-alibaba-cloud/pkg/provider/alibaba/base" - "k8s.io/cloud-provider-alibaba-cloud/pkg/provider/dryrun" + "k8s.io/cloud-provider-alibaba-cloud/pkg/util/dryrun" "k8s.io/klog/v2" ) diff --git a/pkg/util/dryrun/dryrun.go b/pkg/util/dryrun/dryrun.go new file mode 100644 index 000000000..e58383bc4 --- /dev/null +++ b/pkg/util/dryrun/dryrun.go @@ -0,0 +1,49 @@ +package dryrun + +import ( + "sync" + + "k8s.io/apimachinery/pkg/util/sets" +) + +type Instance struct { + lock sync.Mutex + checkNames sets.Set[string] + done chan struct{} +} + +var global = &Instance{ + checkNames: sets.New[string](), + done: make(chan struct{}), +} + +func (i *Instance) RegisterDryRun(checkName string) { + i.lock.Lock() + defer i.lock.Unlock() + global.checkNames.Insert(checkName) +} + +func (i *Instance) Finish(checkName string) { + i.lock.Lock() + defer i.lock.Unlock() + global.checkNames.Delete(checkName) + if global.checkNames.Len() == 0 { + close(global.done) + } +} + +func (i *Instance) Done() <-chan struct{} { + return global.done +} + +func RegisterDryRun(checkName string) { + global.RegisterDryRun(checkName) +} + +func Finish(checkName string) { + global.Finish(checkName) +} + +func Done() <-chan struct{} { + return global.Done() +} diff --git a/pkg/provider/dryrun/event.go b/pkg/util/dryrun/event.go similarity index 99% rename from pkg/provider/dryrun/event.go rename to pkg/util/dryrun/event.go index e2f973522..d7bbf61b8 100644 --- a/pkg/provider/dryrun/event.go +++ b/pkg/util/dryrun/event.go @@ -4,14 +4,15 @@ import ( "context" "encoding/json" "fmt" + "sync" + "time" + v1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/wait" "k8s.io/klog/v2" "sigs.k8s.io/controller-runtime/pkg/client" - "sync" - "time" ) const (