forked from Shopify/krane
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow specifying additional/default labels via command line
This change adds the ability to set additional labels or provide default values for deployment commands. This also works for ejson secrets. `ejson-keys` as shared secret will not be labled. The functionality was not made available on `krane render` due to potentially confusing behaviour around labels on secrets when using `krane render … | krane deploy -f secrets.ejson -f -` . Allowing labels specified in the templates take precedence is an intentional choice. It is the more flexible approach and allows customization for edge cases like migrations and "nested" deployments. see Shopify#682
- Loading branch information
Showing
15 changed files
with
164 additions
and
16 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
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,21 @@ | ||
# frozen_string_literal: true | ||
|
||
module Krane | ||
class ExtraLabels | ||
def self.parse(string) | ||
extra_labels = {} | ||
|
||
string.split(',').each do |kvp| | ||
key, value = kvp.split('=', 2) | ||
|
||
if key.blank? | ||
raise ArgumentError, "key is blank" | ||
end | ||
|
||
extra_labels[key] = value | ||
end | ||
|
||
extra_labels | ||
end | ||
end | ||
end |
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
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,32 @@ | ||
# frozen_string_literal: true | ||
require 'test_helper' | ||
require 'krane/extra_labels' | ||
|
||
class ExtraLabelsTest < ::Minitest::Test | ||
def test_parse_extra_labels | ||
expected = { "foo" => "42", "bar" => "17" } | ||
assert_equal(expected, parse("foo=42,bar=17")) | ||
end | ||
|
||
def test_parse_extra_labels_with_equality_sign | ||
expected = { "foo" => "1=2=3", "bar" => "3", "bla" => "4=7" } | ||
assert_equal(expected, parse("foo=1=2=3,bar=3,bla=4=7")) | ||
end | ||
|
||
def test_parse_extra_labels_with_no_value | ||
expected = { "bla" => nil, "foo" => "" } | ||
assert_equal(expected, parse("bla,foo=")) | ||
end | ||
|
||
def test_parse_extra_labels_with_no_key | ||
assert_raises(ArgumentError, "key is blank") do | ||
parse("=17,foo=42") | ||
end | ||
end | ||
|
||
private | ||
|
||
def parse(string) | ||
Krane::ExtraLabels.parse(string).to_h | ||
end | ||
end |
66 changes: 66 additions & 0 deletions
66
test/unit/krane/kubernetes_resource/kubernetes_resource_test.rb
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,66 @@ | ||
# frozen_string_literal: true | ||
require 'test_helper' | ||
|
||
class KubernetesResourceTest < Krane::TestCase | ||
def test_extra_labels | ||
[ | ||
{kind: "ConfigMap"}, | ||
{kind: "CronJob"}, | ||
{kind: "CustomResourceDefinition"}, | ||
{kind: "DaemonSet"}, | ||
{kind: "Deployment"}, | ||
{kind: "HorizontalPodAutoscaler"}, | ||
{kind: "Ingress"}, | ||
{kind: "Job"}, | ||
{kind: "NetworkPolicy"}, | ||
{kind: "PersistentVolumeClaim"}, | ||
{kind: "PodDisruptionBudget"}, | ||
{kind: "PodSetBase"}, | ||
{kind: "PodTemplate"}, | ||
{kind: "ReplicaSet"}, | ||
{kind: "ResourceQuota"}, | ||
{kind: "Role"}, | ||
{kind: "RoleBinding"}, | ||
{kind: "Secret"}, | ||
{kind: "Service"}, | ||
{kind: "ServiceAccount"}, | ||
{kind: "StatefulSet"}, | ||
|
||
{kind: "Pod", spec: {"containers" => [{"name" => "someContainer"}]}}, | ||
{kind: "SomeCustomResource", init_args: {crd: "SomeCRD"}}, | ||
{kind: "ResourceUnknownToKrane"}, | ||
].each do |resource| | ||
args = { | ||
namespace: 'test', | ||
context: 'nope', | ||
logger: @logger, | ||
statsd_tags: [], | ||
extra_labels: { | ||
"extra" => "label", | ||
"overwritten" => "yes" | ||
}, | ||
definition: { | ||
"kind" => resource.fetch(:kind), | ||
"metadata" => { | ||
"name" => "testsuite", | ||
"labels" => { | ||
"overwritten" => "no" | ||
}, | ||
}, | ||
"spec" => resource.fetch(:spec, {}) | ||
} | ||
} | ||
args.merge!(resource.fetch(:init_args, {})) | ||
|
||
resource = begin | ||
Krane::KubernetesResource.build(**args) | ||
rescue ArgumentError => e | ||
flunk("failed to build #{resource.fetch(:kind)}: #{e.message}") | ||
end | ||
|
||
assert_equal(resource.send(:labels), | ||
{"extra"=>"label", "overwritten"=>"no"}, | ||
"expected #{resource} to apply extra_labels") | ||
end | ||
end | ||
end |