Skip to content

Commit

Permalink
Merge pull request #61 from flanksource/moshloop
Browse files Browse the repository at this point in the history
add  GetDNS1192Name
  • Loading branch information
moshloop authored Jul 25, 2021
2 parents a764e60 + 1c47d02 commit c1656e5
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
7 changes: 6 additions & 1 deletion apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/flanksource/commons/console"
perrors "github.com/pkg/errors"
"github.com/sergi/go-diff/diffmatchpatch"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down Expand Up @@ -105,11 +106,15 @@ func (c *Client) DeleteUnstructured(namespace string, objects ...*unstructured.U
if err != nil {
return err
}
_namespace := namespace
if _namespace == v1.NamespaceAll {
_namespace = unstructuredObj.GetNamespace()
}

if c.ApplyDryRun {
c.Infof("[dry-run] %s %s", GetName(unstructuredObj), deleted)
} else {
if _, err := client.Delete(namespace, unstructuredObj.GetName()); err != nil {
if _, err := client.Delete(_namespace, unstructuredObj.GetName()); err != nil {
return err
}
c.Infof("%s %s", GetName(unstructuredObj), deleted)
Expand Down
39 changes: 39 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,45 @@ func RemoveTaint(taints []v1.Taint, name string) []v1.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 {
Expand Down

0 comments on commit c1656e5

Please sign in to comment.