Skip to content

Commit

Permalink
Use metadata.Client instead of resource.Builder
Browse files Browse the repository at this point in the history
This is more efficient. It only fetches the metadata, not the actual
resources. It also behaves like a classic Kubernetes client and
doesn't require a RESTClientGetter. Since this is the last use of k0s'
RESTClientGetter implementation, this file could be dropped entirely.

Signed-off-by: Tom Wieczorek <twieczorek@mirantis.com>
  • Loading branch information
twz123 committed Jun 18, 2024
1 parent 1dc4f54 commit 3edc61b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 89 deletions.
43 changes: 18 additions & 25 deletions pkg/autopilot/checks/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,22 @@
package checks

import (
"context"
"fmt"

"github.com/k0sproject/k0s/pkg/kubernetes"
"github.com/k0sproject/k0s/static"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/metadata"

"github.com/sirupsen/logrus"
"golang.org/x/mod/semver"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/cli-runtime/pkg/resource"
"sigs.k8s.io/yaml"

"github.com/k0sproject/k0s/pkg/kubernetes"
"github.com/k0sproject/k0s/static"
)

func CanUpdate(log logrus.FieldLogger, clientFactory kubernetes.ClientFactoryInterface, newVersion string) error {
func CanUpdate(ctx context.Context, log logrus.FieldLogger, clientFactory kubernetes.ClientFactoryInterface, newVersion string) error {
removedAPIs, err := GetRemovedAPIsList()
if err != nil {
return err
Expand All @@ -46,13 +49,10 @@ func CanUpdate(log logrus.FieldLogger, clientFactory kubernetes.ClientFactoryInt
}
}

restClientGetter := kubernetes.NewRESTClientGetter(clientFactory, log)
resourceBuilder := resource.NewBuilder(restClientGetter).
Unstructured().
ContinueOnError().
Flatten().
AllNamespaces(true).
Latest()
metaClient, err := metadata.NewForConfig(clientFactory.GetRESTConfig())
if err != nil {
return err
}

for _, r := range resources {
gv, err := schema.ParseGroupVersion(r.GroupVersion)
Expand All @@ -71,26 +71,19 @@ func CanUpdate(log logrus.FieldLogger, clientFactory kubernetes.ClientFactoryInt
gv.Version = ar.Version
}

gvk := gv.WithKind(ar.Kind)

removedInVersion, ok := removedAPIs[gvk]
removedInVersion, ok := removedAPIs[gv.WithKind(ar.Kind)]
if !ok || semver.Compare(newVersion, removedInVersion) < 0 {
continue
}

res := resourceBuilder.ResourceTypeOrNameArgs(true, ar.Kind).Do()
infos, err := res.Infos()
metas, err := metaClient.Resource(gv.WithResource(ar.Name)).
Namespace(metav1.NamespaceAll).
List(ctx, metav1.ListOptions{})
if err != nil {
return err
}

found := 0
for _, i := range infos {
if gvk == i.Mapping.GroupVersionKind {
found++
}
}
if found > 0 {
if found := len(metas.Items); found > 0 {
return fmt.Errorf("%s.%s %s has been removed in Kubernetes %s, but there are %d such resources in the cluster", ar.Name, gv.Group, gv.Version, removedInVersion, found)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (aup *airgapupdate) NewPlan(ctx context.Context, cmd apv1beta2.PlanCommand,
logger := aup.logger.WithField("state", "newplan")
logger.Info("Processing")

if err := checks.CanUpdate(logger, aup.cf, cmd.AirgapUpdate.Version); err != nil {
if err := checks.CanUpdate(ctx, logger, aup.cf, cmd.AirgapUpdate.Version); err != nil {
status.State = appc.PlanWarning
status.Description = err.Error()
return appc.PlanWarning, false, err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (kp *k0supdate) NewPlan(ctx context.Context, cmd apv1beta2.PlanCommand, sta
logger.Info("Processing")

if !cmd.K0sUpdate.ForceUpdate {
if err := checks.CanUpdate(logger, kp.cf, cmd.K0sUpdate.Version); err != nil {
if err := checks.CanUpdate(ctx, logger, kp.cf, cmd.K0sUpdate.Version); err != nil {
status.State = appc.PlanWarning
status.Description = err.Error()
return appc.PlanWarning, false, err
Expand Down
62 changes: 0 additions & 62 deletions pkg/kubernetes/rest.go

This file was deleted.

0 comments on commit 3edc61b

Please sign in to comment.