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
2 changes: 1 addition & 1 deletion pkg/apis/sources/v1alpha1/apiserver_lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (s *ApiServerSourceStatus) PropagateDeploymentAvailability(d *appsv1.Deploy
}
}
if !deploymentAvailableFound {
PingSourceCondSet.Manage(s).MarkUnknown(PingSourceConditionDeployed, "DeploymentUnavailable", "The Deployment '%s' is unavailable.", d.Name)
apiserverCondSet.Manage(s).MarkUnknown(ApiServerConditionDeployed, "DeploymentUnavailable", "The Deployment '%s' is unavailable.", d.Name)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for fixing this!

}
}

Expand Down
116 changes: 112 additions & 4 deletions pkg/apis/sources/v1alpha2/apiserver_defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,118 @@ package v1alpha2
import (
"context"
"testing"

"github.com/google/go-cmp/cmp"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
duckv1 "knative.dev/pkg/apis/duck/v1"
)

// No-op test because method does nothing.
func TestAPIServerSourceDefaults(t *testing.T) {
s := ApiServerSource{}
s.SetDefaults(context.TODO())
func TestApiServerSourceDefaults(t *testing.T) {
testCases := map[string]struct {
initial ApiServerSource
expected ApiServerSource
}{
"no EventMode": {
initial: ApiServerSource{
ObjectMeta: metav1.ObjectMeta{
Name: "test-name",
Namespace: "test-namespace",
},
Spec: ApiServerSourceSpec{
Resources: []APIVersionKindSelector{{
APIVersion: "v1",
Kind: "Foo",
}},
ServiceAccountName: "default",
SourceSpec: duckv1.SourceSpec{
Sink: duckv1.Destination{
Ref: &duckv1.KReference{
APIVersion: "v1alpha1",
Kind: "broker",
Name: "default",
},
},
},
},
},
expected: ApiServerSource{
ObjectMeta: metav1.ObjectMeta{
Name: "test-name",
Namespace: "test-namespace",
},
Spec: ApiServerSourceSpec{
EventMode: ReferenceMode,
Resources: []APIVersionKindSelector{{
APIVersion: "v1",
Kind: "Foo",
}},
ServiceAccountName: "default",
SourceSpec: duckv1.SourceSpec{
Sink: duckv1.Destination{
Ref: &duckv1.KReference{
APIVersion: "v1alpha1",
Kind: "broker",
Name: "default",
},
},
},
},
},
},
"no ServiceAccountName": {
initial: ApiServerSource{
ObjectMeta: metav1.ObjectMeta{
Name: "test-name",
Namespace: "test-namespace",
},
Spec: ApiServerSourceSpec{
Resources: []APIVersionKindSelector{{
APIVersion: "v1",
Kind: "Foo",
}},
EventMode: ReferenceMode,
SourceSpec: duckv1.SourceSpec{
Sink: duckv1.Destination{
Ref: &duckv1.KReference{
APIVersion: "v1alpha1",
Kind: "broker",
Name: "default",
},
},
},
},
},
expected: ApiServerSource{
ObjectMeta: metav1.ObjectMeta{
Name: "test-name",
Namespace: "test-namespace",
},
Spec: ApiServerSourceSpec{
EventMode: ReferenceMode,
Resources: []APIVersionKindSelector{{
APIVersion: "v1",
Kind: "Foo",
}},
ServiceAccountName: "default",
SourceSpec: duckv1.SourceSpec{
Sink: duckv1.Destination{
Ref: &duckv1.KReference{
APIVersion: "v1alpha1",
Kind: "broker",
Name: "default",
},
},
},
},
},
},
}
for n, tc := range testCases {
t.Run(n, func(t *testing.T) {
tc.initial.SetDefaults(context.Background())
if diff := cmp.Diff(tc.expected, tc.initial); diff != "" {
t.Fatalf("Unexpected defaults (-want, +got): %s", diff)
}
})
}
}
15 changes: 1 addition & 14 deletions pkg/apis/sources/v1alpha2/apiserver_lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ const (

// ApiServerConditionSufficientPermissions has status True when the ApiServerSource has sufficient permissions to access resources.
ApiServerConditionSufficientPermissions apis.ConditionType = "SufficientPermissions"

// ApiServerConditionEventTypeProvided has status True when the ApiServerSource has been configured with its event types.
ApiServerConditionEventTypeProvided apis.ConditionType = "EventTypesProvided"
)

var apiserverCondSet = apis.NewLivingConditionSet(
Expand Down Expand Up @@ -109,20 +106,10 @@ func (s *ApiServerSourceStatus) PropagateDeploymentAvailability(d *appsv1.Deploy
}
}
if !deploymentAvailableFound {
PingSourceCondSet.Manage(s).MarkUnknown(PingSourceConditionDeployed, "DeploymentUnavailable", "The Deployment '%s' is unavailable.", d.Name)
apiserverCondSet.Manage(s).MarkUnknown(ApiServerConditionDeployed, "DeploymentUnavailable", "The Deployment '%s' is unavailable.", d.Name)
}
}

