Skip to content

Commit

Permalink
Merge pull request #4656 from twz123/metadata-client
Browse files Browse the repository at this point in the history
Drop k0s' RESTClientGetter implementation
  • Loading branch information
twz123 authored Jun 25, 2024
2 parents 61a70f1 + 3edc61b commit 16e7e1f
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 113 deletions.
1 change: 1 addition & 0 deletions inttest/ap-removedapis/removedapis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func (s *plansRemovedAPIsSuite) TestApply() {
plan, err := aptest.WaitForPlanState(s.Context(), client, apconst.AutopilotName, appc.PlanWarning)
if s.NoError(err) && s.Len(plan.Status.Commands, 1) {
s.Equal(appc.PlanWarning, plan.Status.Commands[0].State)
s.Equal("removedcrds.autopilot.k0sproject.io v1beta1 has been removed in Kubernetes v99.99.99, but there are 1 such resources in the cluster", plan.Status.Commands[0].Description)
}
}

Expand Down
16 changes: 5 additions & 11 deletions pkg/applier/applier.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ type Applier struct {

log *logrus.Entry
clientFactory kubernetes.ClientFactoryInterface

restClientGetter resource.RESTClientGetter
}

// NewApplier creates new Applier
Expand All @@ -55,14 +53,11 @@ func NewApplier(dir string, kubeClientFactory kubernetes.ClientFactoryInterface)
"bundle": name,
})

clientGetter := kubernetes.NewRESTClientGetter(kubeClientFactory, log)

return Applier{
log: log,
Dir: dir,
Name: name,
clientFactory: kubeClientFactory,
restClientGetter: clientGetter,
log: log,
Dir: dir,
Name: name,
clientFactory: kubeClientFactory,
}
}

Expand Down Expand Up @@ -106,8 +101,7 @@ func (a *Applier) parseFiles(files []string) ([]*unstructured.Unstructured, erro
return resources, nil
}

objects, err := resource.NewBuilder(a.restClientGetter).
Local(). // don't fail on unknown CRDs
objects, err := resource.NewLocalBuilder().
Unstructured().
Path(false, files...).
Flatten().
Expand Down
75 changes: 37 additions & 38 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,46 +49,42 @@ 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, _ := schema.ParseGroupVersion(r.GroupVersion)
gv, err := schema.ParseGroupVersion(r.GroupVersion)
if err != nil {
log.WithError(err).Warn("Skipping API version ", r.GroupVersion)
continue
}

for _, ar := range r.APIResources {
gvk := schema.GroupVersionKind{
Group: gv.Group,
Version: gv.Version,
Kind: ar.Kind,
gv := gv // Copy over the default GroupVersion from the list
// Apply resource-specific overrides
if ar.Group != "" {
gv.Group = ar.Group
}
if ar.Version != "" {
gv.Version = ar.Version
}

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

if semver.Compare(newVersion, removedInVersion) >= 0 {
res := resourceBuilder.ResourceTypeOrNameArgs(true, ar.Kind).Do()
infos, err := res.Infos()
if err != nil {
return err
}

found := 0
for _, i := range infos {
if gvk == i.Mapping.GroupVersionKind {
found++
}
}
if found > 0 {
err = fmt.Errorf("%s is removed in Kubernetes %s. There are %d resources of the type in the cluster", gvk.String(), semver.MajorMinor(newVersion), found)
logrus.Error(err)
return err
}
metas, err := metaClient.Resource(gv.WithResource(ar.Name)).
Namespace(metav1.NamespaceAll).
List(ctx, metav1.ListOptions{})
if err != nil {
return err
}

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 16e7e1f

Please sign in to comment.