-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add managed by label to namespace (#604)
# Description When creating a namespace object in Kubernetes, add label “{prefix}/managed: true”. This will help when we want to modify resources created by CaraML services in a shared cluster, e.g. configuring log for the managed namespace only By default the namespace prefix would `caraml.dev/`, and it can be configured in the config yaml # Modifications - As the creation of label for Kubernetes’ object in Merlin previously is bind to metadata struct, while namespace doesn’t have metada, in this changes the functionality of labelling (labeller) is moved to it’s own package - Add one more prefix in `InitKubernetesLabeller` initiation - Add the `{prefix}/managed: true` when creating namespace # Tests # Checklist - [x] Added PR label - [ ] Added unit test, integration, and/or e2e tests - [x] Tested locally - [ ] Updated documentation - [ ] Update Swagger spec if the PR introduce API changes - [ ] Regenerated Golang and Python client if the PR introduces API changes # Release Notes ```release-note ```
- Loading branch information
Showing
18 changed files
with
203 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package labeller | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
) | ||
|
||
const ( | ||
LabelAppName = "app" | ||
LabelComponent = "component" | ||
LabelEnvironment = "environment" | ||
LabelOrchestratorName = "orchestrator" | ||
LabelStreamName = "stream" | ||
LabelTeamName = "team" | ||
LabelManaged = "managed" | ||
) | ||
|
||
var reservedKeys = map[string]bool{ | ||
LabelAppName: true, | ||
LabelComponent: true, | ||
LabelEnvironment: true, | ||
LabelOrchestratorName: true, | ||
LabelStreamName: true, | ||
LabelTeamName: true, | ||
} | ||
|
||
var ( | ||
prefix string | ||
nsPrefix string | ||
environment string | ||
validPrefixRegex = regexp.MustCompile("^(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?(/)$") | ||
) | ||
|
||
// InitKubernetesLabeller builds a new KubernetesLabeller Singleton | ||
func InitKubernetesLabeller(p, ns, e string) error { | ||
if err := isValidPrefix(p, "prefix"); err != nil { | ||
return err | ||
} | ||
if err := isValidPrefix(ns, "namespace prefix"); err != nil { | ||
return err | ||
} | ||
|
||
prefix = p | ||
nsPrefix = ns | ||
environment = e | ||
return nil | ||
} | ||
|
||
// isValidPrefix checks if the given prefix is valid | ||
func isValidPrefix(prefix, name string) error { | ||
if len(prefix) > 253 { | ||
return fmt.Errorf("length of %s is greater than 253 characters", name) | ||
} | ||
if isValidPrefix := validPrefixRegex.MatchString(prefix); !isValidPrefix { | ||
return fmt.Errorf("%s name violates kubernetes label's prefix constraint", name) | ||
} | ||
return nil | ||
} | ||
|
||
// GetLabelName prefixes the label with the config specified label and returns the formatted label prefix | ||
func GetLabelName(name string) string { | ||
return fmt.Sprintf("%s%s", prefix, name) | ||
} | ||
|
||
// GetNamespaceLabel prefixes the label with the config specified namespace label and returns the formatted label prefix | ||
func GetNamespaceLabel(name string) string { | ||
return fmt.Sprintf("%s%s", nsPrefix, name) | ||
} | ||
|
||
// GetPrefix returns the labeller prefix | ||
func GetPrefix() string { | ||
return prefix | ||
} | ||
|
||
// GetEnvironment returns the environment | ||
func GetEnvironment() string { | ||
return environment | ||
} | ||
|
||
// IsReservedKey checks if the key is a reserved key | ||
func IsReservedKey(key string) bool { | ||
_, usingReservedKeys := reservedKeys[key] | ||
return usingReservedKeys | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package labeller | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestInitKubernetesLabeller(t *testing.T) { | ||
defaultNsPrefix := "caraml.dev/" | ||
err := InitKubernetesLabeller("gojek.com/", "caraml.dev/", "dev") | ||
assert.NoError(t, err) | ||
|
||
defer func() { | ||
_ = InitKubernetesLabeller("", "", "") | ||
}() | ||
|
||
tests := []struct { | ||
prefix string | ||
wantErr bool | ||
}{ | ||
{ | ||
"gojek.com/", | ||
false, | ||
}, | ||
{ | ||
"model.caraml.dev/", | ||
false, | ||
}, | ||
{ | ||
"goto/gojek", | ||
true, | ||
}, | ||
{ | ||
"gojek", | ||
true, | ||
}, | ||
{ | ||
"gojek.com/caraml", | ||
true, | ||
}, | ||
{ | ||
"gojek//", | ||
true, | ||
}, | ||
{ | ||
"gojek.com//", | ||
true, | ||
}, | ||
{ | ||
"//gojek.com", | ||
true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.prefix, func(t *testing.T) { | ||
if err := InitKubernetesLabeller(tt.prefix, defaultNsPrefix, "dev"); (err != nil) != tt.wantErr { | ||
t.Errorf("InitKubernetesLabeller() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.