本文整理汇总了Golang中github.com/GoogleCloudPlatform/kubernetes/pkg/api.Service类的典型用法代码示例。如果您正苦于以下问题:Golang Service类的具体用法?Golang Service怎么用?Golang Service使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Service类的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestEtcdUpdateService
func TestEtcdUpdateService(t *testing.T) {
fakeClient := tools.MakeFakeEtcdClient(t)
fakeClient.TestIndex = true
resp, _ := fakeClient.Set("/registry/services/specs/foo", util.MakeJSONString(api.Service{JSONBase: api.JSONBase{ID: "foo"}}), 0)
registry := MakeTestEtcdRegistry(fakeClient, []string{"machine"})
testService := api.Service{
JSONBase: api.JSONBase{ID: "foo", ResourceVersion: resp.Node.ModifiedIndex},
Labels: map[string]string{
"baz": "bar",
},
Selector: map[string]string{
"baz": "bar",
},
}
err := registry.UpdateService(testService)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
svc, err := registry.GetService("foo")
if err != nil {
t.Errorf("unexpected error: %v", err)
}
// Clear modified indices before the equality test.
svc.ResourceVersion = 0
testService.ResourceVersion = 0
if !reflect.DeepEqual(*svc, testService) {
t.Errorf("Unexpected service: got\n %#v\n, wanted\n %#v", svc, testService)
}
}
示例2: TestEtcdUpdateService
func TestEtcdUpdateService(t *testing.T) {
ctx := api.NewDefaultContext()
fakeClient := tools.NewFakeEtcdClient(t)
fakeClient.TestIndex = true
registry := NewTestEtcdRegistry(fakeClient)
key, _ := makeServiceKey(ctx, "uniquefoo")
key = etcdtest.AddPrefix(key)
resp, _ := fakeClient.Set(key, runtime.EncodeOrDie(latest.Codec, &api.Service{ObjectMeta: api.ObjectMeta{Name: "uniquefoo"}}), 0)
testService := api.Service{
ObjectMeta: api.ObjectMeta{
Name: "uniquefoo",
ResourceVersion: strconv.FormatUint(resp.Node.ModifiedIndex, 10),
Labels: map[string]string{
"baz": "bar",
},
},
Spec: api.ServiceSpec{
Selector: map[string]string{
"baz": "bar",
},
SessionAffinity: "None",
Type: api.ServiceTypeClusterIP,
},
}
_, err := registry.UpdateService(ctx, &testService)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
svc, err := registry.GetService(ctx, "uniquefoo")
if err != nil {
t.Errorf("unexpected error: %v", err)
}
// Clear modified indices before the equality test.
svc.ResourceVersion = ""
testService.ResourceVersion = ""
if !api.Semantic.DeepEqual(*svc, testService) {
t.Errorf("Unexpected service: got\n %#v\n, wanted\n %#v", svc, testService)
}
}
示例3: ValidateService
// ValidateService tests if required fields in the service are set.
func ValidateService(service *api.Service) errs.ErrorList {
allErrs := errs.ErrorList{}
if len(service.ID) == 0 {
allErrs = append(allErrs, errs.NewFieldRequired("id", service.ID))
} else if !util.IsDNS952Label(service.ID) {
allErrs = append(allErrs, errs.NewFieldInvalid("id", service.ID))
}
if !util.IsValidPortNum(service.Port) {
allErrs = append(allErrs, errs.NewFieldInvalid("Service.Port", service.Port))
}
if len(service.Protocol) == 0 {
service.Protocol = "TCP"
} else if !supportedPortProtocols.Has(strings.ToUpper(service.Protocol)) {
allErrs = append(allErrs, errs.NewFieldNotSupported("protocol", service.Protocol))
}
if labels.Set(service.Selector).AsSelector().Empty() {
allErrs = append(allErrs, errs.NewFieldRequired("selector", service.Selector))
}
return allErrs
}
示例4: Extract
func (sr *ServiceRegistryStorage) Extract(body string) (interface{}, error) {
var svc api.Service
err := json.Unmarshal([]byte(body), &svc)
svc.Kind = "cluster#service"
return svc, err
}
示例5: runAtomicPutTest
func runAtomicPutTest(c *client.Client) {
var svc api.Service
err := c.Post().Path("services").Body(
api.Service{
JSONBase: api.JSONBase{ID: "atomicservice", APIVersion: "v1beta1"},
Port: 12345,
Labels: map[string]string{
"name": "atomicService",
},
// This is here because validation requires it.
Selector: map[string]string{
"foo": "bar",
},
},
).Do().Into(&svc)
if err != nil {
glog.Fatalf("Failed creating atomicService: %v", err)
}
glog.Info("Created atomicService")
testLabels := labels.Set{
"foo": "bar",
}
for i := 0; i < 5; i++ {
// a: z, b: y, etc...
testLabels[string([]byte{byte('a' + i)})] = string([]byte{byte('z' - i)})
}
var wg sync.WaitGroup
wg.Add(len(testLabels))
for label, value := range testLabels {
go func(l, v string) {
for {
glog.Infof("Starting to update (%s, %s)", l, v)
var tmpSvc api.Service
err := c.Get().
Path("services").
Path(svc.ID).
PollPeriod(100 * time.Millisecond).
Do().
Into(&tmpSvc)
if err != nil {
glog.Errorf("Error getting atomicService: %v", err)
continue
}
if tmpSvc.Selector == nil {
tmpSvc.Selector = map[string]string{l: v}
} else {
tmpSvc.Selector[l] = v
}
glog.Infof("Posting update (%s, %s)", l, v)
err = c.Put().Path("services").Path(svc.ID).Body(&tmpSvc).Do().Error()
if err != nil {
if se, ok := err.(*client.StatusErr); ok {
if se.Status.Code == http.StatusConflict {
glog.Infof("Conflict: (%s, %s)", l, v)
// This is what we expect.
continue
}
}
glog.Errorf("Unexpected error putting atomicService: %v", err)
continue
}
break
}
glog.Infof("Done update (%s, %s)", l, v)
wg.Done()
}(label, value)
}
wg.Wait()
if err := c.Get().Path("services").Path(svc.ID).Do().Into(&svc); err != nil {
glog.Fatalf("Failed getting atomicService after writers are complete: %v", err)
}
if !reflect.DeepEqual(testLabels, labels.Set(svc.Selector)) {
glog.Fatalf("Selector PUTs were not atomic: wanted %v, got %v", testLabels, svc.Selector)
}
glog.Info("Atomic PUTs work.")
}
示例6: TestServiceUpdateLabel
func TestServiceUpdateLabel(t *testing.T) {
kube := mocks.NewKubeClient()
client := new(contrail_mocks.ApiClient)
client.Init()
client.AddInterceptor("virtual-machine-interface", &VmiInterceptor{})
client.AddInterceptor("virtual-network", &NetworkInterceptor{})
client.AddInterceptor("instance-ip", &IpInterceptor{})
client.AddInterceptor("floating-ip", &FloatingIpInterceptor{})
controller := NewTestController(kube, client, nil, nil)
pod1 := &api.Pod{
ObjectMeta: api.ObjectMeta{
Name: "test-sv1",
Namespace: "testns",
UID: kubetypes.UID(uuid.New()),
Labels: map[string]string{
"name": "server",
},
},
}
pod2 := &api.Pod{
ObjectMeta: api.ObjectMeta{
Name: "test-xz2",
Namespace: "testns",
UID: kubetypes.UID(uuid.New()),
Labels: map[string]string{
"name": "client1",
"uses": "red",
},
},
}
pod3 := &api.Pod{
ObjectMeta: api.ObjectMeta{
Name: "test-xz3",
Namespace: "testns",
UID: kubetypes.UID(uuid.New()),
Labels: map[string]string{
"name": "client2",
"uses": "blue",
},
},
}
service := &api.Service{
ObjectMeta: api.ObjectMeta{
Name: "service",
Namespace: "testns",
Labels: map[string]string{
"name": "red",
},
},
Spec: api.ServiceSpec{
Selector: map[string]string{
"name": "server",
},
ClusterIP: "10.254.42.42",
Type: api.ServiceTypeLoadBalancer,
},
}
netnsProject := new(types.Project)
netnsProject.SetFQName("", []string{"default-domain", "testns"})
client.Create(netnsProject)
kube.PodInterface.On("Update", pod1).Return(pod1, nil)
kube.PodInterface.On("Update", pod2).Return(pod2, nil)
kube.PodInterface.On("Update", pod3).Return(pod3, nil)
selectServer := labels.Set(map[string]string{"name": "server"}).AsSelector()
kube.PodInterface.On("List", selectServer, mock.Anything).Return(&api.PodList{Items: []api.Pod{*pod1}}, nil)
kube.ServiceInterface.On("Update", service).Return(service, nil)
shutdown := make(chan struct{})
go controller.Run(shutdown)
controller.AddPod(pod1)
controller.AddPod(pod2)
controller.AddPod(pod3)
controller.AddService(service)
time.Sleep(100 * time.Millisecond)
redPolicy, err := types.NetworkPolicyByName(client, "default-domain:testns:red")
assert.NoError(t, err)
if err == nil {
assert.Len(t, redPolicy.GetNetworkPolicyEntries().PolicyRule, 1)
assert.True(t, policyHasRule(redPolicy, "default-domain:testns:client1", "default-domain:testns:service-red"))
refs, err := redPolicy.GetVirtualNetworkBackRefs()
assert.NoError(t, err)
nameList := getReferenceListNames(refs)
assert.Contains(t, nameList, "default-domain:testns:client1")
assert.Contains(t, nameList, "default-domain:testns:service-red")
}
bluePolicy, err := types.NetworkPolicyByName(client, "default-domain:testns:blue")
assert.NoError(t, err)
if err == nil {
//.........这里部分代码省略.........
示例7: CreateService
func (self *realKubeFramework) CreateService(ns string, service *api.Service) (*api.Service, error) {
service.Namespace = ns
newSvc, err := self.kubeClient.Services(ns).Create(service)
return newSvc, err
}