diff options
author | Lvbo163 <lv.bo163@zte.com.cn> | 2018-08-03 15:40:59 +0800 |
---|---|---|
committer | Lvbo163 <lv.bo163@zte.com.cn> | 2018-08-03 15:40:59 +0800 |
commit | 1d6f431609b6115a2ab933fcec1c866704460d1f (patch) | |
tree | 3fb57d7752d7a6f2976bdb159f4abf2fb28974d6 /msb2pilot/src | |
parent | 2b41e0ff9fb0466e5a209fe12ffe897bfe32f005 (diff) |
access k8s
Issue-ID: MSB-254
Change-Id: Ic93bfe9a7a6616cffa8175ed3590c0d882ff678b
Signed-off-by: Lvbo163 <lv.bo163@zte.com.cn>
Diffstat (limited to 'msb2pilot/src')
-rw-r--r-- | msb2pilot/src/msb2pilot/conf/k8s.yml | 14 | ||||
-rw-r--r-- | msb2pilot/src/msb2pilot/pilot/controller.go | 66 | ||||
-rw-r--r-- | msb2pilot/src/msb2pilot/pilot/controller_test.go | 26 |
3 files changed, 106 insertions, 0 deletions
diff --git a/msb2pilot/src/msb2pilot/conf/k8s.yml b/msb2pilot/src/msb2pilot/conf/k8s.yml new file mode 100644 index 0000000..07f512e --- /dev/null +++ b/msb2pilot/src/msb2pilot/conf/k8s.yml @@ -0,0 +1,14 @@ +apiVersion: v1
+clusters:
+- cluster:
+ server: http://10.74.148.106:28003
+ name: istio
+contexts:
+- context:
+ cluster: istio
+ user: ""
+ name: istio
+current-context: istio
+kind: Config
+preferences: {}
+users: []
diff --git a/msb2pilot/src/msb2pilot/pilot/controller.go b/msb2pilot/src/msb2pilot/pilot/controller.go new file mode 100644 index 0000000..d931605 --- /dev/null +++ b/msb2pilot/src/msb2pilot/pilot/controller.go @@ -0,0 +1,66 @@ +/** + * Copyright (c) 2018 ZTE Corporation. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and the Apache License 2.0 which both accompany this distribution, + * and are available at http://www.eclipse.org/legal/epl-v10.html + * and http://www.apache.org/licenses/LICENSE-2.0 + * + * Contributors: + * ZTE - initial Project + */ +package pilot + +import ( + "errors" + "msb2pilot/log" + + "istio.io/istio/pilot/pkg/config/kube/crd" + "istio.io/istio/pilot/pkg/model" +) + +type Operation string + +var ( + client *crd.Client +) + +func init() { + var err error + client, err = crd.NewClient("k8s.yml", model.ConfigDescriptor{ + model.RouteRule, + model.DestinationPolicy, + model.DestinationRule, + }, "") + + if err != nil { + log.Log.Error("fail to init crd", err) + } +} + +func Get(typ, namespace, name string) (*model.Config, bool) { + proto, err := protoSchema(typ) + if err != nil { + log.Log.Informational("get resource error", err) + return &model.Config{}, false + } + return client.Get(proto.Type, name, namespace) +} + +func protoSchema(typ string) (model.ProtoSchema, error) { + for _, desc := range client.ConfigDescriptor() { + switch typ { + case crd.ResourceName(desc.Type), crd.ResourceName(desc.Plural): + return desc, nil + } + } + return model.ProtoSchema{}, errors.New("can not find this kind of resources:[" + typ + "]") +} + +func List(typ, namespace string) ([]model.Config, error) { + proto, err := protoSchema(typ) + if err != nil { + return nil, err + } + return client.List(proto.Type, namespace) +} diff --git a/msb2pilot/src/msb2pilot/pilot/controller_test.go b/msb2pilot/src/msb2pilot/pilot/controller_test.go new file mode 100644 index 0000000..6dcea5b --- /dev/null +++ b/msb2pilot/src/msb2pilot/pilot/controller_test.go @@ -0,0 +1,26 @@ +/** + * Copyright (c) 2018 ZTE Corporation. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and the Apache License 2.0 which both accompany this distribution, + * and are available at http://www.eclipse.org/legal/epl-v10.html + * and http://www.apache.org/licenses/LICENSE-2.0 + * + * Contributors: + * ZTE - initial Project + */ +package pilot + +import ( + "fmt" + "testing" +) + +func TestList(t *testing.T) { + res, err := List("routerules", "default") + if err != nil { + t.Errorf("List() => got %v", err) + } else { + fmt.Print(res) + } +} |