Skip to content

Commit

Permalink
adding ContainerSource back
Browse files Browse the repository at this point in the history
  • Loading branch information
nachocano committed Mar 24, 2020
1 parent f2224af commit f7452f4
Show file tree
Hide file tree
Showing 44 changed files with 3,556 additions and 2 deletions.
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 @@ -86,6 +86,7 @@ var ourTypes = map[schema.GroupVersionKind]resourcesemantics.GenericCRD{
sourcesv1alpha1.SchemeGroupVersion.WithKind("ApiServerSource"): &sourcesv1alpha1.ApiServerSource{},
sourcesv1alpha1.SchemeGroupVersion.WithKind("PingSource"): &sourcesv1alpha1.PingSource{},
sourcesv1alpha1.SchemeGroupVersion.WithKind("SinkBinding"): &sourcesv1alpha1.SinkBinding{},
sourcesv1alpha1.SchemeGroupVersion.WithKind("ContainerSource"): &sourcesv1alpha1.ContainerSource{},
// v1alpha2
sourcesv1alpha2.SchemeGroupVersion.WithKind("ApiServerSource"): &sourcesv1alpha2.ApiServerSource{},
sourcesv1alpha2.SchemeGroupVersion.WithKind("PingSource"): &sourcesv1alpha2.PingSource{},
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: v1alpha1
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",
}
)
42 changes: 42 additions & 0 deletions pkg/apis/sources/v1alpha1/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 v1alpha1

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
}
115 changes: 115 additions & 0 deletions pkg/apis/sources/v1alpha1/container_defaults_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
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 v1alpha1

import (
"context"
"testing"

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

// No-op test because method does nothing.
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)
}
})
}
}
86 changes: 86 additions & 0 deletions pkg/apis/sources/v1alpha1/container_lifecycle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
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 v1alpha1

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

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

// ContainerConditionSinkProvided has status True when the ContainerSource has been configured with a sink target.
ContainerConditionSinkProvided apis.ConditionType = "SinkProvided"

// ContainerConditionDeployed has status True when the ContainerSource has had it's deployment created.
ContainerConditionDeployed apis.ConditionType = "Deployed"
)

var containerCondSet = apis.NewLivingConditionSet(
ContainerConditionSinkProvided,
ContainerConditionDeployed,
)

// 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()
}

// MarkSink sets the condition that the source has a sink configured.
func (s *ContainerSourceStatus) MarkSink(uri *apis.URL) {
s.SinkURI = uri
if !uri.IsEmpty() {
containerCondSet.Manage(s).MarkTrue(ContainerConditionSinkProvided)
} else {
containerCondSet.Manage(s).MarkFalse(ContainerConditionSinkProvided, "SinkEmpty", "Sink has resolved to empty.%s", "")
}
}

// MarkNoSink sets the condition that the source does not have a sink configured.
func (s *ContainerSourceStatus) MarkNoSink(reason, messageFormat string, messageA ...interface{}) {
containerCondSet.Manage(s).MarkFalse(ContainerConditionSinkProvided, reason, messageFormat, messageA...)
}

// PropagateDeploymentAvailability uses the availability of the provided Deployment to determine if
// ContainerConditionDeployed should be marked as true or false.
func (s *ContainerSourceStatus) PropagateDeploymentAvailability(d *appsv1.Deployment) {
if duck.DeploymentIsAvailable(&d.Status, false) {
containerCondSet.Manage(s).MarkTrue(ContainerConditionDeployed)
} 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(ContainerConditionDeployed, "DeploymentUnavailable", "The Deployment '%s' is unavailable.", d.Name)
}
}

// MarkNotDeployed sets the condition that the source has not been deployed.
func (s *ContainerSourceStatus) MarkNotDeployed(reason, messageFormat string, messageA ...interface{}) {
containerCondSet.Manage(s).MarkFalse(ContainerConditionDeployed, reason, messageFormat, messageA...)
}
Loading

0 comments on commit f7452f4

Please sign in to comment.