2014-10-07 14:24:40 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors.
|
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.
|
|
|
|
*/
|
|
|
|
|
2017-04-10 17:49:54 +00:00
|
|
|
package helper
|
2014-10-07 14:24:40 +00:00
|
|
|
|
|
|
|
import (
|
2017-03-22 04:28:00 +00:00
|
|
|
"encoding/json"
|
2015-05-01 07:31:01 +00:00
|
|
|
"fmt"
|
2015-07-13 19:10:04 +00:00
|
|
|
"strings"
|
2015-01-05 21:38:39 +00:00
|
|
|
|
2017-01-25 13:13:07 +00:00
|
|
|
"k8s.io/apimachinery/pkg/api/resource"
|
2017-01-11 14:09:48 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/conversion"
|
2017-01-19 14:50:16 +00:00
|
|
|
"k8s.io/apimachinery/pkg/fields"
|
2017-01-11 14:09:48 +00:00
|
|
|
"k8s.io/apimachinery/pkg/labels"
|
|
|
|
"k8s.io/apimachinery/pkg/selection"
|
|
|
|
"k8s.io/apimachinery/pkg/util/sets"
|
2017-10-09 15:58:37 +00:00
|
|
|
"k8s.io/kubernetes/pkg/apis/core"
|
2016-07-19 09:17:20 +00:00
|
|
|
)
|
|
|
|
|
2017-08-17 18:16:37 +00:00
|
|
|
// IsHugePageResourceName returns true if the resource name has the huge page
|
|
|
|
// resource prefix.
|
2017-10-09 15:58:37 +00:00
|
|
|
func IsHugePageResourceName(name core.ResourceName) bool {
|
|
|
|
return strings.HasPrefix(string(name), core.ResourceHugePagesPrefix)
|
2017-08-17 18:16:37 +00:00
|
|
|
}
|
|
|
|
|
2017-11-13 01:37:06 +00:00
|
|
|
// IsQuotaHugePageResourceName returns true if the resource name has the quota
|
|
|
|
// related huge page resource prefix.
|
|
|
|
func IsQuotaHugePageResourceName(name core.ResourceName) bool {
|
|
|
|
return strings.HasPrefix(string(name), core.ResourceHugePagesPrefix) || strings.HasPrefix(string(name), core.ResourceRequestsHugePagesPrefix)
|
|
|
|
}
|
|
|
|
|
2017-08-17 18:16:37 +00:00
|
|
|
// HugePageResourceName returns a ResourceName with the canonical hugepage
|
|
|
|
// prefix prepended for the specified page size. The page size is converted
|
|
|
|
// to its canonical representation.
|
2017-10-09 15:58:37 +00:00
|
|
|
func HugePageResourceName(pageSize resource.Quantity) core.ResourceName {
|
|
|
|
return core.ResourceName(fmt.Sprintf("%s%s", core.ResourceHugePagesPrefix, pageSize.String()))
|
2017-08-17 18:16:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HugePageSizeFromResourceName returns the page size for the specified huge page
|
|
|
|
// resource name. If the specified input is not a valid huge page resource name
|
|
|
|
// an error is returned.
|
2017-10-09 15:58:37 +00:00
|
|
|
func HugePageSizeFromResourceName(name core.ResourceName) (resource.Quantity, error) {
|
2017-08-17 18:16:37 +00:00
|
|
|
if !IsHugePageResourceName(name) {
|
|
|
|
return resource.Quantity{}, fmt.Errorf("resource name: %s is not valid hugepage name", name)
|
|
|
|
}
|
2017-10-09 15:58:37 +00:00
|
|
|
pageSize := strings.TrimPrefix(string(name), core.ResourceHugePagesPrefix)
|
2017-08-17 18:16:37 +00:00
|
|
|
return resource.ParseQuantity(pageSize)
|
|
|
|
}
|
|
|
|
|
2016-07-19 09:17:20 +00:00
|
|
|
// NonConvertibleFields iterates over the provided map and filters out all but
|
|
|
|
// any keys with the "non-convertible.kubernetes.io" prefix.
|
|
|
|
func NonConvertibleFields(annotations map[string]string) map[string]string {
|
|
|
|
nonConvertibleKeys := map[string]string{}
|
|
|
|
for key, value := range annotations {
|
2017-10-09 15:58:37 +00:00
|
|
|
if strings.HasPrefix(key, core.NonConvertibleAnnotationPrefix) {
|
2016-07-19 09:17:20 +00:00
|
|
|
nonConvertibleKeys[key] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nonConvertibleKeys
|
|
|
|
}
|
|
|
|
|
2017-10-09 15:58:37 +00:00
|
|
|
// Semantic can do semantic deep equality checks for core objects.
|
2017-01-25 13:39:54 +00:00
|
|
|
// Example: apiequality.Semantic.DeepEqual(aPod, aPodWithNonNilButEmptyMaps) == true
|
2015-01-05 21:38:39 +00:00
|
|
|
var Semantic = conversion.EqualitiesOrDie(
|
|
|
|
func(a, b resource.Quantity) bool {
|
|
|
|
// Ignore formatting, only care that numeric value stayed the same.
|
2015-11-05 10:48:03 +00:00
|
|
|
// TODO: if we decide it's important, it should be safe to start comparing the format.
|
2015-01-05 21:38:39 +00:00
|
|
|
//
|
2015-08-08 21:29:57 +00:00
|
|
|
// Uninitialized quantities are equivalent to 0 quantities.
|
2016-05-17 04:36:56 +00:00
|
|
|
return a.Cmp(b) == 0
|
2015-01-05 21:38:39 +00:00
|
|
|
},
|
2017-08-04 12:02:53 +00:00
|
|
|
func(a, b metav1.MicroTime) bool {
|
|
|
|
return a.UTC() == b.UTC()
|
|
|
|
},
|
2016-12-03 18:57:26 +00:00
|
|
|
func(a, b metav1.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
|
|
|
|
2016-02-22 16:14:11 +00:00
|
|
|
var standardResourceQuotaScopes = sets.NewString(
|
2017-10-09 15:58:37 +00:00
|
|
|
string(core.ResourceQuotaScopeTerminating),
|
|
|
|
string(core.ResourceQuotaScopeNotTerminating),
|
|
|
|
string(core.ResourceQuotaScopeBestEffort),
|
|
|
|
string(core.ResourceQuotaScopeNotBestEffort),
|
2016-02-22 16:14:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// IsStandardResourceQuotaScope returns true if the scope is a standard value
|
|
|
|
func IsStandardResourceQuotaScope(str string) bool {
|
|
|
|
return standardResourceQuotaScopes.Has(str)
|
|
|
|
}
|
|
|
|
|
|
|
|
var podObjectCountQuotaResources = sets.NewString(
|
2017-10-09 15:58:37 +00:00
|
|
|
string(core.ResourcePods),
|
2016-02-22 16:14:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var podComputeQuotaResources = sets.NewString(
|
2017-10-09 15:58:37 +00:00
|
|
|
string(core.ResourceCPU),
|
|
|
|
string(core.ResourceMemory),
|
|
|
|
string(core.ResourceLimitsCPU),
|
|
|
|
string(core.ResourceLimitsMemory),
|
|
|
|
string(core.ResourceRequestsCPU),
|
|
|
|
string(core.ResourceRequestsMemory),
|
2016-02-22 16:14:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// IsResourceQuotaScopeValidForResource returns true if the resource applies to the specified scope
|
2017-10-09 15:58:37 +00:00
|
|
|
func IsResourceQuotaScopeValidForResource(scope core.ResourceQuotaScope, resource string) bool {
|
2016-02-22 16:14:11 +00:00
|
|
|
switch scope {
|
2017-10-09 15:58:37 +00:00
|
|
|
case core.ResourceQuotaScopeTerminating, core.ResourceQuotaScopeNotTerminating, core.ResourceQuotaScopeNotBestEffort:
|
2016-02-22 16:14:11 +00:00
|
|
|
return podObjectCountQuotaResources.Has(resource) || podComputeQuotaResources.Has(resource)
|
2017-10-09 15:58:37 +00:00
|
|
|
case core.ResourceQuotaScopeBestEffort:
|
2016-02-22 16:14:11 +00:00
|
|
|
return podObjectCountQuotaResources.Has(resource)
|
|
|
|
default:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var standardContainerResources = sets.NewString(
|
2017-10-09 15:58:37 +00:00
|
|
|
string(core.ResourceCPU),
|
|
|
|
string(core.ResourceMemory),
|
|
|
|
string(core.ResourceEphemeralStorage),
|
2016-02-22 16:14:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// IsStandardContainerResourceName returns true if the container can make a resource request
|
|
|
|
// for the specified resource
|
|
|
|
func IsStandardContainerResourceName(str string) bool {
|
2017-10-09 15:58:37 +00:00
|
|
|
return standardContainerResources.Has(str) || IsHugePageResourceName(core.ResourceName(str))
|
2016-02-22 16:14:11 +00:00
|
|
|
}
|
|
|
|
|
2017-07-14 06:49:46 +00:00
|
|
|
// IsExtendedResourceName returns true if the resource name is not in the
|
2017-11-04 07:52:15 +00:00
|
|
|
// default namespace.
|
2017-10-09 15:58:37 +00:00
|
|
|
func IsExtendedResourceName(name core.ResourceName) bool {
|
2017-11-04 07:52:15 +00:00
|
|
|
return !IsDefaultNamespaceResource(name)
|
2017-07-14 06:49:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsDefaultNamespaceResource returns true if the resource name is in the
|
|
|
|
// *kubernetes.io/ namespace. Partially-qualified (unprefixed) names are
|
|
|
|
// implicitly in the kubernetes.io/ namespace.
|
2017-10-09 15:58:37 +00:00
|
|
|
func IsDefaultNamespaceResource(name core.ResourceName) bool {
|
2017-07-14 06:49:46 +00:00
|
|
|
return !strings.Contains(string(name), "/") ||
|
2017-10-09 15:58:37 +00:00
|
|
|
strings.Contains(string(name), core.ResourceDefaultNamespacePrefix)
|
2017-07-14 06:49:46 +00:00
|
|
|
}
|
|
|
|
|
2017-10-09 15:58:37 +00:00
|
|
|
var overcommitBlacklist = sets.NewString(string(core.ResourceNvidiaGPU))
|
2017-07-14 06:49:46 +00:00
|
|
|
|
|
|
|
// IsOvercommitAllowed returns true if the resource is in the default
|
|
|
|
// namespace and not blacklisted.
|
2017-10-09 15:58:37 +00:00
|
|
|
func IsOvercommitAllowed(name core.ResourceName) bool {
|
2017-07-14 06:49:46 +00:00
|
|
|
return IsDefaultNamespaceResource(name) &&
|
2017-08-17 18:16:37 +00:00
|
|
|
!IsHugePageResourceName(name) &&
|
2017-07-14 06:49:46 +00:00
|
|
|
!overcommitBlacklist.Has(string(name))
|
|
|
|
}
|
|
|
|
|
2016-03-09 23:11:26 +00:00
|
|
|
var standardLimitRangeTypes = sets.NewString(
|
2017-10-09 15:58:37 +00:00
|
|
|
string(core.LimitTypePod),
|
|
|
|
string(core.LimitTypeContainer),
|
|
|
|
string(core.LimitTypePersistentVolumeClaim),
|
2016-03-09 23:11:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// IsStandardLimitRangeType returns true if the type is Pod or Container
|
|
|
|
func IsStandardLimitRangeType(str string) bool {
|
|
|
|
return standardLimitRangeTypes.Has(str)
|
|
|
|
}
|
|
|
|
|
2016-02-22 16:14:11 +00:00
|
|
|
var standardQuotaResources = sets.NewString(
|
2017-10-09 15:58:37 +00:00
|
|
|
string(core.ResourceCPU),
|
|
|
|
string(core.ResourceMemory),
|
|
|
|
string(core.ResourceEphemeralStorage),
|
|
|
|
string(core.ResourceRequestsCPU),
|
|
|
|
string(core.ResourceRequestsMemory),
|
|
|
|
string(core.ResourceRequestsStorage),
|
|
|
|
string(core.ResourceRequestsEphemeralStorage),
|
|
|
|
string(core.ResourceLimitsCPU),
|
|
|
|
string(core.ResourceLimitsMemory),
|
|
|
|
string(core.ResourceLimitsEphemeralStorage),
|
|
|
|
string(core.ResourcePods),
|
|
|
|
string(core.ResourceQuotas),
|
|
|
|
string(core.ResourceServices),
|
|
|
|
string(core.ResourceReplicationControllers),
|
|
|
|
string(core.ResourceSecrets),
|
|
|
|
string(core.ResourcePersistentVolumeClaims),
|
|
|
|
string(core.ResourceConfigMaps),
|
|
|
|
string(core.ResourceServicesNodePorts),
|
|
|
|
string(core.ResourceServicesLoadBalancers),
|
2016-02-22 16:14:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// IsStandardQuotaResourceName returns true if the resource is known to
|
|
|
|
// the quota tracking system
|
|
|
|
func IsStandardQuotaResourceName(str string) bool {
|
2017-11-13 01:37:06 +00:00
|
|
|
return standardQuotaResources.Has(str) || IsQuotaHugePageResourceName(core.ResourceName(str))
|
2016-02-22 16:14:11 +00:00
|
|
|
}
|
|
|
|
|
2015-09-09 17:45:01 +00:00
|
|
|
var standardResources = sets.NewString(
|
2017-10-09 15:58:37 +00:00
|
|
|
string(core.ResourceCPU),
|
|
|
|
string(core.ResourceMemory),
|
|
|
|
string(core.ResourceEphemeralStorage),
|
|
|
|
string(core.ResourceRequestsCPU),
|
|
|
|
string(core.ResourceRequestsMemory),
|
|
|
|
string(core.ResourceRequestsEphemeralStorage),
|
|
|
|
string(core.ResourceLimitsCPU),
|
|
|
|
string(core.ResourceLimitsMemory),
|
|
|
|
string(core.ResourceLimitsEphemeralStorage),
|
|
|
|
string(core.ResourcePods),
|
|
|
|
string(core.ResourceQuotas),
|
|
|
|
string(core.ResourceServices),
|
|
|
|
string(core.ResourceReplicationControllers),
|
|
|
|
string(core.ResourceSecrets),
|
|
|
|
string(core.ResourceConfigMaps),
|
|
|
|
string(core.ResourcePersistentVolumeClaims),
|
|
|
|
string(core.ResourceStorage),
|
|
|
|
string(core.ResourceRequestsStorage),
|
|
|
|
string(core.ResourceServicesNodePorts),
|
|
|
|
string(core.ResourceServicesLoadBalancers),
|
2015-10-13 21:27:56 +00:00
|
|
|
)
|
2015-01-17 00:34:47 +00:00
|
|
|
|
2015-10-13 21:27:56 +00:00
|
|
|
// IsStandardResourceName returns true if the resource is known to the system
|
2015-01-17 00:34:47 +00:00
|
|
|
func IsStandardResourceName(str string) bool {
|
2017-11-13 01:37:06 +00:00
|
|
|
return standardResources.Has(str) || IsQuotaHugePageResourceName(core.ResourceName(str))
|
2015-01-17 00:34:47 +00:00
|
|
|
}
|
2015-03-05 03:34:31 +00:00
|
|
|
|
2015-10-13 21:27:56 +00:00
|
|
|
var integerResources = sets.NewString(
|
2017-10-09 15:58:37 +00:00
|
|
|
string(core.ResourcePods),
|
|
|
|
string(core.ResourceQuotas),
|
|
|
|
string(core.ResourceServices),
|
|
|
|
string(core.ResourceReplicationControllers),
|
|
|
|
string(core.ResourceSecrets),
|
|
|
|
string(core.ResourceConfigMaps),
|
|
|
|
string(core.ResourcePersistentVolumeClaims),
|
|
|
|
string(core.ResourceServicesNodePorts),
|
|
|
|
string(core.ResourceServicesLoadBalancers),
|
2015-10-13 21:27:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// IsIntegerResourceName returns true if the resource is measured in integer values
|
|
|
|
func IsIntegerResourceName(str string) bool {
|
2017-10-09 15:58:37 +00:00
|
|
|
return integerResources.Has(str) || IsExtendedResourceName(core.ResourceName(str))
|
2015-10-13 21:27:56 +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
|
2017-10-09 15:58:37 +00:00
|
|
|
func IsServiceIPSet(service *core.Service) bool {
|
|
|
|
return service.Spec.ClusterIP != core.ClusterIPNone && service.Spec.ClusterIP != ""
|
2015-03-16 21:36:30 +00:00
|
|
|
}
|
|
|
|
|
2015-09-09 17:45:01 +00:00
|
|
|
var standardFinalizers = sets.NewString(
|
2017-10-09 15:58:37 +00:00
|
|
|
string(core.FinalizerKubernetes),
|
2017-02-23 19:14:55 +00:00
|
|
|
metav1.FinalizerOrphanDependents,
|
2017-05-31 07:21:02 +00:00
|
|
|
metav1.FinalizerDeleteDependents,
|
2016-05-18 03:24:42 +00:00
|
|
|
)
|
2015-03-20 16:48:12 +00:00
|
|
|
|
|
|
|
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
|
2017-10-09 15:58:37 +00:00
|
|
|
func AddToNodeAddresses(addresses *[]core.NodeAddress, addAddresses ...core.NodeAddress) {
|
2015-04-22 17:55:05 +00:00
|
|
|
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
|
|
|
|
2015-05-22 21:33:29 +00:00
|
|
|
// TODO: make method on LoadBalancerStatus?
|
2017-10-09 15:58:37 +00:00
|
|
|
func LoadBalancerStatusEqual(l, r *core.LoadBalancerStatus) bool {
|
2015-05-22 21:33:29 +00:00
|
|
|
return ingressSliceEqual(l.Ingress, r.Ingress)
|
|
|
|
}
|
|
|
|
|
2017-10-09 15:58:37 +00:00
|
|
|
func ingressSliceEqual(lhs, rhs []core.LoadBalancerIngress) bool {
|
2015-05-22 21:33:29 +00:00
|
|
|
if len(lhs) != len(rhs) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i := range lhs {
|
|
|
|
if !ingressEqual(&lhs[i], &rhs[i]) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-10-09 15:58:37 +00:00
|
|
|
func ingressEqual(lhs, rhs *core.LoadBalancerIngress) bool {
|
2015-05-22 21:33:29 +00:00
|
|
|
if lhs.IP != rhs.IP {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if lhs.Hostname != rhs.Hostname {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: make method on LoadBalancerStatus?
|
2017-10-09 15:58:37 +00:00
|
|
|
func LoadBalancerStatusDeepCopy(lb *core.LoadBalancerStatus) *core.LoadBalancerStatus {
|
|
|
|
c := &core.LoadBalancerStatus{}
|
|
|
|
c.Ingress = make([]core.LoadBalancerIngress, len(lb.Ingress))
|
2015-05-22 21:33:29 +00:00
|
|
|
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.
|
2017-10-09 15:58:37 +00:00
|
|
|
func GetAccessModesAsString(modes []core.PersistentVolumeAccessMode) string {
|
2015-07-13 19:10:04 +00:00
|
|
|
modes = removeDuplicateAccessModes(modes)
|
|
|
|
modesStr := []string{}
|
2017-10-09 15:58:37 +00:00
|
|
|
if containsAccessMode(modes, core.ReadWriteOnce) {
|
2015-07-13 19:10:04 +00:00
|
|
|
modesStr = append(modesStr, "RWO")
|
|
|
|
}
|
2017-10-09 15:58:37 +00:00
|
|
|
if containsAccessMode(modes, core.ReadOnlyMany) {
|
2015-07-13 19:10:04 +00:00
|
|
|
modesStr = append(modesStr, "ROX")
|
|
|
|
}
|
2017-10-09 15:58:37 +00:00
|
|
|
if containsAccessMode(modes, core.ReadWriteMany) {
|
2015-07-13 19:10:04 +00:00
|
|
|
modesStr = append(modesStr, "RWX")
|
|
|
|
}
|
|
|
|
return strings.Join(modesStr, ",")
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAccessModesAsString returns an array of AccessModes from a string created by GetAccessModesAsString
|
2017-10-09 15:58:37 +00:00
|
|
|
func GetAccessModesFromString(modes string) []core.PersistentVolumeAccessMode {
|
2015-07-13 19:10:04 +00:00
|
|
|
strmodes := strings.Split(modes, ",")
|
2017-10-09 15:58:37 +00:00
|
|
|
accessModes := []core.PersistentVolumeAccessMode{}
|
2015-07-13 19:10:04 +00:00
|
|
|
for _, s := range strmodes {
|
|
|
|
s = strings.Trim(s, " ")
|
|
|
|
switch {
|
|
|
|
case s == "RWO":
|
2017-10-09 15:58:37 +00:00
|
|
|
accessModes = append(accessModes, core.ReadWriteOnce)
|
2015-07-13 19:10:04 +00:00
|
|
|
case s == "ROX":
|
2017-10-09 15:58:37 +00:00
|
|
|
accessModes = append(accessModes, core.ReadOnlyMany)
|
2015-07-13 19:10:04 +00:00
|
|
|
case s == "RWX":
|
2017-10-09 15:58:37 +00:00
|
|
|
accessModes = append(accessModes, core.ReadWriteMany)
|
2015-07-13 19:10:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return accessModes
|
|
|
|
}
|
|
|
|
|
|
|
|
// removeDuplicateAccessModes returns an array of access modes without any duplicates
|
2017-10-09 15:58:37 +00:00
|
|
|
func removeDuplicateAccessModes(modes []core.PersistentVolumeAccessMode) []core.PersistentVolumeAccessMode {
|
|
|
|
accessModes := []core.PersistentVolumeAccessMode{}
|
2015-07-13 19:10:04 +00:00
|
|
|
for _, m := range modes {
|
|
|
|
if !containsAccessMode(accessModes, m) {
|
|
|
|
accessModes = append(accessModes, m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return accessModes
|
|
|
|
}
|
|
|
|
|
2017-10-09 15:58:37 +00:00
|
|
|
func containsAccessMode(modes []core.PersistentVolumeAccessMode, mode core.PersistentVolumeAccessMode) bool {
|
2015-07-13 19:10:04 +00:00
|
|
|
for _, m := range modes {
|
|
|
|
if m == mode {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2015-09-10 03:46:11 +00:00
|
|
|
|
2017-10-09 15:58:37 +00:00
|
|
|
// NodeSelectorRequirementsAsSelector converts the []NodeSelectorRequirement core type into a struct that implements
|
2016-01-26 23:03:18 +00:00
|
|
|
// labels.Selector.
|
2017-10-09 15:58:37 +00:00
|
|
|
func NodeSelectorRequirementsAsSelector(nsm []core.NodeSelectorRequirement) (labels.Selector, error) {
|
2016-01-26 23:03:18 +00:00
|
|
|
if len(nsm) == 0 {
|
|
|
|
return labels.Nothing(), nil
|
|
|
|
}
|
|
|
|
selector := labels.NewSelector()
|
|
|
|
for _, expr := range nsm {
|
2016-08-15 22:36:37 +00:00
|
|
|
var op selection.Operator
|
2016-01-26 23:03:18 +00:00
|
|
|
switch expr.Operator {
|
2017-10-09 15:58:37 +00:00
|
|
|
case core.NodeSelectorOpIn:
|
2016-08-15 22:36:37 +00:00
|
|
|
op = selection.In
|
2017-10-09 15:58:37 +00:00
|
|
|
case core.NodeSelectorOpNotIn:
|
2016-08-15 22:36:37 +00:00
|
|
|
op = selection.NotIn
|
2017-10-09 15:58:37 +00:00
|
|
|
case core.NodeSelectorOpExists:
|
2016-08-15 22:36:37 +00:00
|
|
|
op = selection.Exists
|
2017-10-09 15:58:37 +00:00
|
|
|
case core.NodeSelectorOpDoesNotExist:
|
2016-08-15 22:36:37 +00:00
|
|
|
op = selection.DoesNotExist
|
2017-10-09 15:58:37 +00:00
|
|
|
case core.NodeSelectorOpGt:
|
2016-08-15 22:36:37 +00:00
|
|
|
op = selection.GreaterThan
|
2017-10-09 15:58:37 +00:00
|
|
|
case core.NodeSelectorOpLt:
|
2016-08-15 22:36:37 +00:00
|
|
|
op = selection.LessThan
|
2016-01-26 23:03:18 +00:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("%q is not a valid node selector operator", expr.Operator)
|
|
|
|
}
|
2016-10-20 09:46:03 +00:00
|
|
|
r, err := labels.NewRequirement(expr.Key, op, expr.Values)
|
2016-01-26 23:03:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
selector = selector.Add(*r)
|
|
|
|
}
|
|
|
|
return selector, nil
|
|
|
|
}
|
|
|
|
|
2017-03-22 04:53:34 +00:00
|
|
|
// GetTolerationsFromPodAnnotations gets the json serialized tolerations data from Pod.Annotations
|
2017-10-09 15:58:37 +00:00
|
|
|
// and converts it to the []Toleration type in core.
|
|
|
|
func GetTolerationsFromPodAnnotations(annotations map[string]string) ([]core.Toleration, error) {
|
|
|
|
var tolerations []core.Toleration
|
|
|
|
if len(annotations) > 0 && annotations[core.TolerationsAnnotationKey] != "" {
|
|
|
|
err := json.Unmarshal([]byte(annotations[core.TolerationsAnnotationKey]), &tolerations)
|
2017-03-22 04:53:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return tolerations, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tolerations, nil
|
|
|
|
}
|
|
|
|
|
2017-03-08 21:22:39 +00:00
|
|
|
// AddOrUpdateTolerationInPod tries to add a toleration to the pod's toleration list.
|
|
|
|
// Returns true if something was updated, false otherwise.
|
2017-10-09 15:58:37 +00:00
|
|
|
func AddOrUpdateTolerationInPod(pod *core.Pod, toleration *core.Toleration) bool {
|
2017-03-08 21:22:39 +00:00
|
|
|
podTolerations := pod.Spec.Tolerations
|
|
|
|
|
2017-10-09 15:58:37 +00:00
|
|
|
var newTolerations []core.Toleration
|
2017-03-08 21:22:39 +00:00
|
|
|
updated := false
|
|
|
|
for i := range podTolerations {
|
|
|
|
if toleration.MatchToleration(&podTolerations[i]) {
|
|
|
|
if Semantic.DeepEqual(toleration, podTolerations[i]) {
|
2017-03-13 14:37:41 +00:00
|
|
|
return false
|
2017-03-08 21:22:39 +00:00
|
|
|
}
|
|
|
|
newTolerations = append(newTolerations, *toleration)
|
|
|
|
updated = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
newTolerations = append(newTolerations, podTolerations[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
if !updated {
|
|
|
|
newTolerations = append(newTolerations, *toleration)
|
|
|
|
}
|
|
|
|
|
|
|
|
pod.Spec.Tolerations = newTolerations
|
2017-03-13 14:37:41 +00:00
|
|
|
return true
|
2017-03-08 21:22:39 +00:00
|
|
|
}
|
|
|
|
|
2017-03-22 05:01:49 +00:00
|
|
|
// GetTaintsFromNodeAnnotations gets the json serialized taints data from Pod.Annotations
|
2017-10-09 15:58:37 +00:00
|
|
|
// and converts it to the []Taint type in core.
|
|
|
|
func GetTaintsFromNodeAnnotations(annotations map[string]string) ([]core.Taint, error) {
|
|
|
|
var taints []core.Taint
|
|
|
|
if len(annotations) > 0 && annotations[core.TaintsAnnotationKey] != "" {
|
|
|
|
err := json.Unmarshal([]byte(annotations[core.TaintsAnnotationKey]), &taints)
|
2017-03-22 05:01:49 +00:00
|
|
|
if err != nil {
|
2017-10-09 15:58:37 +00:00
|
|
|
return []core.Taint{}, err
|
2017-03-22 05:01:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return taints, nil
|
|
|
|
}
|
|
|
|
|
2016-06-14 13:09:53 +00:00
|
|
|
// SysctlsFromPodAnnotations parses the sysctl annotations into a slice of safe Sysctls
|
|
|
|
// and a slice of unsafe Sysctls. This is only a convenience wrapper around
|
|
|
|
// SysctlsFromPodAnnotation.
|
2017-10-09 15:58:37 +00:00
|
|
|
func SysctlsFromPodAnnotations(a map[string]string) ([]core.Sysctl, []core.Sysctl, error) {
|
|
|
|
safe, err := SysctlsFromPodAnnotation(a[core.SysctlsPodAnnotationKey])
|
2016-06-14 13:09:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2017-10-09 15:58:37 +00:00
|
|
|
unsafe, err := SysctlsFromPodAnnotation(a[core.UnsafeSysctlsPodAnnotationKey])
|
2016-06-14 13:09:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return safe, unsafe, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SysctlsFromPodAnnotation parses an annotation value into a slice of Sysctls.
|
2017-10-09 15:58:37 +00:00
|
|
|
func SysctlsFromPodAnnotation(annotation string) ([]core.Sysctl, error) {
|
2016-06-14 13:09:53 +00:00
|
|
|
if len(annotation) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
kvs := strings.Split(annotation, ",")
|
2017-10-09 15:58:37 +00:00
|
|
|
sysctls := make([]core.Sysctl, len(kvs))
|
2016-06-14 13:09:53 +00:00
|
|
|
for i, kv := range kvs {
|
|
|
|
cs := strings.Split(kv, "=")
|
2016-09-30 09:07:04 +00:00
|
|
|
if len(cs) != 2 || len(cs[0]) == 0 {
|
2016-06-14 13:09:53 +00:00
|
|
|
return nil, fmt.Errorf("sysctl %q not of the format sysctl_name=value", kv)
|
|
|
|
}
|
|
|
|
sysctls[i].Name = cs[0]
|
|
|
|
sysctls[i].Value = cs[1]
|
|
|
|
}
|
|
|
|
return sysctls, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PodAnnotationsFromSysctls creates an annotation value for a slice of Sysctls.
|
2017-10-09 15:58:37 +00:00
|
|
|
func PodAnnotationsFromSysctls(sysctls []core.Sysctl) string {
|
2016-06-14 13:09:53 +00:00
|
|
|
if len(sysctls) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
kvs := make([]string, len(sysctls))
|
|
|
|
for i := range sysctls {
|
|
|
|
kvs[i] = fmt.Sprintf("%s=%s", sysctls[i].Name, sysctls[i].Value)
|
|
|
|
}
|
|
|
|
return strings.Join(kvs, ",")
|
|
|
|
}
|
2017-03-02 09:23:55 +00:00
|
|
|
|
|
|
|
// GetPersistentVolumeClass returns StorageClassName.
|
2017-10-09 15:58:37 +00:00
|
|
|
func GetPersistentVolumeClass(volume *core.PersistentVolume) string {
|
2017-03-02 09:23:55 +00:00
|
|
|
// Use beta annotation first
|
2017-10-09 15:58:37 +00:00
|
|
|
if class, found := volume.Annotations[core.BetaStorageClassAnnotation]; found {
|
2017-03-02 09:23:55 +00:00
|
|
|
return class
|
|
|
|
}
|
|
|
|
|
|
|
|
return volume.Spec.StorageClassName
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPersistentVolumeClaimClass returns StorageClassName. If no storage class was
|
|
|
|
// requested, it returns "".
|
2017-10-09 15:58:37 +00:00
|
|
|
func GetPersistentVolumeClaimClass(claim *core.PersistentVolumeClaim) string {
|
2017-03-02 09:23:55 +00:00
|
|
|
// Use beta annotation first
|
2017-10-09 15:58:37 +00:00
|
|
|
if class, found := claim.Annotations[core.BetaStorageClassAnnotation]; found {
|
2017-03-02 09:23:55 +00:00
|
|
|
return class
|
|
|
|
}
|
|
|
|
|
|
|
|
if claim.Spec.StorageClassName != nil {
|
|
|
|
return *claim.Spec.StorageClassName
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// PersistentVolumeClaimHasClass returns true if given claim has set StorageClassName field.
|
2017-10-09 15:58:37 +00:00
|
|
|
func PersistentVolumeClaimHasClass(claim *core.PersistentVolumeClaim) bool {
|
2017-03-02 09:23:55 +00:00
|
|
|
// Use beta annotation first
|
2017-10-09 15:58:37 +00:00
|
|
|
if _, found := claim.Annotations[core.BetaStorageClassAnnotation]; found {
|
2017-03-02 09:23:55 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if claim.Spec.StorageClassName != nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
2017-04-18 05:24:24 +00:00
|
|
|
|
|
|
|
// GetStorageNodeAffinityFromAnnotation gets the json serialized data from PersistentVolume.Annotations
|
2017-10-09 15:58:37 +00:00
|
|
|
// and converts it to the NodeAffinity type in core.
|
2017-04-18 05:24:24 +00:00
|
|
|
// TODO: update when storage node affinity graduates to beta
|
2017-10-09 15:58:37 +00:00
|
|
|
func GetStorageNodeAffinityFromAnnotation(annotations map[string]string) (*core.NodeAffinity, error) {
|
|
|
|
if len(annotations) > 0 && annotations[core.AlphaStorageNodeAffinityAnnotation] != "" {
|
|
|
|
var affinity core.NodeAffinity
|
|
|
|
err := json.Unmarshal([]byte(annotations[core.AlphaStorageNodeAffinityAnnotation]), &affinity)
|
2017-04-18 05:24:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &affinity, nil
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Converts NodeAffinity type to Alpha annotation for use in PersistentVolumes
|
|
|
|
// TODO: update when storage node affinity graduates to beta
|
2017-10-09 15:58:37 +00:00
|
|
|
func StorageNodeAffinityToAlphaAnnotation(annotations map[string]string, affinity *core.NodeAffinity) error {
|
2017-05-22 22:30:27 +00:00
|
|
|
if affinity == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-04-18 05:24:24 +00:00
|
|
|
json, err := json.Marshal(*affinity)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-10-09 15:58:37 +00:00
|
|
|
annotations[core.AlphaStorageNodeAffinityAnnotation] = string(json)
|
2017-04-18 05:24:24 +00:00
|
|
|
return nil
|
|
|
|
}
|