2015-07-21 09:19:11 +00:00
|
|
|
/*
|
|
|
|
Copyright 2015 The Kubernetes Authors 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.
|
|
|
|
*/
|
|
|
|
|
2015-11-23 19:32:50 +00:00
|
|
|
package util
|
2015-07-21 09:19:11 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
|
2015-12-10 14:03:59 +00:00
|
|
|
etcd "github.com/coreos/etcd/client"
|
2015-07-21 09:19:11 +00:00
|
|
|
)
|
|
|
|
|
2015-09-15 02:25:13 +00:00
|
|
|
// IsEtcdNotFound returns true if and only if err is an etcd not found error.
|
2015-07-21 09:19:11 +00:00
|
|
|
func IsEtcdNotFound(err error) bool {
|
2015-12-10 14:03:59 +00:00
|
|
|
return isEtcdErrorNum(err, etcd.ErrorCodeKeyNotFound)
|
2015-07-21 09:19:11 +00:00
|
|
|
}
|
|
|
|
|
2015-09-15 02:25:13 +00:00
|
|
|
// IsEtcdNodeExist returns true if and only if err is an etcd node already exist error.
|
2015-07-21 09:19:11 +00:00
|
|
|
func IsEtcdNodeExist(err error) bool {
|
2015-12-10 14:03:59 +00:00
|
|
|
return isEtcdErrorNum(err, etcd.ErrorCodeNodeExist)
|
2015-07-21 09:19:11 +00:00
|
|
|
}
|
|
|
|
|
2015-09-15 02:25:13 +00:00
|
|
|
// IsEtcdTestFailed returns true if and only if err is an etcd write conflict.
|
2015-07-21 09:19:11 +00:00
|
|
|
func IsEtcdTestFailed(err error) bool {
|
2015-12-10 14:03:59 +00:00
|
|
|
return isEtcdErrorNum(err, etcd.ErrorCodeTestFailed)
|
2015-07-21 09:19:11 +00:00
|
|
|
}
|
|
|
|
|
2015-11-04 20:15:01 +00:00
|
|
|
// IsEtcdWatchExpired returns true if and only if err indicates the watch has expired.
|
|
|
|
func IsEtcdWatchExpired(err error) bool {
|
2015-12-10 14:03:59 +00:00
|
|
|
// NOTE: This seems weird why it wouldn't be etcd.ErrorCodeWatcherCleared
|
|
|
|
// I'm using the previous matching value
|
|
|
|
return isEtcdErrorNum(err, etcd.ErrorCodeEventIndexCleared)
|
2015-11-04 20:15:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsEtcdUnreachable returns true if and only if err indicates the server could not be reached.
|
|
|
|
func IsEtcdUnreachable(err error) bool {
|
2015-12-10 14:03:59 +00:00
|
|
|
// NOTE: The logic has changed previous error code no longer applies
|
|
|
|
return err == etcd.ErrClusterUnavailable
|
2015-11-04 20:15:01 +00:00
|
|
|
}
|
|
|
|
|
2015-09-15 02:25:13 +00:00
|
|
|
// isEtcdErrorNum returns true if and only if err is an etcd error, whose errorCode matches errorCode
|
2015-07-21 09:19:11 +00:00
|
|
|
func isEtcdErrorNum(err error, errorCode int) bool {
|
2015-12-10 14:03:59 +00:00
|
|
|
if err != nil {
|
|
|
|
if etcdError, ok := err.(etcd.Error); ok {
|
|
|
|
return etcdError.Code == errorCode
|
|
|
|
}
|
|
|
|
// NOTE: There are other error types returned
|
|
|
|
}
|
|
|
|
return false
|
2015-07-21 09:19:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetEtcdVersion performs a version check against the provided Etcd server,
|
|
|
|
// returning the string response, and error (if any).
|
|
|
|
func GetEtcdVersion(host string) (string, error) {
|
|
|
|
response, err := http.Get(host + "/version")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer response.Body.Close()
|
|
|
|
if response.StatusCode != http.StatusOK {
|
|
|
|
return "", fmt.Errorf("unsuccessful response from etcd server %q: %v", host, err)
|
|
|
|
}
|
|
|
|
versionBytes, err := ioutil.ReadAll(response.Body)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(versionBytes), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type etcdHealth struct {
|
|
|
|
// Note this has to be public so the json library can modify it.
|
2015-08-08 01:52:23 +00:00
|
|
|
Health string `json:"health"`
|
2015-07-21 09:19:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func EtcdHealthCheck(data []byte) error {
|
|
|
|
obj := etcdHealth{}
|
|
|
|
if err := json.Unmarshal(data, &obj); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if obj.Health != "true" {
|
|
|
|
return fmt.Errorf("Unhealthy status: %s", obj.Health)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|