@@ -40,6 +40,8 @@ import (
40
40
apiregistrationv1 "k8s.io/kube-aggregator/pkg/apis/apiregistration/v1"
41
41
kubeaggregator "k8s.io/kube-aggregator/pkg/client/clientset_generated/clientset"
42
42
"tkestack.io/tke/pkg/util/template"
43
+ apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
44
+ aaclientset "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
43
45
)
44
46
45
47
type object struct {
@@ -49,11 +51,13 @@ type object struct {
49
51
var (
50
52
handlers map [string ]func (context.Context , kubernetes.Interface , []byte ) error
51
53
kaHandlers map [string ]func (context.Context , kubeaggregator.Interface , []byte ) error
54
+ aaHandlers map [string ]func (context.Context , aaclientset.Interface , []byte ) error
52
55
)
53
56
54
57
func init () {
55
58
handlers = make (map [string ]func (context.Context , kubernetes.Interface , []byte ) error )
56
59
kaHandlers = make (map [string ]func (context.Context , kubeaggregator.Interface , []byte ) error )
60
+ aaHandlers = make (map [string ]func (context.Context , aaclientset.Interface , []byte ) error )
57
61
58
62
// apiregistration
59
63
kaHandlers ["APIService" ] = func (ctx context.Context , client kubeaggregator.Interface , data []byte ) error {
@@ -68,6 +72,19 @@ func init() {
68
72
return nil
69
73
}
70
74
75
+ //add CustomResourceDefinition
76
+ aaHandlers ["CustomResourceDefinition" ] = func (ctx context.Context , client aaclientset.Interface , data []byte ) error {
77
+ obj := new (apiextensionsv1.CustomResourceDefinition )
78
+ if err := kuberuntime .DecodeInto (clientsetscheme .Codecs .UniversalDecoder (), data , obj ); err != nil {
79
+ return errors .Wrapf (err , "unable to decode %s" , reflect .TypeOf (obj ).String ())
80
+ }
81
+ err := CreateOrUpdateCustomResourceDefinition (ctx , client , obj )
82
+ if err != nil {
83
+ return err
84
+ }
85
+ return nil
86
+ }
87
+
71
88
// core
72
89
handlers ["ConfigMap" ] = func (ctx context.Context , client kubernetes.Interface , data []byte ) error {
73
90
obj := new (corev1.ConfigMap )
@@ -406,3 +423,55 @@ func CreateKAResourceWithFile(ctx context.Context, client kubernetes.Interface,
406
423
407
424
return nil
408
425
}
426
+
427
+
428
+
429
+ // CreateAsResourceWithFile create k8s and apiextensions-apiserver resource with file
430
+ func CreateAsResourceWithFile (ctx context.Context , client kubernetes.Interface , aaClient aaclientset.Interface , filename string , option interface {}) error {
431
+ var (
432
+ data []byte
433
+ err error
434
+ )
435
+ if option != nil {
436
+ data , err = template .ParseFile (filename , option )
437
+ } else {
438
+ data , err = ioutil .ReadFile (filename )
439
+ }
440
+ if err != nil {
441
+ return err
442
+ }
443
+
444
+ items := strings .Split (string (data ), "\n ---" )
445
+ for _ , item := range items {
446
+ objBytes := []byte (item )
447
+ obj := new (object )
448
+ err := yaml .Unmarshal (objBytes , obj )
449
+ if err != nil {
450
+ return err
451
+ }
452
+ if obj .Kind == "" {
453
+ continue
454
+ }
455
+ if obj .Kind == "CustomResourceDefinition" {
456
+ f , ok := aaHandlers [obj .Kind ]
457
+ if ! ok {
458
+ return errors .Errorf ("unsupport kind %q" , obj .Kind )
459
+ }
460
+ err = f (ctx , aaClient , objBytes )
461
+ if err != nil {
462
+ return err
463
+ }
464
+ } else {
465
+ f , ok := handlers [obj .Kind ]
466
+ if ! ok {
467
+ return errors .Errorf ("unsupport kind %q" , obj .Kind )
468
+ }
469
+ err = f (ctx , client , objBytes )
470
+ if err != nil {
471
+ return err
472
+ }
473
+ }
474
+ }
475
+
476
+ return nil
477
+ }
0 commit comments