This repository has been archived by the owner on Jun 4, 2021. It is now read-only.
/
camelsource.go
189 lines (163 loc) · 6.68 KB
/
camelsource.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
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 reconciler
import (
context "context"
"fmt"
"strings"
camelv1 "github.com/apache/camel-k/pkg/apis/camel/v1"
camelclientset "github.com/apache/camel-k/pkg/client/clientset/versioned"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
v1alpha1 "knative.dev/eventing-contrib/camel/source/pkg/apis/sources/v1alpha1"
"knative.dev/eventing-contrib/camel/source/pkg/reconciler/resources"
"knative.dev/pkg/logging"
"knative.dev/pkg/reconciler"
"knative.dev/pkg/resolver"
camelsource "knative.dev/eventing-contrib/camel/source/pkg/client/injection/reconciler/sources/v1alpha1/camelsource"
)
// newReconciledNormal makes a new reconciler event with event type Normal, and
// reason CamelSourceReconciled.
func newReconciledNormal(namespace, name string) reconciler.Event {
return reconciler.NewEvent(corev1.EventTypeNormal, "CamelSourceReconciled", "CamelSource reconciled: \"%s/%s\"", namespace, name)
}
// Reconciler implements controller.Reconciler for CamelSource resources.
type Reconciler struct {
sinkResolver *resolver.URIResolver
camelClientSet camelclientset.Interface
}
// Check that our Reconciler implements Interface
var _ camelsource.Interface = (*Reconciler)(nil)
// ReconcileKind implements Interface.ReconcileKind.
func (r *Reconciler) ReconcileKind(ctx context.Context, source *v1alpha1.CamelSource) reconciler.Event {
source.Status.InitializeConditions()
source.Status.ObservedGeneration = source.Generation
if source.Spec.Sink == nil {
source.Status.MarkNoSink("SinkMissing", "")
return fmt.Errorf("spec.sink missing")
}
dest := source.Spec.Sink.DeepCopy()
// fill optional data in destination
if dest.Ref != nil {
if dest.Ref.Namespace == "" {
dest.Ref.Namespace = source.GetNamespace()
}
} else if dest.DeprecatedName != "" && dest.DeprecatedNamespace == "" {
dest.DeprecatedNamespace = source.GetNamespace()
}
sinkURI, err := r.sinkResolver.URIFromDestination(*dest, source)
if err != nil {
source.Status.MarkNoSink("NotFound", "")
return err
}
if dest.DeprecatedAPIVersion != "" &&
dest.DeprecatedKind != "" &&
dest.DeprecatedName != "" {
source.Status.MarkSinkWarnRefDeprecated(sinkURI)
} else {
source.Status.MarkSink(sinkURI)
}
// Reconcile and update integration status
if integration, err := r.reconcileIntegration(ctx, source, sinkURI); err != nil {
return err
} else if integration != nil && integration.Status.Phase == camelv1.IntegrationPhaseRunning {
source.Status.MarkDeployed()
}
return newReconciledNormal(source.Namespace, source.Name)
}
func (r *Reconciler) reconcileIntegration(ctx context.Context, source *v1alpha1.CamelSource, sinkURI string) (*camelv1.Integration, reconciler.Event) {
logger := logging.FromContext(ctx)
args := &resources.CamelArguments{
Name: source.Name,
Namespace: source.Namespace,
Source: source.Spec.Source,
Owner: source,
SinkURL: sinkURI,
}
if source.Spec.CloudEventOverrides != nil {
args.Overrides = make(map[string]string)
for k, v := range source.Spec.CloudEventOverrides.Extensions {
args.Overrides[strings.ToLower(k)] = v
}
}
integration, err := r.getIntegration(ctx, source)
if err != nil {
if k8serrors.IsNotFound(err) {
integration, err = r.createIntegration(ctx, source, args)
if err != nil {
return nil, reconciler.NewEvent(corev1.EventTypeWarning, "IntegrationBlocked", "waiting for %v", err)
}
// Since the Deployment has just been created, there's nothing more
// to do until it gets a status. This CamelSource will be reconciled
// again when the Integration is updated.
name := integration.Name
if name == "" {
name = fmt.Sprintf("%s*", integration.GenerateName)
}
source.Status.MarkDeploying("Deploying", "Created integration %s", name)
return integration, reconciler.NewEvent(corev1.EventTypeNormal, "Deployed", "Created integration %q", name)
}
return nil, err
}
// Update Integration spec if it's changed
expected, err := resources.MakeIntegration(args)
if err != nil {
return nil, err
}
// Since the Integration spec has fields defaulted by the webhook, it won't
// be equal to expected. Use DeepDerivative to compare only the fields that
// are set in expected.
if !equality.Semantic.DeepDerivative(expected.Spec, integration.Spec) {
logger.Infof("Integration %q in namespace %q has changed and needs to be updated", integration.Name, integration.Namespace)
integration.Spec = expected.Spec
_, err := r.updateIntegration(ctx, integration)
// if no error, update the status.
name := integration.Name
if name == "" {
name = fmt.Sprintf("%s*", integration.GenerateName)
}
if err == nil {
source.Status.MarkDeploying("IntegrationUpdated", "Updated integration %s", name)
return nil, reconciler.NewEvent(corev1.EventTypeNormal, "IntegrationUpdated", "Updated integration %q", name)
} else {
source.Status.MarkDeploying("IntegrationNeedsUpdate", "Attempting to update integration %s", name)
return nil, reconciler.NewEvent(corev1.EventTypeWarning, "IntegrationNeedsUpdate", "Failed to update integration %q", name)
}
}
return integration, nil
}
func (r *Reconciler) getIntegration(ctx context.Context, source *v1alpha1.CamelSource) (*camelv1.Integration, error) {
all, err := r.camelClientSet.CamelV1().Integrations(source.Namespace).List(metav1.ListOptions{})
if err != nil {
return nil, err
}
for _, a := range all.Items {
if metav1.IsControlledBy(&a, source) {
return &a, nil
}
}
return nil, k8serrors.NewNotFound(schema.GroupResource{}, "")
}
func (r *Reconciler) createIntegration(ctx context.Context, source *v1alpha1.CamelSource, args *resources.CamelArguments) (*camelv1.Integration, error) {
integration, err := resources.MakeIntegration(args)
if err != nil {
return nil, err
}
return r.camelClientSet.CamelV1().Integrations(source.Namespace).Create(integration)
}
func (r *Reconciler) updateIntegration(ctx context.Context, integration *camelv1.Integration) (*camelv1.Integration, error) {
return r.camelClientSet.CamelV1().Integrations(integration.Namespace).Update(integration)
}