-
Notifications
You must be signed in to change notification settings - Fork 7
/
utils.go
757 lines (657 loc) · 19.1 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
package kommons
import (
"bytes"
"encoding/json"
"fmt"
"reflect"
"regexp"
"strings"
"time"
"github.com/flanksource/commons/console"
apps "k8s.io/api/apps/v1"
appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
networking "k8s.io/api/networking/v1"
rbac "k8s.io/api/rbac/v1"
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
yamlutil "k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/client-go/tools/clientcmd"
"sigs.k8s.io/yaml"
)
var KustomizedLabel = "kustomize/patched"
func IsKustomized(to *unstructured.Unstructured) bool {
if _, ok := to.GetAnnotations()[KustomizedLabel]; ok {
return true
}
return false
}
func ToJson(to *unstructured.Unstructured) string {
if IsNil(to) {
return ""
}
data, _ := to.MarshalJSON()
return string(data)
}
func ToYaml(to *unstructured.Unstructured) string {
if IsNil(to) {
return ""
}
data, _ := yaml.Marshal(to)
return string(data)
}
func AsDeployment(obj *unstructured.Unstructured) (*appsv1.Deployment, error) {
if IsNil(obj) {
return nil, nil
}
var deployment appsv1.Deployment
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &deployment); err != nil {
return nil, err
}
return &deployment, nil
}
func AsStatefulSet(obj *unstructured.Unstructured) (*appsv1.StatefulSet, error) {
if IsNil(obj) {
return nil, nil
}
var sts appsv1.StatefulSet
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &sts); err != nil {
return nil, err
}
return &sts, nil
}
func AsSecret(obj *unstructured.Unstructured) (*v1.Secret, error) {
if IsNil(obj) {
return nil, nil
}
var secret v1.Secret
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &secret); err != nil {
return nil, err
}
return &secret, nil
}
func AsService(obj *unstructured.Unstructured) (*v1.Service, error) {
if IsNil(obj) {
return nil, nil
}
var svc v1.Service
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &svc); err != nil {
return nil, err
}
return &svc, nil
}
func AsIngress(obj *unstructured.Unstructured) (*networking.Ingress, error) {
if IsNil(obj) {
return nil, nil
}
var ing networking.Ingress
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &ing); err != nil {
return nil, err
}
return &ing, nil
}
func AsRoleBinding(obj *unstructured.Unstructured) (*rbac.RoleBinding, error) {
if IsNil(obj) {
return nil, nil
}
var rb rbac.RoleBinding
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &rb); err != nil {
return nil, err
}
return &rb, nil
}
func AsClusterRoleBinding(obj *unstructured.Unstructured) (*rbac.ClusterRoleBinding, error) {
if IsNil(obj) {
return nil, nil
}
var crb rbac.ClusterRoleBinding
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &crb); err != nil {
return nil, err
}
return &crb, nil
}
func AsDaemonSet(obj *unstructured.Unstructured) (*appsv1.DaemonSet, error) {
if IsNil(obj) {
return nil, nil
}
var daemonset appsv1.DaemonSet
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &daemonset); err != nil {
return nil, err
}
return &daemonset, nil
}
func AsCustomResourceDefinition(obj *unstructured.Unstructured) (*apiextensions.CustomResourceDefinition, error) {
if IsNil(obj) {
return nil, nil
}
var crd apiextensions.CustomResourceDefinition
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &crd); err != nil {
return nil, err
}
return &crd, nil
}
func AsCustomResourceDefinitionV1Beta1(obj *unstructured.Unstructured) (*apiextensionsv1beta1.CustomResourceDefinition, error) {
if IsNil(obj) {
return nil, nil
}
var crd apiextensionsv1beta1.CustomResourceDefinition
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &crd); err != nil {
return nil, err
}
return &crd, nil
}
func AsPodTemplate(obj *unstructured.Unstructured) (*v1.PodTemplateSpec, error) {
if IsNil(obj) {
return nil, nil
}
var spec v1.PodTemplateSpec
template, _, err := unstructured.NestedMap(obj.Object, "spec", "template")
if err != nil {
return nil, err
}
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(template, &spec); err != nil {
return nil, err
}
return &spec, nil
}
func UnwrapError(err error) string {
if err == nil {
return ""
}
switch err.(type) {
case *errors.StatusError:
return err.(*errors.StatusError).ErrStatus.Message
}
return err.Error()
}
func IsAPIResourceMissing(err error) bool {
msg := UnwrapError(err)
if msg == "" {
return false
}
return strings.Contains(msg, "the server could not find the requested resource") ||
strings.Contains(msg, "no matches for kind") ||
(strings.Contains(msg, "is not recognized") && strings.Contains(msg, "kind"))
}
func GetValidName(name string) string {
return strings.ReplaceAll(name, "_", "-")
}
func GetName(obj interface{}) Name {
name := Name{}
switch obj.(type) {
case *unstructured.Unstructured:
object := obj.(*unstructured.Unstructured)
if object == nil || object.Object == nil {
return name
}
name.Name = object.GetName()
name.Namespace = object.GetNamespace()
case metav1.ObjectMetaAccessor:
object := obj.(metav1.ObjectMetaAccessor).GetObjectMeta()
name.Name = object.GetName()
name.Namespace = object.GetNamespace()
}
switch obj.(type) {
case *unstructured.Unstructured:
object := obj.(*unstructured.Unstructured)
if object == nil || object.Object == nil {
return name
}
name.Kind = object.GetKind()
default:
if t := reflect.TypeOf(obj); t.Kind() == reflect.Ptr {
name.Kind = t.Elem().Name()
} else {
name.Kind = t.Name()
}
}
return name
}
type Name struct {
Name, Kind, Namespace string
}
func (n Name) String() string {
if n.Namespace == "" {
return fmt.Sprintf("%s/%s/%s", console.Bluef(n.Kind), console.Grayf("*"), console.LightWhitef(n.Name))
}
return fmt.Sprintf("%s/%s/%s", console.Bluef(n.Kind), console.Grayf(n.Namespace), console.LightWhitef(n.Name))
}
func (n Name) GetName() string {
return n.Name
}
func (n Name) GetKind() string {
return n.Kind
}
func (n Name) GetNamespace() string {
return n.Namespace
}
type Kindable interface {
GetKind() string
}
type Nameable interface {
GetName() string
GetNamespace() string
}
func Validate(object runtime.Object) error {
switch object.(type) {
case *unstructured.Unstructured:
obj := object.(*unstructured.Unstructured)
if obj == nil {
return fmt.Errorf("empty pointer")
}
if obj.GetKind() == "" {
return fmt.Errorf("%s/%s does not have a kind", obj.GetNamespace(), obj.GetName())
}
if obj.GetAPIVersion() == "" {
return fmt.Errorf("%s/%s/%s does not have an apiVersion", obj.GetNamespace(), obj.GetKind(), obj.GetName())
}
}
return nil
}
func IsNil(object runtime.Object) bool {
if object == nil {
return true
}
switch object.(type) {
case *unstructured.Unstructured:
obj := object.(*unstructured.Unstructured)
if obj == nil {
return true
}
}
return false
}
func IsConfigMap(obj *unstructured.Unstructured) bool {
return obj.GetKind() == "ConfigMap"
}
func IsSecret(obj *unstructured.Unstructured) bool {
return obj.GetKind() == "Secret"
}
func IsPVC(obj *unstructured.Unstructured) bool {
return obj.GetKind() == "PersistentVolumeClaim"
}
func IsService(obj *unstructured.Unstructured) bool {
return obj.GetKind() == "Service"
}
func IsServiceAccount(obj *unstructured.Unstructured) bool {
return obj.GetKind() == "ServiceAccount"
}
func IsIngress(obj *unstructured.Unstructured) bool {
return obj.GetKind() == "Ingress"
}
// IsApp returns true if the obj is a Deployment, Statetefulset or DaemonSet
func IsApp(obj *unstructured.Unstructured) bool {
return IsStatefulSet(obj) || IsDeployment(obj) || IsDaemonSet(obj)
}
func IsDeployment(obj *unstructured.Unstructured) bool {
return obj.GetKind() == "Deployment"
}
func IsDaemonSet(obj *unstructured.Unstructured) bool {
return obj.GetKind() == "DaemonSet"
}
func IsStatefulSet(obj *unstructured.Unstructured) bool {
return obj.GetKind() == "StatefulSet"
}
func IsAnyRoleBinding(obj *unstructured.Unstructured) bool {
return IsRoleBinding(obj) || IsClusterRoleBinding(obj)
}
func IsRoleBinding(obj *unstructured.Unstructured) bool {
return obj.GetKind() == "RoleBinding"
}
func IsClusterRoleBinding(obj *unstructured.Unstructured) bool {
return obj.GetKind() == "ClusterRoleBinding"
}
func IsClusterRole(obj *unstructured.Unstructured) bool {
return obj.GetKind() == "ClusterRole"
}
func IsRole(obj *unstructured.Unstructured) bool {
return obj.GetKind() == "Role"
}
func IsCronJob(obj *unstructured.Unstructured) bool {
return obj.GetKind() == "CronJob"
}
func IsCanary(obj *unstructured.Unstructured) bool {
return obj.GetKind() == "Canary"
}
func IsCustomResourceDefinitionV1Beta1(obj *unstructured.Unstructured) bool {
return obj.GetKind() == "CustomResourceDefinition" && obj.GetAPIVersion() == "apiextensions.k8s.io/v1beta1"
}
func IsCustomResourceDefinition(obj *unstructured.Unstructured) bool {
return obj.GetKind() == "CustomResourceDefinition"
}
func IsConstraintTemplate(obj *unstructured.Unstructured) bool {
return obj.GetKind() == "ConstraintTemplate"
}
func IsElasticsearch(obj *unstructured.Unstructured) bool {
return obj.GetKind() == "Elasticsearch"
}
func IsKibana(obj *unstructured.Unstructured) bool {
return obj.GetKind() == "Kibana"
}
func IsRedisFailover(obj *unstructured.Unstructured) bool {
return obj.GetKind() == "RedisFailover"
}
func IsPostgresql(obj *unstructured.Unstructured) bool {
return strings.ToLower(obj.GetKind()) == "postgresql"
}
func IsPostgresqlDB(obj *unstructured.Unstructured) bool {
return strings.ToLower(obj.GetKind()) == "postgresqldb"
}
func IsMongoDB(obj *unstructured.Unstructured) bool {
return obj.GetKind() == "PerconaServerMongoDB"
}
func IsKafka(obj *unstructured.Unstructured) bool {
return obj.GetKind() == "Kafka"
}
func IsBuilder(obj *unstructured.Unstructured) bool {
return obj.GetKind() == "Builder"
}
func IsImage(obj *unstructured.Unstructured) bool {
return obj.GetKind() == "Image"
}
func IsNode(obj *unstructured.Unstructured) bool {
return obj.GetKind() == "Node"
}
func IsPod(obj *unstructured.Unstructured) bool {
return obj.GetKind() == "Pod"
}
func NewDeployment(ns, name, image string, labels map[string]string, port int32, args ...string) *apps.Deployment {
if labels == nil {
labels = make(map[string]string)
}
if len(labels) == 0 {
labels["app"] = name
}
replicas := int32(1)
deployment := apps.Deployment{
ObjectMeta: NewObjectMeta(ns, name),
Spec: apps.DeploymentSpec{
Replicas: &replicas,
Selector: &metav1.LabelSelector{
MatchLabels: labels,
},
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: labels,
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: name,
Image: image,
ImagePullPolicy: "IfNotPresent",
Ports: []v1.ContainerPort{
v1.ContainerPort{
ContainerPort: port,
},
},
Args: args,
Resources: LowResourceRequirements(),
},
},
},
},
},
}
deployment.Kind = "Deployment"
deployment.APIVersion = "apps/v1"
return &deployment
}
func NewObjectMeta(ns, name string) metav1.ObjectMeta {
return metav1.ObjectMeta{
Name: name,
Namespace: ns,
}
}
func LowResourceRequirements() v1.ResourceRequirements {
return v1.ResourceRequirements{
Limits: v1.ResourceList{
v1.ResourceMemory: resource.MustParse("512Mi"),
v1.ResourceCPU: resource.MustParse("500m"),
},
Requests: v1.ResourceList{
v1.ResourceMemory: resource.MustParse("128Mi"),
v1.ResourceCPU: resource.MustParse("10m"),
},
}
}
func IsPodCrashLoopBackoff(pod v1.Pod) bool {
for _, status := range pod.Status.ContainerStatuses {
if status.State.Waiting != nil && status.State.Waiting.Reason == "CrashLoopBackOff" {
return true
}
}
return false
}
func maxTime(t1 *time.Time, t2 time.Time) *time.Time {
if t1 == nil {
return &t2
}
if t1.Before(t2) {
return &t2
}
return t1
}
func GetPodStatus(pod v1.Pod) string {
if IsPodCrashLoopBackoff(pod) {
return "CrashLoopBackOff"
}
if pod.Status.Phase == v1.PodFailed {
return "Failed"
}
if pod.DeletionTimestamp != nil && !pod.DeletionTimestamp.IsZero() {
return "Terminating"
}
return string(pod.Status.Phase)
}
func GetLastRestartTime(pod v1.Pod) *time.Time {
var max *time.Time
for _, status := range pod.Status.ContainerStatuses {
if status.LastTerminationState.Terminated != nil {
max = maxTime(max, status.LastTerminationState.Terminated.FinishedAt.Time)
}
}
return max
}
func GetContainerStatus(pod v1.Pod) string {
if IsPodHealthy(pod) {
return ""
}
msg := ""
for _, status := range pod.Status.ContainerStatuses {
if status.State.Terminated != nil {
terminated := status.State.Terminated
msg += fmt.Sprintf("%s exit(%s):, %s %s", console.Bluef(status.Name), console.Redf("%d", terminated.ExitCode), terminated.Reason, console.DarkF(terminated.Message))
} else if status.LastTerminationState.Terminated != nil {
terminated := status.LastTerminationState.Terminated
msg += fmt.Sprintf("%s exit(%s): %s %s", console.Bluef(status.Name), console.Redf("%d", terminated.ExitCode), terminated.Reason, console.DarkF(terminated.Message))
}
}
return msg
}
func IsPodHealthy(pod v1.Pod) bool {
if pod.Status.Phase == v1.PodSucceeded {
for _, status := range pod.Status.ContainerStatuses {
if status.State.Terminated != nil && status.State.Terminated.ExitCode != 0 {
return false
}
}
return true
}
if pod.Status.Phase == v1.PodFailed || IsPodCrashLoopBackoff(pod) {
return false
}
for _, condition := range pod.Status.Conditions {
if condition.Status == v1.ConditionFalse {
return false
}
}
return pod.Status.Phase == v1.PodRunning
}
func IsPodFinished(pod v1.Pod) bool {
return pod.Status.Phase == v1.PodSucceeded || pod.Status.Phase == v1.PodFailed
}
func IsPodPending(pod v1.Pod) bool {
return pod.Status.Phase == v1.PodPending
}
func IsPodReady(pod v1.Pod) bool {
for _, condition := range pod.Status.Conditions {
if condition.Type == v1.PodReady && condition.Status == v1.ConditionTrue {
return true
}
}
return false
}
func IsMasterNode(node v1.Node) bool {
_, ok := node.Labels["node-role.kubernetes.io/master"]
return ok
}
func IsDeleted(object metav1.Object) bool {
return object.GetDeletionTimestamp() != nil && !object.GetDeletionTimestamp().IsZero()
}
func IsPodDaemonSet(pod v1.Pod) bool {
controllerRef := metav1.GetControllerOf(&pod)
return controllerRef != nil && controllerRef.Kind == apps.SchemeGroupVersion.WithKind("DaemonSet").Kind
}
// IsStaticPod returns true if the pod is static i.e. declared in /etc/kubernetes/manifests and read directly by the kubelet
func IsStaticPod(pod v1.Pod) bool {
for _, owner := range pod.GetOwnerReferences() {
if owner.Kind == "Node" {
return true
}
}
return false
}
func GetNodeStatus(node v1.Node) string {
s := ""
for _, condition := range node.Status.Conditions {
if condition.Status == v1.ConditionFalse {
continue
}
if s != "" {
s += ", "
}
s += string(condition.Type)
}
return s
}
type Health struct {
RunningPods, PendingPods, ErrorPods, CrashLoopBackOff int
ReadyNodes, UnreadyNodes int
Error error
}
func (h Health) GetNonReadyPods() int {
return h.PendingPods + h.ErrorPods + h.CrashLoopBackOff
}
func (h Health) IsDegradedComparedTo(h2 Health, tolerance int) bool {
if h.GetNonReadyPods()-h2.GetNonReadyPods() > tolerance {
return true
}
if h2.RunningPods-h.RunningPods > tolerance {
return true
}
if h.UnreadyNodes-h2.UnreadyNodes > 0 {
return true
}
return false
}
func (h Health) String() string {
return fmt.Sprintf("pods(running=%d, pending=%s, crashloop=%s, error=%s) nodes(ready=%d, notready=%s)",
h.RunningPods, console.Yellowf("%d", h.PendingPods), console.Redf("%d", h.CrashLoopBackOff), console.Redf("%d", h.ErrorPods), h.ReadyNodes, console.Redf("%d", h.UnreadyNodes))
}
func GetUnstructuredObjects(data []byte) ([]*unstructured.Unstructured, error) {
var items []*unstructured.Unstructured
re := regexp.MustCompile("(?m)^---\\n")
for _, chunk := range re.Split(string(data), -1) {
if strings.TrimSpace(chunk) == "" {
continue
}
decoder := yamlutil.NewYAMLOrJSONDecoder(bytes.NewReader([]byte(chunk)), 1024)
var resource *unstructured.Unstructured
if err := decoder.Decode(&resource); err != nil {
return nil, fmt.Errorf("error decoding %s: %s", chunk, err)
}
if resource != nil {
items = append(items, resource)
}
}
return items, nil
}
func GetUnstructuredObjectsFromJson(data []byte) ([]*unstructured.Unstructured, error) {
var items []*unstructured.Unstructured
if err := json.Unmarshal(data, &items); err != nil {
return nil, err
}
return items, nil
}
// GetCurrentClusterNameFrom returns the name of the cluster associated with the currentContext of the
// specified kubeconfig file
func GetCurrentClusterNameFrom(kubeConfigPath string) string {
config, err := clientcmd.LoadFromFile(kubeConfigPath)
if err != nil {
return err.Error()
}
ctx, ok := config.Contexts[config.CurrentContext]
if !ok {
return fmt.Sprintf("invalid context name: %s", config.CurrentContext)
}
// we strip the prefix that kind automatically adds to cluster names
return strings.Replace(ctx.Cluster, "kind-", "", 1)
}
func RemoveTaint(taints []v1.Taint, name string) []v1.Taint {
list := []v1.Taint{}
for _, taint := range taints {
if taint.Key != name {
list = append(list, taint)
}
}
return list
}
var regexpNonAuthorizedChars = regexp.MustCompile("[^a-zA-Z0-9-.]")
var regexpMultipleDashes = regexp.MustCompile("-+")
var dns1192MaxLength = 40
// GetDNS1192Name trasforms a name into a DNS-1192 compliant name
func GetDNS1192Name(s string) string {
// Adapted from:
// Copyright 2013 by Dobrosław Żybort. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
slug := strings.TrimSpace(s)
slug = strings.ToLower(slug)
// Process all remaining symbols
slug = regexpNonAuthorizedChars.ReplaceAllString(slug, "-")
slug = regexpMultipleDashes.ReplaceAllString(slug, "-")
slug = strings.Trim(slug, "-_")
if len(slug) > dns1192MaxLength {
var truncated string
words := strings.SplitAfter(slug, "-")
// If MaxLength is smaller than length of the first word return word
// truncated after MaxLength.
if len(words[0]) > dns1192MaxLength {
return words[0][:dns1192MaxLength]
}
for _, word := range words {
if len(truncated)+len(word)-1 <= dns1192MaxLength {
truncated = truncated + word
} else {
break
}
}
return strings.Trim(truncated, "-")
}
return slug
}
func HasTaint(node v1.Node, name string) bool {
for _, taint := range node.Spec.Taints {
if taint.Key == name {
return true
}
}
return false
}