From df3b738330e8b23669fd51bad118791540c5a040 Mon Sep 17 00:00:00 2001 From: joshvanl Date: Wed, 18 Oct 2023 21:18:29 +0100 Subject: [PATCH] Delete injector mutating webhook on downgrade from 1.12 to 1.11 Signed-off-by: joshvanl --- cmd/upgrade.go | 3 ++- pkg/kubernetes/upgrade.go | 51 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/cmd/upgrade.go b/cmd/upgrade.go index ee54a7e47..c01714635 100644 --- a/cmd/upgrade.go +++ b/cmd/upgrade.go @@ -14,6 +14,7 @@ limitations under the License. package cmd import ( + "context" "os" "strings" @@ -57,7 +58,7 @@ dapr upgrade -k print.FailureStatusEvent(os.Stderr, err.Error()) os.Exit(1) } - err = kubernetes.Upgrade(kubernetes.UpgradeConfig{ + err = kubernetes.Upgrade(context.Background(), kubernetes.UpgradeConfig{ RuntimeVersion: upgradeRuntimeVersion, DashboardVersion: upgradeDashboardVersion, Args: values, diff --git a/pkg/kubernetes/upgrade.go b/pkg/kubernetes/upgrade.go index f2c944ce7..69dc8696d 100644 --- a/pkg/kubernetes/upgrade.go +++ b/pkg/kubernetes/upgrade.go @@ -14,17 +14,21 @@ limitations under the License. package kubernetes import ( + "context" + "errors" "fmt" "net/http" "os" "time" + "github.com/hashicorp/go-version" helm "helm.sh/helm/v3/pkg/action" "helm.sh/helm/v3/pkg/chart" + admissionregistrationv1 "k8s.io/api/admissionregistration/v1" + apierrors "k8s.io/apimachinery/pkg/api/errors" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/helm/pkg/strvals" - "github.com/hashicorp/go-version" - "github.com/dapr/cli/pkg/print" "github.com/dapr/cli/utils" ) @@ -56,7 +60,7 @@ type UpgradeConfig struct { ImageVariant string } -func Upgrade(conf UpgradeConfig) error { +func Upgrade(ctx context.Context, conf UpgradeConfig) error { helmRepo := utils.GetEnv("DAPR_HELM_REPO_URL", daprHelmRepo) status, err := GetDaprResourcesStatus() if err != nil { @@ -166,7 +170,31 @@ func Upgrade(conf UpgradeConfig) error { return err } + client, err := Client() + if err != nil { + return err + } + + var mutatingWebhookConf *admissionregistrationv1.MutatingWebhookConfiguration + if is12to11Downgrade(conf.RuntimeVersion, daprVersion) { + print.InfoStatusEvent(os.Stdout, "Downgrade from 1.12 to 1.11 detected, temporarily deleting injector mutating webhook...") + + mutatingWebhookConf, err = client.AdmissionregistrationV1().MutatingWebhookConfigurations().Get(ctx, "dapr-sidecar-injector", metav1.GetOptions{}) + if err != nil && !apierrors.IsNotFound(err) { + return err + } + + err = client.AdmissionregistrationV1().MutatingWebhookConfigurations().Delete(ctx, "dapr-sidecar-injector", metav1.DeleteOptions{}) + if err != nil && !apierrors.IsNotFound(err) { + return err + } + } + if _, err = upgradeClient.Run(chart, controlPlaneChart, vals); err != nil { + if mutatingWebhookConf != nil { + _, merr := client.AdmissionregistrationV1().MutatingWebhookConfigurations().Create(ctx, mutatingWebhookConf, metav1.CreateOptions{}) + return errors.Join(err, merr) + } return err } @@ -264,3 +292,20 @@ func isDowngrade(targetVersion, existingVersion string) bool { } return target.LessThan(existing) } + +func is12to11Downgrade(targetVersion, existingVersion string) bool { + target, _ := version.NewVersion(targetVersion) + existing, _ := version.NewVersion(existingVersion) + if target == nil || existing == nil { + return false + } + + tset := target.Segments() + eset := existing.Segments() + + if len(eset) < 2 || len(tset) < 2 { + return false + } + + return eset[0] == 1 && eset[1] == 12 && tset[0] == 1 && tset[1] == 11 +}