Skip to content

Commit

Permalink
fix remote buildkit error: [415: Unsupported Media Type], compatibili…
Browse files Browse the repository at this point in the history
…ty legacy kube api server which not support server-side apply

Signed-off-by: wurenny <[email protected]>
  • Loading branch information
wurenny committed Jun 11, 2024
1 parent 3212b31 commit 5756c48
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 4 deletions.
5 changes: 5 additions & 0 deletions pkg/devspace/build/localregistry/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,16 @@ func (r *LocalRegistry) ensureDeployment(ctx devspacecontext.Context) (*appsv1.D
return nil, err
}

if !ctx.KubeClient().SupportServerSideApply() {
return existing, nil
}

// Use server side apply if it does exist
applyConfiguration, err := appsapplyv1.ExtractDeployment(existing, ApplyFieldManager)
if err != nil {
return nil, err
}

return ctx.KubeClient().KubeClient().AppsV1().Deployments(r.Namespace).Apply(
ctx.Context(),
applyConfiguration,
Expand Down
4 changes: 4 additions & 0 deletions pkg/devspace/build/localregistry/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ func (r *LocalRegistry) ensureService(ctx devspacecontext.Context) (*corev1.Serv
return nil, err
}

if !ctx.KubeClient().SupportServerSideApply() {
return existing, nil
}

// Use server side apply if it does exist
applyConfiguration, err := applyv1.ExtractService(existing, ApplyFieldManager)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions pkg/devspace/build/localregistry/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ func (r *LocalRegistry) ensureStatefulset(ctx devspacecontext.Context) (*appsv1.
return nil, err
}

if !ctx.KubeClient().SupportServerSideApply() {
return existing, nil
}

// Use server side apply if it does exist
applyConfiguration, err := appsapplyv1.ExtractStatefulSet(existing, ApplyFieldManager)
if err != nil {
Expand Down
42 changes: 38 additions & 4 deletions pkg/devspace/kubectl/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ package kubectl
import (
"context"
"fmt"
"io"
"net/http"
"os"
"time"

"github.com/loft-sh/devspace/pkg/devspace/config/localcache"
"github.com/loft-sh/devspace/pkg/devspace/kill"
"github.com/loft-sh/devspace/pkg/devspace/kubectl/util"
Expand All @@ -12,11 +17,7 @@ import (
"github.com/loft-sh/devspace/pkg/util/log"
"github.com/loft-sh/devspace/pkg/util/survey"
"github.com/loft-sh/devspace/pkg/util/terminal"
"io"
"k8s.io/apimachinery/pkg/util/wait"
"net/http"
"os"
"time"

"github.com/mgutz/ansi"
"github.com/pkg/errors"
Expand All @@ -40,6 +41,9 @@ type Client interface {
// KubeClient returns an interface to a kube client
KubeClient() kubernetes.Interface

// KubeGitVersion returns kubernetes server version
KubeGitVersion() string

// Namespace returns the default namespace of the kube context
Namespace() string

Expand All @@ -52,6 +56,9 @@ type Client interface {
// KubeConfigLoader returns the kube config loader interface
KubeConfigLoader() kubeconfig.Loader

// SupportServerSideApply returns if kubernetes support server-side apply
SupportServerSideApply() bool

// CopyFromReader copies and extracts files into the container from the reader interface
CopyFromReader(ctx context.Context, pod *k8sv1.Pod, container, containerPath string, reader io.Reader) error

Expand Down Expand Up @@ -91,6 +98,9 @@ type client struct {
currentContext string
namespace string
isInCluster bool

kubeGitVersion string
supportServerSideApply bool
}

var _, tty = terminal.SetupTTY(os.Stdin, os.Stdout)
Expand Down Expand Up @@ -130,6 +140,15 @@ func NewClientFromContext(context, namespace string, switchContext bool, kubeLoa
return nil, errors.Wrap(err, "new client")
}

kubeGitVersion, err := GetKubeGitVersion(kubeClient)
if err != nil {
return nil, errors.Wrap(err, "get kubernetes version")
}
supportServerSideApply, err := IsSupportServerSideApply(kubeClient)
if err != nil {
return nil, errors.Wrap(err, "if support server side apply")
}

return &client{
Client: kubeClient,
clientConfig: clientConfig,
Expand All @@ -139,6 +158,9 @@ func NewClientFromContext(context, namespace string, switchContext bool, kubeLoa
namespace: activeNamespace,
currentContext: activeContext,
isInCluster: isInCluster,

kubeGitVersion: kubeGitVersion,
supportServerSideApply: supportServerSideApply,
}, nil
}

Expand Down Expand Up @@ -285,6 +307,10 @@ func CheckKubeContext(client Client, localCache localcache.Cache, noWarning, aut
}
}

if !client.SupportServerSideApply() {
log.Debugf("kubernetes version '%s' not support server side apply", ansi.Color(client.KubeGitVersion(), "white+b"))
}

// wake up and ping
if !skipWakeUpPing {
err := wakeUpAndPing(context.TODO(), client, log)
Expand All @@ -304,6 +330,10 @@ func (client *client) KubeClient() kubernetes.Interface {
return client.Client
}

func (client *client) KubeGitVersion() string {
return client.kubeGitVersion
}

func (client *client) Namespace() string {
return client.namespace
}
Expand All @@ -316,6 +346,10 @@ func (client *client) KubeConfigLoader() kubeconfig.Loader {
return client.kubeLoader
}

func (client *client) SupportServerSideApply() bool {
return client.supportServerSideApply
}

func wakeUpAndPing(ctx context.Context, client Client, log log.Logger) error {
err := wakeUp(ctx, client, log)
if err != nil {
Expand Down
26 changes: 26 additions & 0 deletions pkg/devspace/kubectl/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/version"

"github.com/loft-sh/devspace/pkg/devspace/kubectl/portforward"
"github.com/loft-sh/devspace/pkg/util/log"
Expand All @@ -16,6 +17,7 @@ import (
corev1 "k8s.io/api/core/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/transport/spdy"
)

Expand Down Expand Up @@ -274,3 +276,27 @@ func GetKindContext(context string) string {

return strings.TrimPrefix(context, "kind-")
}

func GetKubeGitVersion(kubeClient *kubernetes.Clientset) (string, error) {
kubeVersion, err := kubeClient.Discovery().ServerVersion()
if err != nil {
return "", err
}
return kubeVersion.GitVersion, nil
}

func IsSupportServerSideApply(kubeClient *kubernetes.Clientset) (bool, error) {
kubeGitVersionString, err := GetKubeGitVersion(kubeClient)
if err != nil {
return true, err
}
kubeGitVersion, err := version.ParseSemantic(kubeGitVersionString)
if err != nil {
return true, err
}
supportSSAVersion, err := version.ParseSemantic("v1.22.0")
if err != nil {
return true, err
}
return kubeGitVersion.AtLeast(supportSSAVersion), nil
}

0 comments on commit 5756c48

Please sign in to comment.