From 32a2e244105d0ead9c4c20cfdb0c6a378a558744 Mon Sep 17 00:00:00 2001 From: Amir Blum Date: Wed, 22 May 2024 11:08:45 +0300 Subject: [PATCH 1/2] fix: remove service on upgrade --- cli/cmd/upgrade.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/cli/cmd/upgrade.go b/cli/cmd/upgrade.go index 27fb2fc9a..da9e589c8 100644 --- a/cli/cmd/upgrade.go +++ b/cli/cmd/upgrade.go @@ -4,6 +4,7 @@ Copyright © 2023 NAME HERE package cmd import ( + "context" "fmt" "os" "runtime" @@ -16,6 +17,7 @@ import ( "github.com/odigos-io/odigos/common/consts" "github.com/odigos-io/odigos/common/utils" "github.com/spf13/cobra" + apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -121,6 +123,12 @@ and apply any required migrations and adaptations.`, os.Exit(1) } + err = fixBreakingChangesAfterUpgrade(ctx, client, ns) + if err != nil { + fmt.Println("Odigos upgrade failed - unable to fix breaking changes.") + os.Exit(1) + } + // download a ui binary for the new version _, binaryDir := GetOdigosUiBinaryPath() err = DoDownloadNewUiBinary(targetVersion.String(), binaryDir, runtime.GOARCH, runtime.GOOS) @@ -130,6 +138,31 @@ and apply any required migrations and adaptations.`, }, } +func fixBreakingChangesAfterUpgrade(ctx context.Context, client *kube.Client, ns string) error { + + // to support multiple gateways, odigos service changed it's ClusterIP to None + // this change is not automatically applied to existing installations, we need to delete the service + // so that it can be recreated with the new ClusterIP value + svc, err := client.CoreV1().Services(ns).Get(ctx, "odigos-gateway", metav1.GetOptions{}) + if err != nil && !apierrors.IsNotFound(err) { + fmt.Println("Odigos upgrade failed - unable to read the Odigos gateway service.") + return err + } + + if svc != nil { + if svc.Spec.ClusterIP != "None" { + fmt.Println("Odigos upgrade - recreating the Odigos gateway service to support multiple gateways.") + err = client.CoreV1().Services(ns).Delete(ctx, "odigos-gateway", metav1.DeleteOptions{}) + if err != nil { + fmt.Println("Odigos upgrade failed - unable to delete the Odigos gateway service") + return err + } + } + } + + return nil +} + func init() { rootCmd.AddCommand(upgradeCmd) upgradeCmd.Flags().Bool("yes", false, "skip the confirmation prompt") From c576b81d69fa313cbf14061ef602fd8674030bf3 Mon Sep 17 00:00:00 2001 From: Amir Blum Date: Wed, 22 May 2024 11:25:46 +0300 Subject: [PATCH 2/2] fix: delete old svc in atuoscaler instead of cli --- autoscaler/controllers/gateway/root.go | 6 +++++ autoscaler/controllers/gateway/service.go | 22 +++++++++++++++ cli/cmd/upgrade.go | 33 ----------------------- 3 files changed, 28 insertions(+), 33 deletions(-) diff --git a/autoscaler/controllers/gateway/root.go b/autoscaler/controllers/gateway/root.go index de5f41771..37b6c4d4e 100644 --- a/autoscaler/controllers/gateway/root.go +++ b/autoscaler/controllers/gateway/root.go @@ -82,6 +82,12 @@ func syncGateway(dests *odigosv1.DestinationList, processors *odigosv1.Processor return err } + err = deletePreviousServices(ctx, c, gateway.Namespace) + if err != nil { + logger.Error(err, "Failed to delete previous services") + return err + } + _, err = syncService(gateway, ctx, c, scheme) if err != nil { logger.Error(err, "Failed to sync service") diff --git a/autoscaler/controllers/gateway/service.go b/autoscaler/controllers/gateway/service.go index 6fdfabf54..d15f63046 100644 --- a/autoscaler/controllers/gateway/service.go +++ b/autoscaler/controllers/gateway/service.go @@ -15,6 +15,28 @@ import ( "sigs.k8s.io/controller-runtime/pkg/log" ) +func deletePreviousServices(ctx context.Context, c client.Client, ns string) error { + // to support multiple gateways, odigos service changed it's ClusterIP to None + // this change is not automatically applied to existing installations, we need to delete the service + // so that it can be recreated with the new ClusterIP value + logger := log.FromContext(ctx) + svc := &v1.Service{} + err := c.Get(ctx, client.ObjectKey{Name: kubeObjectName, Namespace: ns}, svc) + if err != nil || svc == nil { + return client.IgnoreNotFound(err) + } + + if svc.Spec.ClusterIP != "None" { + logger.Info("Deleting the Odigos gateway service to support multiple gateways.") + err = c.Delete(ctx, svc, &client.DeleteOptions{}) + if err != nil { + return err + } + } + + return nil +} + func syncService(gateway *odigosv1.CollectorsGroup, ctx context.Context, c client.Client, scheme *runtime.Scheme) (*v1.Service, error) { logger := log.FromContext(ctx) gatewaySvc := &v1.Service{ diff --git a/cli/cmd/upgrade.go b/cli/cmd/upgrade.go index da9e589c8..27fb2fc9a 100644 --- a/cli/cmd/upgrade.go +++ b/cli/cmd/upgrade.go @@ -4,7 +4,6 @@ Copyright © 2023 NAME HERE package cmd import ( - "context" "fmt" "os" "runtime" @@ -17,7 +16,6 @@ import ( "github.com/odigos-io/odigos/common/consts" "github.com/odigos-io/odigos/common/utils" "github.com/spf13/cobra" - apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -123,12 +121,6 @@ and apply any required migrations and adaptations.`, os.Exit(1) } - err = fixBreakingChangesAfterUpgrade(ctx, client, ns) - if err != nil { - fmt.Println("Odigos upgrade failed - unable to fix breaking changes.") - os.Exit(1) - } - // download a ui binary for the new version _, binaryDir := GetOdigosUiBinaryPath() err = DoDownloadNewUiBinary(targetVersion.String(), binaryDir, runtime.GOARCH, runtime.GOOS) @@ -138,31 +130,6 @@ and apply any required migrations and adaptations.`, }, } -func fixBreakingChangesAfterUpgrade(ctx context.Context, client *kube.Client, ns string) error { - - // to support multiple gateways, odigos service changed it's ClusterIP to None - // this change is not automatically applied to existing installations, we need to delete the service - // so that it can be recreated with the new ClusterIP value - svc, err := client.CoreV1().Services(ns).Get(ctx, "odigos-gateway", metav1.GetOptions{}) - if err != nil && !apierrors.IsNotFound(err) { - fmt.Println("Odigos upgrade failed - unable to read the Odigos gateway service.") - return err - } - - if svc != nil { - if svc.Spec.ClusterIP != "None" { - fmt.Println("Odigos upgrade - recreating the Odigos gateway service to support multiple gateways.") - err = client.CoreV1().Services(ns).Delete(ctx, "odigos-gateway", metav1.DeleteOptions{}) - if err != nil { - fmt.Println("Odigos upgrade failed - unable to delete the Odigos gateway service") - return err - } - } - } - - return nil -} - func init() { rootCmd.AddCommand(upgradeCmd) upgradeCmd.Flags().Bool("yes", false, "skip the confirmation prompt")