Skip to content

Commit

Permalink
Merge pull request #1052 from zeot/append-ownerreferences-configmaps-…
Browse files Browse the repository at this point in the history
…and-secrets

Append to list of ownerReferences for primary configmaps and secrets
  • Loading branch information
stefanprodan authored Nov 18, 2021
2 parents 2ddd958 + 5080085 commit 30ab182
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 22 deletions.
75 changes: 53 additions & 22 deletions pkg/canary/config_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,22 +299,38 @@ func (ct *ConfigTracker) CreatePrimaryConfigs(cd *flaggerv1.Canary, refs map[str
case ConfigRefMap:
config, err := ct.KubeClient.CoreV1().ConfigMaps(cd.Namespace).Get(context.TODO(), ref.Name, metav1.GetOptions{})
if err != nil {
return fmt.Errorf("configmap %s.%s get query failed : %w", ref.Name, cd.Name, err)
return fmt.Errorf("configmap %s.%s get query failed : %w", ref.Name, cd.Namespace, err)
}
primaryName := fmt.Sprintf("%s-primary", config.GetName())
ownerReferences := []metav1.OwnerReference{
*metav1.NewControllerRef(cd, schema.GroupVersionKind{
Group: flaggerv1.SchemeGroupVersion.Group,
Version: flaggerv1.SchemeGroupVersion.Version,
Kind: flaggerv1.CanaryKind,
}),
}

oldPrimary, err := ct.KubeClient.CoreV1().ConfigMaps(cd.Namespace).Get(context.TODO(), primaryName, metav1.GetOptions{})
if err != nil {
if !errors.IsNotFound(err) {
return fmt.Errorf("configmap %s.%s get query failed : %w", primaryName, cd.Namespace, err)
}
} else {
for _, ownerRef := range oldPrimary.OwnerReferences {
if ownerRef.Kind != flaggerv1.CanaryKind || ownerRef.Name != cd.Name {
ownerRef.Controller = new(bool)
ownerReferences = append(ownerReferences, ownerRef)
}
}
}

labels := includeLabelsByPrefix(config.Labels, includeLabelPrefix)
primaryConfigMap := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: primaryName,
Namespace: cd.Namespace,
Labels: labels,
OwnerReferences: []metav1.OwnerReference{
*metav1.NewControllerRef(cd, schema.GroupVersionKind{
Group: flaggerv1.SchemeGroupVersion.Group,
Version: flaggerv1.SchemeGroupVersion.Version,
Kind: flaggerv1.CanaryKind,
}),
},
Name: primaryName,
Namespace: cd.Namespace,
Labels: labels,
OwnerReferences: ownerReferences,
},
Data: config.Data,
}
Expand All @@ -337,22 +353,37 @@ func (ct *ConfigTracker) CreatePrimaryConfigs(cd *flaggerv1.Canary, refs map[str
case ConfigRefSecret:
secret, err := ct.KubeClient.CoreV1().Secrets(cd.Namespace).Get(context.TODO(), ref.Name, metav1.GetOptions{})
if err != nil {
return fmt.Errorf("secret %s.%s get query failed : %w", ref.Name, cd.Name, err)
return fmt.Errorf("secret %s.%s get query failed : %w", ref.Name, cd.Namespace, err)
}
primaryName := fmt.Sprintf("%s-primary", secret.GetName())
ownerReferences := []metav1.OwnerReference{
*metav1.NewControllerRef(cd, schema.GroupVersionKind{
Group: flaggerv1.SchemeGroupVersion.Group,
Version: flaggerv1.SchemeGroupVersion.Version,
Kind: flaggerv1.CanaryKind,
}),
}

oldPrimary, err := ct.KubeClient.CoreV1().Secrets(cd.Namespace).Get(context.TODO(), primaryName, metav1.GetOptions{})
if err != nil {
if !errors.IsNotFound(err) {
return fmt.Errorf("secret %s.%s get query failed : %w", primaryName, cd.Namespace, err)
}
} else {
for _, ownerRef := range oldPrimary.OwnerReferences {
if ownerRef.Kind != flaggerv1.CanaryKind || ownerRef.Name != cd.Name {
ownerRef.Controller = new(bool)
ownerReferences = append(ownerReferences, ownerRef)
}
}
}
labels := includeLabelsByPrefix(secret.Labels, includeLabelPrefix)
primarySecret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: primaryName,
Namespace: cd.Namespace,
Labels: labels,
OwnerReferences: []metav1.OwnerReference{
*metav1.NewControllerRef(cd, schema.GroupVersionKind{
Group: flaggerv1.SchemeGroupVersion.Group,
Version: flaggerv1.SchemeGroupVersion.Version,
Kind: flaggerv1.CanaryKind,
}),
},
Name: primaryName,
Namespace: cd.Namespace,
Labels: labels,
OwnerReferences: ownerReferences,
},
Type: secret.Type,
Data: secret.Data,
Expand Down
24 changes: 24 additions & 0 deletions pkg/canary/config_tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,3 +399,27 @@ func Test_fieldIsMandatory(t *testing.T) {
assert.Equal(t, tt.expected, actual)
}
}

func TestConfigTracker_ConfigOwnerMultiDeployment(t *testing.T) {
t.Run("deployment", func(t *testing.T) {
dc := deploymentConfigs{name: "podinfo", label: "name", labelValue: "podinfo"}
mocks := newDeploymentFixture(dc)
mocks.initializeCanary(t)

dep := newDeploymentControllerTest(dc)
dep.Name = "podinfo2"
canary := newDeploymentControllerTestCanary(canaryConfigs{targetName: "podinfo2"})
canary.Name = "podinfo2"
canary.Spec.TargetRef.Name = dep.Name
mocks.kubeClient.AppsV1().Deployments("default").Create(context.TODO(), dep, metav1.CreateOptions{})
mocks.controller.Initialize(canary)

configMapPrimary, err := mocks.kubeClient.CoreV1().ConfigMaps("default").Get(context.TODO(), "podinfo-config-env-primary", metav1.GetOptions{})
require.NoError(t, err)
assert.Len(t, configMapPrimary.OwnerReferences, 2)

secretPrimary, err := mocks.kubeClient.CoreV1().Secrets("default").Get(context.TODO(), "podinfo-secret-env-primary", metav1.GetOptions{})
require.NoError(t, err)
assert.Len(t, secretPrimary.OwnerReferences, 2)
})
}

0 comments on commit 30ab182

Please sign in to comment.