Skip to content
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
42 changes: 18 additions & 24 deletions pkg/reconciler/apiserversource/apiserversource.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ const (
apiserversourceReconciled = "ApiServerSourceReconciled"
apiServerSourceReadinessChanged = "ApiServerSourceReadinessChanged"
apiserversourceUpdateStatusFailed = "ApiServerSourceUpdateStatusFailed"
apiserversourceDeploymentCreated = "ApiServerSourceDeploymentCreated"
apiserversourceDeploymentUpdated = "ApiServerSourceDeploymentUpdated"

// raImageEnvVar is the name of the environment variable that contains the receive adapter's
// image. It must be defined.
Expand Down Expand Up @@ -173,40 +175,32 @@ func (r *Reconciler) getReceiveAdapterImage() string {
}

func (r *Reconciler) createReceiveAdapter(ctx context.Context, src *v1alpha1.ApiServerSource, sinkURI string) (*appsv1.Deployment, error) {
ra, err := r.getReceiveAdapter(ctx, src)
if err != nil && !apierrors.IsNotFound(err) {
logging.FromContext(ctx).Error("Unable to get an existing receive adapter", zap.Error(err))
return nil, err
}
if ra != nil {
logging.FromContext(ctx).Info("Reusing existing receive adapter", zap.Any("receiveAdapter", ra))
return ra, nil
}
adapterArgs := resources.ReceiveAdapterArgs{
Image: r.getReceiveAdapterImage(),
Source: src,
Labels: resources.Labels(src.Name),
SinkURI: sinkURI,
}
expected := resources.MakeReceiveAdapter(&adapterArgs)
if ra != nil {
if r.podSpecChanged(ra.Spec.Template.Spec, expected.Spec.Template.Spec) {
ra.Spec.Template.Spec = expected.Spec.Template.Spec
if ra, err = r.KubeClientSet.AppsV1().Deployments(src.Namespace).Update(ra); err != nil {
return ra, err
}
logging.FromContext(ctx).Info("Receive Adapter updated.", zap.Any("receiveAdapter", ra))
} else {
logging.FromContext(ctx).Info("Reusing existing receive adapter", zap.Any("receiveAdapter", ra))

ra, err := r.getReceiveAdapter(ctx, src)
if apierrors.IsNotFound(err) {
ra, err = r.KubeClientSet.AppsV1().Deployments(src.Namespace).Create(expected)
r.Recorder.Eventf(src, corev1.EventTypeNormal, apiserversourceDeploymentCreated, "Deployment created, error: %v", err)
return ra, err
} else if err != nil {
return nil, fmt.Errorf("error getting receive adapter: %v", err)
} else if r.podSpecChanged(ra.Spec.Template.Spec, expected.Spec.Template.Spec) {
ra.Spec.Template.Spec = expected.Spec.Template.Spec
if ra, err = r.KubeClientSet.AppsV1().Deployments(src.Namespace).Update(ra); err != nil {
return ra, err
}
r.Recorder.Eventf(src, corev1.EventTypeNormal, apiserversourceDeploymentUpdated, "Deployment updated")
return ra, nil
} else {
logging.FromContext(ctx).Debug("Reusing existing receive adapter", zap.Any("receiveAdapter", ra))
}

if ra, err = r.KubeClientSet.AppsV1().Deployments(src.Namespace).Create(expected); err != nil {
return nil, err
}
logging.FromContext(ctx).Info("Receive Adapter created.", zap.Any("receiveAdapter", expected))
return ra, err
return ra, nil
}

func (r *Reconciler) reconcileEventTypes(ctx context.Context, src *v1alpha1.ApiServerSource) error {
Expand Down
173 changes: 172 additions & 1 deletion pkg/reconciler/apiserversource/apiserversource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ var (
const (
image = "github.com/knative/test/image"
sourceName = "test-apiserver-source"
sourceUID = "1234-5678-90"
testNS = "testnamespace"

sinkName = "testsink"
Expand Down Expand Up @@ -124,6 +123,7 @@ func TestReconcile(t *testing.T) {
},
Key: testNS + "/" + sourceName,
WantEvents: []string{
Eventf(corev1.EventTypeNormal, "ApiServerSourceDeploymentCreated", "Deployment created, error: <nil>"),
Eventf(corev1.EventTypeNormal, "ApiServerSourceReconciled", `ApiServerSource reconciled: "%s/%s"`, testNS, sourceName),
Eventf(corev1.EventTypeNormal, "ApiServerSourceReadinessChanged", `ApiServerSource %q became ready`, sourceName),
},
Expand All @@ -149,6 +149,152 @@ func TestReconcile(t *testing.T) {
makeReceiveAdapter(),
},
},
{
Name: "valid - deployment update due to env",
Objects: []runtime.Object{
NewApiServerSource(sourceName, testNS,
WithApiServerSourceSpec(sourcesv1alpha1.ApiServerSourceSpec{
Resources: []sourcesv1alpha1.ApiServerResource{
{
APIVersion: "",
Kind: "Namespace",
},
},
Sink: &sinkRef,
}),
),
NewChannel(sinkName, testNS,
WithInitChannelConditions,
WithChannelAddress(sinkDNS),
),
makeReceiveAdapterWithDifferentEnv(),
},
Key: testNS + "/" + sourceName,
WantEvents: []string{
Eventf(corev1.EventTypeNormal, "ApiServerSourceDeploymentUpdated", "Deployment updated"),
Eventf(corev1.EventTypeNormal, "ApiServerSourceReconciled", `ApiServerSource reconciled: "%s/%s"`, testNS, sourceName),
Eventf(corev1.EventTypeNormal, "ApiServerSourceReadinessChanged", `ApiServerSource %q became ready`, sourceName),
},
WantStatusUpdates: []clientgotesting.UpdateActionImpl{{
Object: NewApiServerSource(sourceName, testNS,
WithApiServerSourceSpec(sourcesv1alpha1.ApiServerSourceSpec{
Resources: []sourcesv1alpha1.ApiServerResource{
{
APIVersion: "",
Kind: "Namespace",
},
},
Sink: &sinkRef,
}),
// Status Update:
WithInitApiServerSourceConditions,
WithApiServerSourceDeployed,
WithApiServerSourceSink(sinkURI),
WithApiServerSourceEventTypes,
),
}},
WantUpdates: []clientgotesting.UpdateActionImpl{{
Object: makeReceiveAdapter(),
}},
},
{
Name: "valid - deployment update due to service account",
Objects: []runtime.Object{
NewApiServerSource(sourceName, testNS,
WithApiServerSourceSpec(sourcesv1alpha1.ApiServerSourceSpec{
Resources: []sourcesv1alpha1.ApiServerResource{
{
APIVersion: "",
Kind: "Namespace",
},
},
Sink: &sinkRef,
ServiceAccountName: "malin",
}),
),
NewChannel(sinkName, testNS,
WithInitChannelConditions,
WithChannelAddress(sinkDNS),
),
makeReceiveAdapterWithDifferentServiceAccount("morgan"),
},
Key: testNS + "/" + sourceName,
WantEvents: []string{
Eventf(corev1.EventTypeNormal, "ApiServerSourceDeploymentUpdated", "Deployment updated"),
Eventf(corev1.EventTypeNormal, "ApiServerSourceReconciled", `ApiServerSource reconciled: "%s/%s"`, testNS, sourceName),
Eventf(corev1.EventTypeNormal, "ApiServerSourceReadinessChanged", `ApiServerSource %q became ready`, sourceName),
},
WantStatusUpdates: []clientgotesting.UpdateActionImpl{{
Object: NewApiServerSource(sourceName, testNS,
WithApiServerSourceSpec(sourcesv1alpha1.ApiServerSourceSpec{
Resources: []sourcesv1alpha1.ApiServerResource{
{
APIVersion: "",
Kind: "Namespace",
},
},
Sink: &sinkRef,
ServiceAccountName: "malin",
}),
// Status Update:
WithInitApiServerSourceConditions,
WithApiServerSourceDeployed,
WithApiServerSourceSink(sinkURI),
WithApiServerSourceEventTypes,
),
}},
WantUpdates: []clientgotesting.UpdateActionImpl{{
Object: makeReceiveAdapterWithDifferentServiceAccount("malin"),
}},
},
{
Name: "valid - deployment update due to container count",
Objects: []runtime.Object{
NewApiServerSource(sourceName, testNS,
WithApiServerSourceSpec(sourcesv1alpha1.ApiServerSourceSpec{
Resources: []sourcesv1alpha1.ApiServerResource{
{
APIVersion: "",
Kind: "Namespace",
},
},
Sink: &sinkRef,
}),
),
NewChannel(sinkName, testNS,
WithInitChannelConditions,
WithChannelAddress(sinkDNS),
),
makeReceiveAdapterWithDifferentContainerCount(),
},
Key: testNS + "/" + sourceName,
WantEvents: []string{
Eventf(corev1.EventTypeNormal, "ApiServerSourceDeploymentUpdated", "Deployment updated"),
Eventf(corev1.EventTypeNormal, "ApiServerSourceReconciled", `ApiServerSource reconciled: "%s/%s"`, testNS, sourceName),
Eventf(corev1.EventTypeNormal, "ApiServerSourceReadinessChanged", `ApiServerSource %q became ready`, sourceName),
},
WantStatusUpdates: []clientgotesting.UpdateActionImpl{{
Object: NewApiServerSource(sourceName, testNS,
WithApiServerSourceSpec(sourcesv1alpha1.ApiServerSourceSpec{
Resources: []sourcesv1alpha1.ApiServerResource{
{
APIVersion: "",
Kind: "Namespace",
},
},
Sink: &sinkRef,
}),
// Status Update:
WithInitApiServerSourceConditions,
WithApiServerSourceDeployed,
WithApiServerSourceSink(sinkURI),
WithApiServerSourceEventTypes,
),
}},
WantUpdates: []clientgotesting.UpdateActionImpl{{
Object: makeReceiveAdapter(),
}},
},
{
Name: "valid with event types to delete",
Objects: []runtime.Object{
Expand All @@ -171,6 +317,7 @@ func TestReconcile(t *testing.T) {
},
Key: testNS + "/" + sourceName,
WantEvents: []string{
Eventf(corev1.EventTypeNormal, "ApiServerSourceDeploymentCreated", "Deployment created, error: <nil>"),
Eventf(corev1.EventTypeNormal, "ApiServerSourceReconciled", `ApiServerSource reconciled: "%s/%s"`, testNS, sourceName),
Eventf(corev1.EventTypeNormal, "ApiServerSourceReadinessChanged", `ApiServerSource %q became ready`, sourceName),
},
Expand Down Expand Up @@ -220,6 +367,7 @@ func TestReconcile(t *testing.T) {
},
Key: testNS + "/" + sourceName,
WantEvents: []string{
Eventf(corev1.EventTypeNormal, "ApiServerSourceDeploymentCreated", "Deployment created, error: <nil>"),
Eventf(corev1.EventTypeNormal, "ApiServerSourceReconciled", `ApiServerSource reconciled: "%s/%s"`, testNS, sourceName),
Eventf(corev1.EventTypeNormal, "ApiServerSourceReadinessChanged", `ApiServerSource %q became ready`, sourceName),
},
Expand Down Expand Up @@ -275,6 +423,7 @@ func TestReconcile(t *testing.T) {
},
Key: testNS + "/" + sourceName,
WantEvents: []string{
Eventf(corev1.EventTypeNormal, "ApiServerSourceDeploymentCreated", "Deployment created, error: <nil>"),
Eventf(corev1.EventTypeNormal, "ApiServerSourceReconciled", `ApiServerSource reconciled: "%s/%s"`, testNS, sourceName),
Eventf(corev1.EventTypeNormal, "ApiServerSourceReadinessChanged", `ApiServerSource %q became ready`, sourceName),
},
Expand Down Expand Up @@ -330,6 +479,7 @@ func TestReconcile(t *testing.T) {
},
Key: testNS + "/" + sourceName,
WantEvents: []string{
Eventf(corev1.EventTypeNormal, "ApiServerSourceDeploymentCreated", "Deployment created, error: <nil>"),
Eventf(corev1.EventTypeNormal, "ApiServerSourceReconciled", `ApiServerSource reconciled: "%s/%s"`, testNS, sourceName),
Eventf(corev1.EventTypeNormal, "ApiServerSourceReadinessChanged", `ApiServerSource %q became ready`, sourceName),
},
Expand Down Expand Up @@ -408,6 +558,27 @@ func makeReceiveAdapter() *appsv1.Deployment {
return resources.MakeReceiveAdapter(&args)
}

func makeReceiveAdapterWithDifferentEnv() *appsv1.Deployment {
ra := makeReceiveAdapter()
ra.Spec.Template.Spec.Containers[0].Env = append(ra.Spec.Template.Spec.Containers[0].Env, corev1.EnvVar{
Name: "not-in",
Value: "the-original",
})
return ra
}

func makeReceiveAdapterWithDifferentServiceAccount(name string) *appsv1.Deployment {
ra := makeReceiveAdapter()
ra.Spec.Template.Spec.ServiceAccountName = name
return ra
}

func makeReceiveAdapterWithDifferentContainerCount() *appsv1.Deployment {
ra := makeReceiveAdapter()
ra.Spec.Template.Spec.Containers = append(ra.Spec.Template.Spec.Containers, corev1.Container{})
return ra
}

func makeEventTypeWithName(eventType, name string) *v1alpha1.EventType {
et := makeEventType(eventType)
et.Name = name
Expand Down