Skip to content

Commit

Permalink
Use discovery to check if resource is namespaced (kyma-project#2200)
Browse files Browse the repository at this point in the history
  • Loading branch information
pPrecel authored Jul 30, 2024
1 parent aaa7570 commit 88e3351
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
5 changes: 3 additions & 2 deletions internal/cmd/alpha/add/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ package add
import (
"bytes"
"fmt"
"io"
"net/http"

"github.com/kyma-project/cli.v3/internal/clierror"
"github.com/kyma-project/cli.v3/internal/cmd/alpha/remove/managed"
"github.com/kyma-project/cli.v3/internal/cmdcommon"
"github.com/kyma-project/cli.v3/internal/communitymodules"
"github.com/spf13/cobra"
"io"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"net/http"
yaml "sigs.k8s.io/yaml/goyaml.v3"
)

Expand Down
30 changes: 16 additions & 14 deletions internal/kube/rootlessdynamic/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package rootlessdynamic
import (
"context"
"errors"
"strings"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -12,7 +14,6 @@ import (
"k8s.io/client-go/discovery/cached/memory"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/rest"
"strings"
)

type Interface interface {
Expand All @@ -39,25 +40,32 @@ func NewClient(dynamic dynamic.Interface, restConfig *rest.Config) (Interface, e
// TODO: Add a script to test applying default resources
func (c *client) ApplyMany(ctx context.Context, objs []unstructured.Unstructured) error {
for _, resource := range objs {
gvr, err := c.discoverGVR(resource.GetAPIVersion(), resource.GetKind())
group, version := groupVersion(resource.GetAPIVersion())
apiResource, err := c.discoverAPIResource(group, version, resource.GetKind())
if err != nil {
return err
}

gvr := &schema.GroupVersionResource{
Group: group,
Version: version,
Resource: apiResource.Name,
}

data, err := runtime.Encode(unstructured.UnstructuredJSONScheme, &resource)
if err != nil {
return err
}

if kind := resource.GetKind(); kind == "CustomResourceDefinition" || kind == "ClusterRole" || kind == "ClusterRoleBinding" {
_, err = c.dynamic.Resource(*gvr).Patch(ctx, resource.GetName(), types.ApplyPatchType, data, metav1.PatchOptions{
if apiResource.Namespaced {
_, err = c.dynamic.Resource(*gvr).Namespace("kyma-system").Patch(ctx, resource.GetName(), types.ApplyPatchType, data, metav1.PatchOptions{
FieldManager: "cli",
})
if err != nil {
return err
}
} else {
_, err = c.dynamic.Resource(*gvr).Namespace("kyma-system").Patch(ctx, resource.GetName(), types.ApplyPatchType, data, metav1.PatchOptions{
_, err = c.dynamic.Resource(*gvr).Patch(ctx, resource.GetName(), types.ApplyPatchType, data, metav1.PatchOptions{
FieldManager: "cli",
})
if err != nil {
Expand All @@ -68,8 +76,7 @@ func (c *client) ApplyMany(ctx context.Context, objs []unstructured.Unstructured
return nil
}

func (c *client) discoverGVR(apiVersion, kind string) (*schema.GroupVersionResource, error) {
group, version := groupVersion(apiVersion)
func (c *client) discoverAPIResource(group, version, kind string) (*metav1.APIResource, error) {
groupVersion := schema.GroupVersion{Group: group, Version: version}

apiResourceList, err := c.discovery.ServerResourcesForGroupVersion(groupVersion.String())
Expand All @@ -79,15 +86,10 @@ func (c *client) discoverGVR(apiVersion, kind string) (*schema.GroupVersionResou

for _, apiResource := range apiResourceList.APIResources {
if apiResource.Kind == kind {
return &schema.GroupVersionResource{
Group: group,
Version: version,
Resource: apiResource.Name,
}, nil

return &apiResource, nil
}
}
return nil, errors.New("Resource " + kind + " in apiVersion " + apiVersion + " not registered on cluster")
return nil, errors.New("Resource " + kind + " in group " + group + " and version " + version + " not registered on cluster")
}

func groupVersion(version string) (string, string) {
Expand Down

0 comments on commit 88e3351

Please sign in to comment.