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

v1 types for ContainerSource #4257

Merged
merged 1 commit into from
Oct 9, 2020
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: 42 additions & 0 deletions pkg/apis/sources/v1/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 v1

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/v1/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 v1

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 container 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.Fatal("Unexpected defaults (-want, +got):", diff)
}
})
}
}
110 changes: 110 additions & 0 deletions pkg/apis/sources/v1/container_lifecycle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
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 v1

import (
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"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,
)

// GetConditionSet retrieves the condition set for this resource. Implements the KRShaped interface.
func (*ContainerSource) GetConditionSet() apis.ConditionSet {
return containerCondSet
}

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

// GetTopLevelCondition returns the top level condition.
func (s *ContainerSourceStatus) GetTopLevelCondition() *apis.Condition {
return containerCondSet.Manage(s).GetTopLevelCondition()
}

// 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) {
deploymentAvailableFound := false
for _, cond := range d.Status.Conditions {
if cond.Type == appsv1.DeploymentAvailable {
deploymentAvailableFound = true
if cond.Status == corev1.ConditionTrue {
containerCondSet.Manage(s).MarkTrue(ContainerSourceConditionReceiveAdapterReady)
} else if cond.Status == corev1.ConditionFalse {
containerCondSet.Manage(s).MarkFalse(ContainerSourceConditionReceiveAdapterReady, cond.Reason, cond.Message)
} else if cond.Status == corev1.ConditionUnknown {
containerCondSet.Manage(s).MarkUnknown(ContainerSourceConditionReceiveAdapterReady, cond.Reason, cond.Message)
}
}
}
if !deploymentAvailableFound {
containerCondSet.Manage(s).MarkUnknown(ContainerSourceConditionReceiveAdapterReady, "DeploymentUnavailable", "The Deployment '%s' is unavailable.", d.Name)
}
}
Loading