Add Server.Version and cmd/client test code
Signed-off-by: Haiyan Meng <hmeng@redhat.com>
This commit is contained in:
parent
0fc314456a
commit
5c4a79543f
20 changed files with 4643 additions and 3 deletions
31
server/exec.go
Normal file
31
server/exec.go
Normal file
|
@ -0,0 +1,31 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func execCmd(name string, args ...string) (string, error) {
|
||||
cmd := exec.Command(name, args...)
|
||||
var out bytes.Buffer
|
||||
cmd.Stdout = &out
|
||||
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return out.String(), nil
|
||||
}
|
||||
|
||||
func execRuncVersion(name string, args ...string) (string, error) {
|
||||
out, err := execCmd(name, args...)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
firstLine := out[:strings.Index(out, "\n")]
|
||||
v := firstLine[strings.LastIndex(firstLine, " ")+1:]
|
||||
return v, nil
|
||||
}
|
|
@ -1,6 +1,9 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
pb "github.com/kubernetes/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
@ -9,8 +12,36 @@ import (
|
|||
type Server struct{}
|
||||
|
||||
// Version returns the runtime name, runtime version and runtime API version
|
||||
func (s *Server) Version(context.Context, *pb.VersionRequest) (*pb.VersionResponse, error) {
|
||||
return nil, nil
|
||||
func (s *Server) Version(ctx context.Context, req *pb.VersionRequest) (*pb.VersionResponse, error) {
|
||||
var err error
|
||||
|
||||
version, err := getGPRCVersion()
|
||||
if err != nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// FIXME: the logic here may need to be changed. How to determine whether the client/server APIs are compatible?
|
||||
if strings.Compare(version, *req.Version) != 0 {
|
||||
return &pb.VersionResponse{
|
||||
Version: &version,
|
||||
}, errors.New("The version of the gRPC server API is different from the version of the gRPC client API.")
|
||||
}
|
||||
|
||||
runtimeName := "runc"
|
||||
|
||||
runtimeVersion, err := execRuncVersion("runc", "-v")
|
||||
if err != nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
runtimeApiVersion := "v1alpha1"
|
||||
|
||||
return &pb.VersionResponse{
|
||||
Version: &version,
|
||||
RuntimeName: &runtimeName,
|
||||
RuntimeVersion: &runtimeVersion,
|
||||
RuntimeApiVersion: &runtimeApiVersion,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// CreatePodSandbox creates a pod-level sandbox.
|
||||
|
|
27
server/utils.go
Normal file
27
server/utils.go
Normal file
|
@ -0,0 +1,27 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
func getGPRCVersion() (string, error) {
|
||||
_, file, _, ok := runtime.Caller(0)
|
||||
if !ok {
|
||||
return "", errors.New("Failed to recover the caller information.")
|
||||
}
|
||||
|
||||
ocidRoot := filepath.Dir(filepath.Dir(file))
|
||||
p := filepath.Join(ocidRoot, "Godeps/Godeps.json")
|
||||
|
||||
grepCmd := fmt.Sprintf(`grep -r "\"google.golang.org/grpc\"" %s -A 1 | grep "\"Rev\"" | cut -d: -f2 | tr -d ' "\n'`, p)
|
||||
|
||||
out, err := execCmd("bash", "-c", grepCmd)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return out, err
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue