Skip to content

Commit

Permalink
Delete injector mutating webhook on downgrade from 1.12 to 1.11
Browse files Browse the repository at this point in the history
Signed-off-by: joshvanl <[email protected]>
  • Loading branch information
JoshVanL committed Oct 18, 2023
1 parent 1d60280 commit df3b738
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
3 changes: 2 additions & 1 deletion cmd/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ limitations under the License.
package cmd

import (
"context"
"os"
"strings"

Expand Down Expand Up @@ -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,
Expand Down
51 changes: 48 additions & 3 deletions pkg/kubernetes/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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()

Check failure on line 65 in pkg/kubernetes/upgrade.go

View workflow job for this annotation

GitHub Actions / Build linux_amd64 binaries

Function `GetDaprResourcesStatus->Status->Status$1->ListPodsInterface` should pass the context parameter (contextcheck)
if err != nil {
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

0 comments on commit df3b738

Please sign in to comment.