Skip to content

Commit

Permalink
Labels and Annotations propagation for Secret (#596)
Browse files Browse the repository at this point in the history
* Adding the  to allow annotations copy opt-in for the kubernetes secrets

* Adding the labels and annotations of the KamusSecret

* Adding the  property for the alpha2 version tests

* Adding the labels and annotations

* Adding tests for labels and annotations copy

* Not overriding kind cluster binary

* Chaing from  to

* Bumping the version

* Adding ignore paths

* Adding the annotations without the last-applied annotation

* Removing the default value

* Fixing the test failure

* Fixing some test bugs

* Comvinging all of the deletion to one try/catch

* Removing the unused import
  • Loading branch information
AmitBenAmi committed Nov 29, 2020
1 parent 14bfb4d commit 0c26061
Show file tree
Hide file tree
Showing 12 changed files with 115 additions and 26 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Expand Up @@ -45,4 +45,8 @@ init-container/token.txt
init-container/decrypted/**

kubectl
kind
kind

docker-cache-api
src/crd-controller/*.key
src/crd-controller/*.crt
11 changes: 10 additions & 1 deletion src/crd-controller/HostedServices/V1Alpha2Controller.cs
@@ -1,5 +1,5 @@
using System;
using System.Linq;
using System.Collections.Generic;
using System.Reactive.Linq;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -147,12 +147,21 @@ private async Task<V1Secret> CreateSecret(KamusSecret kamusSecret)
}
};

IDictionary<string, string> annotations = null;
if (kamusSecret.PropagateAnnotations)
{
annotations = kamusSecret.Metadata.Annotations;
annotations.Remove("kubectl.kubernetes.io/last-applied-configuration");
}

return new V1Secret
{
Metadata = new V1ObjectMeta
{
Name = kamusSecret.Metadata.Name,
NamespaceProperty = @namespace,
Labels = kamusSecret.Metadata.Labels,
Annotations = annotations,
OwnerReferences = ownerReference
},
Type = kamusSecret.Type,
Expand Down
2 changes: 1 addition & 1 deletion src/crd-controller/Models/V1Alpha2/KamusSecret.cs
Expand Up @@ -11,6 +11,6 @@ public class KamusSecret : KubernetesObject
public string Type { get; set; }
public V1ObjectMeta Metadata { get; set; }
public string ServiceAccount { get; set; }

public bool PropagateAnnotations { get; set; }
}
}
2 changes: 1 addition & 1 deletion src/crd-controller/crd-controller.csproj
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<PropertyGroup>
<Version>0.8.0.0</Version>
<Version>0.8.1.0</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/decrypt-api/decrypt-api.csproj
Expand Up @@ -3,7 +3,7 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<PropertyGroup>
<Version>0.8.0.0</Version>
<Version>0.8.1.0</Version>
</PropertyGroup>
<ItemGroup>
<Folder Include="Models\" />
Expand Down
2 changes: 1 addition & 1 deletion src/encrypt-api/encrypt-api.csproj
Expand Up @@ -3,7 +3,7 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<PropertyGroup>
<Version>0.8.0.0</Version>
<Version>0.8.1.0</Version>
</PropertyGroup>
<ItemGroup>
<Folder Include="Models\" />
Expand Down
82 changes: 66 additions & 16 deletions tests/crd-controller/FlowTest.cs
@@ -1,6 +1,7 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Text;
Expand Down Expand Up @@ -84,6 +85,68 @@ public async Task CreateKamusSecretV1Alpha2_SecretCreated()
Assert.Equal(File.ReadAllText("key.crt"), Encoding.UTF8.GetString(v1Secret.Data["key3"]));
}

[Fact]
public async Task CreateKamusSecret_LabelsCopiedAndAnnotationsNot()
{
Cleanup();
await DeployController();
var kubernetes = new Kubernetes(KubernetesClientConfiguration.BuildDefaultConfig());

var result = await kubernetes.ListNamespacedSecretWithHttpMessagesAsync(
"default",
watch: true
);

var subject = new ReplaySubject<(WatchEventType, V1Secret)>();

result.Watch<V1Secret>(
onEvent: (@type, @event) => subject.OnNext((@type, @event)),
onError: e => subject.OnError(e),
onClosed: () => subject.OnCompleted());

RunKubectlCommand("apply -f tls-KamusSecretV1Alpha2.yaml");
mTestOutputHelper.WriteLine("Waiting for secret creation");
var (_, v1Secret) = await subject
.Where(t => t.Item1 == WatchEventType.Added && t.Item2.Metadata.Name == "my-tls-secret").Timeout(TimeSpan.FromSeconds(30)).FirstAsync();

Assert.Equal(1, v1Secret.Metadata.Labels.Count);
Assert.True(v1Secret.Metadata.Labels.Keys.Contains("key"));
Assert.Equal("value", v1Secret.Metadata.Labels.First(x => x.Key == "key").Value);
Assert.Null(v1Secret.Metadata.Annotations);
}

[Fact]
public async Task CreateKamusSecret_LabelsAndAnnotationsCopied()
{
Cleanup();
await DeployController();
var kubernetes = new Kubernetes(KubernetesClientConfiguration.BuildDefaultConfig());

var result = await kubernetes.ListNamespacedSecretWithHttpMessagesAsync(
"default",
watch: true
);

var subject = new ReplaySubject<(WatchEventType, V1Secret)>();

result.Watch<V1Secret>(
onEvent: (@type, @event) => subject.OnNext((@type, @event)),
onError: e => subject.OnError(e),
onClosed: () => subject.OnCompleted());

RunKubectlCommand("apply -f tls-KamusSecretV1Alpha2-with-annotations.yaml");
mTestOutputHelper.WriteLine("Waiting for secret creation");
var (_, v1Secret) = await subject
.Where(t => t.Item1 == WatchEventType.Added && t.Item2.Metadata.Name == "my-tls-secret").Timeout(TimeSpan.FromSeconds(30)).FirstAsync();

Assert.Equal(1, v1Secret.Metadata.Labels.Count);
Assert.True(v1Secret.Metadata.Labels.Keys.Contains("key"));
Assert.Equal("value", v1Secret.Metadata.Labels.First(x => x.Key == "key").Value);
Assert.Equal(1, v1Secret.Metadata.Annotations.Count);
Assert.True(v1Secret.Metadata.Annotations.Keys.Contains("key"));
Assert.Equal("value", v1Secret.Metadata.Annotations.First(x => x.Key == "key").Value);
}

[Theory]
[InlineData("updated-tls-KamusSecret.yaml")]
[InlineData("updated-tls-KamusSecretV1Alpha2.yaml")]
Expand Down Expand Up @@ -162,23 +225,10 @@ private void Cleanup()
try
{
RunKubectlCommand("delete -f tls-KamusSecret.yaml --ignore-not-found");
}
catch
{
// ignored
}

try
{
RunKubectlCommand("delete -f tls-KamusSecretV1Alpha2.yaml --ignore-not-found");
RunKubectlCommand("delete -f tls-KamusSecretV1Alpha2-with-annotations.yaml --ignore-not-found");
RunKubectlCommand("delete -f updated-tls-KamusSecret.yaml --ignore-not-found");
}
catch
{
// ignored
}

try
{
RunKubectlCommand("delete -f updated-tls-KamusSecretV1Alpha2.yaml --ignore-not-found");
RunKubectlCommand("delete -f tls-Secret.yaml --ignore-not-found");
}
catch
Expand Down
3 changes: 3 additions & 0 deletions tests/crd-controller/crd-controller.csproj
Expand Up @@ -28,6 +28,9 @@
<None Update="tls-KamusSecretV1Alpha2.yaml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="tls-KamusSecretV1Alpha2-with-annotations.yaml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="updated-tls-KamusSecret.yaml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
2 changes: 2 additions & 0 deletions tests/crd-controller/crd.yaml
Expand Up @@ -42,6 +42,8 @@ spec:
type: string
type:
type: string
propagateAnnotations:
type: boolean
# either Namespaced or Cluster
scope: Namespaced
names:
Expand Down
10 changes: 6 additions & 4 deletions tests/crd-controller/run-tests.sh
Expand Up @@ -31,9 +31,12 @@ cleanup() {
echo 'Removing e2e container...'
docker kill e2e > /dev/null 2>&1
echo 'Removing kind e2e-test cluster'
kind delete clusters e2e-test
./kind delete clusters e2e-test
echo 'Restoring kubeconfig'
mv "$HOME/.kube/config.bkp" "$HOME/.kube/config" || echo "No original kubeconfig to backup was found."
echo 'Removing kubectl and kind binaries'
rm kubectl
rm kind
echo 'Done!'
}

Expand All @@ -48,7 +51,6 @@ create_kind_cluster() {

curl -sfSLo kind "https://github.com/kubernetes-sigs/kind/releases/download/v$KIND_VERSION/kind-$machine-amd64"
chmod +x kind
sudo mv kind /usr/local/bin/kind

curl -sfSLO https://storage.googleapis.com/kubernetes-release/release/"$K8S_VERSION"/bin/linux/amd64/kubectl
chmod +x kubectl
Expand All @@ -57,9 +59,9 @@ create_kind_cluster() {

kind_config="kind-config.yaml"

TMPDIR=$HOME kind create cluster --name "$CLUSTER_NAME" --config tests/crd-controller/$kind_config --image "kindest/node:$K8S_VERSION"
TMPDIR=$HOME ./kind create cluster --name "$CLUSTER_NAME" --config tests/crd-controller/$kind_config --image "kindest/node:$K8S_VERSION"

kind load image-archive docker-cache-api/crd-controller.tar --name "$CLUSTER_NAME"
./kind load image-archive docker-cache-api/crd-controller.tar --name "$CLUSTER_NAME"
docker_exec mkdir -p /root/.kube

echo 'Copying kubeconfig to container...'
Expand Down
15 changes: 15 additions & 0 deletions tests/crd-controller/tls-KamusSecretV1Alpha2-with-annotations.yaml
@@ -0,0 +1,15 @@
apiVersion: "soluto.com/v1alpha2"
kind: KamusSecret
metadata:
annotations:
key: value
labels:
key: value
name: my-tls-secret
type: TlsSecret
stringData:
key: J9NYLzTC/O44DvlCEZ+LfQ==:Cc9O5zQzFOyxwTD5ZHseqg==
data:
key3: 5SRnC8HJ6gJEOCpgby3ZSQ==:LUfzADu23z1l9CWFXzR71/Ua74IxI12Ehn18d5hnqA+C40D6o1qfaaOLuPzwPYILwsViZvg7FymTB9c/1vLlwnqQJfRXNNKg07cTD5tGoufKrQrgMpOHcEKsq/k3jwQ5MSyU99npBmPFkCZ93uE1b6llSbBkzp80cBQB+Peb7WtyfG4WoOn15hy7Eb71tcdNcRLd1r5pWaPWEIlMnVB8TERCfCpTUakXmuM5mvq5PTOp/OfRGpbqG3VRf/iDHSpSOqiNKhq6eiOWvG+WTCVqjpMv4iQiMlTCcJT31ayUHxafx1bg8EX5SuMVc0sHT7wwX4BBMDUCQ8i7qXHNS5gGAl6LXHC2fhfsK/PAJVDZ18PS/ISDuzAs/iV3zffqqLCZxy5qFDyBq5hdjE2deLhH40pL1G9i7fto40ikftdqKLFaa7pxc0A88bvQihkDuPj9gtAmXt7k+hnUXlPdP5VuoKGhfJUMaEDIwTxUekzcCPDPas9XrmLWyN27BFs6Z+s/kDL+8ZGHP4yyeJyxZV0Gxf0k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QwnMB1FHfPIYup10oVTJvoH2mHSVRCrCuKRQqqB/uCVIglgDjA/jnK68+yoUBD+pwOy3xiyVkQBTmxpnGURMzqXxTypP2YOXHFISnuyTElr7JRNoaK7Oep2hhiXO+jlKEXHe3jeWOKlOW8gpPKKd2rxBrrX+IeaJx7EWKdxkCbackd7cpaeRLVQV8WCUIb91HeMUqz1DEALky+lsfXOr5VwaiikRcJzrlwJkIarNSoM2644Qf5Gyo87z3kmnHyHTqKerC5VrRSgEcH2Z3Ag4Skdf5qMnYC3jm90jp2hhZzq4pMcgGhambiUb1
serviceAccount: some-sa
propagateAnnotations: true
4 changes: 4 additions & 0 deletions tests/crd-controller/tls-KamusSecretV1Alpha2.yaml
@@ -1,6 +1,10 @@
apiVersion: "soluto.com/v1alpha2"
kind: KamusSecret
metadata:
annotations:
key: value
labels:
key: value
name: my-tls-secret
type: TlsSecret
stringData:
Expand Down

0 comments on commit 0c26061

Please sign in to comment.