Feat: support remote cluster client to allow user use CRD resource in managed cluster without installing CRD to hub cluster (#6008)

Signed-off-by: Somefive <yd219913@alibaba-inc.com>
This commit is contained in:
Somefive
2023-05-23 10:15:20 +08:00
committed by GitHub
parent 2c341db8c0
commit c66a63909e
6 changed files with 82 additions and 4 deletions

View File

@@ -34,6 +34,7 @@ import (
rbacv1 "k8s.io/api/rbac/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/clientcmd"
@@ -1216,5 +1217,34 @@ var _ = Describe("Test multicluster scenario", func() {
g.Expect(string(secret.Data["key"])).Should(Equal("vvv\n"))
}).WithPolling(2 * time.Second).WithTimeout(20 * time.Second).Should(Succeed())
})
It("Test application apply resources into managed cluster without installing CRD on the control plane", func() {
ctx := context.Background()
crd := &unstructured.Unstructured{}
bs, err := os.ReadFile("./testdata/kube/sample-crd.yaml")
Expect(err).Should(Succeed())
Expect(yaml.Unmarshal(bs, crd)).Should(Succeed())
Expect(client.IgnoreAlreadyExists(k8sClient.Create(workerCtx, crd))).Should(Succeed())
app := &v1beta1.Application{}
bs, err = os.ReadFile("./testdata/app/app-remote-resource.yaml")
Expect(err).Should(Succeed())
Expect(yaml.Unmarshal(bs, app)).Should(Succeed())
app.SetNamespace(namespace)
Eventually(func(g Gomega) {
g.Expect(k8sClient.Create(ctx, app)).Should(Succeed())
}).WithPolling(2 * time.Second).WithTimeout(5 * time.Second).Should(Succeed())
appKey := client.ObjectKeyFromObject(app)
Eventually(func(g Gomega) {
_app := &v1beta1.Application{}
g.Expect(k8sClient.Get(ctx, appKey, _app)).Should(Succeed())
g.Expect(_app.Status.Phase).Should(Equal(common.ApplicationRunning))
}).WithPolling(2 * time.Second).WithTimeout(20 * time.Second).Should(Succeed())
obj := &unstructured.Unstructured{}
obj.SetAPIVersion("sample.custom.io/v1alpha1")
obj.SetKind("Foo")
Expect(k8sClient.Get(workerCtx, appKey, obj)).Should(Succeed())
Expect(obj.Object["spec"].(map[string]interface{})["key"]).Should(Equal("value"))
})
})
})

View File

@@ -72,7 +72,7 @@ var _ = BeforeSuite(func() {
options := client.Options{Scheme: common.Scheme}
config := config.GetConfigOrDie()
config.Wrap(multicluster.NewTransportWrapper())
k8sClient, err = client.New(config, options)
k8sClient, err = multicluster.NewClient(config, multicluster.ClientOptions{Options: options})
Expect(err).Should(Succeed())
k8sCli, err = kubernetes.NewForConfig(config)
Expect(err).Should(Succeed())

View File

@@ -0,0 +1,21 @@
apiVersion: core.oam.dev/v1beta1
kind: Application
metadata:
name: remote
spec:
components:
- type: k8s-objects
name: remote
properties:
objects:
- apiVersion: sample.custom.io/v1alpha1
kind: Foo
metadata:
name: remote
spec:
key: value
policies:
- type: topology
name: topology
properties:
clusters: ["cluster-worker"]

View File

@@ -0,0 +1,24 @@
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: foos.sample.custom.io
spec:
group: sample.custom.io
versions:
- name: v1alpha1
served: true
storage: true
schema:
# schema used for validation
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
key:
type: string
names:
kind: Foo
plural: foos
scope: Namespaced