| 
									
										
										
										
											2014-10-07 22:24:40 +08:00
										 |  |  | /* | 
					
						
							| 
									
										
										
										
											2015-05-02 00:19:44 +08:00
										 |  |  | Copyright 2014 The Kubernetes Authors All rights reserved. | 
					
						
							| 
									
										
										
										
											2014-10-07 22:24:40 +08: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 15:31:01 +08:00
										 |  |  | 	"crypto/md5" | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2015-01-09 09:34:53 +08:00
										 |  |  | 	"reflect" | 
					
						
							| 
									
										
										
										
											2015-01-06 05:38:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/GoogleCloudPlatform/kubernetes/pkg/api/resource" | 
					
						
							|  |  |  | 	"github.com/GoogleCloudPlatform/kubernetes/pkg/conversion" | 
					
						
							| 
									
										
										
										
											2015-03-23 05:43:00 +08:00
										 |  |  | 	"github.com/GoogleCloudPlatform/kubernetes/pkg/fields" | 
					
						
							|  |  |  | 	"github.com/GoogleCloudPlatform/kubernetes/pkg/labels" | 
					
						
							| 
									
										
										
										
											2015-05-01 15:31:01 +08:00
										 |  |  | 	"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime" | 
					
						
							| 
									
										
										
										
											2015-01-17 08:34:47 +08:00
										 |  |  | 	"github.com/GoogleCloudPlatform/kubernetes/pkg/util" | 
					
						
							| 
									
										
										
										
											2015-01-09 09:34:53 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/davecgh/go-spew/spew" | 
					
						
							| 
									
										
										
										
											2015-01-06 05:38:39 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-09 09:34:53 +08: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-06 05:38:39 +08: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.
 | 
					
						
							|  |  |  | 		//
 | 
					
						
							|  |  |  | 		// Uninitialized quantities are equivilent to 0 quantities.
 | 
					
						
							|  |  |  | 		if a.Amount == nil && b.MilliValue() == 0 { | 
					
						
							|  |  |  | 			return true | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if b.Amount == nil && a.MilliValue() == 0 { | 
					
						
							|  |  |  | 			return true | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-01-09 09:34:53 +08:00
										 |  |  | 		if a.Amount == nil || b.Amount == nil { | 
					
						
							|  |  |  | 			return false | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-01-06 05:38:39 +08:00
										 |  |  | 		return a.Amount.Cmp(b.Amount) == 0 | 
					
						
							|  |  |  | 	}, | 
					
						
							| 
									
										
										
										
											2015-03-06 08:52:43 +08:00
										 |  |  | 	func(a, b util.Time) bool { | 
					
						
							| 
									
										
										
										
											2015-03-06 13:03:21 +08:00
										 |  |  | 		return a.UTC() == b.UTC() | 
					
						
							| 
									
										
										
										
											2015-03-06 08:52:43 +08:00
										 |  |  | 	}, | 
					
						
							| 
									
										
										
										
											2015-03-23 05:43:00 +08: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 22:24:40 +08:00
										 |  |  | ) | 
					
						
							| 
									
										
										
										
											2015-01-17 08:34:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-24 01:38:30 +08:00
										 |  |  | var standardResources = util.NewStringSet( | 
					
						
							|  |  |  | 	string(ResourceMemory), | 
					
						
							|  |  |  | 	string(ResourceCPU), | 
					
						
							|  |  |  | 	string(ResourcePods), | 
					
						
							|  |  |  | 	string(ResourceQuotas), | 
					
						
							|  |  |  | 	string(ResourceServices), | 
					
						
							| 
									
										
										
										
											2015-03-24 02:18:11 +08:00
										 |  |  | 	string(ResourceReplicationControllers), | 
					
						
							| 
									
										
										
										
											2015-04-09 05:03:56 +08:00
										 |  |  | 	string(ResourceSecrets), | 
					
						
							|  |  |  | 	string(ResourcePersistentVolumeClaims), | 
					
						
							| 
									
										
										
										
											2015-03-24 02:18:11 +08:00
										 |  |  | 	string(ResourceStorage)) | 
					
						
							| 
									
										
										
										
											2015-01-17 08:34:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func IsStandardResourceName(str string) bool { | 
					
						
							|  |  |  | 	return standardResources.Has(str) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-03-05 11:34:31 +08: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-17 05:36:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // this function aims to check if the service portal IP is set or not
 | 
					
						
							|  |  |  | // the objective is not to perform validation here
 | 
					
						
							|  |  |  | func IsServiceIPSet(service *Service) bool { | 
					
						
							|  |  |  | 	return service.Spec.PortalIP != PortalIPNone && service.Spec.PortalIP != "" | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // this function aims to check if the service portal IP is requested or not
 | 
					
						
							|  |  |  | func IsServiceIPRequested(service *Service) bool { | 
					
						
							|  |  |  | 	return service.Spec.PortalIP == "" | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-03-21 00:48:12 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | var standardFinalizers = util.NewStringSet( | 
					
						
							|  |  |  | 	string(FinalizerKubernetes)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func IsStandardFinalizerName(str string) bool { | 
					
						
							|  |  |  | 	return standardFinalizers.Has(str) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-04-23 01:55:05 +08: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 15:31:01 +08: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-23 05:33:29 +08: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 | 
					
						
							|  |  |  | } |