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

Bringing ContainerSource back #2824

Merged
merged 18 commits into from
Mar 26, 2020
2 changes: 2 additions & 0 deletions cmd/controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

"knative.dev/eventing/pkg/reconciler/apiserversource"
"knative.dev/eventing/pkg/reconciler/channel"
"knative.dev/eventing/pkg/reconciler/containersource"
"knative.dev/eventing/pkg/reconciler/eventtype"
"knative.dev/eventing/pkg/reconciler/parallel"
pingsource "knative.dev/eventing/pkg/reconciler/pingsource/controller"
Expand Down Expand Up @@ -51,6 +52,7 @@ func main() {
// Sources
apiserversource.NewController,
pingsource.NewController,
containersource.NewController,
// Sources CRD
sourcecrd.NewController,
)
Expand Down
1 change: 1 addition & 0 deletions cmd/webhook/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ var ourTypes = map[schema.GroupVersionKind]resourcesemantics.GenericCRD{
sourcesv1alpha2.SchemeGroupVersion.WithKind("ApiServerSource"): &sourcesv1alpha2.ApiServerSource{},
sourcesv1alpha2.SchemeGroupVersion.WithKind("PingSource"): &sourcesv1alpha2.PingSource{},
sourcesv1alpha2.SchemeGroupVersion.WithKind("SinkBinding"): &sourcesv1alpha2.SinkBinding{},
sourcesv1alpha2.SchemeGroupVersion.WithKind("ContainerSource"): &sourcesv1alpha2.ContainerSource{},

// For group flows.knative.dev
// v1alpha1
Expand Down
1 change: 1 addition & 0 deletions config/300-containersource.yaml
62 changes: 62 additions & 0 deletions config/core/resources/containersource.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# 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.

apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
labels:
eventing.knative.dev/release: devel
eventing.knative.dev/source: "true"
duck.knative.dev/source: "true"
knative.dev/crd-install: "true"
name: containersources.sources.knative.dev
spec:
group: sources.knative.dev
names:
categories:
- all
- knative
- eventing
- sources
kind: ContainerSource
plural: containersources
scope: Namespaced
subresources:
status: {}
preserveUnknownFields: false
validation:
openAPIV3Schema:
type: object
# this is a work around so we don't need to flesh out the
# schema for each version at this time
#
# see issue: https://github.com/knative/serving/issues/912
x-kubernetes-preserve-unknown-fields: true
additionalPrinterColumns:
- name: Ready
type: string
JSONPath: ".status.conditions[?(@.type==\"Ready\")].status"
- name: Reason
type: string
JSONPath: ".status.conditions[?(@.type=='Ready')].reason"
- name: Sink
type: string
JSONPath: ".status.sinkUri"
- name: Age
type: date
JSONPath: .metadata.creationTimestamp
versions:
- name: v1alpha2
served: true
storage: true
3 changes: 2 additions & 1 deletion config/core/roles/source-observer-clusterrole.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ rules:
resources:
- apiserversources
- pingsources
- sinkbinding
- sinkbindings
- containersources
verbs:
- get
- list
Expand Down
3 changes: 3 additions & 0 deletions config/core/roles/sources-controller-clusterroles.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ rules:
- "pingsources"
- "pingsources/status"
- "pingsources/finalizers"
- "containersources"
- "containersources/status"
- "containersources/finalizers"
verbs: *everything

# Knative Services admin
Expand Down
6 changes: 6 additions & 0 deletions pkg/apis/sources/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,10 @@ var (
Group: GroupName,
Resource: "sinkbindings",
}

// ContainerSourceResource respresents a Knative Eventing Sources ContainerSource
ContainerSourceResource = schema.GroupResource{
Group: GroupName,
Resource: "containersources",
}
)
4 changes: 2 additions & 2 deletions pkg/apis/sources/v1alpha1/implements_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ func TestTypesImplements(t *testing.T) {
iface duck.Implementable
}{
// PingSource
{instance: &ApiServerSource{}, iface: &duckv1.Conditions{}},
// ContainerSource
{instance: &PingSource{}, iface: &duckv1.Conditions{}},
// ApiServerSource
{instance: &ApiServerSource{}, iface: &duckv1.Conditions{}},
// SinkBinding
{instance: &SinkBinding{}, iface: &duckv1.Conditions{}},
Expand Down
2 changes: 2 additions & 0 deletions pkg/apis/sources/v1alpha1/register_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ func TestKnownTypes(t *testing.T) {
"ApiServerSourceList",
"SinkBinding",
"SinkBindingList",
"PingSource",
"PingSourceList",
} {
if _, ok := types[name]; !ok {
t.Errorf("Did not find %q as registered type", name)
Expand Down
42 changes: 42 additions & 0 deletions pkg/apis/sources/v1alpha2/container_defaults.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
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 v1alpha2

import (
"context"
"fmt"

corev1 "k8s.io/api/core/v1"
"knative.dev/pkg/apis"
)

func (s *ContainerSource) SetDefaults(ctx context.Context) {
withName := apis.WithinParent(ctx, s.ObjectMeta)
s.Spec.SetDefaults(withName)
}

func (ss *ContainerSourceSpec) SetDefaults(ctx context.Context) {
containers := make([]corev1.Container, 0, len(ss.Template.Spec.Containers))
for i, c := range ss.Template.Spec.Containers {
// If the Container specified has no name, then default to "<source_name>_<i>".
if c.Name == "" {
c.Name = fmt.Sprintf("%s-%d", apis.ParentMeta(ctx).Name, i)
}
containers = append(containers, c)
}
ss.Template.Spec.Containers = containers
}
114 changes: 114 additions & 0 deletions pkg/apis/sources/v1alpha2/container_defaults_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
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 v1alpha2

import (
"context"
"testing"

"github.com/google/go-cmp/cmp"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestContainerSourceDefaults(t *testing.T) {
testCases := map[string]struct {
initial ContainerSource
expected ContainerSource
}{
"no container name": {
initial: ContainerSource{
ObjectMeta: metav1.ObjectMeta{
Name: "test-name",
Namespace: "test-namespace",
},
Spec: ContainerSourceSpec{
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{{
Image: "test-image",
}},
},
},
},
},
expected: ContainerSource{
ObjectMeta: metav1.ObjectMeta{
Name: "test-name",
Namespace: "test-namespace",
},
Spec: ContainerSourceSpec{
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{{
Name: "test-name-0",
Image: "test-image",
}},
},
},
},
},
},
"one with ontainer name one without": {
initial: ContainerSource{
ObjectMeta: metav1.ObjectMeta{
Name: "test-name",
Namespace: "test-namespace",
},
Spec: ContainerSourceSpec{
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{{
Name: "test-container",
Image: "test-image",
}, {
Image: "test-another-image",
}},
},
},
},
},
expected: ContainerSource{
ObjectMeta: metav1.ObjectMeta{
Name: "test-name",
Namespace: "test-namespace",
},
Spec: ContainerSourceSpec{
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{{
Name: "test-container",
Image: "test-image",
}, {
Name: "test-name-1",
Image: "test-another-image",
}},
},
},
},
},
},
}
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)
}
})
}
}
92 changes: 92 additions & 0 deletions pkg/apis/sources/v1alpha2/container_lifecycle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
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 v1alpha2

