k3s/pkg/apiserver/operation.go

208 lines
5.1 KiB
Go
Raw Normal View History

2014-06-25 20:21:32 +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 apiserver
import (
"net/http"
2014-06-25 20:21:32 +00:00
"sort"
"strconv"
2014-06-25 20:21:32 +00:00
"sync"
"sync/atomic"
2014-06-25 20:21:32 +00:00
"time"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
2014-06-25 20:21:32 +00:00
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
)
type OperationHandler struct {
ops *Operations
codec runtime.Codec
}
func (h *OperationHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
parts := splitPath(req.URL.Path)
if len(parts) > 1 || req.Method != "GET" {
notFound(w, req)
return
}
if len(parts) == 0 {
// List outstanding operations.
list := h.ops.List()
writeJSON(http.StatusOK, h.codec, list, w)
return
}
op := h.ops.Get(parts[0])
if op == nil {
notFound(w, req)
return
}
obj, complete := op.StatusOrResult()
if complete {
writeJSON(http.StatusOK, h.codec, obj, w)
} else {
writeJSON(http.StatusAccepted, h.codec, obj, w)
}
}
2014-06-25 20:21:32 +00:00
// Operation represents an ongoing action which the server is performing.
type Operation struct {
2014-09-26 00:20:28 +00:00
ID string
result runtime.Object
onReceive func(runtime.Object)
awaiting <-chan runtime.Object
finished *time.Time
lock sync.Mutex
notify chan struct{}
2014-06-25 20:21:32 +00:00
}
// Operations tracks all the ongoing operations.
type Operations struct {
// Access only using functions from atomic.
lastID int64
// 'lock' guards the ops map.
lock sync.Mutex
ops map[string]*Operation
2014-06-25 20:21:32 +00:00
}
2014-07-08 07:09:16 +00:00
// NewOperations returns a new Operations repository.
2014-06-25 20:21:32 +00:00
func NewOperations() *Operations {
ops := &Operations{
ops: map[string]*Operation{},
}
go util.Forever(func() { ops.expire(10 * time.Minute) }, 5*time.Minute)
return ops
}
2014-09-26 00:20:28 +00:00
// NewOperation adds a new operation. It is lock-free. 'onReceive' will be called
// with the value read from 'from', when it is read.
func (ops *Operations) NewOperation(from <-chan runtime.Object, onReceive func(runtime.Object)) *Operation {
id := atomic.AddInt64(&ops.lastID, 1)
2014-06-25 20:21:32 +00:00
op := &Operation{
2014-09-26 00:20:28 +00:00
ID: strconv.FormatInt(id, 10),
awaiting: from,
onReceive: onReceive,
notify: make(chan struct{}),
2014-06-25 20:21:32 +00:00
}
go op.wait()
go ops.insert(op)
2014-06-25 20:21:32 +00:00
return op
}
// insert inserts op into the ops map.
func (ops *Operations) insert(op *Operation) {
ops.lock.Lock()
defer ops.lock.Unlock()
ops.ops[op.ID] = op
}
// List lists operations for an API client.
func (ops *Operations) List() *api.ServerOpList {
2014-06-25 20:21:32 +00:00
ops.lock.Lock()
defer ops.lock.Unlock()
ids := []string{}
for id := range ops.ops {
ids = append(ids, id)
}
sort.StringSlice(ids).Sort()
ol := &api.ServerOpList{}
2014-06-25 20:21:32 +00:00
for _, id := range ids {
ol.Items = append(ol.Items, api.ServerOp{TypeMeta: api.TypeMeta{ID: id}})
2014-06-25 20:21:32 +00:00
}
return ol
}
// Get returns the operation with the given ID, or nil.
2014-06-25 20:21:32 +00:00
func (ops *Operations) Get(id string) *Operation {
ops.lock.Lock()
defer ops.lock.Unlock()
return ops.ops[id]
}
// expire garbage collect operations that have finished longer than maxAge ago.
2014-06-25 20:21:32 +00:00
func (ops *Operations) expire(maxAge time.Duration) {
ops.lock.Lock()
defer ops.lock.Unlock()
keep := map[string]*Operation{}
limitTime := time.Now().Add(-maxAge)
for id, op := range ops.ops {
if !op.expired(limitTime) {
keep[id] = op
}
}
ops.ops = keep
}
// wait waits forever for the operation to complete; call via go when
2014-06-25 20:21:32 +00:00
// the operation is created. Sets op.finished when the operation
// does complete, and closes the notify channel, in case there
// are any WaitFor() calls in progress.
// Does not keep op locked while waiting.
2014-06-25 20:21:32 +00:00
func (op *Operation) wait() {
defer util.HandleCrash()
result := <-op.awaiting
op.lock.Lock()
defer op.lock.Unlock()
2014-09-26 00:20:28 +00:00
if op.onReceive != nil {
op.onReceive(result)
}
2014-06-25 20:21:32 +00:00
op.result = result
finished := time.Now()
op.finished = &finished
close(op.notify)
2014-06-25 20:21:32 +00:00
}
2014-07-08 07:09:16 +00:00
// WaitFor waits for the specified duration, or until the operation finishes,
2014-06-25 20:21:32 +00:00
// whichever happens first.
func (op *Operation) WaitFor(timeout time.Duration) {
select {
case <-time.After(timeout):
case <-op.notify:
}
}
// expired returns true if this operation finished before limitTime.
2014-06-25 20:21:32 +00:00
func (op *Operation) expired(limitTime time.Time) bool {
op.lock.Lock()
defer op.lock.Unlock()
if op.finished == nil {
return false
}
return op.finished.Before(limitTime)
}
2014-07-08 07:09:16 +00:00
// StatusOrResult returns status information or the result of the operation if it is complete,
2014-06-25 20:21:32 +00:00
// with a bool indicating true in the latter case.
func (op *Operation) StatusOrResult() (description runtime.Object, finished bool) {
2014-06-25 20:21:32 +00:00
op.lock.Lock()
defer op.lock.Unlock()
if op.finished == nil {
return &api.Status{
2014-06-25 20:21:32 +00:00
Status: api.StatusWorking,
Reason: api.StatusReasonWorking,
Evolve the api.Status object with Reason/Details Contains breaking API change on api.Status#Details (type change) Turn Details from string -> StatusDetails - a general bucket for keyed error behavior. Define an open enumeration ReasonType exposed as Reason on the status object to provide machine readable subcategorization beyond HTTP Status Code. Define a human readable field Message which is common convention (previously this was joined into Details). Precedence order: HTTP Status Code, Reason, Details. apiserver would impose restraints on the ReasonTypes defined by the main apiobject, and ensure their use is consistent. There are four long term scenarios this change supports: 1. Allow a client access to a machine readable field that can be easily switched on for improving or translating the generic server Message. 2. Return a 404 when a composite operation on multiple resources fails with enough data so that a client can distinguish which item does not exist. E.g. resource Parent and resource Child, POST /parents/1/children to create a new Child, but /parents/1 is deleted. POST returns 404, ReasonTypeNotFound, and Details.ID = "1", Details.Kind = "parent" 3. Allow a client to receive validation data that is keyed by attribute for building user facing UIs around field submission. Validation is usually expressed as map[string][]string, but that type is less appropriate for many other uses. 4. Allow specific API errors to return more granular failure status for specific operations. An example might be a minion proxy, where the operation that failed may be both proxying OR the minion itself. In this case a reason may be defined "proxy_failed" corresponding to 502, where the Details field may be extended to contain a nested error object. At this time only ID and Kind are exposed
2014-07-31 18:12:26 +00:00
Details: &api.StatusDetails{ID: op.ID, Kind: "operation"},
2014-06-25 20:21:32 +00:00
}, false
}
return op.result, true
}