Skip to content

Commit

Permalink
add owner ref to mtping deployment (#3040)
Browse files Browse the repository at this point in the history
  • Loading branch information
lionelvillard authored Apr 24, 2020
1 parent b0da872 commit 004ebd4
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 1 deletion.
8 changes: 8 additions & 0 deletions config/core/deployments/controller.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ spec:
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: CONTROLLER_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: CONTROLLER_UID
valueFrom:
fieldRef:
fieldPath: metadata.uid
- name: CONFIG_LOGGING_NAME
value: config-logging
- name: CONFIG_OBSERVABILITY_NAME
Expand Down
4 changes: 4 additions & 0 deletions pkg/reconciler/pingsource/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ import (
type envConfig struct {
Image string `envconfig:"PING_IMAGE" required:"true"`
MTImage string `envconfig:"MT_PING_IMAGE" required:"true"`

// Add this for validation purpose only of validation.
ControllerName string `envconfig:"CONTROLLER_NAME" required:"true"`
ControllerUID string `envconfig:"CONTROLLER_UID" required:"true"`
}

// NewController initializes the controller and is called by the generated code
Expand Down
12 changes: 12 additions & 0 deletions pkg/reconciler/pingsource/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,25 @@ func TestNew(t *testing.T) {
if err := os.Setenv("PING_IMAGE", "anything"); err != nil {
t.Fatalf("Failed to set env var: %v", err)
}
if err := os.Setenv("CONTROLLER_NAME", "anything"); err != nil {
t.Fatalf("Failed to set env var: %v", err)
}
if err := os.Setenv("CONTROLLER_UID", "anything"); err != nil {
t.Fatalf("Failed to set env var: %v", err)
}
if err := os.Setenv("MT_PING_IMAGE", "anything"); err != nil {
t.Fatalf("Failed to set env var: %v", err)
}
defer func() {
if err := os.Unsetenv("PING_IMAGE"); err != nil {
t.Fatalf("Failed to unset env var: %v", err)
}
if err := os.Unsetenv("CONTROLLER_NAME"); err != nil {
t.Fatalf("Failed to unset env var: %v", err)
}
if err := os.Unsetenv("CONTROLLER_UID"); err != nil {
t.Fatalf("Failed to unset env var: %v", err)
}
if err := os.Unsetenv("MT_PING_IMAGE"); err != nil {
t.Fatalf("Failed to unset env var: %v", err)
}
Expand Down
16 changes: 15 additions & 1 deletion pkg/reconciler/pingsource/resources/mt_receive_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ limitations under the License.
package resources

import (
"os"

v1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"k8s.io/apimachinery/pkg/types"
"knative.dev/pkg/system"
)

Expand All @@ -41,6 +43,8 @@ type MTArgs struct {
// MakeMTReceiveAdapter generates the mtping deployment for pingsources
func MakeMTReceiveAdapter(args MTArgs) *v1.Deployment {
replicas := int32(1)
blockOwnerDeletion := true
isController := true

return &v1.Deployment{
TypeMeta: metav1.TypeMeta{
Expand All @@ -50,6 +54,16 @@ func MakeMTReceiveAdapter(args MTArgs) *v1.Deployment {
ObjectMeta: metav1.ObjectMeta{
Namespace: system.Namespace(),
Name: args.MTAdapterName,
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: v1.SchemeGroupVersion.String(),
Kind: "Deployment",
Name: os.Getenv("CONTROLLER_NAME"), // guarantee to be non-empty
UID: types.UID(os.Getenv("CONTROLLER_UID")), // guarantee to be non-empty
Controller: &isController,
BlockOwnerDeletion: &blockOwnerDeletion,
},
},
},
Spec: v1.DeploymentSpec{
Replicas: &replicas,
Expand Down
10 changes: 10 additions & 0 deletions pkg/reconciler/pingsource/resources/mt_receive_adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import (

func TestMakeMTPingAdapter(t *testing.T) {
replicas := int32(1)
blockOwnerDeletion := true
isController := true

args := MTArgs{
ServiceAccountName: "test-sa",
Expand All @@ -45,6 +47,14 @@ func TestMakeMTPingAdapter(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Namespace: system.Namespace(),
Name: args.MTAdapterName,
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: "apps/v1",
Kind: "Deployment",
Controller: &isController,
BlockOwnerDeletion: &blockOwnerDeletion,
},
},
},
Spec: v1.DeploymentSpec{
Replicas: &replicas,
Expand Down

0 comments on commit 004ebd4

Please sign in to comment.