k3s/pkg/registry/registrytest/service.go

129 lines
3.0 KiB
Go
Raw Normal View History

2014-06-06 23:40:48 +00:00
/*
Copyright 2014 The Kubernetes Authors.
2014-06-06 23:40:48 +00:00
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
2014-06-23 18:32:11 +00:00
package registrytest
2014-06-06 23:40:48 +00:00
import (
2014-09-18 23:03:34 +00:00
"sync"
2017-01-11 14:09:48 +00:00
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/watch"
2017-01-17 10:38:25 +00:00
genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
2015-08-05 22:03:47 +00:00
"k8s.io/kubernetes/pkg/api"
2014-06-06 23:40:48 +00:00
)
func NewServiceRegistry() *ServiceRegistry {
return &ServiceRegistry{}
}
type ServiceRegistry struct {
2015-03-15 06:03:46 +00:00
mu sync.Mutex
List api.ServiceList
Service *api.Service
Updates []api.Service
2015-03-15 06:03:46 +00:00
Err error
DeletedID string
GottenID string
UpdatedID string
2014-06-06 23:40:48 +00:00
}
func (r *ServiceRegistry) SetError(err error) {
r.mu.Lock()
defer r.mu.Unlock()
r.Err = err
}
func (r *ServiceRegistry) ListServices(ctx genericapirequest.Context, options *api.ListOptions) (*api.ServiceList, error) {
2014-09-18 23:03:34 +00:00
r.mu.Lock()
defer r.mu.Unlock()
ns, _ := genericapirequest.NamespaceFrom(ctx)
// Copy metadata from internal list into result
2014-09-18 23:03:34 +00:00
res := new(api.ServiceList)
res.TypeMeta = r.List.TypeMeta
res.ListMeta = r.List.ListMeta
if ns != api.NamespaceAll {
for _, service := range r.List.Items {
if ns == service.Namespace {
res.Items = append(res.Items, service)
}
}
} else {
res.Items = append([]api.Service{}, r.List.Items...)
}
2014-09-18 23:03:34 +00:00
return res, r.Err
2014-06-06 23:40:48 +00:00
}
func (r *ServiceRegistry) CreateService(ctx genericapirequest.Context, svc *api.Service) (*api.Service, error) {
2014-09-18 23:03:34 +00:00
r.mu.Lock()
defer r.mu.Unlock()
r.Service = new(api.Service)
clone, err := api.Scheme.DeepCopy(svc)
if err != nil {
return nil, err
}
r.Service = clone.(*api.Service)
2014-09-08 04:14:18 +00:00
r.List.Items = append(r.List.Items, *svc)
return svc, r.Err
2014-06-06 23:40:48 +00:00
}
func (r *ServiceRegistry) GetService(ctx genericapirequest.Context, id string, options *metav1.GetOptions) (*api.Service, error) {
2014-09-18 23:03:34 +00:00
r.mu.Lock()
defer r.mu.Unlock()
r.GottenID = id
return r.Service, r.Err
2014-06-06 23:40:48 +00:00
}
func (r *ServiceRegistry) DeleteService(ctx genericapirequest.Context, id string) error {
2014-09-18 23:03:34 +00:00
r.mu.Lock()
defer r.mu.Unlock()
r.DeletedID = id
2014-09-18 23:03:34 +00:00
r.Service = nil
return r.Err
2014-06-06 23:40:48 +00:00
}
func (r *ServiceRegistry) UpdateService(ctx genericapirequest.Context, svc *api.Service) (*api.Service, error) {
2014-09-18 23:03:34 +00:00
r.mu.Lock()
defer r.mu.Unlock()
2014-10-22 17:02:02 +00:00
r.UpdatedID = svc.Name
*r.Service = *svc
r.Updates = append(r.Updates, *svc)
return svc, r.Err
2014-06-06 23:40:48 +00:00
}
func (r *ServiceRegistry) WatchServices(ctx genericapirequest.Context, options *api.ListOptions) (watch.Interface, error) {
2014-09-18 23:03:34 +00:00
r.mu.Lock()
defer r.mu.Unlock()
return nil, r.Err
}
2016-03-05 16:23:50 +00:00
func (r *ServiceRegistry) ExportService(ctx genericapirequest.Context, name string, options metav1.ExportOptions) (*api.Service, error) {
2016-03-05 16:23:50 +00:00
r.mu.Lock()
defer r.mu.Unlock()
2016-03-05 16:23:50 +00:00
return r.Service, r.Err
}