Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Append to list of ownerReferences for primary configmaps and secrets #1052

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
stefanprodan marked this conversation as resolved.
Show resolved Hide resolved
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)
})
}