import (
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"knative.dev/eventing/pkg/apis/duck"
"knative.dev/pkg/apis"
)

const (
// ContainerSourceConditionReady has status True when the ContainerSource is ready to send events.
ContainerSourceConditionReady = apis.ConditionReady

// ContainerSourceConditionSinkBindingReady has status True when the ContainerSource's SinkBinding is ready.
ContainerSourceConditionSinkBindingReady apis.ConditionType = "SinkBindingReady"

// ContainerSourceConditionReceiveAdapterReady has status True when the ContainerSource's ReceiveAdapter is ready.
ContainerSourceConditionReceiveAdapterReady apis.ConditionType = "ReceiveAdapterReady"
)

var containerCondSet = apis.NewLivingConditionSet(
ContainerSourceConditionSinkBindingReady,
ContainerSourceConditionReceiveAdapterReady,
)

// GetCondition returns the condition currently associated with the given type, or nil.
func (s *ContainerSourceStatus) GetCondition(t apis.ConditionType) *apis.Condition {
return containerCondSet.Manage(s).GetCondition(t)
}

// IsReady returns true if the resource is ready overall.
func (s *ContainerSourceStatus) IsReady() bool {
return containerCondSet.Manage(s).IsHappy()
}

// InitializeConditions sets relevant unset conditions to Unknown state.
func (s *ContainerSourceStatus) InitializeConditions() {
containerCondSet.Manage(s).InitializeConditions()
}

// PropagateSinkBindingStatus uses the availability of the provided Deployment to determine if
// ContainerSourceConditionSinkBindingReady should be marked as true, false or unknown.
func (s *ContainerSourceStatus) PropagateSinkBindingStatus(status *SinkBindingStatus) {
// Do not copy conditions nor observedGeneration
conditions := s.Conditions
observedGeneration := s.ObservedGeneration
s.SourceStatus = status.SourceStatus
s.Conditions = conditions
s.ObservedGeneration = observedGeneration

cond := status.GetCondition(apis.ConditionReady)
switch {
case cond == nil:
containerCondSet.Manage(s).MarkUnknown(ContainerSourceConditionSinkBindingReady, "", "")
case cond.Status == corev1.ConditionTrue:
containerCondSet.Manage(s).MarkTrue(ContainerSourceConditionSinkBindingReady)
case cond.Status == corev1.ConditionFalse:
containerCondSet.Manage(s).MarkFalse(ContainerSourceConditionSinkBindingReady, cond.Reason, cond.Message)
case cond.Status == corev1.ConditionUnknown:
containerCondSet.Manage(s).MarkUnknown(ContainerSourceConditionSinkBindingReady, cond.Reason, cond.Message)
default:
containerCondSet.Manage(s).MarkUnknown(ContainerSourceConditionSinkBindingReady, cond.Reason, cond.Message)
}
}

// PropagateReceiveAdapterStatus uses the availability of the provided Deployment to determine if
// ContainerSourceConditionReceiveAdapterReady should be marked as true or false.
func (s *ContainerSourceStatus) PropagateReceiveAdapterStatus(d *appsv1.Deployment) {
if duck.DeploymentIsAvailable(&d.Status, false) {
containerCondSet.Manage(s).MarkTrue(ContainerSourceConditionReceiveAdapterReady)
} else {
// I don't know how to propagate the status well, so just give the name of the Deployment
// for now.
containerCondSet.Manage(s).MarkFalse(ContainerSourceConditionReceiveAdapterReady, "DeploymentUnavailable", "The Deployment '%s' is unavailable.", d.Name)
}
}
Loading