k3s/pkg/registry/pod_registry.go

107 lines
2.9 KiB
Go
Raw Normal View History

2014-06-09 04:42:09 +00:00
/*
Copyright 2014 Google Inc. All rights reserved.
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.
*/
package registry
import (
"encoding/json"
"fmt"
2014-06-12 20:17:34 +00:00
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
2014-06-09 04:42:09 +00:00
"github.com/GoogleCloudPlatform/kubernetes/pkg/apiserver"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
2014-06-17 02:10:43 +00:00
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
2014-06-09 04:42:09 +00:00
)
2014-06-09 05:38:45 +00:00
// PodRegistryStorage implements the RESTStorage interface in terms of a PodRegistry
type PodRegistryStorage struct {
2014-06-09 04:42:09 +00:00
registry PodRegistry
containerInfo client.ContainerInfo
scheduler Scheduler
}
2014-06-09 05:38:45 +00:00
func MakePodRegistryStorage(registry PodRegistry, containerInfo client.ContainerInfo, scheduler Scheduler) apiserver.RESTStorage {
return &PodRegistryStorage{
2014-06-09 04:42:09 +00:00
registry: registry,
containerInfo: containerInfo,
scheduler: scheduler,
}
}
2014-06-17 02:10:43 +00:00
func (storage *PodRegistryStorage) List(query labels.Query) (interface{}, error) {
2014-06-12 20:17:34 +00:00
var result api.PodList
2014-06-09 05:38:45 +00:00
pods, err := storage.registry.ListPods(query)
2014-06-09 04:42:09 +00:00
if err == nil {
2014-06-17 02:10:43 +00:00
result.Items = pods
2014-06-09 04:42:09 +00:00
}
2014-06-09 05:38:45 +00:00
result.Kind = "cluster#podList"
2014-06-09 04:42:09 +00:00
return result, err
}
2014-06-13 03:53:10 +00:00
func makePodStatus(info interface{}) string {
if state, ok := info.(map[string]interface{})["State"]; ok {
if running, ok := state.(map[string]interface{})["Running"]; ok {
if running.(bool) {
return "Running"
} else {
return "Stopped"
}
}
}
return "Pending"
}
2014-06-09 05:38:45 +00:00
func (storage *PodRegistryStorage) Get(id string) (interface{}, error) {
pod, err := storage.registry.GetPod(id)
2014-06-09 04:42:09 +00:00
if err != nil {
2014-06-09 05:38:45 +00:00
return pod, err
2014-06-09 04:42:09 +00:00
}
2014-06-09 05:38:45 +00:00
info, err := storage.containerInfo.GetContainerInfo(pod.CurrentState.Host, id)
2014-06-09 04:42:09 +00:00
if err != nil {
2014-06-09 05:38:45 +00:00
return pod, err
2014-06-09 04:42:09 +00:00
}
2014-06-09 05:38:45 +00:00
pod.CurrentState.Info = info
2014-06-13 03:53:10 +00:00
pod.CurrentState.Status = makePodStatus(info)
2014-06-09 05:38:45 +00:00
pod.Kind = "cluster#pod"
return pod, err
2014-06-09 04:42:09 +00:00
}
2014-06-09 05:38:45 +00:00
func (storage *PodRegistryStorage) Delete(id string) error {
return storage.registry.DeletePod(id)
2014-06-09 04:42:09 +00:00
}
2014-06-09 05:38:45 +00:00
func (storage *PodRegistryStorage) Extract(body string) (interface{}, error) {
2014-06-12 20:17:34 +00:00
pod := api.Pod{}
2014-06-09 05:38:45 +00:00
err := json.Unmarshal([]byte(body), &pod)
2014-06-09 06:29:04 +00:00
pod.Kind = "cluster#pod"
2014-06-09 05:38:45 +00:00
return pod, err
2014-06-09 04:42:09 +00:00
}
2014-06-09 05:38:45 +00:00
func (storage *PodRegistryStorage) Create(pod interface{}) error {
2014-06-12 20:17:34 +00:00
podObj := pod.(api.Pod)
2014-06-09 05:38:45 +00:00
if len(podObj.ID) == 0 {
return fmt.Errorf("id is unspecified: %#v", pod)
2014-06-09 04:42:09 +00:00
}
2014-06-09 05:38:45 +00:00
machine, err := storage.scheduler.Schedule(podObj)
2014-06-09 04:42:09 +00:00
if err != nil {
return err
}
2014-06-09 05:38:45 +00:00
return storage.registry.CreatePod(machine, podObj)
2014-06-09 04:42:09 +00:00
}
2014-06-09 05:38:45 +00:00
func (storage *PodRegistryStorage) Update(pod interface{}) error {
2014-06-12 20:17:34 +00:00
return storage.registry.UpdatePod(pod.(api.Pod))
2014-06-09 04:42:09 +00:00
}