diff --git a/internal/controller/mastodonserver_controller.go b/internal/controller/mastodonserver_controller.go index 36e06dc..c2173de 100644 --- a/internal/controller/mastodonserver_controller.go +++ b/internal/controller/mastodonserver_controller.go @@ -5,6 +5,7 @@ import ( "encoding/base64" "errors" "fmt" + "time" k8serrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/labels" @@ -194,11 +195,13 @@ func (r *MastodonServerReconciler) Reconcile(ctx context.Context, req ctrl.Reque if err := r.createMigrationJob(ctx, &server, k8sStatus.migratingImageMap, jobPreMigration); err != nil { return ctrl.Result{}, err } + return ctrl.Result{}, nil case shouldCreatePostMigrationJob: if err := r.createMigrationJob(ctx, &server, k8sStatus.migratingImageMap, jobPostMigration); err != nil { return ctrl.Result{}, err } + return ctrl.Result{}, nil case shouldSetMigratingStatus: server.Status.Migrating = &magoutv1.MastodonServerMigratingStatus{ @@ -209,22 +212,26 @@ func (r *MastodonServerReconciler) Reconcile(ctx context.Context, req ctrl.Reque if err := r.Client.Status().Update(ctx, &server); err != nil { return ctrl.Result{}, err } + return ctrl.Result{}, nil case shouldUnsetMigratingStatus: server.Status.Migrating = nil if err := r.Client.Status().Update(ctx, &server); err != nil { return ctrl.Result{}, err } + return ctrl.Result{}, nil case shouldCreateOrUpdateDeploysWithSpec: if err := r.createOrUpdateDeployments(ctx, &server, k8sStatus.specImageMap); err != nil { return ctrl.Result{}, err } + return ctrl.Result{}, nil case shouldCreateOrUpdateDeploysWithMigratingImages: if err := r.createOrUpdateDeployments(ctx, &server, k8sStatus.migratingImageMap); err != nil { return ctrl.Result{}, err } + return ctrl.Result{RequeueAfter: 5 * time.Second}, nil case shouldDeletePostMigrationJob: if err := r.deleteJob( @@ -234,6 +241,7 @@ func (r *MastodonServerReconciler) Reconcile(ctx context.Context, req ctrl.Reque ); err != nil { return ctrl.Result{}, err } + return ctrl.Result{}, nil case shouldDeletePreMigrationJob: if err := r.deleteJob( @@ -243,14 +251,13 @@ func (r *MastodonServerReconciler) Reconcile(ctx context.Context, req ctrl.Reque ); err != nil { return ctrl.Result{}, err } + return ctrl.Result{}, nil case shouldDoNothing: - - default: - panic("unreachable") + return ctrl.Result{}, nil } - return ctrl.Result{}, nil + panic("unreachable") } func (r *MastodonServerReconciler) fetchK8sStatus( @@ -486,18 +493,18 @@ func (r *MastodonServerReconciler) createOrUpdateDeployment( deploy.SetNamespace(server.GetNamespace()) result, err := ctrl.CreateOrUpdate(ctx, r.Client, &deploy, func() error { - if deployAnnotations == nil { - deployAnnotations = map[string]string{} + if deploy.ObjectMeta.Annotations == nil { + deploy.ObjectMeta.Annotations = map[string]string{} } for k, v := range deployAnnotations { - deployAnnotations[k] = v + deploy.ObjectMeta.Annotations[k] = v } - if deployLabels == nil { - deployLabels = map[string]string{} + if deploy.ObjectMeta.Labels == nil { + deploy.ObjectMeta.Labels = map[string]string{} } for k, v := range deployLabels { - deployLabels[k] = v + deploy.ObjectMeta.Labels[k] = v } selector := map[string]string{ diff --git a/test/e2e/e2e_test.go b/test/e2e/e2e_test.go index 2d49bae..44e5351 100644 --- a/test/e2e/e2e_test.go +++ b/test/e2e/e2e_test.go @@ -12,6 +12,10 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" + + appsv1 "k8s.io/api/apps/v1" + corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/api/resource" ) var ( @@ -121,6 +125,59 @@ func checkSchemaMigrationsCount(namespace string, expected int) error { return nil } +func getDeployment(name, namespace string) (*appsv1.Deployment, error) { + stdout, _, err := kubectl(nil, "get", "-n", namespace, "deploy", name, "-o", "json") + if err != nil { + return nil, err + } + + var deploy appsv1.Deployment + if err := json.Unmarshal(stdout, &deploy); err != nil { + return nil, err + } + + return &deploy, err +} + +func checkDeployResources( + name, namespace string, + expected corev1.ResourceRequirements, +) error { + deploy, err := getDeployment(name, namespace) + if err != nil { + return err + } + + got := deploy.Spec.Template.Spec.Containers[0].Resources + if !got.Limits.Cpu().Equal(*expected.Limits.Cpu()) { + return errors.New("limits cpu not equal") + } + if !got.Limits.Memory().Equal(*expected.Limits.Memory()) { + return errors.New("limits cpu not equal") + } + if !got.Requests.Cpu().Equal(*expected.Requests.Cpu()) { + return errors.New("limits cpu not equal") + } + if !got.Requests.Memory().Equal(*expected.Requests.Memory()) { + return errors.New("limits cpu not equal") + } + + return nil +} + +func checkDeployAnnotation(name, namespace, key, expectedValue string) error { + deploy, err := getDeployment(name, namespace) + if err != nil { + return err + } + + if deploy.GetAnnotations()[key] != expectedValue { + return errors.New("annotation not matched") + } + + return nil +} + var _ = Describe("controller", Ordered, func() { Context("Operator", func() { It("should run successfully", func() { @@ -146,18 +203,85 @@ var _ = Describe("controller", Ordered, func() { if err := waitDeployAvailable("mastodon0-gateway", namespace); err != nil { return err } + + if err := checkDeployResources("mastodon0-web", "e2e", corev1.ResourceRequirements{ + Limits: corev1.ResourceList{ + corev1.ResourceCPU: resource.MustParse("1"), + corev1.ResourceMemory: resource.MustParse("1000Mi"), + }, + Requests: corev1.ResourceList{ + corev1.ResourceCPU: resource.MustParse("100m"), + corev1.ResourceMemory: resource.MustParse("100Mi"), + }, + }); err != nil { + return err + } + + if err := checkDeployAnnotation("mastodon0-sidekiq", namespace, + "test.magout.anqou.net/role", "sidekiq"); err != nil { + return err + } + if err := checkDeployAnnotation("mastodon0-streaming", namespace, + "test.magout.anqou.net/role", "streaming"); err != nil { + return err + } + if err := checkDeployAnnotation("mastodon0-web", namespace, + "test.magout.anqou.net/role", "web"); err != nil { + return err + } + if err := httpGet("http://mastodon0-gateway.e2e.svc/health"); err != nil { return err } + if err := checkMastodonVersion("mastodon.test", "http://mastodon0-gateway.e2e.svc", "4.2.12"); err != nil { return err } + if err := checkSchemaMigrationsCount(namespace, 422); err != nil { return err } return nil }).Should(Succeed()) + + _, _, err = helm(nil, "upgrade", "--install", "--namespace", namespace, + "magout-mastodon-server", "../../charts/magout-mastodon-server", "--wait", + "-f", "testdata/values-v4.3.0b1.yaml") + Expect(err).NotTo(HaveOccurred()) + + Eventually(func() error { + return checkSchemaMigrationsCount(namespace, 460) + }).Should(Succeed()) + + Eventually(func() error { + if err := waitDeployAvailable("mastodon0-web", namespace); err != nil { + return err + } + if err := waitDeployAvailable("mastodon0-sidekiq", namespace); err != nil { + return err + } + if err := waitDeployAvailable("mastodon0-streaming", namespace); err != nil { + return err + } + if err := waitDeployAvailable("mastodon0-gateway", namespace); err != nil { + return err + } + + if err := httpGet("http://mastodon0-gateway.e2e.svc/health"); err != nil { + return err + } + + if err := checkSchemaMigrationsCount(namespace, 468); err != nil { + return err + } + return nil + }).Should(Succeed()) + + Eventually(func() error { + return checkMastodonVersion("mastodon.test", + "http://mastodon0-gateway.e2e.svc", "4.3.0-beta.1") + }, "300s", "10s").Should(Succeed()) }) }) }) diff --git a/test/e2e/testdata/values-v4.2.12.yaml b/test/e2e/testdata/values-v4.2.12.yaml index 25fdff8..5059a4e 100644 --- a/test/e2e/testdata/values-v4.2.12.yaml +++ b/test/e2e/testdata/values-v4.2.12.yaml @@ -9,12 +9,14 @@ gateway: mastodonServer: sidekiq: + annotations: + test.magout.anqou.net/role: sidekiq envFrom: - secretRef: name: secret-env streaming: annotations: - test.mahout.anqou.net/role: streaming + test.magout.anqou.net/role: streaming envFrom: - secretRef: name: secret-env @@ -24,7 +26,7 @@ mastodonServer: name: secret-env replicas: 1 annotations: - test.mahout.anqou.net/role: web + test.magout.anqou.net/role: web resources: requests: cpu: 100m diff --git a/test/e2e/testdata/values-v4.3.0b1.yaml b/test/e2e/testdata/values-v4.3.0b1.yaml new file mode 100644 index 0000000..8b7ab22 --- /dev/null +++ b/test/e2e/testdata/values-v4.3.0b1.yaml @@ -0,0 +1,36 @@ +fullnameOverride: "mastodon0" + +mastodonVersion: + image: ghcr.io/mastodon/mastodon:4.3.0b1 + streamingImage: ghcr.io/mastodon/mastodon-streaming:4.3.0b1 + +gateway: + serverName: mastodon.test + +mastodonServer: + sidekiq: + annotations: + test.magout.anqou.net/role: sidekiq + envFrom: + - secretRef: + name: secret-env + streaming: + annotations: + test.magout.anqou.net/role: streaming + envFrom: + - secretRef: + name: secret-env + web: + envFrom: + - secretRef: + name: secret-env + replicas: 1 + annotations: + test.magout.anqou.net/role: web + resources: + requests: + cpu: 100m + memory: 100Mi + limits: + cpu: "1" + memory: 1000Mi