2014-10-07 14:24:40 +00:00
|
|
|
/*
|
2015-05-01 16:19:44 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors All rights reserved.
|
2014-10-07 14:24:40 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2015-05-01 07:31:01 +00:00
|
|
|
"crypto/md5"
|
|
|
|
"fmt"
|
2015-01-09 01:34:53 +00:00
|
|
|
"reflect"
|
2015-07-13 19:10:04 +00:00
|
|
|
"strings"
|
2015-01-05 21:38:39 +00:00
|
|
|
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/resource"
|
|
|
|
"k8s.io/kubernetes/pkg/conversion"
|
|
|
|
"k8s.io/kubernetes/pkg/fields"
|
|
|
|
"k8s.io/kubernetes/pkg/labels"
|
|
|
|
"k8s.io/kubernetes/pkg/runtime"
|
|
|
|
"k8s.io/kubernetes/pkg/util"
|
2015-09-09 17:45:01 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util/sets"
|
2015-01-09 01:34:53 +00:00
|
|
|
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
2015-01-05 21:38:39 +00:00
|
|
|
)
|
|
|
|
|
2015-01-09 01:34:53 +00:00
|
|
|
// Conversion error conveniently packages up errors in conversions.
|
|
|
|
type ConversionError struct {
|
|
|
|
In, Out interface{}
|
|
|
|
Message string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return a helpful string about the error
|
|
|
|
func (c *ConversionError) Error() string {
|
|
|
|
return spew.Sprintf(
|
|
|
|
"Conversion error: %s. (in: %v(%+v) out: %v)",
|
|
|
|
c.Message, reflect.TypeOf(c.In), c.In, reflect.TypeOf(c.Out),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2015-01-05 21:38:39 +00:00
|
|
|
// Semantic can do semantic deep equality checks for api objects.
|
|
|
|
// Example: api.Semantic.DeepEqual(aPod, aPodWithNonNilButEmptyMaps) == true
|
|
|
|
var Semantic = conversion.EqualitiesOrDie(
|
|
|
|
func(a, b resource.Quantity) bool {
|
|
|
|
// Ignore formatting, only care that numeric value stayed the same.
|
|
|
|
// TODO: if we decide it's important, after we drop v1beta1/2, we
|
|
|
|
// could start comparing format.
|
|
|
|
//
|
2015-08-08 21:29:57 +00:00
|
|
|
// Uninitialized quantities are equivalent to 0 quantities.
|
2015-01-05 21:38:39 +00:00
|
|
|
if a.Amount == nil && b.MilliValue() == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if b.Amount == nil && a.MilliValue() == 0 {
|
|
|
|
return true
|
|
|
|
}
|
2015-01-09 01:34:53 +00:00
|
|
|
if a.Amount == nil || b.Amount == nil {
|
|
|
|
return false
|
|
|
|
}
|
2015-01-05 21:38:39 +00:00
|
|
|
return a.Amount.Cmp(b.Amount) == 0
|
|
|
|
},
|
2015-03-06 00:52:43 +00:00
|
|
|
func(a, b util.Time) bool {
|
2015-03-06 05:03:21 +00:00
|
|
|
return a.UTC() == b.UTC()
|
2015-03-06 00:52:43 +00:00
|
|
|
},
|
2015-03-22 21:43:00 +00:00
|
|
|
func(a, b labels.Selector) bool {
|
|
|
|
return a.String() == b.String()
|
|
|
|
},
|
|
|
|
func(a, b fields.Selector) bool {
|
|
|
|
return a.String() == b.String()
|
|
|
|
},
|
2014-10-07 14:24:40 +00:00
|
|
|
)
|
2015-01-17 00:34:47 +00:00
|
|
|
|
2015-09-09 17:45:01 +00:00
|
|
|
var standardResources = sets.NewString(
|
2015-01-23 17:38:30 +00:00
|
|
|
string(ResourceMemory),
|
|
|
|
string(ResourceCPU),
|
|
|
|
string(ResourcePods),
|
|
|
|
string(ResourceQuotas),
|
|
|
|
string(ResourceServices),
|
2015-03-23 18:18:11 +00:00
|
|
|
string(ResourceReplicationControllers),
|
2015-04-08 21:03:56 +00:00
|
|
|
string(ResourceSecrets),
|
|
|
|
string(ResourcePersistentVolumeClaims),
|
2015-03-23 18:18:11 +00:00
|
|
|
string(ResourceStorage))
|
2015-01-17 00:34:47 +00:00
|
|
|
|
|
|
|
func IsStandardResourceName(str string) bool {
|
|
|
|
return standardResources.Has(str)
|
|
|
|
}
|
2015-03-05 03:34:31 +00:00
|
|
|
|
|
|
|
// NewDeleteOptions returns a DeleteOptions indicating the resource should
|
|
|
|
// be deleted within the specified grace period. Use zero to indicate
|
|
|
|
// immediate deletion. If you would prefer to use the default grace period,
|
|
|
|
// use &api.DeleteOptions{} directly.
|
|
|
|
func NewDeleteOptions(grace int64) *DeleteOptions {
|
|
|
|
return &DeleteOptions{GracePeriodSeconds: &grace}
|
|
|
|
}
|
2015-03-16 21:36:30 +00:00
|
|
|
|
2015-05-23 20:41:11 +00:00
|
|
|
// this function aims to check if the service's ClusterIP is set or not
|
2015-03-16 21:36:30 +00:00
|
|
|
// the objective is not to perform validation here
|
|
|
|
func IsServiceIPSet(service *Service) bool {
|
2015-05-23 20:41:11 +00:00
|
|
|
return service.Spec.ClusterIP != ClusterIPNone && service.Spec.ClusterIP != ""
|
2015-03-16 21:36:30 +00:00
|
|
|
}
|
|
|
|
|
2015-05-23 20:41:11 +00:00
|
|
|
// this function aims to check if the service's cluster IP is requested or not
|
2015-03-16 21:36:30 +00:00
|
|
|
func IsServiceIPRequested(service *Service) bool {
|
2015-05-23 20:41:11 +00:00
|
|
|
return service.Spec.ClusterIP == ""
|
2015-03-16 21:36:30 +00:00
|
|
|
}
|
2015-03-20 16:48:12 +00:00
|
|
|
|
2015-09-09 17:45:01 +00:00
|
|
|
var standardFinalizers = sets.NewString(
|
2015-03-20 16:48:12 +00:00
|
|
|
string(FinalizerKubernetes))
|
|
|
|
|
|
|
|
func IsStandardFinalizerName(str string) bool {
|
|
|
|
return standardFinalizers.Has(str)
|
|
|
|
}
|
2015-04-22 17:55:05 +00:00
|
|
|
|
|
|
|
// AddToNodeAddresses appends the NodeAddresses to the passed-by-pointer slice,
|
|
|
|
// only if they do not already exist
|
|
|
|
func AddToNodeAddresses(addresses *[]NodeAddress, addAddresses ...NodeAddress) {
|
|
|
|
for _, add := range addAddresses {
|
|
|
|
exists := false
|
|
|
|
for _, existing := range *addresses {
|
|
|
|
if existing.Address == add.Address && existing.Type == add.Type {
|
|
|
|
exists = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !exists {
|
|
|
|
*addresses = append(*addresses, add)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-01 07:31:01 +00:00
|
|
|
|
|
|
|
func HashObject(obj runtime.Object, codec runtime.Codec) (string, error) {
|
|
|
|
data, err := codec.Encode(obj)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%x", md5.Sum(data)), nil
|
|
|
|
}
|
2015-05-22 21:33:29 +00:00
|
|
|
|
|
|
|
// TODO: make method on LoadBalancerStatus?
|
|
|
|
func LoadBalancerStatusEqual(l, r *LoadBalancerStatus) bool {
|
|
|
|
return ingressSliceEqual(l.Ingress, r.Ingress)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ingressSliceEqual(lhs, rhs []LoadBalancerIngress) bool {
|
|
|
|
if len(lhs) != len(rhs) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i := range lhs {
|
|
|
|
if !ingressEqual(&lhs[i], &rhs[i]) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func ingressEqual(lhs, rhs *LoadBalancerIngress) bool {
|
|
|
|
if lhs.IP != rhs.IP {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if lhs.Hostname != rhs.Hostname {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: make method on LoadBalancerStatus?
|
|
|
|
func LoadBalancerStatusDeepCopy(lb *LoadBalancerStatus) *LoadBalancerStatus {
|
|
|
|
c := &LoadBalancerStatus{}
|
|
|
|
c.Ingress = make([]LoadBalancerIngress, len(lb.Ingress))
|
|
|
|
for i := range lb.Ingress {
|
|
|
|
c.Ingress[i] = lb.Ingress[i]
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
2015-07-13 19:10:04 +00:00
|
|
|
|
|
|
|
// GetAccessModesAsString returns a string representation of an array of access modes.
|
|
|
|
// modes, when present, are always in the same order: RWO,ROX,RWX.
|
|
|
|
func GetAccessModesAsString(modes []PersistentVolumeAccessMode) string {
|
|
|
|
modes = removeDuplicateAccessModes(modes)
|
|
|
|
modesStr := []string{}
|
|
|
|
if containsAccessMode(modes, ReadWriteOnce) {
|
|
|
|
modesStr = append(modesStr, "RWO")
|
|
|
|
}
|
|
|
|
if containsAccessMode(modes, ReadOnlyMany) {
|
|
|
|
modesStr = append(modesStr, "ROX")
|
|
|
|
}
|
|
|
|
if containsAccessMode(modes, ReadWriteMany) {
|
|
|
|
modesStr = append(modesStr, "RWX")
|
|
|
|
}
|
|
|
|
return strings.Join(modesStr, ",")
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAccessModesAsString returns an array of AccessModes from a string created by GetAccessModesAsString
|
|
|
|
func GetAccessModesFromString(modes string) []PersistentVolumeAccessMode {
|
|
|
|
strmodes := strings.Split(modes, ",")
|
|
|
|
accessModes := []PersistentVolumeAccessMode{}
|
|
|
|
for _, s := range strmodes {
|
|
|
|
s = strings.Trim(s, " ")
|
|
|
|
switch {
|
|
|
|
case s == "RWO":
|
|
|
|
accessModes = append(accessModes, ReadWriteOnce)
|
|
|
|
case s == "ROX":
|
|
|
|
accessModes = append(accessModes, ReadOnlyMany)
|
|
|
|
case s == "RWX":
|
|
|
|
accessModes = append(accessModes, ReadWriteMany)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return accessModes
|
|
|
|
}
|
|
|
|
|
|
|
|
// removeDuplicateAccessModes returns an array of access modes without any duplicates
|
|
|
|
func removeDuplicateAccessModes(modes []PersistentVolumeAccessMode) []PersistentVolumeAccessMode {
|
|
|
|
accessModes := []PersistentVolumeAccessMode{}
|
|
|
|
for _, m := range modes {
|
|
|
|
if !containsAccessMode(accessModes, m) {
|
|
|
|
accessModes = append(accessModes, m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return accessModes
|
|
|
|
}
|
|
|
|
|
|
|
|
func containsAccessMode(modes []PersistentVolumeAccessMode, mode PersistentVolumeAccessMode) bool {
|
|
|
|
for _, m := range modes {
|
|
|
|
if m == mode {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|