// MarkEventTypes sets the condition that the source has set its event type.
func (s *ApiServerSourceStatus) MarkEventTypes() {
apiserverCondSet.Manage(s).MarkTrue(ApiServerConditionEventTypeProvided)
}

// MarkNoEventTypes sets the condition that the source does not its event type configured.
func (s *ApiServerSourceStatus) MarkNoEventTypes(reason, messageFormat string, messageA ...interface{}) {
apiserverCondSet.Manage(s).MarkFalse(ApiServerConditionEventTypeProvided, reason, messageFormat, messageA...)
}

// MarkSufficientPermissions sets the condition that the source has enough permissions to access the resources.
func (s *ApiServerSourceStatus) MarkSufficientPermissions() {
apiserverCondSet.Manage(s).MarkTrue(ApiServerConditionSufficientPermissions)
Expand Down
6 changes: 1 addition & 5 deletions pkg/apis/sources/v1alpha2/apiserver_lifecycle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ func TestApiServerSourceStatusIsReady(t *testing.T) {
s: func() *ApiServerSourceStatus {
s := &ApiServerSourceStatus{}
s.InitializeConditions()
s.MarkEventTypes()
return s
}(),
wantConditionStatus: corev1.ConditionUnknown,
Expand Down Expand Up @@ -158,14 +157,13 @@ func TestApiServerSourceStatusIsReady(t *testing.T) {
wantConditionStatus: corev1.ConditionUnknown,
want: false,
}, {
name: "mark sink and sufficient permissions and deployed and event types",
name: "mark sink and sufficient permissions and deployed",
s: func() *ApiServerSourceStatus {
s := &ApiServerSourceStatus{}
s.InitializeConditions()
s.MarkSink(sink)
s.MarkSufficientPermissions()
s.PropagateDeploymentAvailability(TestHelper.AvailableDeployment())
s.MarkEventTypes()
return s
}(),
wantConditionStatus: corev1.ConditionTrue,
Expand Down Expand Up @@ -273,7 +271,6 @@ func TestApiServerSourceStatusGetCondition(t *testing.T) {
s.MarkSink(sink)
s.MarkSufficientPermissions()
s.PropagateDeploymentAvailability(TestHelper.AvailableDeployment())
s.MarkEventTypes()
return s
}(),
condQuery: ApiServerConditionReady,
Expand All @@ -289,7 +286,6 @@ func TestApiServerSourceStatusGetCondition(t *testing.T) {
s.MarkSink(nil)
s.MarkSufficientPermissions()
s.PropagateDeploymentAvailability(TestHelper.AvailableDeployment())
s.MarkEventTypes()
return s
}(),
condQuery: ApiServerConditionReady,
Expand Down
34 changes: 34 additions & 0 deletions pkg/apis/sources/v1beta1/apiserver_conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
Copyright 2020 The Knative Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1beta1

import (
"context"
"fmt"

"knative.dev/pkg/apis"
)

// ConvertTo implements apis.Convertible
func (source *ApiServerSource) ConvertTo(ctx context.Context, sink apis.Convertible) error {
return fmt.Errorf("v1beta1 is the highest known version, got: %T", sink)
}

// ConvertFrom implements apis.Convertible
func (sink *ApiServerSource) ConvertFrom(ctx context.Context, source apis.Convertible) error {
return fmt.Errorf("v1beta1 is the highest known version, got: %T", source)
}
34 changes: 34 additions & 0 deletions pkg/apis/sources/v1beta1/apiserver_conversion_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
Copyright 2020 The Knative Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1beta1

import (
"context"
"testing"
)

func TestApiServerConversionBadType(t *testing.T) {
good, bad := &ApiServerSource{}, &ApiServerSource{}

if err := good.ConvertTo(context.Background(), bad); err == nil {
t.Errorf("ConvertTo() = %#v, wanted error", bad)
}

if err := good.ConvertFrom(context.Background(), bad); err == nil {
t.Errorf("ConvertFrom() = %#v, wanted error", good)
}
}
36 changes: 36 additions & 0 deletions pkg/apis/sources/v1beta1/apiserver_defaults.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Copyright 2020 The Knative Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1beta1

import (
"context"
)

func (s *ApiServerSource) SetDefaults(ctx context.Context) {
s.Spec.SetDefaults(ctx)
}

func (ss *ApiServerSourceSpec) SetDefaults(ctx context.Context) {

if ss.EventMode == "" {
ss.EventMode = ReferenceMode
}

if ss.ServiceAccountName == "" {
ss.ServiceAccountName = "default"
}
}
Loading